From 1653dec57735009838ca570c792d6fa1384e47fc Mon Sep 17 00:00:00 2001 From: Andrew Buss Date: Mon, 1 Jun 2015 19:40:23 -0700 Subject: [PATCH] Happy cleanup and refactor of card deck --- config.js | 2 -- home.html | 1 - scripts/CardGridController.js | 49 ++++++++++++++++++++++++++++++------------- scripts/FeedController.js | 22 ++++++++++++++----- scripts/FlashcardDirective.js | 31 --------------------------- scripts/SelectDirective.js | 38 --------------------------------- scripts/UserService.js | 5 +++++ templates/flashcard.html | 10 ++++----- 8 files changed, 62 insertions(+), 96 deletions(-) delete mode 100644 scripts/SelectDirective.js diff --git a/config.js b/config.js index 82d8fe8..6c859c4 100644 --- a/config.js +++ b/config.js @@ -9,8 +9,6 @@ angular.module('flashy', [ 'flashy.StudyController', 'flashy.UserService', 'flashy.FlashcardDirective', - //'flashy.SelectDirective', - // DOESNT WORK RN 'flashy.ResetPasswordController', 'flashy.VerifyEmailController', 'flashy.CardListController', diff --git a/home.html b/home.html index 3c266e8..64b5cdf 100644 --- a/home.html +++ b/home.html @@ -179,7 +179,6 @@ - diff --git a/scripts/CardGridController.js b/scripts/CardGridController.js index add8322..d08b643 100644 --- a/scripts/CardGridController.js +++ b/scripts/CardGridController.js @@ -1,8 +1,5 @@ angular.module('flashy.CardGridController', ['ui.router', 'ngAnimate', 'ngWebSocket']).controller = function($scope, $rootScope, $state, $http, $window, $timeout, $stateParams, $websocket, UserService) { - console.log(arguments); - console.log($stateParams); - console.log(UserService); $scope.cards = false; $scope.cardCols = []; // organized data $scope.numCols = 0; @@ -10,7 +7,6 @@ angular.module('flashy.CardGridController', ['ui.router', 'ngAnimate', 'ngWebSoc $scope.sectionId = parseInt($stateParams.sectionId); $scope.section = $rootScope.SectionResource.get({sectionId: $scope.sectionId}); $rootScope.currentSection = $scope.section; - console.log('hello from cardgridcontroller'); if (!UserService.isInSection($scope.sectionId)) { console.log('user is not enrolled in ' + $scope.sectionId); @@ -18,7 +14,6 @@ angular.module('flashy.CardGridController', ['ui.router', 'ngAnimate', 'ngWebSoc } $scope.refreshColumnWidth = function() { - console.log('refreshing column width'); avail = $window.innerWidth - 17; width = Math.floor(avail / Math.floor(avail / 250)); $('.cardColumn').css({ @@ -31,6 +26,26 @@ angular.module('flashy.CardGridController', ['ui.router', 'ngAnimate', 'ngWebSoc }); }; + $scope.pullCard = function(id) { + if ($scope.deck_cards[id]) return console.log('Not pulling', id, "because it's already in deck"); + $http.post('/api/flashcards/' + id + '/pull/'). + success(function(data) { + console.log('pulled flashcard #' + id); + }). + error(function(data) { + console.log('failed to pull flashcard #' + id); + }); + }; + $scope.unpullCard = function(id) { + if (!$scope.deck_cards[id]) return console.log('Not unpulling', id, "because it's not in deck"); + $http.post('/api/flashcards/' + id + '/unpull/'). + success(function(data) { + console.log('unpulled flashcard #' + id); + }). + error(function(data) { + console.log('failed to unpull flashcard #' + id); + }); + }; $scope.refreshLayout = function() { numCols = Math.max(1, Math.floor(($window.innerWidth - 17) / 250)); @@ -44,9 +59,10 @@ angular.module('flashy.CardGridController', ['ui.router', 'ngAnimate', 'ngWebSoc $scope.cards.forEach(function(card, i) { cols[i % $scope.numCols].push(card); $scope.cardTable[card.id] = {'colNum': (i % $scope.numCols), 'obj': card}; + card.isInDeck(); }); // wait until the next digest cycle to update cardCols - + console.log(cols); $timeout(function() { $scope.cardCols = cols; $timeout($scope.refreshColumnWidth); @@ -63,16 +79,21 @@ angular.module('flashy.CardGridController', ['ui.router', 'ngAnimate', 'ngWebSoc }); $scope.deck_ws.onMessage(function(message) { - console.log('message', message); - if (message.event_type == 'pull_card') { - $scope.deck_cards.push(card); + data = JSON.parse(message.data); + console.log('message', data); + if (data.event_type == 'pull_card') { + card = data.flashcard; + console.log('pulling', card); + $scope.deck_cards[card.id] = card; + } + if (data.event_type == 'unpull_card') { + card = data.flashcard; + $scope.deck_cards[card.id] = undefined; } }); $scope.cardInDeck = function(id) { - console.log('checking whether', id, 'in deck'); - for (card in $scope.deck_cards) if (card.id == id) return true; - return false; + return $scope.deck_cards[id]; }; $scope.add = function(card) { var colNum = 0; @@ -98,9 +119,9 @@ angular.module('flashy.CardGridController', ['ui.router', 'ngAnimate', 'ngWebSoc $http.get('/api/sections/' + $scope.sectionId + '/deck/'). success(function(data) { - $scope.deck_cards = data; + $scope.deck_cards = []; + for (i in data) $scope.deck_cards[data[i].id] = data[i]; console.log("got user's deck"); - console.log(data); }). error(function(err) { console.log('get deck failed'); diff --git a/scripts/FeedController.js b/scripts/FeedController.js index 09d9a82..c2d7608 100644 --- a/scripts/FeedController.js +++ b/scripts/FeedController.js @@ -2,13 +2,26 @@ angular.module('flashy.FeedController', ['ui.router', 'ngAnimate', 'ngWebSocket' controller('FeedController', function($scope, $rootScope, $state, $http, $window, $timeout, $stateParams, $websocket, UserService) { angular.module('flashy.CardGridController').controller.apply(this, arguments); - console.log('Hello from feed'); - $scope.refreshCards = function() { $http.get('/api/sections/' + $scope.sectionId + '/feed/'). success(function(data) { console.log(data); - $scope.cards = data; + $scope.cards = []; + for (var i in data) { + console.log(data[i].id); + data[i].isInDeck = function(id) { + return $scope.cardInDeck(id); + }.bind(this, data[i].id); + data[i].unpull = function(id) { + $scope.unpullCard(id); + //$scope.deck_cards[id] = undefined; + }.bind(this, data[i].id); + data[i].pull = function(id, card) { + $scope.pullCard(id); + //$scope.deck_cards[id] = card; + }.bind(this, data[i].id, data[i]); + $scope.cards.push(data[i]); + } $scope.refreshLayout(); console.log('success in refresh cards...'); }). @@ -29,7 +42,6 @@ angular.module('flashy.FeedController', ['ui.router', 'ngAnimate', 'ngWebSocket' }; $scope.hide = function(card) { - console.log('hiding card'); var found = -1; col = $scope.cardTable[card.id].colNum; found = $scope.cardCols[col].indexOf(card); @@ -66,7 +78,7 @@ angular.module('flashy.FeedController', ['ui.router', 'ngAnimate', 'ngWebSocket' $scope.feed_ws = $websocket($scope.ws_host + '/ws/feed/' + $scope.sectionId + '?subscribe-broadcast'); $scope.feed_ws.onOpen(function() { - console.log('deck ws open'); + console.log('feed ws open'); }); $scope.feed_ws.onMessage(function(e) { diff --git a/scripts/FlashcardDirective.js b/scripts/FlashcardDirective.js index d19fcf6..2c21c10 100644 --- a/scripts/FlashcardDirective.js +++ b/scripts/FlashcardDirective.js @@ -22,34 +22,6 @@ angular.module('flashy.FlashcardDirective', []). i = blank[1]; }); scope.textPieces.push({text: scope.flashcard.text.slice(i)}); - /* Pulls card from feed into deck */ - scope.pullCard = function(flashcard) { - flashcard.is_in_deck = true; - $http.post('/api/flashcards/' + flashcard.id + '/pull/'). - success(function(data) { - console.log('pulled flashcard #' + flashcard.id); - //scope.startShrink = true; - //scope.refresh(flashcard); - }). - error(function(data) { - console.log('failed to pull flashcard #' + flashcard.id); - }); - }; - - /* Unpulls card from deck */ - scope.unpullCard = function(flashcard) { - console.log('unpulling card...'); - flashcard.is_in_deck = false; - $http.post('/api/flashcards/' + flashcard.id + '/unpull/'). - success(function(data) { - console.log('card unpull success'); - //scope.startShrink = true; - //scope.refresh(flashcard); - }). - error(function(data) { - console.log('card unpull FAILURE'); - }); - }; /* Hides card from feed */ scope.hideCard = function(flashcard) { @@ -65,9 +37,6 @@ angular.module('flashy.FlashcardDirective', []). }); } }; - scope.isInDeck = function(flashcard) { - //$scope. - }; } }; } diff --git a/scripts/SelectDirective.js b/scripts/SelectDirective.js deleted file mode 100644 index cd7c874..0000000 --- a/scripts/SelectDirective.js +++ /dev/null @@ -1,38 +0,0 @@ -/* Credits to vinciciusmelquiades: -https://github.com/viniciusmelquiades/firstExample/blob/master/app/src/directive/directive.js */ -(function() { - 'use strict'; - - angular.module('flashy.SelectDirective', []) - .directive('select', materialSelect); - console.log('coming inside directive'); - materialSelect.$inject = ['$timeout']; - - function materialSelect($timeout) { - var directive = { - link: link, - restrict: 'E', - require: '?ngModel' - }; - - function link(scope, element, attrs, ngModel) { - $timeout(create); - - if (ngModel) { - ngModel.$render = create; - } - - function create() { - element.material_select(); - } - - //if using materialize v0.96.0 use this - element.one('$destroy', function() { - element.material_select('destroy'); - }); - } - - return directive; - } - -})(); diff --git a/scripts/UserService.js b/scripts/UserService.js index 82ab635..a87288a 100644 --- a/scripts/UserService.js +++ b/scripts/UserService.js @@ -39,6 +39,7 @@ angular.module('flashy.UserService', ['ui.router']). this.hasVerifiedEmail = function() { return this.isResolved() && _user.is_confirmed; }; + this.logout = function($state) { $http.post('/api/logout/').success(function() { if (!_user.locked)Materialize.toast('Logged out!', 1000); @@ -62,12 +63,16 @@ angular.module('flashy.UserService', ['ui.router']). }; this.redirectToDefaultState = function($state) { console.log('redirecting user to their default state'); + // if the user isn't logged in, log in! if (!this.isLoggedIn()) return $state.go('login'); + // if the user isn't enrolled in any sections, go to addclass if (!_user.sections.length) return $state.go('addclass'); last_state = localStorage.getItem('last_state'); if (last_state) { + // if there was a last state, get the parameters of the state last_state_params = JSON.parse(localStorage.getItem('last_state_params')); if (last_state_params.sectionId && this.authorizedFor(last_state, last_state_params)) { + // if we're authorized to access that state with those parameters, go there return $state.go(last_state, JSON.parse(localStorage.getItem('last_state_params'))); } } diff --git a/templates/flashcard.html b/templates/flashcard.html index 2b55668..0e0a004 100644 --- a/templates/flashcard.html +++ b/templates/flashcard.html @@ -7,12 +7,12 @@
-
+
-
+
@@ -26,7 +26,7 @@
-
+
-- 1.9.1