FlashcardFactory.js
3.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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 ? '<b>' + p.text + '</b>' : 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;
});