Blame view

config.js 7.38 KB
4da3dd303   Andrew Buss   Tried unbreaking ...
1
  angular.module('flashy', [
4da3dd303   Andrew Buss   Tried unbreaking ...
2
      'flashy.LoginController',
80d1e57aa   Andrew Buss   materialized thin...
3
      'flashy.RootController',
4da3dd303   Andrew Buss   Tried unbreaking ...
4
      'flashy.FeedController',
3c4c48c96   Kevin Mach   removed errors fo...
5
      'flashy.DeckController',
bbba33088   Andrew Buss   Added addclass st...
6
      'flashy.ClassAddController',
331ca39d2   Melody   Crude Drop Class ...
7
      'flashy.ClassDropController',
bbba33088   Andrew Buss   Added addclass st...
8
      'flashy.RequestResetController',
58b21808a   Rachel Lee   Rename review to ...
9
      'flashy.StudyController',
80d1e57aa   Andrew Buss   materialized thin...
10
      'flashy.UserService',
8e8058a82   Rachel Lee   Working on flashc...
11
      'flashy.FlashcardDirective',
9eb54c646   Rachel Lee   Not working study...
12
13
      //'flashy.SelectDirective',
      // DOESNT WORK RN
8607c44bb   Rachel Lee   Merge branch 'mas...
14
      'flashy.ResetPasswordController',
f05ccab30   Andrew Buss   half-implemented ...
15
      'flashy.VerifyEmailController',
54c942ddf   Andrew Buss   recovered cardlis...
16
      'flashy.CardListController',
b12a56132   Andrew Buss   try to fix (hah) ...
17
      'flashy.HelpController',
9eb54c646   Rachel Lee   Not working study...
18
      'flashy.SettingsController',
4da3dd303   Andrew Buss   Tried unbreaking ...
19
      'ngCookies']).
f107af9c8   Andrew Buss   don't strip slash...
20
      config(function($stateProvider, $urlRouterProvider, $resourceProvider, $httpProvider, $locationProvider) {
4c3a5e272   Andrew Buss   cleanup; redirect...
21
22
23
24
          'use strict';
          $httpProvider.defaults.withCredentials = true;
          $httpProvider.defaults.xsrfCookieName = 'csrftoken';
          $httpProvider.defaults.xsrfHeaderName = 'X-CSRFToken';
f107af9c8   Andrew Buss   don't strip slash...
25
          $resourceProvider.defaults.stripTrailingSlashes = false;
4c3a5e272   Andrew Buss   cleanup; redirect...
26
27
28
29
30
31
          var arrayMethods = Object.getOwnPropertyNames(Array.prototype);
          arrayMethods.forEach(attachArrayMethodsToNodeList);
          function attachArrayMethodsToNodeList(methodName) {
              if (methodName !== 'length') {
                  NodeList.prototype[methodName] = Array.prototype[methodName];
              }
4fb8a3f36   Andrew Buss   start handling lo...
32
          }
efc77eb59   Andrew Buss   blanks kinda work
33

107013870   Andrew Buss   ui tweaks, check ...
34
          $httpProvider.interceptors.push(function($q, $rootScope) {
4c3a5e272   Andrew Buss   cleanup; redirect...
35
              return {
107013870   Andrew Buss   ui tweaks, check ...
36
                  'responseError': function(rejection) { // need a better redirect
4c3a5e272   Andrew Buss   cleanup; redirect...
37
38
39
                      if (rejection.status >= 500) {
                          console.log('got error');
                          console.log(rejection);
b12a56132   Andrew Buss   try to fix (hah) ...
40
                          $rootScope.$broadcast('server_error', rejection);
0b8a1b605   Andrew Buss   still trying to f...
41
                      }
620ed5155   Andrew Buss   try to handle loc...
42
                      if (rejection.status == 403) {
4fb8a3f36   Andrew Buss   start handling lo...
43
                          console.log(rejection);
107013870   Andrew Buss   ui tweaks, check ...
44
                          if (rejection.data && rejection.data.detail == 'Please verify your email before continuing') {
0c65d0c07   Andrew Buss   try to fix behavi...
45
46
                              UserService.showLockedMessage();
                              UserService.logout();
4fb8a3f36   Andrew Buss   start handling lo...
47
                          }
0b8a1b605   Andrew Buss   still trying to f...
48
                      }
4c3a5e272   Andrew Buss   cleanup; redirect...
49
50
                      return $q.reject(rejection);
                  }
0b8a1b605   Andrew Buss   still trying to f...
51
              };
4c3a5e272   Andrew Buss   cleanup; redirect...
52
53
          });
          $locationProvider.html5Mode(true);
35387f308   Andrew Buss   cleaned up userse...
54
          $urlRouterProvider.otherwise('/404');
4c3a5e272   Andrew Buss   cleanup; redirect...
55
          var auth_resolve = {
bc596fb4f   Andrew Buss   hopefully resolve...
56
57
58
59
              authorize: function($q, $rootScope, $state, $stateParams, UserService) {
  
                  console.log('do we need to authorize a user for', $rootScope.nextState.name);
                  if (UserService.noAuthRequired($rootScope.nextState)) {
337886871   Andrew Buss   actually resolve ...
60
61
                      console.log('no auth required for', $rootScope.nextState.name);
                      return UserService.getUserData();
0c65d0c07   Andrew Buss   try to fix behavi...
62
                  }
fcda9ffd2   Tetranoir   feed controller a...
63
                  console.log('resolving user before continuing for ' + $state.name);
fe2c7edc6   Andrew Buss   fix flashcard sca...
64
                  var redirectAsNeeded = function() {
35387f308   Andrew Buss   cleaned up userse...
65
66
67
68
69
                      if (!UserService.isLoggedIn()) {
                          console.log(UserService.getUserData());
                          console.log('making the user log in');
                          $state.go('login');
                      }
88ca25463   Andrew Buss   fixed broken auth...
70
71
                      if (!UserService.authorizedFor($rootScope.nextState, $rootScope.nextStateParams)) {
                          console.log('user not authorized for ' + $rootScope.nextState.name);
ca76999e9   Andrew Buss   make deck less in...
72
73
                          $state.go('addclass');
                      }
35387f308   Andrew Buss   cleaned up userse...
74
75
76
                  };
                  if (UserService.isResolved()) return redirectAsNeeded();
                  return UserService.getUserData().then(redirectAsNeeded);
4c3a5e272   Andrew Buss   cleanup; redirect...
77
78
79
80
              }
          };
          $stateProvider.
              state('login', {
594706b11   Andrew Buss   don't trust the u...
81
                  resolve: auth_resolve,
4c3a5e272   Andrew Buss   cleanup; redirect...
82
83
84
85
                  url: '/login',
                  templateUrl: 'templates/login.html',
                  controller: 'LoginController'
              }).
4c3a5e272   Andrew Buss   cleanup; redirect...
86
87
              state('root', {
                  resolve: auth_resolve,
107013870   Andrew Buss   ui tweaks, check ...
88
                  url: '',
4c3a5e272   Andrew Buss   cleanup; redirect...
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
                  controller: 'RootController'
              }).
              state('feed', {
                  resolve: auth_resolve,
                  url: '/feed/{sectionId}',
                  templateUrl: 'templates/feed.html',
                  controller: 'FeedController'
              }).
              state('cardlist', {
                  resolve: auth_resolve,
                  url: '/cards/{sectionId}',
                  templateUrl: 'templates/cardlist.html',
                  controller: 'CardListController'
              }).
              state('addclass', {
                  resolve: auth_resolve,
                  url: '/addclass',
                  templateUrl: 'templates/addclass.html',
                  controller: 'ClassAddController'
              }).
331ca39d2   Melody   Crude Drop Class ...
109
110
111
112
113
114
              state('dropclass', {
                  resolve: auth_resolve,
                  url: '/settings/dropclass',
                  templateUrl: 'templates/dropclass.html',
                  controller: 'ClassDropController'
              }).
4c3a5e272   Andrew Buss   cleanup; redirect...
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
              state('deck', {
                  resolve: auth_resolve,
                  url: '/deck/{sectionId}',
                  templateUrl: 'templates/deck.html',
                  controller: 'DeckController'
              }).
              state('study', {
                  resolve: auth_resolve,
                  url: '/study',
                  templateUrl: 'templates/study.html',
                  controller: 'StudyController'
              }).
              state('flashcard', {
                  resolve: auth_resolve,
                  url: '/flashcard',
                  templateUrl: 'templates/flashcard.html',
                  controller: 'FlashcardController'
              }).
620ed5155   Andrew Buss   try to handle loc...
133
134
135
136
137
138
              state('settings', {
                  resolve: auth_resolve,
                  url: '/settings',
                  templateUrl: 'templates/settings.html',
                  controller: 'SettingsController'
              }).
4c3a5e272   Andrew Buss   cleanup; redirect...
139
140
141
142
143
144
145
146
147
148
149
              state('requestpasswordreset', {
                  url: '/requestpasswordreset',
                  templateUrl: 'templates/requestpasswordreset.html',
                  controller: 'RequestResetController'
              }).
              state('resetpassword', {
                  url: '/resetpassword/{uid}/{token}',
                  templateUrl: 'templates/resetpassword.html',
                  controller: 'ResetPasswordController'
              }).
              state('verifyemail', {
4c3a5e272   Andrew Buss   cleanup; redirect...
150
151
152
                  url: '/verifyemail/{key}',
                  templateUrl: 'templates/verifyemail.html',
                  controller: 'VerifyEmailController'
35387f308   Andrew Buss   cleaned up userse...
153
154
155
156
              }).
              state('404', {
                  url: '/404',
                  template: "<h1>This page doesn't exist!</h1>"
b12a56132   Andrew Buss   try to fix (hah) ...
157
158
159
160
161
162
              }).
              state('help', {
                  resolve: auth_resolve,
                  url: '/help',
                  templateUrl: 'templates/help.html',
                  controller: 'HelpController'
f05ccab30   Andrew Buss   half-implemented ...
163
              });
4c3a5e272   Andrew Buss   cleanup; redirect...
164
      }).
fe2c7edc6   Andrew Buss   fix flashcard sca...
165
166
      run(function($rootScope, $state, $stateParams, $location, UserService) {
          $rootScope.$on('$stateChangeError', function(event, toState, toParams, fromState, fromParams, error) {
4c3a5e272   Andrew Buss   cleanup; redirect...
167
168
169
              console.log('failed to change state: ' + error);
              $state.go('login');
          });
fe2c7edc6   Andrew Buss   fix flashcard sca...
170
          $rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams) {
bc596fb4f   Andrew Buss   hopefully resolve...
171
              $rootScope.nextState = toState;
88ca25463   Andrew Buss   fixed broken auth...
172
              $rootScope.nextStateParams = toParams;
bc596fb4f   Andrew Buss   hopefully resolve...
173
              console.log('changing state to', toState);
9e9dd40af   Andrew Buss   save a user's las...
174
175
176
177
178
              if (['feed', 'deck', 'cardlist'].indexOf(toState.name) >= 0) {
                  localStorage.setItem('last_state', toState.name);
                  localStorage.setItem('last_state_params', JSON.stringify(toParams));
              }
          });
4c3a5e272   Andrew Buss   cleanup; redirect...
179
      });