diff --git a/scripts/CardGridController.js b/scripts/CardGridController.js index cc70ebb..0821244 100644 --- a/scripts/CardGridController.js +++ b/scripts/CardGridController.js @@ -1,7 +1,7 @@ angular.module('flashy.CardGridController', ['ui.router', 'ngAnimate', 'ngWebSocket']).controller = function($scope, $rootScope, $state, $http, $window, $timeout, $stateParams, $websocket, UserService) { $scope.cards = []; - $scope.deck_cards = []; + $scope.deck = []; $scope.cardCols = []; // organized data $scope.numCols = 0; $scope.cardTable = {}; // look up table of cards, {'colNum':col, 'obj':card} @@ -62,21 +62,21 @@ angular.module('flashy.CardGridController', ['ui.router', 'ngAnimate', 'ngWebSoc $scope.deck_ws.onMessage(function(message) { data = JSON.parse(message.data); console.log('message', data); - if (data.event_type == 'pull_card') { + if (data.event_type == 'card_pulled') { card = data.flashcard; console.log('pulling', card); - $scope.deck_cards[card.id] = card; + $scope.deck[card.id] = card; } - if (data.event_type == 'unpull_card') { + if (data.event_type == 'card_unpulled') { card = data.flashcard; - $scope.deck_cards[card.id] = undefined; + $scope.deck[card.id] = undefined; } }); $scope.cardInDeck = function(id) { - return $scope.deck_cards[id]; + return $scope.deck[id]; }; - $scope.add = function(card) { + $scope.addCardToGrid = function(card) { var colNum = 0; var lowestCol = $scope.cardCols[0]; var lowestColNum = 0; @@ -103,7 +103,7 @@ angular.module('flashy.CardGridController', ['ui.router', 'ngAnimate', 'ngWebSoc $http.get('/api/sections/' + $scope.sectionId + '/deck/'). success(function(data) { - for (i in data) $scope.deck_cards[data[i].id] = data[i]; + for (i in data) $scope.deck[data[i].id] = data[i]; console.log("got user's deck"); }). error(function(err) { diff --git a/scripts/DeckController.js b/scripts/DeckController.js index 04638b6..11cac86 100644 --- a/scripts/DeckController.js +++ b/scripts/DeckController.js @@ -2,7 +2,7 @@ angular.module('flashy.DeckController', ['ui.router', 'ngWebSocket']). controller('DeckController', function($scope, $rootScope, $state, $http, $window, $timeout, $stateParams, $websocket, UserService) { angular.module('flashy.CardGridController').controller.apply(this, arguments); - $scope.cards = $scope.deck_cards; + $scope.cards = $scope.deck; $scope.refreshLayout(); } ); diff --git a/scripts/FeedController.js b/scripts/FeedController.js index 2d19684..ab40863 100644 --- a/scripts/FeedController.js +++ b/scripts/FeedController.js @@ -7,7 +7,7 @@ angular.module('flashy.FeedController', ['ui.router', 'ngAnimate', 'ngWebSocket' success(function(data) { console.log(data); $scope.cards = []; - for (var i in data) $scope.cards.push(new Flashcard(data[i], $scope.deck_cards)); + for (var i in data) $scope.cards.push(new Flashcard(data[i], $scope.deck)); $scope.refreshLayout(); console.log('success in refresh cards...'); }). @@ -27,54 +27,24 @@ angular.module('flashy.FeedController', ['ui.router', 'ngAnimate', 'ngWebSocket' }); }; - $scope.hide = function(card) { - var found = -1; - col = $scope.cardTable[card.id].colNum; - found = $scope.cardCols[col].indexOf(card); - if (found != -1) { - $scope.cardCols[col].splice(found, 1); - console.log('card hidden'); - return col; - } - console.log('Error finding card to hide:'); - console.log(card); - return -1; - }; - - $scope.update = function(id, new_score) { - card = $scope.cardTable[id].obj; - if (Math.abs(new_score - card.score) < .0001) { - console.log('score same, no update required'); - return; - } - console.log('updating'); - console.log(card); - var column = $scope.cardCols[$scope.cardTable[id].colNum]; - var found = column.indexOf(card); - var i = 0; - for (; i < column.length; i++) - if (column[i].score <= new_score) break; - card.score = new_score; - if ($scope.$$phase) { // most of the time it is "$digest" - column.splice(i, 0, column.splice(found, 1)[0]); - } else { - $scope.$apply(column.splice(i, 0, column.splice(found, 1)[0])); - } + $scope.updateCardScore = function(flashcard) { + card = new Flashcard(flashcard.id); + console.log('old score', card.score); + card.score = flashcard.score; + console.log('new score', card.score); + $scope.cardCols[$scope.cardTable[card.id].colNum].sort(function(a, b) { + return b.score - a.score; + }); }; $scope.feed_ws = $websocket($scope.ws_host + '/ws/feed/' + $scope.sectionId + '?subscribe-broadcast'); - $scope.feed_ws.onOpen(function() { - console.log('feed ws open'); - }); - $scope.feed_ws.onMessage(function(e) { data = JSON.parse(e.data); - console.log('got websocket message ' + e.data); - console.log(data); + console.log('message', data); if (data.event_type == 'new_card') { - $scope.add(data.flashcard); + $scope.addCardToGrid(new Flashcard(data.flashcard, $scope.deck)); } else if (data.event_type == 'score_change') { - $scope.update(data.flashcard_id, data.new_score); + $scope.updateCardScore(data.flashcard); } }); diff --git a/scripts/FlashcardDirective.js b/scripts/FlashcardDirective.js index 8bd7c85..eb22ba0 100644 --- a/scripts/FlashcardDirective.js +++ b/scripts/FlashcardDirective.js @@ -11,7 +11,6 @@ angular.module('flashy.FlashcardDirective', []). }, link: function(scope, element) { /* Handles width of the card */ - } }; } diff --git a/scripts/FlashcardFactory.js b/scripts/FlashcardFactory.js index a16ac46..d612e30 100644 --- a/scripts/FlashcardFactory.js +++ b/scripts/FlashcardFactory.js @@ -1,6 +1,8 @@ angular.module('flashy.FlashcardFactory', ['ui.router']). factory('Flashcard', function ($http) { + var FlashcardCache = []; var Flashcard = function (data, deck) { + if (typeof data == 'number') return FlashcardCache[data]; for (var k in data) this[k] = data[k]; this.deck = deck; this.textPieces = []; @@ -15,8 +17,8 @@ angular.module('flashy.FlashcardFactory', ['ui.router']). i = blank[1]; }, this); this.textPieces.push({text: this.text.slice(i)}); + FlashcardCache[this.id] = this; }; - Flashcard.prototype.isInDeck = function () { return this.deck[this.id]; }; diff --git a/scripts/SettingsController.js b/scripts/SettingsController.js index dec35cb..fd686d8 100644 --- a/scripts/SettingsController.js +++ b/scripts/SettingsController.js @@ -1,42 +1,42 @@ angular.module('flashy.SettingsController', ['ui.router']). - controller('SettingsController', function($scope, $http) { - $scope.changePassword = function(oldPassword, newPassword, confirmedNewPassword) { + controller('SettingsController', function($scope, $http) { + $scope.changePassword = function(oldPassword, newPassword, confirmedNewPassword) { - }; - console.log("checking to see if chrome"); - if (!chrome) { return; } - console.log("chrome"); - $scope.registerCallback = function(registrationId) { - if (chrome.runtime.lastError) { - console.log("Registration failed") - } + }; + console.log('checking to see if chrome'); + if (!chrome) { return; } + console.log('chrome'); + $scope.registerCallback = function(registrationId) { + if (chrome.runtime.lastError) { + console.log('Registration failed'); + } - sendRegistrationId(registrationId, function(succeed) { - if (succeed) { - chrome.storage.local.set({registered: true}); - } - }); - }; + sendRegistrationId(registrationId, function(succeed) { + if (succeed) { + chrome.storage.local.set({registered: true}); + } + }); + }; - function sendRegistrationId(registrationId, callback) { - console.log("registration id: "+registrationId); - $http.post('/api/subscribe/', JSON.stringify({ - 'registration_id': registrationId - })); - callback(true); - } + function sendRegistrationId(registrationId, callback) { + console.log('registration id: '+ registrationId); + $http.post('/api/subscribe/', JSON.stringify({ + 'registration_id': registrationId + })); + callback(true); + } - console.log(chrome.runtime.onStartup); - chrome.runtime.onStartup.addListener(function() { - chrome.storage.local.get("registered", function(result) { - // If already registered, bail out. - if (result["registered"]) - return; + console.log(chrome.runtime.onStartup); + chrome.runtime.onStartup.addListener(function() { + chrome.storage.local.get('registered', function(result) { + // If already registered, bail out. + if (result['registered']) + return; - // Up to 100 senders are allowed. - var senderIds = ["45066531702"]; - chrome.gcm.register(senderIds, registerCallback); - }); - }); - }); + // Up to 100 senders are allowed. + var senderIds = ['45066531702']; + chrome.gcm.register(senderIds, registerCallback); + }); + }); + });