FeedController.js
7.82 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
angular.module('flashy.FeedController', ['ui.router', 'ngAnimate', 'ngWebSocket', 'contenteditable']).
controller('FeedController', function($scope, $rootScope, $state, $http, $window, $timeout, $stateParams, $websocket, UserService) {
angular.module('flashy.CardGridController').controller.apply(this, arguments);
console.log('Hello from feed');
$scope.refreshCards = function() {
$http.get('/api/sections/' + $scope.sectionId + '/feed/').
success(function(data) {
console.log(data);
$scope.cards = data;
$scope.refreshLayout();
console.log('success in refresh cards...');
}).
error(function(err) {
console.log('refresh fail');
console.log(err);
});
};
$scope.sortAdd = function(card, array) {
console.log('sort add');
array.forEach(function(ele, i, ary) {
if (ele.score <= card.score) {
ary.splice(i, 0, card);
return;
}
});
};
$scope.hide = function(card) {
console.log('hiding card');
var found = -1;
col = $scope.cardTable[card.id].colNum;
found = $scope.cardCols[col].indexOf(card);
if (found != -1) {
$scope.cardCols[col].splice(found, 1);
console.log('card hidden');
return col;
}
console.log('Error finding card to hide:');
console.log(card);
return -1;
};
$scope.update = function(id, new_score) {
card = $scope.cardTable[id].obj;
if (Math.abs(new_score - card.score) < .0001) {
console.log('score same, no update required');
return;
}
console.log('updating');
console.log(card);
var column = $scope.cardCols[$scope.cardTable[id].colNum];
var found = column.indexOf(card);
var i = 0;
for (; i < column.length; i++)
if (column[i].score <= new_score) break;
card.score = new_score;
if ($scope.$$phase) { // most of the time it is "$digest"
column.splice(i, 0, column.splice(found, 1)[0]);
} else {
$scope.$apply(column.splice(i, 0, column.splice(found, 1)[0]));
}
};
$scope.feed_ws = $websocket($scope.ws_host + '/ws/feed/' + $scope.sectionId + '?subscribe-broadcast');
$scope.feed_ws.onOpen(function() {
console.log('deck ws open');
});
$scope.feed_ws.onMessage(function(e) {
data = JSON.parse(e.data);
console.log('got websocket message ' + e.data);
console.log(data);
if (data.event_type == 'new_card') {
$scope.add(data.flashcard);
} else if (data.event_type == 'score_change') {
$scope.update(data.flashcard_id, data.new_score);
}
});
$scope.pushCard = function() {
var i = 0;
var blanks = [];
$('#new-card-input')[0].childNodes.forEach(function(node) {
if (typeof node.data == 'undefined') {
console.log('undefined node');
}
node = $(node)[0];
if (node.tagName == 'B') {
var text = $(node).text();
console.log(text.length, text);
var leftspaces = 0, rightspaces = 0;
// awful way to find the first non-space character from the left or the right. thanks.js
while (text[leftspaces] == ' ' || text[leftspaces] == '\xa0') leftspaces++;
while (text[text.length - 1 - rightspaces] == ' ' || text[text.length - 1 - rightspaces] == '\xa0') rightspaces++;
if (leftspaces != text.length) blanks.push([i + leftspaces, i + text.length - rightspaces]);
i += text.length;
} else if (!node.data) {
console.log('weird node', node);
i += $(node).text().length;
} else {
i += node.data.length;
}
});
var myCard = {
// we can't trim this string because it'd mess up the blanks. Something to fix.
'text': $('#new-card-input').text(),
'mask': blanks,
section: $scope.section.id
};
if (myCard.text == '') {
console.log('blank flashcard not pushed:' + myCard.text);
return closeNewCard();
}
$http.post('/api/flashcards/', myCard).
success(function(data) {
console.log('flashcard pushed: ' + myCard.text);
if (!UserService.hasVerifiedEmail()) {
Materialize.toast("<p>Thanks for contributing! However, others won't see your card until you verify your email address<p>", 4000);
}
}).
error(function(error) {
console.log('something went wrong pushing a card!');
});
return $scope.closeNewCardModal();
};
/* Key bindings for the whole feed window. Hotkey it up! */
var listenForC = true;
// Need to pass these options into openmodal and leanmodal,
// otherwise the ready handler doesn't get called
modal_options = {
dismissible: true, // Modal can be dismissed by clicking outside of the modal
opacity: 0, // Opacity of modal background
in_duration: 300, // Transition in duration
out_duration: 200, // Transition out duration
ready: function() {
$('#new-card-input').focus();
document.execCommand('selectAll', false, null);
}
};
$(document).keydown(function(e) {
var keyed = e.which;
if (keyed == 67 && listenForC) { // "c" for compose
$scope.openNewCardModal();
e.preventDefault();
return false;
} else if (keyed == 27) { // clear on ESC
$scope.closeNewCardModal();
}
});
$scope.openNewCardModal = function() {
$('#newCard').openModal(modal_options);
listenForC = false;
$('#new-card-input').html('Write a flashcard!');
};
$scope.closeNewCardModal = function() {
listenForC = true;
$('#new-card-input').html('').blur();
$('#newCard').closeModal(modal_options);
};
$('.tooltipped').tooltip({delay: 50});
// the "href" attribute of .modal-trigger must specify the modal ID that wants to be triggered
$('.modal-trigger').leanModal(modal_options);
$('#new-card-input').on('keydown', function(e) {
if (e.which == 13) {
e.preventDefault();
$scope.pushCard();
listenForC = true;
return false;
}
});
$('button#blank-selected').click(function() {
console.log(window.getSelection());
document.execCommand('bold');
});
$scope.refreshCards();
$scope.shuffleCards = function() {
$timeout(function() {
(function(o) {
for (var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
return o;
})($scope.cardCols[0]);
});
};
$scope.$on('$destroy', function() {
$scope.feed_ws.close();
});
});