Blame view
scripts/FeedController.js
8.69 KB
4a8b43927
|
1 |
angular.module('flashy.FeedController', ['ui.router', 'ngAnimate', 'ngWebSocket', 'contenteditable']). |
9c420b35c
|
2 |
|
cf476b03e
|
3 |
controller('FeedController', function($scope, $rootScope, $state, $http, $window, $timeout, $stateParams, $websocket, UserService) { |
ee30a48eb
|
4 |
angular.module('flashy.CardGridController').controller.apply(this, arguments); |
bbba33088
|
5 |
console.log('Hello from feed'); |
fe2c7edc6
|
6 |
|
cf476b03e
|
7 |
$scope.refreshCards = function() { |
c3350cb1f
|
8 |
$http.get('/api/sections/' + $scope.sectionId + '/feed/'). |
cf476b03e
|
9 |
success(function(data) { |
5ce17b918
|
10 |
console.log(data); |
9822fe0ff
|
11 |
$scope.cards = data; |
425c32c56
|
12 |
$scope.refreshLayout(); |
5ce17b918
|
13 14 |
console.log('success in refresh cards...'); }). |
cf476b03e
|
15 |
error(function(err) { |
5ce17b918
|
16 |
console.log('refresh fail'); |
107013870
|
17 |
console.log(err); |
5ce17b918
|
18 19 |
}); }; |
fe2c7edc6
|
20 |
|
cf476b03e
|
21 |
$scope.sortAdd = function(card, array) { |
7cf67e561
|
22 |
console.log('sort add'); |
cf476b03e
|
23 |
array.forEach(function(ele, i, ary) { |
7cf67e561
|
24 25 26 27 28 29 |
if (ele.score <= card.score) { ary.splice(i, 0, card); return; } }); }; |
fe2c7edc6
|
30 |
|
cf476b03e
|
31 |
$scope.hide = function(card) { |
fe2c7edc6
|
32 |
console.log('hiding card'); |
fe2c7edc6
|
33 |
var found = -1; |
7cf67e561
|
34 35 36 37 38 39 40 |
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; } |
fe2c7edc6
|
41 42 |
console.log('Error finding card to hide:'); console.log(card); |
7cf67e561
|
43 44 |
return -1; }; |
0c65d0c07
|
45 |
|
cf476b03e
|
46 |
$scope.update = function(id, new_score) { |
c3350cb1f
|
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
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])); } }; |
fe2c7edc6
|
66 |
|
ee30a48eb
|
67 |
$scope.feed_ws = $websocket($scope.ws_host + '/ws/feed/' + $scope.sectionId + '?subscribe-broadcast'); |
cf476b03e
|
68 |
$scope.feed_ws.onOpen(function() { |
ee30a48eb
|
69 70 |
console.log('deck ws open'); }); |
235b8e0ec
|
71 |
|
cf476b03e
|
72 |
$scope.feed_ws.onMessage(function(e) { |
7cf67e561
|
73 |
data = JSON.parse(e.data); |
235b8e0ec
|
74 |
console.log('got websocket message ' + e.data); |
7cf67e561
|
75 |
console.log(data); |
f71cb843b
|
76 |
if (data.event_type == 'new_card') { |
425c32c56
|
77 |
$scope.add(data.flashcard); |
517adf2f1
|
78 |
} else if (data.event_type == 'score_change') { |
7cf67e561
|
79 80 |
$scope.update(data.flashcard_id, data.new_score); } |
ee30a48eb
|
81 |
}); |
107013870
|
82 |
|
cf476b03e
|
83 |
$scope.pushCard = function() { |
1167ab44a
|
84 |
var myCard = { |
a7ee2b64a
|
85 86 |
// we can't trim this string because it'd mess up the blanks. Something to fix. 'text': $('#new-card-input').text(), |
88ca25463
|
87 |
'mask': $scope.newCardBlanks, |
63d40a399
|
88 |
section: $scope.section.id |
2f199068c
|
89 |
}; |
107013870
|
90 91 |
if (myCard.text == '') { console.log('blank flashcard not pushed:' + myCard.text); |
4a8b43927
|
92 |
return closeNewCard(); |
107013870
|
93 |
} |
1167ab44a
|
94 |
$http.post('/api/flashcards/', myCard). |
cf476b03e
|
95 |
success(function(data) { |
065f0cdac
|
96 |
console.log('flashcard pushed: ' + myCard.text); |
f71cb843b
|
97 98 99 |
if (!UserService.hasVerifiedEmail()) { Materialize.toast("<p>Thanks for contributing! However, others won't see your card until you verify your email address<p>", 4000); } |
8fa323ef6
|
100 |
}). |
cf476b03e
|
101 |
error(function(error) { |
807399a02
|
102 |
console.log('something went wrong pushing a card!'); |
8fa323ef6
|
103 |
}); |
4a8b43927
|
104 |
return $scope.closeNewCardModal(); |
32b3331d8
|
105 |
}; |
2ad496102
|
106 |
/* Key bindings for the whole feed window. Hotkey it up! */ |
b83f46393
|
107 |
var listenForC = true; |
807399a02
|
108 109 110 111 112 113 114 115 116 |
// 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 |
cf476b03e
|
117 |
ready: function() { |
807399a02
|
118 |
$('#new-card-input').focus(); |
4a8b43927
|
119 |
document.execCommand('selectAll', false, null); |
807399a02
|
120 121 |
} }; |
cf476b03e
|
122 |
$(document).keydown(function(e) { |
456616544
|
123 |
var keyed = e.which; |
c6ac1ffcf
|
124 |
if (keyed == 67 && listenForC) { // "c" for compose |
4a8b43927
|
125 |
$scope.openNewCardModal(); |
807399a02
|
126 |
e.preventDefault(); |
b83f46393
|
127 |
return false; |
c6ac1ffcf
|
128 |
} else if (keyed == 27) { // clear on ESC |
4a8b43927
|
129 |
$scope.closeNewCardModal(); |
456616544
|
130 131 |
} }); |
4a8b43927
|
132 |
|
cf476b03e
|
133 |
$scope.openNewCardModal = function() { |
4a8b43927
|
134 135 136 |
$('#newCard').openModal(modal_options); listenForC = false; $('#new-card-input').html('Write a flashcard!'); |
4a8b43927
|
137 |
}; |
cf476b03e
|
138 |
$scope.closeNewCardModal = function() { |
4a8b43927
|
139 140 141 142 |
listenForC = true; $('#new-card-input').html('').blur(); $('#newCard').closeModal(modal_options); }; |
a7ee2b64a
|
143 144 145 |
$('.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); |
cf476b03e
|
146 |
$('#new-card-input').on('keydown', function(e) { |
a7ee2b64a
|
147 148 149 150 151 |
if (e.which == 13) { e.preventDefault(); $scope.pushCard(); listenForC = true; return false; |
88ca25463
|
152 |
} else { |
a7ee2b64a
|
153 154 |
} }); |
cf476b03e
|
155 |
$('button#blank-selected').click(function() { |
a7ee2b64a
|
156 157 |
console.log(window.getSelection()); document.execCommand('bold'); |
34a342819
|
158 |
}); |
c6ac1ffcf
|
159 |
$scope.refreshCards(); |
88ca25463
|
160 |
$scope.newCardBlanks = []; |
cf476b03e
|
161 |
$scope.refreshNewCardInput = function() { |
88ca25463
|
162 163 |
$scope.newCardText = $('#new-card-input').text(); $scope.submit_enabled = $scope.newCardText.length >= 5 && $scope.newCardText.length <= 160; |
7a53b3dae
|
164 |
var i = 0; |
63f4d4ca5
|
165 |
$scope.newCardBlanks = []; |
cf476b03e
|
166 |
$('#new-card-input')[0].childNodes.forEach(function(node) { |
88ca25463
|
167 168 169 170 171 172 |
if (typeof node.data == 'undefined') { console.log('undefined node'); } node = $(node)[0]; if (node.tagName == 'B') { var text = $(node).text(); |
88ca25463
|
173 174 175 176 |
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++; |
63f4d4ca5
|
177 |
console.log(leftspaces, text.length); |
88ca25463
|
178 179 180 181 182 183 184 185 186 |
if (leftspaces != text.length) $scope.newCardBlanks.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; } }); |
cf476b03e
|
187 |
$scope.newCardBlanks.sort(function(a, b) { |
7a53b3dae
|
188 189 |
return a[0] - b[0]; }); |
63f4d4ca5
|
190 |
i = 0; |
7a53b3dae
|
191 |
newtext = ''; |
63f4d4ca5
|
192 |
console.log($scope.newCardBlanks); |
cf476b03e
|
193 |
$scope.newCardBlanks.forEach(function(blank) { |
7a53b3dae
|
194 195 196 197 198 199 |
newtext += $scope.newCardText.slice(i, blank[0]); newtext += '<b>' + $scope.newCardText.slice(blank[0], blank[1]) + '</b>'; i = blank[1]; }); newtext += $scope.newCardText.slice(i); //$scope.newCardFormattedText = newtext; |
88ca25463
|
200 |
}; |
cf476b03e
|
201 202 203 |
$scope.shuffleCards = function() { $timeout(function() { (function(o) { |
684d730ca
|
204 205 206 207 208 |
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]); }); }; |
cf476b03e
|
209 |
$scope.$on('$destroy', function() { |
ee30a48eb
|
210 211 |
$scope.feed_ws.close(); }); |
f0541be90
|
212 |
}); |