diff --git a/config.js b/config.js index 06175b0..ef6dbb1 100644 --- a/config.js +++ b/config.js @@ -9,6 +9,7 @@ angular.module('flashy', [ 'flashy.StudyController', 'flashy.UserService', 'flashy.FlashcardDirective', + 'flashy.FlashcardFactory', 'flashy.ResetPasswordController', 'flashy.VerifyEmailController', 'flashy.CardListController', diff --git a/home.html b/home.html index 2a4ddb4..f0c56fc 100644 --- a/home.html +++ b/home.html @@ -158,6 +158,8 @@ + + diff --git a/scripts/CardGridController.js b/scripts/CardGridController.js index cf872af..cc70ebb 100644 --- a/scripts/CardGridController.js +++ b/scripts/CardGridController.js @@ -1,6 +1,7 @@ angular.module('flashy.CardGridController', ['ui.router', 'ngAnimate', 'ngWebSocket']).controller = function($scope, $rootScope, $state, $http, $window, $timeout, $stateParams, $websocket, UserService) { - $scope.cards = false; + $scope.cards = []; + $scope.deck_cards = []; $scope.cardCols = []; // organized data $scope.numCols = 0; $scope.cardTable = {}; // look up table of cards, {'colNum':col, 'obj':card} @@ -26,26 +27,6 @@ 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)); @@ -118,7 +99,7 @@ angular.module('flashy.CardGridController', ['ui.router', 'ngAnimate', 'ngWebSoc }; - $scope.deck_cards = []; + $http.get('/api/sections/' + $scope.sectionId + '/deck/'). success(function(data) { diff --git a/scripts/DeckController.js b/scripts/DeckController.js index dc9fcb0..04638b6 100644 --- a/scripts/DeckController.js +++ b/scripts/DeckController.js @@ -2,19 +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.refreshCards = function() { - $http.get('/api/sections/' + $scope.sectionId + '/deck/'). - success(function(data) { - console.log(data); - $scope.cards = data; - $scope.refreshLayout(); - console.log('success in refresh cards...'); - }). - error(function(err) { - console.log('refresh fail'); - }); - }; - $scope.refreshCards(); + $scope.cards = $scope.deck_cards; + $scope.refreshLayout(); } ); diff --git a/scripts/FeedController.js b/scripts/FeedController.js index c2d7608..2d19684 100644 --- a/scripts/FeedController.js +++ b/scripts/FeedController.js @@ -1,27 +1,13 @@ angular.module('flashy.FeedController', ['ui.router', 'ngAnimate', 'ngWebSocket', 'contenteditable']). - controller('FeedController', function($scope, $rootScope, $state, $http, $window, $timeout, $stateParams, $websocket, UserService) { + controller('FeedController', function($scope, $rootScope, $state, $http, $window, $timeout, $stateParams, $websocket, UserService, Flashcard) { angular.module('flashy.CardGridController').controller.apply(this, arguments); $scope.refreshCards = function() { $http.get('/api/sections/' + $scope.sectionId + '/feed/'). success(function(data) { console.log(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]); - } + for (var i in data) $scope.cards.push(new Flashcard(data[i], $scope.deck_cards)); $scope.refreshLayout(); console.log('success in refresh cards...'); }). diff --git a/scripts/FlashcardDirective.js b/scripts/FlashcardDirective.js index 2c21c10..8bd7c85 100644 --- a/scripts/FlashcardDirective.js +++ b/scripts/FlashcardDirective.js @@ -11,32 +11,7 @@ angular.module('flashy.FlashcardDirective', []). }, link: function(scope, element) { /* Handles width of the card */ - scope.textPieces = []; - scope.flashcard.mask.sort(function(a, b) { - return a[0] - b[0]; - }); - var i = 0; - scope.flashcard.mask.forEach(function(blank) { - scope.textPieces.push({text: scope.flashcard.text.slice(i, blank[0])}); - scope.textPieces.push({text: scope.flashcard.text.slice(blank[0], blank[1]), blank: true}); - i = blank[1]; - }); - scope.textPieces.push({text: scope.flashcard.text.slice(i)}); - /* Hides card from feed */ - scope.hideCard = function(flashcard) { - if ($state.current.name == 'feed') { - $http.post('/api/flashcards/' + flashcard.id + '/hide/'). - success(function(data) { - console.log('card hide success'); - scope.startShrink = true; - scope.refresh(flashcard); - }). - error(function(data) { - console.log('card hide FAILURE'); - }); - } - }; } }; } diff --git a/scripts/FlashcardFactory.js b/scripts/FlashcardFactory.js new file mode 100644 index 0000000..a16ac46 --- /dev/null +++ b/scripts/FlashcardFactory.js @@ -0,0 +1,36 @@ +angular.module('flashy.FlashcardFactory', ['ui.router']). + factory('Flashcard', function ($http) { + var Flashcard = function (data, deck) { + for (var k in data) this[k] = data[k]; + this.deck = deck; + this.textPieces = []; + this.mask.sort(function (a, b) { + return a[0] - b[0]; + }); + + var i = 0; + this.mask.forEach(function (blank) { + this.textPieces.push({text: this.text.slice(i, blank[0])}); + this.textPieces.push({text: this.text.slice(blank[0], blank[1]), blank: true}); + i = blank[1]; + }, this); + this.textPieces.push({text: this.text.slice(i)}); + }; + + Flashcard.prototype.isInDeck = function () { + return this.deck[this.id]; + }; + Flashcard.prototype.pull = function () { + if (this.deck[this.id]) 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 (!this.deck[this.id]) 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 () { + return $http.post('/api/flashcards/' + this.id + '/hide/'); + }; + + return Flashcard; + }); \ No newline at end of file diff --git a/templates/flashcard.html b/templates/flashcard.html index 0e0a004..52dd5c0 100644 --- a/templates/flashcard.html +++ b/templates/flashcard.html @@ -2,7 +2,7 @@ ng-class="{'shrinky': startShrink}">