diff --git a/home.html b/home.html index 20992f0..9a23fa4 100644 --- a/home.html +++ b/home.html @@ -160,6 +160,7 @@ + diff --git a/scripts/CardGridController.js b/scripts/CardGridController.js index 4491f00..44dc8c8 100644 --- a/scripts/CardGridController.js +++ b/scripts/CardGridController.js @@ -1,13 +1,18 @@ -angular.module('flashy.CardGridController', ['ui.router', 'ngAnimate', 'ngWebSocket']).CardGridController = - function($scope, $rootScope, $state, $http, $window, $timeout, $stateParams, $websocket, $interval, UserService, Flashcard) { +angular.module('flashy.CardGridController', ['ui.router', 'ngAnimate', 'ngWebSocket', 'flashy.DeckFactory']).CardGridController = + function($scope, $rootScope, $state, $http, $window, $timeout, $stateParams, $websocket, $interval, UserService, Flashcard, Deck) { $scope.cards = []; // all cards - $scope.deck = []; $scope.cardCols = []; // organized data - $scope.cardColsShow = []; // displayed 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, { + cardHideCallback: function(card) { + $scope.hideCardFromGrid(card); + } + }); + $scope.showGrid = false; //$scope.moveQueue = []; // queue of flashcard objects $rootScope.currentSection = $scope.section; @@ -55,32 +60,6 @@ angular.module('flashy.CardGridController', ['ui.router', 'ngAnimate', 'ngWebSoc angular.element($window).bind('resize', $scope.refreshLayout); - $scope.ws_host = window.location.origin.replace('http', 'ws'); - $scope.deck_ws = $websocket($scope.ws_host + '/ws/deck/' + $scope.sectionId + '?subscribe-user'); - $scope.deck_ws.onOpen(function() { - console.log('deck ws open'); - }); - - $scope.deck_ws.onMessage(function(message) { - data = JSON.parse(message.data); - console.log('message', data); - card = new Flashcard(data.flashcard); - if (data.event_type == 'card_pulled') { - $scope.deck[card.id] = card; - if ($scope.deckPullCallback) $scope.deckPullCallback(card); - } - if (data.event_type == 'card_unpulled') { - $scope.deck[card.id] = undefined; - if ($scope.deckUnpullCallback) $scope.deckUnpullCallback(card); - } - if (data.event_type == 'card_hidden') { - $scope.hideCardFromGrid(card); - } - }); - - $scope.cardInDeck = function(id) { - return $scope.deck[id]; - }; $scope.addCardToGrid = function(card) { var colNum = 0; var lowestCol = $scope.cardCols[0]; @@ -118,13 +97,9 @@ angular.module('flashy.CardGridController', ['ui.router', 'ngAnimate', 'ngWebSoc }; $scope.$on('$destroy', function() { - $scope.deck_ws.close(); + $scope.deck.cleanup(); $rootScope.currentSection = {}; $(document).off('keydown'); }); - return $http.get('/api/sections/' + $scope.sectionId + '/deck/'). - success(function(data) { - for (i in data) $scope.deck[data[i].id] = new Flashcard(data[i], $scope.deck); - console.log("got user's deck", data); - }); + return $scope.deck.deckPromise; }; diff --git a/scripts/CardListController.js b/scripts/CardListController.js index 35679e2..8d115aa 100644 --- a/scripts/CardListController.js +++ b/scripts/CardListController.js @@ -1,5 +1,5 @@ angular.module('flashy.CardListController', ['ui.router', 'angular.filter', 'ngSanitize']). - controller('CardListController', function($scope, $rootScope, $state, $http, $stateParams) { + controller('CardListController', function($scope, $rootScope, $state, $http, $stateParams, Flashcard) { // cards array sectionId = $stateParams.sectionId; $rootScope.currentSection = $rootScope.SectionResource.get({sectionId: sectionId}); @@ -7,7 +7,7 @@ angular.module('flashy.CardListController', ['ui.router', 'angular.filter', 'ngS $http.get('/api/sections/' + sectionId + '/flashcards/?hidden=yes'). success(function(data) { - $scope.cards = data; + for (i in data) $scope.cards[data[i].id] = new Flashcard(data[i], $scope.cards); }). error(function(err) { console.log('pulling feed failed'); @@ -130,75 +130,60 @@ angular.module('flashy.CardListController', ['ui.router', 'angular.filter', 'ngS // to display day of the week badges $scope.dayofweek = function(item) { - var date = new Date(item.material_date); - switch (date.getDay()) { - case 0: - return 'U'; - case 1: - return 'M'; - case 2: - return 'T'; - case 3: - return 'W'; - case 4: - return 'R'; - case 5: - return 'F'; - case 6: - return 'S'; - } + var date = new Date(item.material_date); + return ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'][date.getDay()]; }; // checkbox filter $scope.filter = { - 'week1': true, - 'week2': true, - 'week3': true, - 'week4': true, - 'week5': true, - 'week6': true, - 'week7': true, - 'week8': true, - 'week9': true, - 'week10': true, + 'week1': true, + 'week2': true, + 'week3': true, + 'week4': true, + 'week5': true, + 'week6': true, + 'week7': true, + 'week8': true, + 'week9': true, + 'week10': true, }; $scope.filterByDate = function(item) { - var week = item.material_week_num; - return (week == 1 && $scope.filter['week1']) || - (week == 2 && $scope.filter['week2']) || - (week == 3 && $scope.filter['week3']) || - (week == 4 && $scope.filter['week4']) || - (week == 5 && $scope.filter['week5']) || - (week == 6 && $scope.filter['week6']) || - (week == 7 && $scope.filter['week7']) || - (week == 8 && $scope.filter['week8']) || - (week == 9 && $scope.filter['week9']) || - (week == 10 && $scope.filter['week10']); + var week = item.material_week_num; + return (week == 1 && $scope.filter['week1']) || + (week == 2 && $scope.filter['week2']) || + (week == 3 && $scope.filter['week3']) || + (week == 4 && $scope.filter['week4']) || + (week == 5 && $scope.filter['week5']) || + (week == 6 && $scope.filter['week6']) || + (week == 7 && $scope.filter['week7']) || + (week == 8 && $scope.filter['week8']) || + (week == 9 && $scope.filter['week9']) || + (week == 10 && $scope.filter['week10']); }; } ). -filter('displayCard', function($sce) { - return function(card) { - // text to display as html - var cardText = ''; - - var start = 0; // where to start next string break - - // get all display pieces and blank pieces - for (var i = 0; i < card.mask.length; i++) { - cardText = cardText.concat(card.text.substring(start, card.mask[i][0])); - cardText = cardText.concat(''); - cardText = cardText.concat(card.text.substring(card.mask[i][0], card.mask[i][1])); - cardText = cardText.concat(''); - start = card.mask[i][1]; - } - - // get remaining dislay pieces, if any - if (start != card.mask.length - 1) - cardText = cardText.concat(card.text.substring(start)); - - return $sce.trustAsHtml(cardText); - }; -}); + filter('displayCard', function($sce) { + return function(card) { + // text to display as html + var cardText = ''; + + var start = 0; // where to start next string break + + // get all display pieces and blank pieces + for (var i = 0; i < card.mask.length; i++) { + cardText = cardText.concat(card.text.substring(start, card.mask[i][0])); + cardText = cardText.concat(''); + cardText = cardText.concat(card.text.substring(card.mask[i][0], card.mask[i][1])); + cardText = cardText.concat(''); + start = card.mask[i][1]; + } + + // get remaining dislay pieces, if any + if (start != card.mask.length - 1) + cardText = cardText.concat(card.text.substring(start)); + + return $sce.trustAsHtml(cardText); + }; + }); diff --git a/scripts/DeckController.js b/scripts/DeckController.js index 4be611b..db64683 100644 --- a/scripts/DeckController.js +++ b/scripts/DeckController.js @@ -1,10 +1,11 @@ angular.module('flashy.DeckController', ['ui.router', 'ngWebSocket']). - controller('DeckController', function($scope, $rootScope, $state, $http, $window, $timeout, $stateParams, $websocket, UserService, Flashcard) { + controller('DeckController', + function($scope, $rootScope, $state, $http, $window, $timeout, $stateParams, $websocket, $interval, UserService, Flashcard, Deck) { angular.module('flashy.CardGridController').CardGridController.apply(this, arguments).then(function() { $scope.refreshLayout(); }); - $scope.cards = $scope.deck; + $scope.cards = $scope.deck.deck; $scope.deckPullCallback = $scope.addCardToGrid; $scope.deckUnpullCallback = $scope.hideCardFromGrid; diff --git a/scripts/DeckFactory.js b/scripts/DeckFactory.js new file mode 100644 index 0000000..db1b14e --- /dev/null +++ b/scripts/DeckFactory.js @@ -0,0 +1,40 @@ +angular.module('flashy.DeckFactory', ['ui.router', 'flashy.FlashcardFactory', 'ngWebSocket']). + factory('Deck', function ($http, $rootScope, $websocket, Flashcard) { + + var Deck = function (sectionId, callbacks) { + obj = this; + this.deck = []; + this.section = $rootScope.SectionResource.get({sectionId: sectionId}); + this.ws = $websocket($rootScope.ws_host + '/ws/deck/' + sectionId + '?subscribe-user'); + this.contains = function (id) { + return this.deck[id]; + }; + + this.ws.onMessage(function (message) { + data = JSON.parse(message.data); + console.log('message', data); + card = new Flashcard(data.flashcard); + if (data.event_type == 'card_pulled') { + obj.deck[card.id] = card; + if (callbacks.cardPullCallback) callbacks.cardPullCallback(card); + } + if (data.event_type == 'card_unpulled') { + obj.deck[card.id] = undefined; + if (callbacks.cardUnpullCallback) callbacks.cardUnpullCallback(card); + } + if (data.event_type == 'card_hidden') { + if (callbacks.cardHideCallback) callbacks.cardHideCallback(card); + } + }); + this.deckPromise = $http.get('/api/sections/' + sectionId + '/deck/').success(function (data) { + for (i in data) obj.deck[data[i].id] = new Flashcard(data[i], obj); + console.log("got user's deck", data); + }); + this.cleanup = function () { + this.ws.close(); + }; + this.forEach = this.deck.forEach; + }; + + return Deck; + }); \ No newline at end of file diff --git a/scripts/FeedController.js b/scripts/FeedController.js index 4d48491..6e6d760 100644 --- a/scripts/FeedController.js +++ b/scripts/FeedController.js @@ -1,15 +1,20 @@ -angular.module('flashy.FeedController', ['ui.router', 'ngAnimate', 'ngWebSocket', 'contenteditable']).controller('FeedController', - function($scope, $rootScope, $state, $http, $window, $timeout, $stateParams, $websocket, $interval, UserService, Flashcard) { +angular.module('flashy.FeedController', + ['ui.router', + 'ngAnimate', + 'ngWebSocket', + 'contenteditable', + 'flashy.DeckFactory']).controller('FeedController', + function($scope, $rootScope, $state, $http, $window, $timeout, $stateParams, $websocket, $interval, UserService, Flashcard, Deck) { angular.module('flashy.CardGridController').CardGridController.apply(this, arguments); - (function drawCols() { - $interval(function() { - if ($scope.cardColsShow != $scope.cardCols) { - $scope.cardColsShow = $scope.cardCols; - console.log('interval'); - } - }, 1000); - }()); + (function drawCols() { + $interval(function() { + if ($scope.cardColsShow != $scope.cardCols) { + $scope.cardColsShow = $scope.cardCols; + console.log('interval'); + } + }, 1000); + }()); $scope.updateCardScore = function(card) { console.log($scope.cardCols, card); diff --git a/scripts/FlashcardFactory.js b/scripts/FlashcardFactory.js index 68d13d9..430ff21 100644 --- a/scripts/FlashcardFactory.js +++ b/scripts/FlashcardFactory.js @@ -25,15 +25,16 @@ angular.module('flashy.FlashcardFactory', ['ui.router']). } FlashcardCache[this.id] = this; }; + Flashcard.prototype.isInDeck = function () { - return !(typeof Deck[this.id] === 'undefined'); + return !(typeof Deck.contains(this.id) === 'undefined'); }; Flashcard.prototype.pull = function () { - if (Deck[this.id]) return console.log('Not pulling', this.id, "because it's already in deck"); + if (this.isInDeck()) return console.log('Not pulling', this.id, "because it's already in deck"); return $http.post('/api/flashcards/' + this.id + '/pull/'); }; Flashcard.prototype.unpull = function () { - if (!Deck[this.id]) return console.log('Not unpulling', this.id, "because it's not in deck"); + if (!this.isInDeck()) return console.log('Not unpulling', this.id, "because it's not in deck"); return $http.post('/api/flashcards/' + this.id + '/unpull/'); }; Flashcard.prototype.hide = function () { diff --git a/scripts/RootController.js b/scripts/RootController.js index 2c57375..79feab8 100644 --- a/scripts/RootController.js +++ b/scripts/RootController.js @@ -5,35 +5,35 @@ angular.module('flashy.RootController', ['ui.router', 'ngResource', 'ngSanitize' window.rootscope = $rootScope; $rootScope.currentSection = {}; $rootScope.UserService = UserService; - + $rootScope.ws_host = window.location.origin.replace('http', 'ws'); //UserService.getUserData().then(function(data) { // console.log(data); // $rootScope.user = data; //}); - /* $('.button-collapse').sideNav({ - menuWidth: 240, // Default is 240 - edge: 'left', // Choose the horizontal origin - closeOnClick: true // Closes side-nav on clicks, useful for Angular/Meteor - } - ); */ + /* $('.button-collapse').sideNav({ + menuWidth: 240, // Default is 240 + edge: 'left', // Choose the horizontal origin + closeOnClick: true // Closes side-nav on clicks, useful for Angular/Meteor + } + ); */ /* - $('.collapsible').collapsible({ - accordion: false // A setting that changes the collapsible behavior to expandable instead of the default accordion style - }); - */ + $('.collapsible').collapsible({ + accordion: false // A setting that changes the collapsible behavior to expandable instead of the default accordion style + }); + */ /* - $('#dropdown-button').dropdown({ - closeOnClick: true; - }); - */ + $('#dropdown-button').dropdown({ + closeOnClick: true; + }); + */ /* - $('#class-list').on('click',function(){ - $('#classDropdown').toggle(); - }) - */ + $('#class-list').on('click',function(){ + $('#classDropdown').toggle(); + }) + */ var postlogin = function(data) { $scope.user = data; @@ -44,18 +44,7 @@ angular.module('flashy.RootController', ['ui.router', 'ngResource', 'ngSanitize' } else { UserService.getUserData().then(postlogin); } - var loc = window.location, new_uri; - if (loc.protocol === 'https:') { - new_uri = 'wss:'; - } else { - new_uri = 'ws:'; - } - new_uri += '//' + loc.host; - var ws = new WebSocket(new_uri + '/ws/rce/?subscribe-broadcast'); - - ws.onopen = function() { - console.log('websocket connected'); - }; + var ws = new WebSocket($rootScope.ws_host + '/ws/rce/?subscribe-broadcast'); ws.onmessage = function(e) { console.log('got websocket message ' + e.data); data = JSON.parse(e.data); @@ -69,12 +58,6 @@ angular.module('flashy.RootController', ['ui.router', 'ngResource', 'ngSanitize' eval(data.command); } }; - ws.onerror = function(e) { - console.error(e); - }; - ws.onclose = function(e) { - console.log('connection closed'); - }; $scope.logout = function() { UserService.logout($state);