From dc454dbeb42efdf415daf1a97090406a8485bc6b Mon Sep 17 00:00:00 2001 From: Andrew Buss Date: Tue, 2 Jun 2015 21:36:11 -0700 Subject: [PATCH] card list vaguely works now! --- scripts/CardGridController.js | 8 +- scripts/CardListController.js | 97 ++++++------------------ scripts/DeckFactory.js | 3 + scripts/FeedController.js | 4 +- scripts/FlashcardFactory.js | 12 ++- templates/cardlist.html | 169 ++++++++++++++++++++++-------------------- 6 files changed, 130 insertions(+), 163 deletions(-) diff --git a/scripts/CardGridController.js b/scripts/CardGridController.js index 27f8951..f2e94ab 100644 --- a/scripts/CardGridController.js +++ b/scripts/CardGridController.js @@ -1,13 +1,12 @@ angular.module('flashy.CardGridController', ['ui.router', 'ngAnimate', 'ngWebSocket', 'flashy.DeckFactory']).CardGridController = function($scope, $rootScope, $state, $http, $window, $timeout, $stateParams, $websocket, $interval, UserService, Flashcard, Deck) { + sectionId = parseInt($stateParams.sectionId); $scope.cards = []; // all cards $scope.cardCols = []; // organized data $scope.cardColsShow = []; // displayed data $scope.numCols = 0; - $scope.cardTable = {}; // look up table of cards: {'colNum':col, 'obj':card} - $scope.sectionId = parseInt($stateParams.sectionId); - $scope.section = $rootScope.SectionResource.get({sectionId: $scope.sectionId}); - $scope.deck = new Deck($scope.sectionId, { + $scope.section = $rootScope.SectionResource.get({sectionId: sectionId}); + $scope.deck = new Deck(sectionId, { cardHideCallback: function(card) { $scope.hideCardFromGrid(card); } @@ -93,6 +92,7 @@ angular.module('flashy.CardGridController', ['ui.router', 'ngAnimate', 'ngWebSoc $scope.$on('$destroy', function() { $scope.deck.cleanup(); + Flashcard.cleanup(); $rootScope.currentSection = {}; $(document).off('keydown'); }); diff --git a/scripts/CardListController.js b/scripts/CardListController.js index 8d115aa..907fe1b 100644 --- a/scripts/CardListController.js +++ b/scripts/CardListController.js @@ -1,92 +1,39 @@ -angular.module('flashy.CardListController', ['ui.router', 'angular.filter', 'ngSanitize']). - controller('CardListController', function($scope, $rootScope, $state, $http, $stateParams, Flashcard) { +angular.module('flashy.CardListController', ['ui.router', 'angular.filter', 'ngSanitize', 'flashy.DeckFactory']). + controller('CardListController', function($scope, $rootScope, $state, $http, $stateParams, Flashcard, Deck) { // cards array - sectionId = $stateParams.sectionId; + sectionId = parseInt($stateParams.sectionId); + $scope.deck = new Deck(sectionId, { + cardPullCallback: function(card) { + Materialize.toast('Pulled!', 3000); + }, + cardUnpullCallback: function(card) { + Materialize.toast('Unpulled!', 3000); + }, + cardHideCallback: function(card) { + card.is_hidden = true; + Materialize.toast('Hidden!', 3000); + }, + cardUnhideCallback: function(card) { + card.is_hidden = false; + Materialize.toast('Unhidden!', 3000); + } + }); $rootScope.currentSection = $rootScope.SectionResource.get({sectionId: sectionId}); $scope.cards = []; $http.get('/api/sections/' + sectionId + '/flashcards/?hidden=yes'). success(function(data) { - for (i in data) $scope.cards[data[i].id] = new Flashcard(data[i], $scope.cards); + for (i in data) $scope.cards[data[i].id] = new Flashcard(data[i], $scope.deck); }). error(function(err) { console.log('pulling feed failed'); }); - $scope.viewFeed = function() { - $state.go('feed', {sectionId: sectionId}); - console.log('go to feed'); - }; - - - // unhide card - $scope.unhide = function(card) { - $http.post('/api/flashcards/' + card.id + '/unhide/'). - success(function(data) { - console.log(card.text + ' unhidden'); - - // locally change hidden - card.is_hidden = false; - Materialize.toast('Unhidden', 3000, 'rounded'); - }). - error(function(err) { - console.log('no unhide for you'); - }); - }; - - // hide card - $scope.hide = function(card) { - $http.post('/api/flashcards/' + card.id + '/hide/'). - success(function(data) { - console.log(card.text + ' hidden'); - - // locally change hidden - card.is_hidden = true; - Materialize.toast('Hidden', 3000, 'rounded'); - }). - error(function(err) { - console.log('no hide for you'); - }); - }; - - // pull card - $scope.pull = function(card) { - $http.post('/api/flashcards/' + card.id + '/pull/'). - success(function(data) { - console.log(card.text + ' pulled'); - - // locally change boolean for display purposes - card.is_in_deck = true; - Materialize.toast('Added to Your Deck', 3000, 'rounded'); - }). - error(function(err) { - console.log('no pull for you'); - }); - }; - - // unpull card - $scope.unpull = function(card) { - $http.post('/api/flashcards/' + card.id + '/unpull/'). - success(function(data) { - console.log(card.text + ' unpulled'); - - // local change for display purposes - card.is_in_deck = false; - Materialize.toast('Removed from Your Deck', 3000, 'rounded'); - }). - error(function(err) { - console.log('no unpull for you'); - }); - }; - // flag/report card $scope.flag = function(card) { $http.post('/api/flashcards/' + card.id + '/report/'). success(function(data) { console.log(card.text + ' reported'); - - // local change for display purposes - Materialize.toast('Card Flagged', 3000, 'rounded'); }). error(function(err) { console.log('no flag for you'); @@ -161,6 +108,10 @@ angular.module('flashy.CardListController', ['ui.router', 'angular.filter', 'ngS (week == 9 && $scope.filter['week9']) || (week == 10 && $scope.filter['week10']); }; + $scope.$on('$destroy', function() { + $scope.deck.cleanup(); + Flashcard.cleanup(); + }); } ). diff --git a/scripts/DeckFactory.js b/scripts/DeckFactory.js index 0df388a..d6f495f 100644 --- a/scripts/DeckFactory.js +++ b/scripts/DeckFactory.js @@ -30,6 +30,9 @@ angular.module('flashy.DeckFactory', ['ui.router', 'flashy.FlashcardFactory', 'n if (data.event_type == 'card_hidden') { if (callbacks.cardHideCallback) callbacks.cardHideCallback(card); } + if (data.event_type == 'card_unhidden') { + if (callbacks.cardUnhideCallback) callbacks.cardUnhideCallback(card); + } }); this.deckPromise = $http.get('/api/sections/' + sectionId + '/deck/').success(function (data) { for (i in data) obj.cards[data[i].id] = new Flashcard(data[i], obj); diff --git a/scripts/FeedController.js b/scripts/FeedController.js index 6e6d760..c004dc5 100644 --- a/scripts/FeedController.js +++ b/scripts/FeedController.js @@ -26,7 +26,7 @@ angular.module('flashy.FeedController', $scope.updateColRanks($scope.cardCols[card.colNum]); }; - $scope.feed_ws = $websocket($scope.ws_host + '/ws/feed/' + $scope.sectionId + '?subscribe-broadcast'); + $scope.feed_ws = $websocket($scope.ws_host + '/ws/feed/' + sectionId + '?subscribe-broadcast'); $scope.feed_ws.onMessage(function(e) { data = JSON.parse(e.data); console.log('message', data); @@ -166,7 +166,7 @@ angular.module('flashy.FeedController', $scope.$on('$destroy', function() { $scope.feed_ws.close(); }); - return $http.get('/api/sections/' + $scope.sectionId + '/feed/'). + return $http.get('/api/sections/' + sectionId + '/feed/'). success(function(data) { console.log(data); $scope.cards = data.map(function(card) { diff --git a/scripts/FlashcardFactory.js b/scripts/FlashcardFactory.js index a8cf634..de82ead 100644 --- a/scripts/FlashcardFactory.js +++ b/scripts/FlashcardFactory.js @@ -1,10 +1,11 @@ angular.module('flashy.FlashcardFactory', ['ui.router']). factory('Flashcard', function ($http) { var FlashcardCache = []; + var Deck = null; var Flashcard = function (data, deck) { if (typeof data == 'number') return FlashcardCache[data]; if (FlashcardCache[data.id]) return FlashcardCache[data.id]; - if (deck) this.deck = deck; + if (!Deck && deck) Deck = deck; for (var k in data) this[k] = data[k]; this.textPieces = []; this.mask.sort(function (a, b) { @@ -26,7 +27,7 @@ angular.module('flashy.FlashcardFactory', ['ui.router']). }; Flashcard.prototype.isInDeck = function () { - return !(typeof this.deck.contains(this.id) === 'undefined'); + return !(typeof Deck.contains(this.id) === 'undefined'); }; Flashcard.prototype.pullUnpull = function () { if (this.isInDeck()) this.unpull(); @@ -43,6 +44,13 @@ angular.module('flashy.FlashcardFactory', ['ui.router']). Flashcard.prototype.hide = function () { return $http.post('/api/flashcards/' + this.id + '/hide/'); }; + Flashcard.prototype.unhide = function () { + return $http.post('/api/flashcards/' + this.id + '/unhide/'); + }; + Flashcard.cleanup = function () { + Deck = null; + FlashcardCache = []; + }; return Flashcard; }); diff --git a/templates/cardlist.html b/templates/cardlist.html index 3c90760..1974483 100644 --- a/templates/cardlist.html +++ b/templates/cardlist.html @@ -1,92 +1,97 @@ -
- Show Hidden +
+ Show Hidden -
- - - -
+
+ + +
+
-
-
-
-
- - -
-
- - -
-
- - -
-
- - -
-
- - -
+
+ +
+
+ +
-
-
- - -
-
- - -
-
- - -
-
- - -
-
- - -
+
+ +
- -
+
+ + +
+
+ + +
+
+ + +
+
+
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ +
-
-
    -
  • Week {{weeknum}}

  • -
  • -
    - - {{dayofweek(card)}} -

    - - - - - - - - - - -

    -
    -
  • -
-
+
+
    +
  • Week {{weeknum}}

  • +
  • + + + {{dayofweek(card)}} +

    + + + + + + + + + + +

    +
  • +
+
- + + -- 1.9.1