FeedController.js
8.29 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
angular.module('flashy.FeedController', ['ui.router', 'ngAnimate']).
controller('FeedController', function($scope, $rootScope, $stateParams, $state, $http, $window, $timeout, UserService) {
console.log('Hello from feed');
sectionId = $stateParams.sectionId;
$rootScope.currentSection = $rootScope.SectionResource.get({sectionId: sectionId});
$scope.cards = false;
$scope.cardCols = []; // organized data
$scope.numCols = 0;
function calculate_cols() {
var avail = $window.innerWidth - 17;
return Math.max(1, Math.floor(avail / 250));
}
$scope.refreshColumnWidth = function() {
console.log('refreshing column width');
avail = $window.innerWidth - 17;
width = Math.floor(avail / Math.floor(avail / 250));
$('.cardColumn').css({
width: width + 'px',
'font-size': 100 * width / 250 + '%'
});
$('.cardColumn .card.flashy').css({
width: width - 12 + 'px',
height: (width * 3 / 5) + 'px'
});
};
$scope.refreshLayout = function() {
// check if we actually need to refresh the whole layout
if (calculate_cols() == $scope.numCols) return $scope.refreshColumnWidth();
$scope.numCols = calculate_cols();
console.log('refreshing layout for ' + $scope.numCols + ' columns');
$scope.cardCols = [];
var cols = [];
for (i = 0; i < $scope.numCols; i++) cols.push([]);
$scope.cards.forEach(function(card, i) {
cols[i % $scope.numCols].push(card);
});
// wait until the next digest cycle to update cardCols
$timeout(function() {
$scope.cardCols = cols;
$timeout($scope.refreshColumnWidth);
});
};
angular.element($window).bind('resize', $scope.refreshLayout);
$scope.refreshCards = function() {
$http.get('/api/sections/' + 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');
});
};
$scope.add = function(card) {
var colNum = 0;
while (colNum < $scope.numCols) {
if ($scope.cardCols[colNum].length == 0) break;
colNum++;
}
if (colNum == $scope.numCols) {
colNum = Math.floor(Math.random() * $scope.numCols);
}
console.log('adding card to column ' + colNum);
console.log(card);
$scope.cards.push(data);
$timeout(function() {
$scope.cardCols[colNum].unshift(card);
$timeout($scope.refreshColumnWidth);
});
};
$scope.hide = function(card) {
console.log('hiding card');
var found = -1;
for (i = 0; i < $scope.cardCols.length; i++) {
found = $scope.cardCols[i].indexOf(card);
if (found != -1) {
$timeout(function() {
console.log('card to hide, found');
});
$scope.cardCols[i].splice(found, 1);
return;
}
}
console.log('Error finding card to hide:');
console.log(card);
};
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/feed/' + sectionId + '?subscribe-broadcast');
ws.onopen = function() {
console.log('websocket connected');
};
ws.onmessage = function(e) {
console.log('got websocket message ' + e.data);
data = JSON.parse(e.data);
if (data.event_type == 'new_card') {
$scope.add(data.flashcard);
}
};
ws.onerror = function(e) {
console.error(e);
};
ws.onclose = function(e) {
console.log('connection closed');
};
$scope.pushCard = function() {
var i = 0;
var blanks = [];
$('#new-card-input')[0].childNodes.forEach(function(node) {
node = $(node)[0];
console.log(node);
if (node.tagName == 'B') {
text = $(node).text();
blanks.push([i, i + text.length]);
i += text.length;
} else {
i += node.data.length;
}
});
var myCard = {
'text': $('#new-card-input').text(),
'mask': blanks,
section: sectionId
};
$http.post('/api/flashcards/', myCard).
success(function(data) {
console.log('flashcard push succeeded');
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!');
});
};
/* 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() {
listenForC = false;
console.log('modal OPENING');
$('#new-card-input').focus();
},
complete: function() {
listenForC = true;
console.log('modal done, closing');
$('#new-card-input').blur();
}
};
$(document).keydown(function(e) {
var keyed = e.which;
if (keyed == 67 && listenForC) { // "c" for compose
$('#newCard').openModal(modal_options);
e.preventDefault();
listenForC = false;
return false;
} else if (keyed == 27) { // clear on ESC
listenForC = true;
document.getElementById('new-card-input').value = '';
}
});
$(document).ready(function() {
$('.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();
$('#new-card-input').html('');
$('#newCard').closeModal(modal_options);
listenForC = true;
return false;
}
});
$('button#blank-selected').click(function() {
console.log(window.getSelection());
document.execCommand('bold');
});
});
$scope.refreshCards();
$scope.$on('$destroy', function() {
ws.close();
$rootScope.currentSection = {};
$(document).off('keydown');
});
$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]);
});
};
window.feedscope = $scope;
});