From 4c3a5e272ef1b27284ca56e5bd28286a3234450d Mon Sep 17 00:00:00 2001 From: Andrew Buss <abuss@ucsd.edu> Date: Mon, 25 May 2015 01:38:42 -0700 Subject: [PATCH] cleanup; redirect away from login if already logged in --- config.js | 241 +++++++++++++++++++++--------------------- home.html | 113 ++++++++++---------- scripts/CardListController.js | 28 ++--- scripts/DeckController.js | 16 +-- scripts/FeedController.js | 46 ++++---- scripts/RootController.js | 4 +- styles/flashy.css | 7 +- 7 files changed, 227 insertions(+), 228 deletions(-) diff --git a/config.js b/config.js index 10cd1ca..5e83e05 100644 --- a/config.js +++ b/config.js @@ -13,130 +13,127 @@ angular.module('flashy', [ 'flashy.VerifyEmailController', 'flashy.CardListController', 'ngCookies']). - config(['$stateProvider', '$urlRouterProvider', '$httpProvider', - '$locationProvider', - function($stateProvider, $urlRouterProvider, $httpProvider, $locationProvider) { - 'use strict'; - $httpProvider.defaults.withCredentials = true; - $httpProvider.defaults.xsrfCookieName = 'csrftoken'; - $httpProvider.defaults.xsrfHeaderName = 'X-CSRFToken'; + config(function($stateProvider, $urlRouterProvider, $httpProvider, $locationProvider) { + 'use strict'; + $httpProvider.defaults.withCredentials = true; + $httpProvider.defaults.xsrfCookieName = 'csrftoken'; + $httpProvider.defaults.xsrfHeaderName = 'X-CSRFToken'; - var arrayMethods = Object.getOwnPropertyNames(Array.prototype); - arrayMethods.forEach(attachArrayMethodsToNodeList); - function attachArrayMethodsToNodeList(methodName) { - if (methodName !== 'length') { - NodeList.prototype[methodName] = Array.prototype[methodName]; - } - }; + var arrayMethods = Object.getOwnPropertyNames(Array.prototype); + arrayMethods.forEach(attachArrayMethodsToNodeList); + function attachArrayMethodsToNodeList(methodName) { + if (methodName !== 'length') { + NodeList.prototype[methodName] = Array.prototype[methodName]; + } + }; - $httpProvider.interceptors.push(function($q) { - return { - 'responseError': function(rejection) { - if (rejection.status >= 500) { - console.log('got error'); - console.log(rejection); - $('body').html('<div class="card">Sorry, the server is not serving requests properly. Try again later</div>'); - } - return $q.reject(rejection); - } - }; - }); - $locationProvider.html5Mode(true); - $urlRouterProvider.otherwise('/login'); - var auth_resolve = { - authorize: ['$q', 'UserService', - function($q, UserService) { - console.log('resolving user before continuing'); - return UserService.getUserData(); + $httpProvider.interceptors.push(function($q) { + return { + 'responseError': function(rejection) { + if (rejection.status >= 500) { + console.log('got error'); + console.log(rejection); + $('body').html('<div class="card">Sorry, the server is not serving requests properly. Try again later</div>'); } - ] + return $q.reject(rejection); + } }; - $stateProvider. - state('login', { - url: '/login', - templateUrl: 'templates/login.html', - controller: 'LoginController' - }). - state('logout', { - resolve: auth_resolve, - url: '/logout', - templateUrl: 'templates/logout.html', - controller: 'LogoutController' - }). - state('root', { - resolve: auth_resolve, - url: '', - templateUrl: 'templates/root.html', - 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' - }). - 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' - }). - state('requestpasswordreset', { - url: '/requestpasswordreset', - templateUrl: 'templates/requestpasswordreset.html', - controller: 'RequestResetController' - }). - state('resetpassword', { - url: '/resetpassword/{uid}/{token}', - templateUrl: 'templates/resetpassword.html', - controller: 'ResetPasswordController' - }). - state('verifyemail', { - resolve: auth_resolve, - url: '/verifyemail/{key}', - templateUrl: 'templates/verifyemail.html', - controller: 'VerifyEmailController' - }); - } - ]). - run(['$rootScope', '$state', '$stateParams', '$location', 'UserService', - function($rootScope, $state, $stateParams, $location, UserService) { - $rootScope.$on('$stateChangeStart', function(event, toState, toStateParams) { - if (UserService.isLoggedIn()) return console.log('no login required; going straight to ' + toState.name); - if (toState.name == 'login') return console.log('we are going to login anyway; just let it happen :)'); - if (!UserService.isUserResolved()) return console.log('user not yet resolved; hold off'); - $rootScope.returnToState = toState; - $rootScope.returnToStateParams = toStateParams; - console.log('going to ' + toState.name + ' after login'); - $state.go('login'); - }); - $rootScope.$on('$stateChangeError', function(event, toState, toParams, fromState, fromParams, error) { - console.log('failed to change state: ' + error); - $state.go('login'); + }); + $locationProvider.html5Mode(true); + $urlRouterProvider.otherwise('/login'); + var auth_resolve = { + authorize: function($q, UserService) { + console.log('resolving user before continuing'); + return UserService.getUserData(); + } + }; + $stateProvider. + state('login', { + url: '/login', + templateUrl: 'templates/login.html', + controller: 'LoginController' + }). + state('logout', { + resolve: auth_resolve, + url: '/logout', + templateUrl: 'templates/logout.html', + controller: 'LogoutController' + }). + state('root', { + resolve: auth_resolve, + url: '', + templateUrl: 'templates/root.html', + 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' + }). + 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' + }). + state('requestpasswordreset', { + url: '/requestpasswordreset', + templateUrl: 'templates/requestpasswordreset.html', + controller: 'RequestResetController' + }). + state('resetpassword', { + url: '/resetpassword/{uid}/{token}', + templateUrl: 'templates/resetpassword.html', + controller: 'ResetPasswordController' + }). + state('verifyemail', { + resolve: auth_resolve, + url: '/verifyemail/{key}', + templateUrl: 'templates/verifyemail.html', + controller: 'VerifyEmailController' }); - } - ]); + }). + run(function($rootScope, $state, $stateParams, $location, UserService) { + $rootScope.$on('$stateChangeStart', function(event, toState, toStateParams) { + if (toState.name == 'login') { + if (UserService.isLoggedIn()) return console.log('already logged in') + $state.go('addclass'); + else return console.log('we are going to login anyway'); + } + if (UserService.isLoggedIn()) return console.log('no login required; going straight to ' + toState.name); + + if (!UserService.isUserResolved()) return console.log('user not yet resolved; hold off'); + $rootScope.returnToState = toState; + $rootScope.returnToStateParams = toStateParams; + console.log('going to ' + toState.name + ' after login'); + + }); + $rootScope.$on('$stateChangeError', function(event, toState, toParams, fromState, fromParams, error) { + console.log('failed to change state: ' + error); + $state.go('login'); + }); + }); diff --git a/home.html b/home.html index 716c1d7..305500b 100644 --- a/home.html +++ b/home.html @@ -13,66 +13,69 @@ <title>Flashy</title> </head> <header> + <nav> + <div class="nav-wrapper"> + <a href="#" data-activates="mobile-demo" class="left button-collapse"><i class="mdi-navigation-menu"></i></a> + <ul ng-show="currentSection.id && isLoggedIn" class="left hide-on-small-and-down"> + <li ui-sref-active="active"><a ui-sref="feed({sectionId:currentSection.id})" class="tooltipped" + data-position="bottom" + data-delay="50" data-tooltip="Feed"><i + class="mdi-action-view-module"></i></a></li> + <li ui-sref-active="active"><a ui-sref="deck({sectionId:currentSection.id})" class="tooltipped" + data-position="bottom" + data-delay="50" data-tooltip="Deck"><i + class="mdi-action-view-carousel"></i></a></li> + <li ui-sref-active="active"><a ui-sref="cardlist({sectionId:currentSection.id})" class="tooltipped" + data-position="bottom" + data-delay="50" data-tooltip="Card List"><i + class="mdi-action-view-list"></i></a></li> + </ul> + <a href="#" class="brand-logo center">Flashy</a> + + <ul ng-show="isLoggedIn" id="nav-mobile" class="right hide-on-small-and-down"> + <!-- User's classes dropdown --> + <ul id="dropdown1" class="dropdown-content"> + <li ui-sref-active="active" ng-repeat="section in sections"> + <a class="class bold" ui-sref="feed({sectionId:section.id})">{{section.short_name}}</a> + </li> + <li class="divider"></li> + <li><a ui-sref="addclass">Add Class</a></li> + </ul> + <li><a class="dropdown-button ng-cloak" href="#!" data-activates="dropdown1">{{currentSection.id?currentSection.short_name:"Classes"}}<i + class="mdi-navigation-arrow-drop-down right"></i></a></li> + <li><a ui-sref="study">Study</a></li> + <li><a ui-sref="logout">Logout</a></li> + </ul> + + <!-- Slide-in side-nav for small screens --> + <ul class="side-nav" id="mobile-demo"> + <!-- Collapsible menu for all the User's classes --> + <ul class="collapsible" data-collapsible="accordion"> + <li class="bold"> + <a class="collapsible-header black-text"> + Classes<i class="mdi-navigation-arrow-drop-down right"></i> + </a> + </li> + <div class="collapsible-body" style="display: block"> + <ul> + <li ng-repeat="section in sections"> + <a ui-sref-active="active" class="class bold" ui-sref="feed({sectionId:section.id})">{{section.short_name}}</a> + </li> + <hr> + <li><a ui-sref="addclass"><i class="tiny mdi-content-add"> Add Class</i></a></li> + </ul> + </div> + </ul> + <li><a ui-sref="study">Study</a></li> + <li><a ui-sref="logout">Logout</a></li> + </ul> + </div> + </nav> </header> <body ng-controller="RootController"> <!-- Menu Bar --> -<nav> - <div class="nav-wrapper"> - <a href="#" data-activates="mobile-demo" class="left button-collapse"><i class="mdi-navigation-menu"></i></a> - <ul ng-show="currentSection.id && isLoggedIn" class="left hide-on-small-and-down"> - <li ui-sref-active="active"><a ui-sref="feed({sectionId:currentSection.id})" class="tooltipped" data-position="bottom" - data-delay="50" data-tooltip="Feed"><i - class="mdi-action-view-module"></i></a></li> - <li ui-sref-active="active"><a ui-sref="deck({sectionId:currentSection.id})" class="tooltipped" data-position="bottom" - data-delay="50" data-tooltip="Deck"><i - class="mdi-action-view-carousel"></i></a></li> - <li ui-sref-active="active"><a ui-sref="cardlist({sectionId:currentSection.id})" class="tooltipped" data-position="bottom" - data-delay="50" data-tooltip="Card List"><i - class="mdi-action-view-list"></i></a></li> - </ul> - <a href="#" class="brand-logo center">Flashy</a> - - <ul ng-show="isLoggedIn" id="nav-mobile" class="right hide-on-small-and-down"> - <!-- User's classes dropdown --> - <ul id="dropdown1" class="dropdown-content"> - <li ui-sref-active="active" ng-repeat="section in sections"> - <a class="class bold" ui-sref="feed({sectionId:section.id})">{{section.short_name}}</a> - </li> - <li class="divider"></li> - <li><a ui-sref="addclass">Add Class</a></li> - </ul> - <li><a class="dropdown-button ng-cloak" href="#!" data-activates="dropdown1">{{currentSection.id?currentSection.short_name:"Classes"}}<i - class="mdi-navigation-arrow-drop-down right"></i></a></li> - <li><a ui-sref="study">Study</a></li> - <li><a ui-sref="logout">Logout</a></li> - </ul> - - <!-- Slide-in side-nav for small screens --> - <ul class="side-nav" id="mobile-demo"> - <!-- Collapsible menu for all the User's classes --> - <ul class="collapsible" data-collapsible="accordion"> - <li class="bold"> - <a class="collapsible-header black-text"> - Classes<i class="mdi-navigation-arrow-drop-down right"></i> - </a> - </li> - <div class="collapsible-body" style="display: block"> - <ul> - <li ng-repeat="section in sections"> - <a ui-sref-active="active" class="class bold" ui-sref="feed({sectionId:section.id})">{{section.short_name}}</a> - </li> - <hr> - <li><a ui-sref="addclass"><i class="tiny mdi-content-add"> Add Class</i></a></li> - </ul> - </div> - </ul> - <li><a ui-sref="study">Study</a></li> - <li><a ui-sref="logout">Logout</a></li> - </ul> - </div> -</nav> <main ui-view></main> <footer class="page-footer"> <div class="footer-copyright"> diff --git a/scripts/CardListController.js b/scripts/CardListController.js index a707e26..7a71727 100644 --- a/scripts/CardListController.js +++ b/scripts/CardListController.js @@ -1,44 +1,44 @@ angular.module('flashy.CardListController', ['ui.router']). - controller('CardListController', function ($scope, $rootScope, $state, $http, $stateParams) { + controller('CardListController', function($scope, $rootScope, $state, $http, $stateParams) { // cards array sectionId = $stateParams.sectionId; $rootScope.currentSection = $rootScope.SectionResource.get({sectionId: sectionId}); $scope.cards = []; $http.get('/api/sections/' + sectionId + '/flashcards/'). - success(function (data) { + success(function(data) { console.log(data); $scope.cards = data; }). - error(function (err) { + error(function(err) { console.log('pulling feed failed'); }); - $scope.viewFeed = function () { + $scope.viewFeed = function() { $state.go('feed', {sectionId: sectionId}); console.log('go to feed'); }; // unhide card (dunno if it works yet) - $scope.unhide = function (card) { + $scope.unhide = function(card) { $http.post('/api/flashcards/' + card.id + '/unhide'). - success(function (data) { - console.log(card.text + " unhidden"); + success(function(data) { + console.log(card.text + ' unhidden'); }). - error(function (err) { + error(function(err) { console.log('no unhide for you'); }); }; // toggle button text from show to hide - $(function () { - $("#showHidden").click(function () { - $(this).text(function (i, text) { - return text === "Show Hidden" ? "Hide Hidden" : "Show Hidden"; - }) + $(function() { + $('#showHidden').click(function() { + $(this).text(function(i, text) { + return text === 'Show Hidden' ? 'Hide Hidden' : 'Show Hidden'; + }); }); }); - $scope.$on('$destroy', function () { + $scope.$on('$destroy', function() { $rootScope.currentSection = {}; $(document).off('keydown'); }); diff --git a/scripts/DeckController.js b/scripts/DeckController.js index 23c72de..4109882 100644 --- a/scripts/DeckController.js +++ b/scripts/DeckController.js @@ -1,6 +1,6 @@ angular.module('flashy.DeckController', ['ui.router']). - controller('DeckController', function ($scope, $rootScope, $state, $http, $stateParams) { + controller('DeckController', function($scope, $rootScope, $state, $http, $stateParams) { // cards array sectionId = $stateParams.sectionId; $rootScope.currentSection = $rootScope.SectionResource.get({sectionId: sectionId}); @@ -9,31 +9,31 @@ angular.module('flashy.DeckController', ['ui.router']). // Populate our page with cards. $http.get('/api/sections/' + sectionId + '/deck/'). - success(function (data) { + success(function(data) { console.log(data); $scope.cards = data; }). - error(function (err) { + error(function(err) { console.log('pulling feed failed'); }); /* Lets page refresh the cards shown on the page. */ - $scope.refreshCards = function () { + $scope.refreshCards = function() { var myDelay = 200; // ms - setTimeout(function () { + setTimeout(function() { $http.get('/api/sections/' + sectionId + '/deck/'). - success(function (data) { + success(function(data) { console.log(data); $scope.cards = data; console.log('success in refresh cards...'); }). - error(function (err) { + error(function(err) { console.log('refresh fail'); }); }, myDelay); }; - $scope.$on('$destroy', function () { + $scope.$on('$destroy', function() { $rootScope.currentSection = {}; }); } diff --git a/scripts/FeedController.js b/scripts/FeedController.js index 3b37f73..564ec5b 100644 --- a/scripts/FeedController.js +++ b/scripts/FeedController.js @@ -1,5 +1,5 @@ angular.module('flashy.FeedController', ['ui.router']). - controller('FeedController', function ($scope, $rootScope, $stateParams, $state, $http) { + controller('FeedController', function($scope, $rootScope, $stateParams, $state, $http) { console.log('Hello from feed'); sectionId = $stateParams.sectionId; $rootScope.currentSection = $rootScope.SectionResource.get({sectionId: sectionId}); @@ -14,39 +14,39 @@ angular.module('flashy.FeedController', ['ui.router']). new_uri += '//' + loc.host; var ws = new WebSocket(new_uri + '/ws/feed/' + sectionId + '?subscribe-broadcast'); - ws.onopen = function () { + ws.onopen = function() { console.log('websocket connected'); }; - ws.onmessage = function (e) { + ws.onmessage = function(e) { console.log('got websocket message ' + e.data); $scope.refreshCards(); }; - ws.onerror = function (e) { + ws.onerror = function(e) { console.error(e); }; - ws.onclose = function (e) { + ws.onclose = function(e) { console.log('connection closed'); }; $http.get('/api/sections/' + sectionId + '/feed/'). - success(function (data) { + success(function(data) { console.log(data); $scope.cards = data; }). - error(function (err) { + error(function(err) { console.log('pulling feed failed'); }); - $scope.viewDeck = function () { + $scope.viewDeck = function() { $state.go('deck', {sectionId: sectionId}); console.log('go to deck'); }; - $scope.pushCard = function () { + $scope.pushCard = function() { var pushed = new Date(Date.now()); var i = 0; var blanks = []; - $('#new-card-input')[0].childNodes.forEach(function (node) { + $('#new-card-input')[0].childNodes.forEach(function(node) { node = $(node)[0]; console.log(node); if (node.tagName == 'B') { @@ -66,25 +66,25 @@ angular.module('flashy.FeedController', ['ui.router']). section: sectionId }; $http.post('/api/flashcards/', myCard). - success(function (data) { + success(function(data) { console.log('pushed a card!'); listenForC = true; }). - error(function (error) { + error(function(error) { console.log('something went wrong pushing a card!'); }); $('#new-card-input').html(''); }; - $scope.refreshCards = function () { + $scope.refreshCards = function() { $http.get('/api/sections/' + sectionId + '/feed/'). - success(function (data) { + success(function(data) { console.log(data); $scope.cards = data; console.log('success in refresh cards...'); }). - error(function (err) { + error(function(err) { console.log('refresh fail'); }); }; @@ -100,19 +100,19 @@ angular.module('flashy.FeedController', ['ui.router']). opacity: 0, // Opacity of modal background in_duration: 300, // Transition in duration out_duration: 200, // Transition out duration - ready: function () { + ready: function() { listenForC = false; console.log('modal OPENING'); $('#new-card-input').focus(); }, - complete: function () { + complete: function() { listenForC = true; console.log('modal done, closing'); $('#new-card-input').blur(); } }; - $(document).keydown(function (e) { + $(document).keydown(function(e) { console.log(e.which); var keyed = e.which; if (keyed == 67 && listenForC) { // "c" or "C" for compose @@ -129,10 +129,10 @@ angular.module('flashy.FeedController', ['ui.router']). $scope.text = ''; var selected_start = 0; var selected_end = 0; - $(document).ready(function () { + $(document).ready(function() { // the "href" attribute of .modal-trigger must specify the modal ID that wants to be triggered $('.modal-trigger').leanModal(modal_options); - $('#new-card-input').on('keydown', function (e) { + $('#new-card-input').on('keydown', function(e) { if (e.which == 13) { e.preventDefault(); $scope.pushCard(); @@ -140,16 +140,16 @@ angular.module('flashy.FeedController', ['ui.router']). return false; } }); - $('#new-card-input').on('mouseup', function () { + $('#new-card-input').on('mouseup', function() { console.log('got selection: ' + selected_start); }); - $('button#blank-selected').click(function () { + $('button#blank-selected').click(function() { console.log(window.getSelection()); document.execCommand('bold'); }); }); - $scope.$on('$destroy', function () { + $scope.$on('$destroy', function() { ws.close(); $rootScope.currentSection = {}; $(document).off('keydown'); diff --git a/scripts/RootController.js b/scripts/RootController.js index 7ddc9a5..5a3bd22 100644 --- a/scripts/RootController.js +++ b/scripts/RootController.js @@ -1,11 +1,11 @@ angular.module('flashy.RootController', ['ui.router', 'ngResource']). - controller('RootController', function ($rootScope, $resource, $scope, $state, UserService) { + controller('RootController', function($rootScope, $resource, $scope, $state, UserService) { $rootScope.SectionResource = $resource('/api/sections/:sectionId/'); window.rootscope = $rootScope; $rootScope.isLoggedIn = false; $rootScope.currentSection = {}; - UserService.getUserData().then(function (data) { + UserService.getUserData().then(function(data) { console.log(data); $rootScope.user = data; }); diff --git a/styles/flashy.css b/styles/flashy.css index ccb0644..7e4932d 100644 --- a/styles/flashy.css +++ b/styles/flashy.css @@ -266,15 +266,14 @@ nav .button-collapse { } h2 { - text-align: center; + text-align: center; } md-content.md-default-theme { - background-color: rgba(255,255,255,0); - border: 1px solid #fff; + background-color: rgba(255, 255, 255, 0); + border: 1px solid #fff; } - /*#sidenav-overlay { background-color: rgba(0, 0, 0, 0) !important; }*/ -- 1.9.1