Commit cf476b03e519eab86a15b6171c7f1dc6ac4603f4
1 parent
7a53b3daec
Exists in
master
and in
1 other branch
properly check whether a user can go straight to a class
Showing 3 changed files with 30 additions and 29 deletions Inline Diff
config.js
View file @
cf476b0
angular.module('flashy', [ | 1 | 1 | angular.module('flashy', [ | |
'flashy.LoginController', | 2 | 2 | 'flashy.LoginController', | |
'flashy.RootController', | 3 | 3 | 'flashy.RootController', | |
'flashy.FeedController', | 4 | 4 | 'flashy.FeedController', | |
'flashy.DeckController', | 5 | 5 | 'flashy.DeckController', | |
'flashy.ClassAddController', | 6 | 6 | 'flashy.ClassAddController', | |
'flashy.ClassDropController', | 7 | 7 | 'flashy.ClassDropController', | |
'flashy.RequestResetController', | 8 | 8 | 'flashy.RequestResetController', | |
'flashy.StudyController', | 9 | 9 | 'flashy.StudyController', | |
'flashy.UserService', | 10 | 10 | 'flashy.UserService', | |
'flashy.FlashcardDirective', | 11 | 11 | 'flashy.FlashcardDirective', | |
//'flashy.SelectDirective', | 12 | 12 | //'flashy.SelectDirective', | |
// DOESNT WORK RN | 13 | 13 | // DOESNT WORK RN | |
'flashy.ResetPasswordController', | 14 | 14 | 'flashy.ResetPasswordController', | |
'flashy.VerifyEmailController', | 15 | 15 | 'flashy.VerifyEmailController', | |
'flashy.CardListController', | 16 | 16 | 'flashy.CardListController', | |
'flashy.HelpController', | 17 | 17 | 'flashy.HelpController', | |
'flashy.SettingsController', | 18 | 18 | 'flashy.SettingsController', | |
'ngCookies']). | 19 | 19 | 'ngCookies']). | |
config(function($stateProvider, $urlRouterProvider, $resourceProvider, $httpProvider, $locationProvider) { | 20 | 20 | config(function($stateProvider, $urlRouterProvider, $resourceProvider, $httpProvider, $locationProvider) { | |
'use strict'; | 21 | 21 | 'use strict'; | |
$httpProvider.defaults.withCredentials = true; | 22 | 22 | $httpProvider.defaults.withCredentials = true; | |
$httpProvider.defaults.xsrfCookieName = 'csrftoken'; | 23 | 23 | $httpProvider.defaults.xsrfCookieName = 'csrftoken'; | |
$httpProvider.defaults.xsrfHeaderName = 'X-CSRFToken'; | 24 | 24 | $httpProvider.defaults.xsrfHeaderName = 'X-CSRFToken'; | |
$resourceProvider.defaults.stripTrailingSlashes = false; | 25 | 25 | $resourceProvider.defaults.stripTrailingSlashes = false; | |
var arrayMethods = Object.getOwnPropertyNames(Array.prototype); | 26 | 26 | var arrayMethods = Object.getOwnPropertyNames(Array.prototype); | |
arrayMethods.forEach(attachArrayMethodsToNodeList); | 27 | 27 | arrayMethods.forEach(attachArrayMethodsToNodeList); | |
function attachArrayMethodsToNodeList(methodName) { | 28 | 28 | function attachArrayMethodsToNodeList(methodName) { | |
if (methodName !== 'length') { | 29 | 29 | if (methodName !== 'length') { | |
NodeList.prototype[methodName] = Array.prototype[methodName]; | 30 | 30 | NodeList.prototype[methodName] = Array.prototype[methodName]; | |
} | 31 | 31 | } | |
} | 32 | 32 | } | |
33 | 33 | |||
$httpProvider.interceptors.push(function($q, $rootScope) { | 34 | 34 | $httpProvider.interceptors.push(function($q, $rootScope) { | |
return { | 35 | 35 | return { | |
'responseError': function(rejection) { // need a better redirect | 36 | 36 | 'responseError': function(rejection) { // need a better redirect | |
if (rejection.status >= 500) { | 37 | 37 | if (rejection.status >= 500) { | |
console.log('got error'); | 38 | 38 | console.log('got error'); | |
console.log(rejection); | 39 | 39 | console.log(rejection); | |
$rootScope.$broadcast('server_error', rejection); | 40 | 40 | $rootScope.$broadcast('server_error', rejection); | |
} | 41 | 41 | } | |
if (rejection.status == 403) { | 42 | 42 | if (rejection.status == 403) { | |
console.log(rejection); | 43 | 43 | console.log(rejection); | |
if (rejection.data && rejection.data.detail == 'Please verify your email before continuing') { | 44 | 44 | if (rejection.data && rejection.data.detail == 'Please verify your email before continuing') { | |
UserService.showLockedMessage(); | 45 | 45 | UserService.showLockedMessage(); | |
UserService.logout(); | 46 | 46 | UserService.logout(); | |
} | 47 | 47 | } | |
} | 48 | 48 | } | |
return $q.reject(rejection); | 49 | 49 | return $q.reject(rejection); | |
} | 50 | 50 | } | |
}; | 51 | 51 | }; | |
}); | 52 | 52 | }); | |
$locationProvider.html5Mode(true); | 53 | 53 | $locationProvider.html5Mode(true); | |
$urlRouterProvider.otherwise('/404'); | 54 | 54 | $urlRouterProvider.otherwise('/404'); | |
var auth_resolve = { | 55 | 55 | var auth_resolve = { | |
authorize: function($q, $rootScope, $state, $stateParams, UserService) { | 56 | 56 | authorize: function($q, $rootScope, $state, $stateParams, UserService) { | |
57 | 57 | |||
console.log('do we need to authorize a user for', $rootScope.nextState.name); | 58 | 58 | console.log('do we need to authorize a user for', $rootScope.nextState.name); | |
if (UserService.noAuthRequired($rootScope.nextState)) { | 59 | 59 | if (UserService.noAuthRequired($rootScope.nextState)) { | |
console.log('no auth required for', $rootScope.nextState.name); | 60 | 60 | console.log('no auth required for', $rootScope.nextState.name); | |
return UserService.getUserData(); | 61 | 61 | return UserService.getUserData(); | |
} | 62 | 62 | } | |
console.log('resolving user before continuing for ' + $state.name); | 63 | 63 | console.log('resolving user before continuing to ' + $rootScope.nextState.name); | |
var redirectAsNeeded = function() { | 64 | 64 | var redirectAsNeeded = function() { | |
if (!UserService.isLoggedIn()) { | 65 | 65 | if (!UserService.isLoggedIn()) { | |
console.log(UserService.getUserData()); | 66 | 66 | console.log(UserService.getUserData()); | |
console.log('making the user log in'); | 67 | 67 | console.log('making the user log in'); | |
$state.go('login'); | 68 | 68 | $state.go('login'); | |
} | 69 | 69 | } | |
if (!UserService.authorizedFor($rootScope.nextState, $rootScope.nextStateParams)) { | 70 | 70 | if (!UserService.authorizedFor($rootScope.nextState, $rootScope.nextStateParams)) { | |
console.log('user not authorized for ' + $rootScope.nextState.name); | 71 | 71 | console.log('user not authorized for ' + $rootScope.nextState.name); | |
$state.go('addclass'); | 72 | 72 | $state.go('addclass'); | |
} | 73 | 73 | } | |
}; | 74 | 74 | }; | |
if (UserService.isResolved()) return redirectAsNeeded(); | 75 | 75 | if (UserService.isResolved()) return redirectAsNeeded(); | |
return UserService.getUserData().then(redirectAsNeeded); | 76 | 76 | return UserService.getUserData().then(redirectAsNeeded); | |
} | 77 | 77 | } | |
}; | 78 | 78 | }; | |
$stateProvider. | 79 | 79 | $stateProvider. | |
state('login', { | 80 | 80 | state('login', { | |
resolve: auth_resolve, | 81 | 81 | resolve: auth_resolve, | |
url: '/login', | 82 | 82 | url: '/login', | |
templateUrl: 'templates/login.html', | 83 | 83 | templateUrl: 'templates/login.html', | |
controller: 'LoginController' | 84 | 84 | controller: 'LoginController' | |
}). | 85 | 85 | }). | |
state('root', { | 86 | 86 | state('root', { | |
resolve: auth_resolve, | 87 | 87 | resolve: auth_resolve, | |
url: '', | 88 | 88 | url: '', | |
controller: 'RootController' | 89 | 89 | controller: 'RootController' | |
}). | 90 | 90 | }). | |
state('feed', { | 91 | 91 | state('feed', { | |
resolve: auth_resolve, | 92 | 92 | resolve: auth_resolve, | |
url: '/feed/{sectionId}', | 93 | 93 | url: '/feed/{sectionId}', | |
templateUrl: 'templates/feed.html', | 94 | 94 | templateUrl: 'templates/feed.html', | |
controller: 'FeedController' | 95 | 95 | controller: 'FeedController' | |
}). | 96 | 96 | }). | |
state('cardlist', { | 97 | 97 | state('cardlist', { | |
resolve: auth_resolve, | 98 | 98 | resolve: auth_resolve, | |
url: '/cards/{sectionId}', | 99 | 99 | url: '/cards/{sectionId}', | |
templateUrl: 'templates/cardlist.html', | 100 | 100 | templateUrl: 'templates/cardlist.html', | |
controller: 'CardListController' | 101 | 101 | controller: 'CardListController' | |
}). | 102 | 102 | }). | |
state('addclass', { | 103 | 103 | state('addclass', { | |
resolve: auth_resolve, | 104 | 104 | resolve: auth_resolve, | |
url: '/addclass', | 105 | 105 | url: '/addclass', | |
templateUrl: 'templates/addclass.html', | 106 | 106 | templateUrl: 'templates/addclass.html', | |
controller: 'ClassAddController' | 107 | 107 | controller: 'ClassAddController' | |
}). | 108 | 108 | }). | |
state('dropclass', { | 109 | 109 | state('dropclass', { | |
resolve: auth_resolve, | 110 | 110 | resolve: auth_resolve, | |
url: '/settings/dropclass', | 111 | 111 | url: '/settings/dropclass', | |
templateUrl: 'templates/dropclass.html', | 112 | 112 | templateUrl: 'templates/dropclass.html', | |
controller: 'ClassDropController' | 113 | 113 | controller: 'ClassDropController' | |
}). | 114 | 114 | }). | |
state('deck', { | 115 | 115 | state('deck', { | |
resolve: auth_resolve, | 116 | 116 | resolve: auth_resolve, | |
url: '/deck/{sectionId}', | 117 | 117 | url: '/deck/{sectionId}', | |
templateUrl: 'templates/deck.html', | 118 | 118 | templateUrl: 'templates/deck.html', | |
controller: 'DeckController' | 119 | 119 | controller: 'DeckController' | |
}). | 120 | 120 | }). | |
state('study', { | 121 | 121 | state('study', { | |
resolve: auth_resolve, | 122 | 122 | resolve: auth_resolve, | |
url: '/study', | 123 | 123 | url: '/study', | |
templateUrl: 'templates/study.html', | 124 | 124 | templateUrl: 'templates/study.html', | |
controller: 'StudyController' | 125 | 125 | controller: 'StudyController' | |
}). | 126 | 126 | }). | |
state('flashcard', { | 127 | 127 | state('flashcard', { | |
resolve: auth_resolve, | 128 | 128 | resolve: auth_resolve, | |
url: '/flashcard', | 129 | 129 | url: '/flashcard', | |
templateUrl: 'templates/flashcard.html', | 130 | 130 | templateUrl: 'templates/flashcard.html', | |
controller: 'FlashcardController' | 131 | 131 | controller: 'FlashcardController' | |
}). | 132 | 132 | }). | |
state('settings', { | 133 | 133 | state('settings', { | |
resolve: auth_resolve, | 134 | 134 | resolve: auth_resolve, | |
url: '/settings', | 135 | 135 | url: '/settings', | |
templateUrl: 'templates/settings.html', | 136 | 136 | templateUrl: 'templates/settings.html', | |
controller: 'SettingsController' | 137 | 137 | controller: 'SettingsController' | |
}). | 138 | 138 | }). | |
state('requestpasswordreset', { | 139 | 139 | state('requestpasswordreset', { | |
url: '/requestpasswordreset', | 140 | 140 | url: '/requestpasswordreset', | |
templateUrl: 'templates/requestpasswordreset.html', | 141 | 141 | templateUrl: 'templates/requestpasswordreset.html', | |
controller: 'RequestResetController' | 142 | 142 | controller: 'RequestResetController' | |
}). | 143 | 143 | }). | |
state('resetpassword', { | 144 | 144 | state('resetpassword', { | |
url: '/resetpassword/{uid}/{token}', | 145 | 145 | url: '/resetpassword/{uid}/{token}', | |
templateUrl: 'templates/resetpassword.html', | 146 | 146 | templateUrl: 'templates/resetpassword.html', | |
controller: 'ResetPasswordController' | 147 | 147 | controller: 'ResetPasswordController' | |
}). | 148 | 148 | }). | |
state('verifyemail', { | 149 | 149 | state('verifyemail', { | |
url: '/verifyemail/{key}', | 150 | 150 | url: '/verifyemail/{key}', | |
templateUrl: 'templates/verifyemail.html', | 151 | 151 | templateUrl: 'templates/verifyemail.html', | |
controller: 'VerifyEmailController' | 152 | 152 | controller: 'VerifyEmailController' | |
}). | 153 | 153 | }). | |
state('404', { | 154 | 154 | state('404', { | |
url: '/404', | 155 | 155 | url: '/404', | |
template: "<h1>This page doesn't exist!</h1>" | 156 | 156 | template: "<h1>This page doesn't exist!</h1>" | |
}). | 157 | 157 | }). | |
state('help', { | 158 | 158 | state('help', { | |
resolve: auth_resolve, | 159 | 159 | resolve: auth_resolve, | |
url: '/help', | 160 | 160 | url: '/help', | |
templateUrl: 'templates/help.html', | 161 | 161 | templateUrl: 'templates/help.html', | |
controller: 'HelpController' | 162 | 162 | controller: 'HelpController' | |
}); | 163 | 163 | }); | |
}). | 164 | 164 | }). | |
run(function($rootScope, $state, $stateParams, $location, UserService) { | 165 | 165 | run(function($rootScope, $state, $stateParams, $location, UserService) { | |
$rootScope.$on('$stateChangeError', function(event, toState, toParams, fromState, fromParams, error) { | 166 | 166 | $rootScope.$on('$stateChangeError', function(event, toState, toParams, fromState, fromParams, error) { | |
console.log('failed to change state: ' + error); | 167 | 167 | console.log('failed to change state: ' + error); | |
$state.go('login'); | 168 | 168 | $state.go('login'); | |
}); | 169 | 169 | }); | |
$rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams) { | 170 | 170 | $rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams) { | |
$rootScope.nextState = toState; | 171 | 171 | $rootScope.nextState = toState; | |
$rootScope.nextStateParams = toParams; | 172 | 172 | $rootScope.nextStateParams = toParams; | |
console.log('changing state to', toState); | 173 | 173 | console.log('changing state to', toState); | |
if (['feed', 'deck', 'cardlist'].indexOf(toState.name) >= 0) { | 174 | 174 | if (['feed', 'deck', 'cardlist'].indexOf(toState.name) >= 0) { | |
localStorage.setItem('last_state', toState.name); | 175 | 175 | localStorage.setItem('last_state', toState.name); | |
localStorage.setItem('last_state_params', JSON.stringify(toParams)); | 176 | 176 | localStorage.setItem('last_state_params', JSON.stringify(toParams)); | |
} | 177 | 177 | } | |
}); | 178 | 178 | }); |
scripts/FeedController.js
View file @
cf476b0
angular.module('flashy.FeedController', ['ui.router', 'ngAnimate', 'ngWebSocket', 'contenteditable']). | 1 | 1 | angular.module('flashy.FeedController', ['ui.router', 'ngAnimate', 'ngWebSocket', 'contenteditable']). | |
2 | 2 | |||
controller('FeedController', function ($scope, $rootScope, $state, $http, $window, $timeout, $stateParams, $websocket, UserService) { | 3 | 3 | controller('FeedController', function($scope, $rootScope, $state, $http, $window, $timeout, $stateParams, $websocket, UserService) { | |
angular.module('flashy.CardGridController').controller.apply(this, arguments); | 4 | 4 | angular.module('flashy.CardGridController').controller.apply(this, arguments); | |
console.log('Hello from feed'); | 5 | 5 | console.log('Hello from feed'); | |
6 | 6 | |||
$scope.refreshCards = function () { | 7 | 7 | $scope.refreshCards = function() { | |
$http.get('/api/sections/' + $scope.sectionId + '/feed/'). | 8 | 8 | $http.get('/api/sections/' + $scope.sectionId + '/feed/'). | |
success(function (data) { | 9 | 9 | success(function(data) { | |
console.log(data); | 10 | 10 | console.log(data); | |
$scope.cards = data; | 11 | 11 | $scope.cards = data; | |
$scope.refreshLayout(); | 12 | 12 | $scope.refreshLayout(); | |
console.log('success in refresh cards...'); | 13 | 13 | console.log('success in refresh cards...'); | |
}). | 14 | 14 | }). | |
error(function (err) { | 15 | 15 | error(function(err) { | |
console.log('refresh fail'); | 16 | 16 | console.log('refresh fail'); | |
console.log(err); | 17 | 17 | console.log(err); | |
}); | 18 | 18 | }); | |
}; | 19 | 19 | }; | |
20 | 20 | |||
$scope.sortAdd = function (card, array) { | 21 | 21 | $scope.sortAdd = function(card, array) { | |
console.log('sort add'); | 22 | 22 | console.log('sort add'); | |
array.forEach(function (ele, i, ary) { | 23 | 23 | array.forEach(function(ele, i, ary) { | |
if (ele.score <= card.score) { | 24 | 24 | if (ele.score <= card.score) { | |
ary.splice(i, 0, card); | 25 | 25 | ary.splice(i, 0, card); | |
return; | 26 | 26 | return; | |
} | 27 | 27 | } | |
}); | 28 | 28 | }); | |
}; | 29 | 29 | }; | |
30 | 30 | |||
$scope.hide = function (card) { | 31 | 31 | $scope.hide = function(card) { | |
console.log('hiding card'); | 32 | 32 | console.log('hiding card'); | |
var found = -1; | 33 | 33 | var found = -1; | |
col = $scope.cardTable[card.id].colNum; | 34 | 34 | col = $scope.cardTable[card.id].colNum; | |
found = $scope.cardCols[col].indexOf(card); | 35 | 35 | found = $scope.cardCols[col].indexOf(card); | |
if (found != -1) { | 36 | 36 | if (found != -1) { | |
$scope.cardCols[col].splice(found, 1); | 37 | 37 | $scope.cardCols[col].splice(found, 1); | |
console.log('card hidden'); | 38 | 38 | console.log('card hidden'); | |
return col; | 39 | 39 | return col; | |
} | 40 | 40 | } | |
console.log('Error finding card to hide:'); | 41 | 41 | console.log('Error finding card to hide:'); | |
console.log(card); | 42 | 42 | console.log(card); | |
return -1; | 43 | 43 | return -1; | |
}; | 44 | 44 | }; | |
45 | 45 | |||
$scope.update = function (id, new_score) { | 46 | 46 | $scope.update = function(id, new_score) { | |
card = $scope.cardTable[id].obj; | 47 | 47 | card = $scope.cardTable[id].obj; | |
if (Math.abs(new_score - card.score) < .0001) { | 48 | 48 | if (Math.abs(new_score - card.score) < .0001) { | |
console.log('score same, no update required'); | 49 | 49 | console.log('score same, no update required'); | |
return; | 50 | 50 | return; | |
} | 51 | 51 | } | |
console.log('updating'); | 52 | 52 | console.log('updating'); | |
console.log(card); | 53 | 53 | console.log(card); | |
var column = $scope.cardCols[$scope.cardTable[id].colNum]; | 54 | 54 | var column = $scope.cardCols[$scope.cardTable[id].colNum]; | |
var found = column.indexOf(card); | 55 | 55 | var found = column.indexOf(card); | |
var i = 0; | 56 | 56 | var i = 0; | |
for (; i < column.length; i++) | 57 | 57 | for (; i < column.length; i++) | |
if (column[i].score <= new_score) break; | 58 | 58 | if (column[i].score <= new_score) break; | |
card.score = new_score; | 59 | 59 | card.score = new_score; | |
if ($scope.$$phase) { // most of the time it is "$digest" | 60 | 60 | if ($scope.$$phase) { // most of the time it is "$digest" | |
column.splice(i, 0, column.splice(found, 1)[0]); | 61 | 61 | column.splice(i, 0, column.splice(found, 1)[0]); | |
} else { | 62 | 62 | } else { | |
$scope.$apply(column.splice(i, 0, column.splice(found, 1)[0])); | 63 | 63 | $scope.$apply(column.splice(i, 0, column.splice(found, 1)[0])); | |
} | 64 | 64 | } | |
}; | 65 | 65 | }; | |
66 | 66 | |||
$scope.feed_ws = $websocket($scope.ws_host + '/ws/feed/' + $scope.sectionId + '?subscribe-broadcast'); | 67 | 67 | $scope.feed_ws = $websocket($scope.ws_host + '/ws/feed/' + $scope.sectionId + '?subscribe-broadcast'); | |
$scope.feed_ws.onOpen(function () { | 68 | 68 | $scope.feed_ws.onOpen(function() { | |
console.log('deck ws open'); | 69 | 69 | console.log('deck ws open'); | |
}); | 70 | 70 | }); | |
71 | 71 | |||
$scope.feed_ws.onMessage(function (e) { | 72 | 72 | $scope.feed_ws.onMessage(function(e) { | |
data = JSON.parse(e.data); | 73 | 73 | data = JSON.parse(e.data); | |
console.log('got websocket message ' + e.data); | 74 | 74 | console.log('got websocket message ' + e.data); | |
console.log(data); | 75 | 75 | console.log(data); | |
if (data.event_type == 'new_card') { | 76 | 76 | if (data.event_type == 'new_card') { | |
$scope.add(data.flashcard); | 77 | 77 | $scope.add(data.flashcard); | |
} else if (data.event_type == 'score_change') { | 78 | 78 | } else if (data.event_type == 'score_change') { | |
$scope.update(data.flashcard_id, data.new_score); | 79 | 79 | $scope.update(data.flashcard_id, data.new_score); | |
} | 80 | 80 | } | |
}); | 81 | 81 | }); | |
82 | 82 | |||
$scope.pushCard = function () { | 83 | 83 | $scope.pushCard = function() { | |
var myCard = { | 84 | 84 | var myCard = { | |
// we can't trim this string because it'd mess up the blanks. Something to fix. | 85 | 85 | // we can't trim this string because it'd mess up the blanks. Something to fix. | |
'text': $('#new-card-input').text(), | 86 | 86 | 'text': $('#new-card-input').text(), | |
'mask': $scope.newCardBlanks, | 87 | 87 | 'mask': $scope.newCardBlanks, | |
section: $scope.section.id | 88 | 88 | section: $scope.section.id | |
}; | 89 | 89 | }; | |
if (myCard.text == '') { | 90 | 90 | if (myCard.text == '') { | |
console.log('blank flashcard not pushed:' + myCard.text); | 91 | 91 | console.log('blank flashcard not pushed:' + myCard.text); | |
return closeNewCard(); | 92 | 92 | return closeNewCard(); | |
} | 93 | 93 | } | |
$http.post('/api/flashcards/', myCard). | 94 | 94 | $http.post('/api/flashcards/', myCard). | |
success(function (data) { | 95 | 95 | success(function(data) { | |
console.log('flashcard pushed: ' + myCard.text); | 96 | 96 | console.log('flashcard pushed: ' + myCard.text); | |
if (!UserService.hasVerifiedEmail()) { | 97 | 97 | if (!UserService.hasVerifiedEmail()) { | |
Materialize.toast("<p>Thanks for contributing! However, others won't see your card until you verify your email address<p>", 4000); | 98 | 98 | Materialize.toast("<p>Thanks for contributing! However, others won't see your card until you verify your email address<p>", 4000); | |
} | 99 | 99 | } | |
}). | 100 | 100 | }). | |
error(function (error) { | 101 | 101 | error(function(error) { | |
console.log('something went wrong pushing a card!'); | 102 | 102 | console.log('something went wrong pushing a card!'); | |
}); | 103 | 103 | }); | |
return $scope.closeNewCardModal(); | 104 | 104 | return $scope.closeNewCardModal(); | |
}; | 105 | 105 | }; | |
106 | 106 | |||
/* Key bindings for the whole feed window. Hotkey it up! */ | 107 | 107 | /* Key bindings for the whole feed window. Hotkey it up! */ | |
var listenForC = true; | 108 | 108 | var listenForC = true; | |
109 | 109 | |||
// Need to pass these options into openmodal and leanmodal, | 110 | 110 | // Need to pass these options into openmodal and leanmodal, | |
// otherwise the ready handler doesn't get called | 111 | 111 | // otherwise the ready handler doesn't get called | |
112 | 112 | |||
modal_options = { | 113 | 113 | modal_options = { | |
dismissible: true, // Modal can be dismissed by clicking outside of the modal | 114 | 114 | dismissible: true, // Modal can be dismissed by clicking outside of the modal | |
opacity: 0, // Opacity of modal background | 115 | 115 | opacity: 0, // Opacity of modal background | |
in_duration: 300, // Transition in duration | 116 | 116 | in_duration: 300, // Transition in duration | |
out_duration: 200, // Transition out duration | 117 | 117 | out_duration: 200, // Transition out duration | |
ready: function () { | 118 | 118 | ready: function() { | |
$('#new-card-input').focus(); | 119 | 119 | $('#new-card-input').focus(); | |
document.execCommand('selectAll', false, null); | 120 | 120 | document.execCommand('selectAll', false, null); | |
} | 121 | 121 | } | |
}; | 122 | 122 | }; | |
123 | 123 | |||
$(document).keydown(function (e) { | 124 | 124 | $(document).keydown(function(e) { | |
var keyed = e.which; | 125 | 125 | var keyed = e.which; | |
if (keyed == 67 && listenForC) { // "c" for compose | 126 | 126 | if (keyed == 67 && listenForC) { // "c" for compose | |
$scope.openNewCardModal(); | 127 | 127 | $scope.openNewCardModal(); | |
e.preventDefault(); | 128 | 128 | e.preventDefault(); | |
return false; | 129 | 129 | return false; | |
} else if (keyed == 27) { // clear on ESC | 130 | 130 | } else if (keyed == 27) { // clear on ESC | |
$scope.closeNewCardModal(); | 131 | 131 | $scope.closeNewCardModal(); | |
} | 132 | 132 | } | |
}); | 133 | 133 | }); | |
134 | 134 | |||
$scope.openNewCardModal = function () { | 135 | 135 | $scope.openNewCardModal = function() { | |
$('#newCard').openModal(modal_options); | 136 | 136 | $('#newCard').openModal(modal_options); | |
listenForC = false; | 137 | 137 | listenForC = false; | |
$('#new-card-input').html('Write a flashcard!'); | 138 | 138 | $('#new-card-input').html('Write a flashcard!'); | |
}; | 139 | 139 | }; | |
140 | 140 | |||
$scope.closeNewCardModal = function () { | 141 | 141 | $scope.closeNewCardModal = function() { | |
listenForC = true; | 142 | 142 | listenForC = true; | |
$('#new-card-input').html('').blur(); | 143 | 143 | $('#new-card-input').html('').blur(); | |
$('#newCard').closeModal(modal_options); | 144 | 144 | $('#newCard').closeModal(modal_options); | |
}; | 145 | 145 | }; | |
146 | 146 | |||
$('.tooltipped').tooltip({delay: 50}); | 147 | 147 | $('.tooltipped').tooltip({delay: 50}); | |
// the "href" attribute of .modal-trigger must specify the modal ID that wants to be triggered | 148 | 148 | // the "href" attribute of .modal-trigger must specify the modal ID that wants to be triggered | |
$('.modal-trigger').leanModal(modal_options); | 149 | 149 | $('.modal-trigger').leanModal(modal_options); | |
$('#new-card-input').on('keydown', function (e) { | 150 | 150 | $('#new-card-input').on('keydown', function(e) { | |
if (e.which == 13) { | 151 | 151 | if (e.which == 13) { | |
e.preventDefault(); | 152 | 152 | e.preventDefault(); | |
$scope.pushCard(); | 153 | 153 | $scope.pushCard(); | |
listenForC = true; | 154 | 154 | listenForC = true; | |
return false; | 155 | 155 | return false; | |
} else { | 156 | 156 | } else { | |
157 | 157 | |||
} | 158 | 158 | } | |
}); | 159 | 159 | }); | |
$('button#blank-selected').click(function () { | 160 | 160 | $('button#blank-selected').click(function() { | |
console.log(window.getSelection()); | 161 | 161 | console.log(window.getSelection()); | |
document.execCommand('bold'); | 162 | 162 | document.execCommand('bold'); | |
}); | 163 | 163 | }); | |
$scope.refreshCards(); | 164 | 164 | $scope.refreshCards(); | |
$scope.newCardBlanks = []; | 165 | 165 | $scope.newCardBlanks = []; | |
$scope.refreshNewCardInput = function () { | 166 | 166 | $scope.refreshNewCardInput = function() { | |
$scope.newCardText = $('#new-card-input').text(); | 167 | 167 | $scope.newCardText = $('#new-card-input').text(); | |
$scope.submit_enabled = $scope.newCardText.length >= 5 && $scope.newCardText.length <= 160; | 168 | 168 | $scope.submit_enabled = $scope.newCardText.length >= 5 && $scope.newCardText.length <= 160; | |
var i = 0; | 169 | 169 | var i = 0; | |
$('#new-card-input')[0].childNodes.forEach(function (node) { | 170 | 170 | $('#new-card-input')[0].childNodes.forEach(function(node) { | |
if (typeof node.data == 'undefined') { | 171 | 171 | if (typeof node.data == 'undefined') { | |
console.log('undefined node'); | 172 | 172 | console.log('undefined node'); | |
} | 173 | 173 | } | |
node = $(node)[0]; | 174 | 174 | node = $(node)[0]; | |
$scope.newCardBlanks = []; | 175 | 175 | $scope.newCardBlanks = []; | |
if (node.tagName == 'B') { | 176 | 176 | if (node.tagName == 'B') { | |
var text = $(node).text(); | 177 | 177 | var text = $(node).text(); | |
console.log(text.length, text); | 178 | 178 | console.log(text.length, text); | |
var leftspaces = 0, rightspaces = 0; | 179 | 179 | var leftspaces = 0, rightspaces = 0; | |
// awful way to find the first non-space character from the left or the right. thanks.js | 180 | 180 | // awful way to find the first non-space character from the left or the right. thanks.js | |
while (text[leftspaces] == ' ' || text[leftspaces] == '\xa0') leftspaces++; | 181 | 181 | while (text[leftspaces] == ' ' || text[leftspaces] == '\xa0') leftspaces++; | |
while (text[text.length - 1 - rightspaces] == ' ' || text[text.length - 1 - rightspaces] == '\xa0') rightspaces++; | 182 | 182 | while (text[text.length - 1 - rightspaces] == ' ' || text[text.length - 1 - rightspaces] == '\xa0') rightspaces++; | |
if (leftspaces != text.length) $scope.newCardBlanks.push([i + leftspaces, i + text.length - rightspaces]); | 183 | 183 | if (leftspaces != text.length) $scope.newCardBlanks.push([i + leftspaces, i + text.length - rightspaces]); | |
i += text.length; | 184 | 184 | i += text.length; | |
} else if (!node.data) { | 185 | 185 | } else if (!node.data) { | |
console.log('weird node', node); | 186 | 186 | console.log('weird node', node); | |
i += $(node).text().length; | 187 | 187 | i += $(node).text().length; | |
} else { | 188 | 188 | } else { | |
i += node.data.length; | 189 | 189 | i += node.data.length; | |
} | 190 | 190 | } | |
}); | 191 | 191 | }); | |
$scope.newCardBlanks.sort(function (a, b) { | 192 | 192 | $scope.newCardBlanks.sort(function(a, b) { | |
return a[0] - b[0]; | 193 | 193 | return a[0] - b[0]; | |
}); | 194 | 194 | }); | |
var i = 0; | 195 | 195 | var i = 0; | |
newtext = ''; | 196 | 196 | newtext = ''; | |
$scope.newCardBlanks.forEach(function (blank) { | 197 | 197 | $scope.newCardBlanks.forEach(function(blank) { | |
newtext += $scope.newCardText.slice(i, blank[0]); | 198 | 198 | newtext += $scope.newCardText.slice(i, blank[0]); | |
newtext += '<b>' + $scope.newCardText.slice(blank[0], blank[1]) + '</b>'; | 199 | 199 | newtext += '<b>' + $scope.newCardText.slice(blank[0], blank[1]) + '</b>'; | |
i = blank[1]; | 200 | 200 | i = blank[1]; | |
}); | 201 | 201 | }); | |
newtext += $scope.newCardText.slice(i); | 202 | 202 | newtext += $scope.newCardText.slice(i); | |
//$scope.newCardFormattedText = newtext; | 203 | 203 | //$scope.newCardFormattedText = newtext; | |
}; | 204 | 204 | }; | |
$scope.shuffleCards = function () { | 205 | 205 | $scope.shuffleCards = function() { | |
$timeout(function () { | 206 | 206 | $timeout(function() { | |
(function (o) { | 207 | 207 | (function(o) { | |
for (var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x); | 208 | 208 | for (var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x); | |
return o; | 209 | 209 | return o; | |
})($scope.cardCols[0]); | 210 | 210 | })($scope.cardCols[0]); | |
}); | 211 | 211 | }); | |
}; | 212 | 212 | }; | |
$scope.$on('$destroy', function () { | 213 | 213 | $scope.$on('$destroy', function() { | |
$scope.feed_ws.close(); | 214 | 214 | $scope.feed_ws.close(); | |
}); | 215 | 215 | }); | |
}); | 216 | 216 | }); | |
217 | 217 | |||
scripts/UserService.js
View file @
cf476b0
angular.module('flashy.UserService', ['ui.router']). | 1 | 1 | angular.module('flashy.UserService', ['ui.router']). | |
service('UserService', function($rootScope, $http, $q) { | 2 | 2 | service('UserService', function($rootScope, $http, $q) { | |
var deferred = $q.defer(); | 3 | 3 | var deferred = $q.defer(); | |
var _user = false; | 4 | 4 | var _user = false; | |
var login = function(data) { | 5 | 5 | var login = function(data) { | |
if (data.locked) { | 6 | 6 | if (data.locked) { | |
$rootScope.UserService.showLockedMessage(); | 7 | 7 | $rootScope.UserService.showLockedMessage(); | |
return deferred.reject('account locked'); | 8 | 8 | return deferred.reject('account locked'); | |
} | 9 | 9 | } | |
if (!data.is_confirmed) { | 10 | 10 | if (!data.is_confirmed) { | |
Materialize.toast('Please verify your email address! ' + | 11 | 11 | Materialize.toast('Please verify your email address! ' + | |
'<a class="btn-flat cyan-text" onclick="rootscope.UserService.resendConfirmationEmail()">' + | 12 | 12 | '<a class="btn-flat cyan-text" onclick="rootscope.UserService.resendConfirmationEmail()">' + | |
'Resend Verification Email</a>', 4000); | 13 | 13 | 'Resend Verification Email</a>', 4000); | |
} | 14 | 14 | } | |
_user = data; | 15 | 15 | _user = data; | |
_user.sectionIdList = _user.sections.map(function(x) { | 16 | 16 | _user.sectionIdList = _user.sections.map(function(x) { | |
return x.id; | 17 | 17 | return x.id; | |
}); | 18 | 18 | }); | |
deferred.resolve(data); | 19 | 19 | deferred.resolve(data); | |
}; | 20 | 20 | }; | |
this.login = login; | 21 | 21 | this.login = login; | |
$http.get('/api/me/').success(function(data) { | 22 | 22 | $http.get('/api/me/').success(function(data) { | |
console.log('user is logged in!'); | 23 | 23 | console.log('user is logged in!'); | |
login(data); | 24 | 24 | login(data); | |
}).error(function(data) { | 25 | 25 | }).error(function(data) { | |
console.log(data); | 26 | 26 | console.log(data); | |
console.log('not logged in yet: ' + data.detail); | 27 | 27 | console.log('not logged in yet: ' + data.detail); | |
_user = {email: false}; | 28 | 28 | _user = {email: false}; | |
deferred.resolve(_user); | 29 | 29 | deferred.resolve(_user); | |
}); | 30 | 30 | }); | |
31 | 31 | |||
this.isResolved = function() { | 32 | 32 | this.isResolved = function() { | |
return !!_user; | 33 | 33 | return !!_user; | |
}; | 34 | 34 | }; | |
this.getUserData = function() { | 35 | 35 | this.getUserData = function() { | |
if (this.isResolved()) return _user; | 36 | 36 | if (this.isResolved()) return _user; | |
else return deferred.promise; | 37 | 37 | else return deferred.promise; | |
}; | 38 | 38 | }; | |
this.hasVerifiedEmail = function() { | 39 | 39 | this.hasVerifiedEmail = function() { | |
return this.isResolved() && _user.is_confirmed; | 40 | 40 | return this.isResolved() && _user.is_confirmed; | |
}; | 41 | 41 | }; | |
this.logout = function($state) { | 42 | 42 | this.logout = function($state) { | |
$http.post('/api/logout/').success(function() { | 43 | 43 | $http.post('/api/logout/').success(function() { | |
if (!_user.locked)Materialize.toast('Logged out!', 1000); | 44 | 44 | if (!_user.locked)Materialize.toast('Logged out!', 1000); | |
}).error(function() { | 45 | 45 | }).error(function() { | |
console.log('Problem logging out'); | 46 | 46 | console.log('Problem logging out'); | |
}); | 47 | 47 | }); | |
_user = false; | 48 | 48 | _user = false; | |
deferred.resolve({}); | 49 | 49 | deferred.resolve({}); | |
$state.go('login'); | 50 | 50 | $state.go('login'); | |
}; | 51 | 51 | }; | |
this.addClass = function(section) { | 52 | 52 | this.addClass = function(section) { | |
_user.sections.push(section); | 53 | 53 | _user.sections.push(section); | |
_user.sectionIdList.push(section.id); | 54 | 54 | _user.sectionIdList.push(section.id); | |
}; | 55 | 55 | }; | |
this.isLoggedIn = function() { | 56 | 56 | this.isLoggedIn = function() { | |
rv = this.isResolved() && _user.email; | 57 | 57 | rv = this.isResolved() && _user.email; | |
return rv; | 58 | 58 | return rv; | |
}; | 59 | 59 | }; | |
this.isInSection = function(sectionId) { | 60 | 60 | this.isInSection = function(sectionId) { | |
return (_user.sectionIdList.indexOf(sectionId) >= 0); | 61 | 61 | return (_user.sectionIdList.indexOf(sectionId) >= 0); | |
}; | 62 | 62 | }; | |
this.redirectToDefaultState = function($state) { | 63 | 63 | this.redirectToDefaultState = function($state) { | |
console.log('redirecting user to their default state'); | 64 | 64 | console.log('redirecting user to their default state'); | |
if (!this.isLoggedIn()) return $state.go('login'); | 65 | 65 | if (!this.isLoggedIn()) return $state.go('login'); | |
if (!_user.sections.length) return $state.go('addclass'); | 66 | 66 | if (!_user.sections.length) return $state.go('addclass'); | |
last_state = localStorage.getItem('last_state'); | 67 | 67 | last_state = localStorage.getItem('last_state'); | |
if (last_state) { | 68 | 68 | if (last_state) { | |
last_state_params = JSON.parse(localStorage.getItem('last_state_params')); | 69 | 69 | last_state_params = JSON.parse(localStorage.getItem('last_state_params')); | |
if (last_state_params.sectionId && this.authorizedFor(last_state, last_state_params)) { | 70 | 70 | if (last_state_params.sectionId && this.authorizedFor(last_state, last_state_params)) { | |
return $state.go(last_state, JSON.parse(localStorage.getItem('last_state_params'))); | 71 | 71 | return $state.go(last_state, JSON.parse(localStorage.getItem('last_state_params'))); | |
} | 72 | 72 | } | |
} | 73 | 73 | } | |
$state.go('feed', {sectionId: _user.sections[0].id}); | 74 | 74 | $state.go('feed', {sectionId: _user.sections[0].id}); | |
}; | 75 | 75 | }; | |
this.authorizedFor = function(state, stateParams) { | 76 | 76 | this.authorizedFor = function(state, stateParams) { | |
if (['feed', 'deck', 'cardlist'].indexOf(state.name) >= 0) { | 77 | 77 | if (['feed', 'deck', 'cardlist'].indexOf(state.name) >= 0) { | |
if (_user.sectionIdList.indexOf(stateParams.sectionId) < 0) { | 78 | 78 | console.log('checking whether', stateParams, 'in', _user.sectionIdList); | |
79 | if (_user.sectionIdList.indexOf(parseInt(stateParams.sectionId)) < 0) { | |||
return false; | 79 | 80 | return false; | |
} | 80 | 81 | } | |
} | 81 | 82 | } | |
return true; | 82 | 83 | return true; | |
}; | 83 | 84 | }; | |
this.showLockedMessage = function() { | 84 | 85 | this.showLockedMessage = function() { | |
Materialize.toast('You must verify your email address before continuing.' + | 85 | 86 | Materialize.toast('You must verify your email address before continuing.' + | |
'<a class="btn-flat cyan-text" onclick="rootscope.UserService.resendConfirmationEmail()">' + | 86 | 87 | '<a class="btn-flat cyan-text" onclick="rootscope.UserService.resendConfirmationEmail()">' + | |
'Resend Verification Email</a>', 4000); | 87 | 88 | 'Resend Verification Email</a>', 4000); | |
}; | 88 | 89 | }; | |
this.noAuthRequired = function(state) { | 89 | 90 | this.noAuthRequired = function(state) { | |
if (['verifyemail', 'login'].indexOf(state.name) >= 0) { | 90 | 91 | if (['verifyemail', 'login'].indexOf(state.name) >= 0) { | |
return true; | 91 | 92 | return true; | |
} | 92 | 93 | } | |
return false; | 93 | 94 | return false; | |
}; | 94 | 95 | }; | |
this.resendConfirmationEmail = function() { | 95 | 96 | this.resendConfirmationEmail = function() { | |
console.log('Requesting resend of confirmation email'); | 96 | 97 | console.log('Requesting resend of confirmation email'); | |
$http.post('/api/resend_confirmation_email/').success(function() { | 97 | 98 | $http.post('/api/resend_confirmation_email/').success(function() { | |
Materialize.toast('Resent confirmation email! Check your spam folder too.', 4000); | 98 | 99 | Materialize.toast('Resent confirmation email! Check your spam folder too.', 4000); | |
}); | 99 | 100 | }); | |
}; | 100 | 101 | }; | |
}); | 101 | 102 | }); |