Commit bf0941f00ca8219665f7816f41e640a688ab050d

Authored by Andrew Buss
1 parent 08fac4c5ea

continue refactoring deck, feed sorting

Showing 6 changed files with 59 additions and 88 deletions Side-by-side Diff

scripts/CardGridController.js View file @ bf0941f
1 1 angular.module('flashy.CardGridController', ['ui.router', 'ngAnimate', 'ngWebSocket']).controller =
2 2 function($scope, $rootScope, $state, $http, $window, $timeout, $stateParams, $websocket, UserService) {
3 3 $scope.cards = [];
4   - $scope.deck_cards = [];
  4 + $scope.deck = [];
5 5 $scope.cardCols = []; // organized data
6 6 $scope.numCols = 0;
7 7 $scope.cardTable = {}; // look up table of cards, {'colNum':col, 'obj':card}
8 8  
9 9  
10 10  
11 11  
12 12  
... ... @@ -62,21 +62,21 @@
62 62 $scope.deck_ws.onMessage(function(message) {
63 63 data = JSON.parse(message.data);
64 64 console.log('message', data);
65   - if (data.event_type == 'pull_card') {
  65 + if (data.event_type == 'card_pulled') {
66 66 card = data.flashcard;
67 67 console.log('pulling', card);
68   - $scope.deck_cards[card.id] = card;
  68 + $scope.deck[card.id] = card;
69 69 }
70   - if (data.event_type == 'unpull_card') {
  70 + if (data.event_type == 'card_unpulled') {
71 71 card = data.flashcard;
72   - $scope.deck_cards[card.id] = undefined;
  72 + $scope.deck[card.id] = undefined;
73 73 }
74 74 });
75 75  
76 76 $scope.cardInDeck = function(id) {
77   - return $scope.deck_cards[id];
  77 + return $scope.deck[id];
78 78 };
79   - $scope.add = function(card) {
  79 + $scope.addCardToGrid = function(card) {
80 80 var colNum = 0;
81 81 var lowestCol = $scope.cardCols[0];
82 82 var lowestColNum = 0;
... ... @@ -103,7 +103,7 @@
103 103 $http.get('/api/sections/' + $scope.sectionId + '/deck/').
104 104 success(function(data) {
105 105  
106   - for (i in data) $scope.deck_cards[data[i].id] = data[i];
  106 + for (i in data) $scope.deck[data[i].id] = data[i];
107 107 console.log("got user's deck");
108 108 }).
109 109 error(function(err) {
scripts/DeckController.js View file @ bf0941f
... ... @@ -2,7 +2,7 @@
2 2  
3 3 controller('DeckController', function($scope, $rootScope, $state, $http, $window, $timeout, $stateParams, $websocket, UserService) {
4 4 angular.module('flashy.CardGridController').controller.apply(this, arguments);
5   - $scope.cards = $scope.deck_cards;
  5 + $scope.cards = $scope.deck;
6 6 $scope.refreshLayout();
7 7 }
8 8 );
scripts/FeedController.js View file @ bf0941f
... ... @@ -7,7 +7,7 @@
7 7 success(function(data) {
8 8 console.log(data);
9 9 $scope.cards = [];
10   - for (var i in data) $scope.cards.push(new Flashcard(data[i], $scope.deck_cards));
  10 + for (var i in data) $scope.cards.push(new Flashcard(data[i], $scope.deck));
11 11 $scope.refreshLayout();
12 12 console.log('success in refresh cards...');
13 13 }).
14 14  
15 15  
16 16  
17 17  
18 18  
... ... @@ -27,54 +27,24 @@
27 27 });
28 28 };
29 29  
30   - $scope.hide = function(card) {
31   - var found = -1;
32   - col = $scope.cardTable[card.id].colNum;
33   - found = $scope.cardCols[col].indexOf(card);
34   - if (found != -1) {
35   - $scope.cardCols[col].splice(found, 1);
36   - console.log('card hidden');
37   - return col;
38   - }
39   - console.log('Error finding card to hide:');
40   - console.log(card);
41   - return -1;
  30 + $scope.updateCardScore = function(flashcard) {
  31 + card = new Flashcard(flashcard.id);
  32 + console.log('old score', card.score);
  33 + card.score = flashcard.score;
  34 + console.log('new score', card.score);
  35 + $scope.cardCols[$scope.cardTable[card.id].colNum].sort(function(a, b) {
  36 + return b.score - a.score;
  37 + });
42 38 };
43 39  
44   - $scope.update = function(id, new_score) {
45   - card = $scope.cardTable[id].obj;
46   - if (Math.abs(new_score - card.score) < .0001) {
47   - console.log('score same, no update required');
48   - return;
49   - }
50   - console.log('updating');
51   - console.log(card);
52   - var column = $scope.cardCols[$scope.cardTable[id].colNum];
53   - var found = column.indexOf(card);
54   - var i = 0;
55   - for (; i < column.length; i++)
56   - if (column[i].score <= new_score) break;
57   - card.score = new_score;
58   - if ($scope.$$phase) { // most of the time it is "$digest"
59   - column.splice(i, 0, column.splice(found, 1)[0]);
60   - } else {
61   - $scope.$apply(column.splice(i, 0, column.splice(found, 1)[0]));
62   - }
63   - };
64   -
65 40 $scope.feed_ws = $websocket($scope.ws_host + '/ws/feed/' + $scope.sectionId + '?subscribe-broadcast');
66   - $scope.feed_ws.onOpen(function() {
67   - console.log('feed ws open');
68   - });
69   -
70 41 $scope.feed_ws.onMessage(function(e) {
71 42 data = JSON.parse(e.data);
72   - console.log('got websocket message ' + e.data);
73   - console.log(data);
  43 + console.log('message', data);
74 44 if (data.event_type == 'new_card') {
75   - $scope.add(data.flashcard);
  45 + $scope.addCardToGrid(new Flashcard(data.flashcard, $scope.deck));
76 46 } else if (data.event_type == 'score_change') {
77   - $scope.update(data.flashcard_id, data.new_score);
  47 + $scope.updateCardScore(data.flashcard);
78 48 }
79 49 });
80 50  
scripts/FlashcardDirective.js View file @ bf0941f
... ... @@ -11,7 +11,6 @@
11 11 },
12 12 link: function(scope, element) {
13 13 /* Handles width of the card */
14   -
15 14 }
16 15 };
17 16 }
scripts/FlashcardFactory.js View file @ bf0941f
1 1 angular.module('flashy.FlashcardFactory', ['ui.router']).
2 2 factory('Flashcard', function ($http) {
  3 + var FlashcardCache = [];
3 4 var Flashcard = function (data, deck) {
  5 + if (typeof data == 'number') return FlashcardCache[data];
4 6 for (var k in data) this[k] = data[k];
5 7 this.deck = deck;
6 8 this.textPieces = [];
7 9  
... ... @@ -15,8 +17,8 @@
15 17 i = blank[1];
16 18 }, this);
17 19 this.textPieces.push({text: this.text.slice(i)});
  20 + FlashcardCache[this.id] = this;
18 21 };
19   -
20 22 Flashcard.prototype.isInDeck = function () {
21 23 return this.deck[this.id];
22 24 };
scripts/SettingsController.js View file @ bf0941f
1 1 angular.module('flashy.SettingsController', ['ui.router']).
2 2  
3   - controller('SettingsController', function($scope, $http) {
4   - $scope.changePassword = function(oldPassword, newPassword, confirmedNewPassword) {
  3 + controller('SettingsController', function($scope, $http) {
  4 + $scope.changePassword = function(oldPassword, newPassword, confirmedNewPassword) {
5 5  
6   - };
7   - console.log("checking to see if chrome");
8   - if (!chrome) { return; }
9   - console.log("chrome");
10   - $scope.registerCallback = function(registrationId) {
11   - if (chrome.runtime.lastError) {
12   - console.log("Registration failed")
13   - }
  6 + };
  7 + console.log('checking to see if chrome');
  8 + if (!chrome) { return; }
  9 + console.log('chrome');
  10 + $scope.registerCallback = function(registrationId) {
  11 + if (chrome.runtime.lastError) {
  12 + console.log('Registration failed');
  13 + }
14 14  
15   - sendRegistrationId(registrationId, function(succeed) {
16   - if (succeed) {
17   - chrome.storage.local.set({registered: true});
18   - }
19   - });
20   - };
  15 + sendRegistrationId(registrationId, function(succeed) {
  16 + if (succeed) {
  17 + chrome.storage.local.set({registered: true});
  18 + }
  19 + });
  20 + };
21 21  
22   - function sendRegistrationId(registrationId, callback) {
23   - console.log("registration id: "+registrationId);
24   - $http.post('/api/subscribe/', JSON.stringify({
25   - 'registration_id': registrationId
26   - }));
27   - callback(true);
28   - }
  22 + function sendRegistrationId(registrationId, callback) {
  23 + console.log('registration id: '+ registrationId);
  24 + $http.post('/api/subscribe/', JSON.stringify({
  25 + 'registration_id': registrationId
  26 + }));
  27 + callback(true);
  28 + }
29 29  
30   - console.log(chrome.runtime.onStartup);
31   - chrome.runtime.onStartup.addListener(function() {
32   - chrome.storage.local.get("registered", function(result) {
33   - // If already registered, bail out.
34   - if (result["registered"])
35   - return;
  30 + console.log(chrome.runtime.onStartup);
  31 + chrome.runtime.onStartup.addListener(function() {
  32 + chrome.storage.local.get('registered', function(result) {
  33 + // If already registered, bail out.
  34 + if (result['registered'])
  35 + return;
36 36  
37   - // Up to 100 senders are allowed.
38   - var senderIds = ["45066531702"];
39   - chrome.gcm.register(senderIds, registerCallback);
40   - });
41   - });
42   - });
  37 + // Up to 100 senders are allowed.
  38 + var senderIds = ['45066531702'];
  39 + chrome.gcm.register(senderIds, registerCallback);
  40 + });
  41 + });
  42 + });