angular.module('flashy.FlashcardFactory', ['ui.router']). factory('Flashcard', function ($http, UserService) { var FlashcardCache = []; var Deck = null; var Flashcard = function (data) { if (typeof data == 'number') return FlashcardCache[data]; if (FlashcardCache[data.id]) return FlashcardCache[data.id]; for (var k in data) this[k] = data[k]; 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)}); this.formatted_text = ''; for (i in this.textPieces) { p = this.textPieces[i]; this.formatted_text += p.blank ? '' + p.text + '' : p.text; } this.moveUp = false; this.moveDown = false; this.colNum = 0; this.colRank = 0; FlashcardCache[this.id] = this; }; Flashcard.prototype.isInDeck = function () { return !(typeof Deck.contains(this.id) === 'undefined'); }; Flashcard.prototype.pullUnpull = function () { if (this.isInDeck()) this.unpull(); else this.pull(); }; Flashcard.prototype.pull = function () { 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 (!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 () { 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 = []; }; Flashcard.linkDeck = function(deck){ Deck = deck; } Flashcard.prototype.edit = function () { var editableText = this.formatted_text; $('.modal-trigger').leanModal({ dismissible: true, // Modal can be dismissed by clicking outside of the modal opacity: .5, // Opacity of modal background in_duration: 300, // Transition in duration out_duration: 200, // Transition out duration ready: function () { $('#edit-card-input').html(editableText); }, // Callback for Modal open complete: function () { console.log("EDIT MODAL CLOSED"); } // Callback for Modal close }); }; return Flashcard; });