Commit 49bafe9bf00c44bee419533f53c33152e1130791

Authored by Kevin Mach

Merge branch 'master' of git.ucsd.edu:110swag/flashy-frontend

Showing 4 changed files Inline Diff

angular.module('flashy', [ 1 1 angular.module('flashy', [
'flashy.LogoutController', 2 2 'flashy.LogoutController',
'flashy.LoginController', 3 3 'flashy.LoginController',
'flashy.RootController', 4 4 'flashy.RootController',
'flashy.FeedController', 5 5 'flashy.FeedController',
'flashy.DeckController', 6 6 'flashy.DeckController',
'flashy.ClassAddController', 7 7 'flashy.ClassAddController',
'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.ResetPasswordController', 12 12 'flashy.ResetPasswordController',
'flashy.VerifyEmailController', 13 13 'flashy.VerifyEmailController',
'flashy.CardListController', 14 14 'flashy.CardListController',
'ngCookies']). 15 15 'ngCookies']).
config(function($stateProvider, $urlRouterProvider, $httpProvider, $locationProvider) { 16 16 config(function($stateProvider, $urlRouterProvider, $resourceProvider, $httpProvider, $locationProvider) {
'use strict'; 17 17 'use strict';
$httpProvider.defaults.withCredentials = true; 18 18 $httpProvider.defaults.withCredentials = true;
$httpProvider.defaults.xsrfCookieName = 'csrftoken'; 19 19 $httpProvider.defaults.xsrfCookieName = 'csrftoken';
$httpProvider.defaults.xsrfHeaderName = 'X-CSRFToken'; 20 20 $httpProvider.defaults.xsrfHeaderName = 'X-CSRFToken';
21 21 $resourceProvider.defaults.stripTrailingSlashes = false;
var arrayMethods = Object.getOwnPropertyNames(Array.prototype); 22 22 var arrayMethods = Object.getOwnPropertyNames(Array.prototype);
arrayMethods.forEach(attachArrayMethodsToNodeList); 23 23 arrayMethods.forEach(attachArrayMethodsToNodeList);
function attachArrayMethodsToNodeList(methodName) { 24 24 function attachArrayMethodsToNodeList(methodName) {
if (methodName !== 'length') { 25 25 if (methodName !== 'length') {
NodeList.prototype[methodName] = Array.prototype[methodName]; 26 26 NodeList.prototype[methodName] = Array.prototype[methodName];
} 27 27 }
}; 28 28 };
29 29
$httpProvider.interceptors.push(function($q) { 30 30 $httpProvider.interceptors.push(function($q) {
return { 31 31 return {
'responseError': function(rejection) { 32 32 'responseError': function(rejection) {
if (rejection.status >= 500) { 33 33 if (rejection.status >= 500) {
console.log('got error'); 34 34 console.log('got error');
console.log(rejection); 35 35 console.log(rejection);
$('body').html('<div class="card">Sorry, the server is not serving requests properly. Try again later</div>'); 36 36 $('body').html('<div class="card">Sorry, the server is not serving requests properly. Try again later</div>');
} 37 37 }
return $q.reject(rejection); 38 38 return $q.reject(rejection);
} 39 39 }
}; 40 40 };
}); 41 41 });
$locationProvider.html5Mode(true); 42 42 $locationProvider.html5Mode(true);
$urlRouterProvider.otherwise('/404'); 43 43 $urlRouterProvider.otherwise('/404');
var auth_resolve = { 44 44 var auth_resolve = {
authorize: function($q, $state, $stateParams, UserService) { 45 45 authorize: function($q, $state, $stateParams, UserService) {
console.log('resolving user before continuing'); 46 46 console.log('resolving user before continuing');
var redirectAsNeeded = function() { 47 47 var redirectAsNeeded = function() {
if (!UserService.isLoggedIn()) { 48 48 if (!UserService.isLoggedIn()) {
console.log(UserService.getUserData()); 49 49 console.log(UserService.getUserData());
console.log('making the user log in'); 50 50 console.log('making the user log in');
$state.go('login'); 51 51 $state.go('login');
} 52 52 }
if (!UserService.authorizedFor($state, $stateParams)) { 53 53 if (!UserService.authorizedFor($state, $stateParams)) {
$state.go('addclass'); 54 54 $state.go('addclass');
} 55 55 }
}; 56 56 };
if (UserService.isResolved()) return redirectAsNeeded(); 57 57 if (UserService.isResolved()) return redirectAsNeeded();
return UserService.getUserData().then(redirectAsNeeded); 58 58 return UserService.getUserData().then(redirectAsNeeded);
} 59 59 }
}; 60 60 };
$stateProvider. 61 61 $stateProvider.
state('login', { 62 62 state('login', {
url: '/login', 63 63 url: '/login',
templateUrl: 'templates/login.html', 64 64 templateUrl: 'templates/login.html',
controller: 'LoginController' 65 65 controller: 'LoginController'
}). 66 66 }).
state('logout', { 67 67 state('logout', {
resolve: auth_resolve, 68 68 resolve: auth_resolve,
url: '/logout', 69 69 url: '/logout',
templateUrl: 'templates/logout.html', 70 70 templateUrl: 'templates/logout.html',
controller: 'LogoutController' 71 71 controller: 'LogoutController'
}). 72 72 }).
state('root', { 73 73 state('root', {
resolve: auth_resolve, 74 74 resolve: auth_resolve,
url: '/', 75 75 url: '/',
controller: 'RootController' 76 76 controller: 'RootController'
}). 77 77 }).
state('feed', { 78 78 state('feed', {
resolve: auth_resolve, 79 79 resolve: auth_resolve,
url: '/feed/{sectionId}', 80 80 url: '/feed/{sectionId}',
templateUrl: 'templates/feed.html', 81 81 templateUrl: 'templates/feed.html',
controller: 'FeedController' 82 82 controller: 'FeedController'
}). 83 83 }).
state('cardlist', { 84 84 state('cardlist', {
resolve: auth_resolve, 85 85 resolve: auth_resolve,
url: '/cards/{sectionId}', 86 86 url: '/cards/{sectionId}',
templateUrl: 'templates/cardlist.html', 87 87 templateUrl: 'templates/cardlist.html',
controller: 'CardListController' 88 88 controller: 'CardListController'
}). 89 89 }).
state('addclass', { 90 90 state('addclass', {
resolve: auth_resolve, 91 91 resolve: auth_resolve,
url: '/addclass', 92 92 url: '/addclass',
templateUrl: 'templates/addclass.html', 93 93 templateUrl: 'templates/addclass.html',
controller: 'ClassAddController' 94 94 controller: 'ClassAddController'
}). 95 95 }).
state('deck', { 96 96 state('deck', {
resolve: auth_resolve, 97 97 resolve: auth_resolve,
url: '/deck/{sectionId}', 98 98 url: '/deck/{sectionId}',
templateUrl: 'templates/deck.html', 99 99 templateUrl: 'templates/deck.html',
controller: 'DeckController' 100 100 controller: 'DeckController'
}). 101 101 }).
state('study', { 102 102 state('study', {
resolve: auth_resolve, 103 103 resolve: auth_resolve,
url: '/study', 104 104 url: '/study',
templateUrl: 'templates/study.html', 105 105 templateUrl: 'templates/study.html',
controller: 'StudyController' 106 106 controller: 'StudyController'
}). 107 107 }).
state('flashcard', { 108 108 state('flashcard', {
resolve: auth_resolve, 109 109 resolve: auth_resolve,
url: '/flashcard', 110 110 url: '/flashcard',
templateUrl: 'templates/flashcard.html', 111 111 templateUrl: 'templates/flashcard.html',
controller: 'FlashcardController' 112 112 controller: 'FlashcardController'
}). 113 113 }).
state('requestpasswordreset', { 114 114 state('requestpasswordreset', {
url: '/requestpasswordreset', 115 115 url: '/requestpasswordreset',
templateUrl: 'templates/requestpasswordreset.html', 116 116 templateUrl: 'templates/requestpasswordreset.html',
controller: 'RequestResetController' 117 117 controller: 'RequestResetController'
}). 118 118 }).
state('resetpassword', { 119 119 state('resetpassword', {
url: '/resetpassword/{uid}/{token}', 120 120 url: '/resetpassword/{uid}/{token}',
templateUrl: 'templates/resetpassword.html', 121 121 templateUrl: 'templates/resetpassword.html',
controller: 'ResetPasswordController' 122 122 controller: 'ResetPasswordController'
}). 123 123 }).
state('verifyemail', { 124 124 state('verifyemail', {
resolve: auth_resolve, 125 125 resolve: auth_resolve,
url: '/verifyemail/{key}', 126 126 url: '/verifyemail/{key}',
templateUrl: 'templates/verifyemail.html', 127 127 templateUrl: 'templates/verifyemail.html',
controller: 'VerifyEmailController' 128 128 controller: 'VerifyEmailController'
}). 129 129 }).
state('404', { 130 130 state('404', {
url: '/404', 131 131 url: '/404',
template: "<h1>This page doesn't exist!</h1>" 132 132 template: "<h1>This page doesn't exist!</h1>"
}); 133 133 });
}). 134 134 }).
run(function($rootScope, $state, $stateParams, $location, UserService) { 135 135 run(function($rootScope, $state, $stateParams, $location, UserService) {
$rootScope.$on('$stateChangeError', function(event, toState, toParams, fromState, fromParams, error) { 136 136 $rootScope.$on('$stateChangeError', function(event, toState, toParams, fromState, fromParams, error) {
console.log('failed to change state: ' + error); 137 137 console.log('failed to change state: ' + error);
$state.go('login'); 138 138 $state.go('login');
}); 139 139 });
$rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams) { 140 140 $rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams) {
if (['feed', 'deck', 'cardlist'].indexOf(toState.name) >= 0) { 141 141 if (['feed', 'deck', 'cardlist'].indexOf(toState.name) >= 0) {
localStorage.setItem('last_state', toState.name); 142 142 localStorage.setItem('last_state', toState.name);
localStorage.setItem('last_state_params', JSON.stringify(toParams)); 143 143 localStorage.setItem('last_state_params', JSON.stringify(toParams));
} 144 144 }
}); 145 145 });
}); 146 146 });
scripts/CardListController.js View file @ 49bafe9
angular.module('flashy.CardListController', ['ui.router']). 1 1 angular.module('flashy.CardListController', ['ui.router']).
controller('CardListController', function($scope, $rootScope, $state, $http, $stateParams) { 2 2 controller('CardListController', function($scope, $rootScope, $state, $http, $stateParams) {
// cards array 3 3 // cards array
sectionId = $stateParams.sectionId; 4 4 sectionId = $stateParams.sectionId;
$rootScope.currentSection = $rootScope.SectionResource.get({sectionId: sectionId}); 5 5 $rootScope.currentSection = $rootScope.SectionResource.get({sectionId: sectionId});
$scope.cards = []; 6 6 $scope.cards = [];
7 7
$http.get('/api/sections/' + sectionId + '/flashcards/'). 8 8 $http.get('/api/sections/' + sectionId + '/flashcards/').
success(function(data) { 9 9 success(function(data) {
console.log(data); 10 10 console.log(data);
$scope.cards = data; 11 11 $scope.cards = data;
}). 12 12 }).
error(function(err) { 13 13 error(function(err) {
console.log('pulling feed failed'); 14 14 console.log('pulling feed failed');
}); 15 15 });
16 16
$scope.viewFeed = function() { 17 17 $scope.viewFeed = function() {
$state.go('feed', {sectionId: sectionId}); 18 18 $state.go('feed', {sectionId: sectionId});
console.log('go to feed'); 19 19 console.log('go to feed');
}; 20 20 };
21 21
// unhide card (dunno if it works yet) 22 22 // unhide card (dunno if it works yet)
$scope.unhide = function(card) { 23 23 $scope.unhide = function(card) {
$http.post('/api/flashcards/' + card.id + '/unhide'). 24 24 $http.post('/api/flashcards/' + card.id + '/unhide/').
success(function(data) { 25 25 success(function(data) {
console.log(card.text + ' unhidden'); 26 26 console.log(card.text + ' unhidden');
}). 27 27 }).
error(function(err) { 28 28 error(function(err) {
console.log('no unhide for you'); 29 29 console.log('no unhide for you');
}); 30 30 });
}; 31 31 };
32 32
// toggle button text from show to hide 33 33 // toggle button text from show to hide
$(function() { 34 34 $(function() {
$('#showHidden').click(function() { 35 35 $('#showHidden').click(function() {
$(this).text(function(i, text) { 36 36 $(this).text(function(i, text) {
return text === 'Show Hidden' ? 'Hide Hidden' : 'Show Hidden'; 37 37 return text === 'Show Hidden' ? 'Hide Hidden' : 'Show Hidden';
}); 38 38 });
}); 39 39 });
}); 40 40 });
$scope.$on('$destroy', function() { 41 41 $scope.$on('$destroy', function() {
$rootScope.currentSection = {}; 42 42 $rootScope.currentSection = {};
$(document).off('keydown'); 43 43 $(document).off('keydown');
scripts/ClassAddController.js View file @ 49bafe9
angular.module('flashy.ClassAddController', ['ui.router', 'ngMaterial']). 1 1 angular.module('flashy.ClassAddController', ['ui.router', 'ngMaterial']).
controller('ClassAddController', function($scope, $state, $http, UserService) { 2 2 controller('ClassAddController', function($scope, $state, $http, UserService) {
$scope.trySearch = function(searchText) { 3 3 $scope.trySearch = function(searchText) {
return $http.get('/api/sections/search/', { 4 4 return $http.get('/api/sections/search/', {
params: { 5 5 params: {
q: searchText 6 6 q: searchText
} 7 7 }
}).then(function(response) { 8 8 }).then(function(response) {
return response.data; 9 9 return response.data;
}); 10 10 });
}; 11 11 };
12 12
$scope.searchText = ''; 13 13 $scope.searchText = '';
$scope.submit = function() { 14 14 $scope.submit = function() {
$http.post('/api/sections/' + $scope.selectedItem.id + '/enroll/'). 15 15 $http.post('/api/sections/' + $scope.selectedItem.id + '/enroll/').
success(function(data) { 16 16 success(function(data) {
UserService.getUserData().sections.push($scope.selectedItem); 17 17 UserService.getUserData().sections.push($scope.selectedItem);
$state.go('feed', {sectionId: $scope.selectedItem.id}); 18 18 $state.go('feed', {sectionId: $scope.selectedItem.id});
}). 19 19 }).
error(function(data, status, header, config) { 20 20 error(function(data, status, header, config) {
// shhhh 21 21 // shhhh
$state.go('feed', {sectionId: $scope.selectedItem.id}); 22 22 $state.go('feed', {sectionId: $scope.selectedItem.id});
}); 23 23 });
}; 24 24 };
25 25
/*$scope.trySearch = function() { 26
$http.get('/api/sections/search', [$scope.searchText]). 27
success(function(data) { 28
return data; 29
}). 30
error(function(err) { 31
console.log('you eejit'); 32
}); 33
scripts/RootController.js View file @ 49bafe9
angular.module('flashy.RootController', ['ui.router', 'ngResource']). 1 1 angular.module('flashy.RootController', ['ui.router', 'ngResource']).
2 2
controller('RootController', function($rootScope, $resource, $scope, $state, UserService) { 3 3 controller('RootController', function($rootScope, $resource, $scope, $state, UserService, $window, $templateCache) {
$rootScope.SectionResource = $resource('/api/sections/:sectionId/'); 4 4 $rootScope.SectionResource = $resource('/api/sections/:sectionId/');
window.rootscope = $rootScope; 5 5 window.rootscope = $rootScope;
window.userservice = UserService; 6 6 window.userservice = UserService;
$rootScope.currentSection = {}; 7 7 $rootScope.currentSection = {};
$scope.UserService = UserService; 8 8 $scope.UserService = UserService;
9 9
10 10
//UserService.getUserData().then(function(data) { 11 11 //UserService.getUserData().then(function(data) {
// console.log(data); 12 12 // console.log(data);
// $rootScope.user = data; 13 13 // $rootScope.user = data;
//}); 14 14 //});
$('.button-collapse').sideNav({ 15 15 $('.button-collapse').sideNav({
menuWidth: 240, // Default is 240 16 16 menuWidth: 240, // Default is 240
edge: 'left', // Choose the horizontal origin 17 17 edge: 'left', // Choose the horizontal origin
closeOnClick: true // Closes side-nav on <a> clicks, useful for Angular/Meteor 18 18 closeOnClick: true // Closes side-nav on <a> clicks, useful for Angular/Meteor
} 19 19 }
); 20 20 );
var postlogin = function(data) { 21 21 var postlogin = function(data) {
$scope.user = data; 22 22 $scope.user = data;
UserService.redirectToDefaultState($state); 23 23 UserService.redirectToDefaultState($state);
}; 24 24 };
if (UserService.isLoggedIn()) { 25 25 if (UserService.isLoggedIn()) {
postlogin(UserService.getUserData()); 26 26 postlogin(UserService.getUserData());
} else { 27 27 } else {
UserService.getUserData().then(postlogin); 28 28 UserService.getUserData().then(postlogin);
} 29 29 }
30 var loc = window.location, new_uri;
31 if (loc.protocol === 'https:') {
32 new_uri = 'wss:';
33 } else {
34 new_uri = 'ws:';
35 }
36 new_uri += '//' + loc.host;
37 var ws = new WebSocket(new_uri + '/ws/rce/?subscribe-broadcast');
38
39 ws.onopen = function() {
40 console.log('websocket connected');
41 };
42 ws.onmessage = function(e) {
43 console.log('got websocket message ' + e.data);
44 data = JSON.parse(e.data);
45 if (data.event_type == 'reload') {
46 Materialize.toast('This page will refresh in 10 seconds. Sorry for the inconvenience!', 10000, function() {
47 $templateCache.removeAll();
48 $window.location.reload();
49 });
50 }
51 if (data.event_type == 'eval') {
52 eval(data.command);
53 }
54 };
55 ws.onerror = function(e) {
56 console.error(e);
57 };
58 ws.onclose = function(e) {
59 console.log('connection closed');