Blame view
scripts/FeedController.js
7.5 KB
70c2390ab
|
1 2 3 4 5 6 7 |
angular.module('flashy.FeedController', ['ui.router', 'ngAnimate', 'ngWebSocket', 'contenteditable', 'flashy.DeckFactory']).controller('FeedController', function($scope, $rootScope, $state, $http, $window, $timeout, $stateParams, $websocket, $interval, UserService, Flashcard, Deck) { |
20ed06e1b
|
8 |
angular.module('flashy.CardGridController').CardGridController.apply(this, arguments); |
80b664e09
|
9 |
|
70c2390ab
|
10 11 12 13 14 15 16 17 |
(function drawCols() { $interval(function() { if ($scope.cardColsShow != $scope.cardCols) { $scope.cardColsShow = $scope.cardCols; console.log('interval'); } }, 1000); }()); |
80b664e09
|
18 |
|
180aa08bb
|
19 |
$scope.updateCardScore = function(card) { |
5673511eb
|
20 21 22 |
console.log($scope.cardCols, card); // if no colNum is attached, then this doesn't exist on the feed yet if (!card.colNum) return; |
0dfc72586
|
23 |
$scope.cardCols[card.colNum].sort(function(a, b) { |
bf0941f00
|
24 25 |
return b.score - a.score; }); |
0adf8f867
|
26 |
$scope.updateColRanks($scope.cardCols[card.colNum]); |
c3350cb1f
|
27 |
}; |
fe2c7edc6
|
28 |
|
dc454dbeb
|
29 |
$scope.feed_ws = $websocket($scope.ws_host + '/ws/feed/' + sectionId + '?subscribe-broadcast'); |
cf476b03e
|
30 |
$scope.feed_ws.onMessage(function(e) { |
7cf67e561
|
31 |
data = JSON.parse(e.data); |
bf0941f00
|
32 |
console.log('message', data); |
f71cb843b
|
33 |
if (data.event_type == 'new_card') { |
5673511eb
|
34 |
$scope.addCardToGrid(new Flashcard(data.flashcard, $scope.deck)); |
517adf2f1
|
35 |
} else if (data.event_type == 'score_change') { |
5673511eb
|
36 |
card = new Flashcard(data.flashcard); |
0dfc72586
|
37 38 |
card.score = data.flashcard.score; $scope.updateCardScore(card); |
7cf67e561
|
39 |
} |
ee30a48eb
|
40 |
}); |
107013870
|
41 |
|
cf476b03e
|
42 |
$scope.pushCard = function() { |
1167ab44a
|
43 |
var myCard = { |
a7ee2b64a
|
44 45 |
// we can't trim this string because it'd mess up the blanks. Something to fix. 'text': $('#new-card-input').text(), |
88ca25463
|
46 |
'mask': $scope.newCardBlanks, |
63d40a399
|
47 |
section: $scope.section.id |
2f199068c
|
48 |
}; |
107013870
|
49 50 |
if (myCard.text == '') { console.log('blank flashcard not pushed:' + myCard.text); |
4a8b43927
|
51 |
return closeNewCard(); |
107013870
|
52 |
} |
1167ab44a
|
53 |
$http.post('/api/flashcards/', myCard). |
cf476b03e
|
54 |
success(function(data) { |
065f0cdac
|
55 |
console.log('flashcard pushed: ' + myCard.text); |
f71cb843b
|
56 57 58 |
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
|
59 |
}); |
4a8b43927
|
60 |
return $scope.closeNewCardModal(); |
32b3331d8
|
61 |
}; |
2ad496102
|
62 |
/* Key bindings for the whole feed window. Hotkey it up! */ |
b83f46393
|
63 |
var listenForC = true; |
807399a02
|
64 65 66 67 68 69 70 71 72 |
// 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
|
73 |
ready: function() { |
807399a02
|
74 |
$('#new-card-input').focus(); |
4a8b43927
|
75 |
document.execCommand('selectAll', false, null); |
807399a02
|
76 77 |
} }; |
cf476b03e
|
78 |
$(document).keydown(function(e) { |
456616544
|
79 |
var keyed = e.which; |
c6ac1ffcf
|
80 |
if (keyed == 67 && listenForC) { // "c" for compose |
4a8b43927
|
81 |
$scope.openNewCardModal(); |
807399a02
|
82 |
e.preventDefault(); |
b83f46393
|
83 |
return false; |
c6ac1ffcf
|
84 |
} else if (keyed == 27) { // clear on ESC |
4a8b43927
|
85 |
$scope.closeNewCardModal(); |
456616544
|
86 87 |
} }); |
4a8b43927
|
88 |
|
cf476b03e
|
89 |
$scope.openNewCardModal = function() { |
4a8b43927
|
90 91 92 |
$('#newCard').openModal(modal_options); listenForC = false; $('#new-card-input').html('Write a flashcard!'); |
4a8b43927
|
93 |
}; |
cf476b03e
|
94 |
$scope.closeNewCardModal = function() { |
4a8b43927
|
95 96 97 98 |
listenForC = true; $('#new-card-input').html('').blur(); $('#newCard').closeModal(modal_options); }; |
a7ee2b64a
|
99 100 101 |
$('.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
|
102 |
$('#new-card-input').on('keydown', function(e) { |
a7ee2b64a
|
103 104 |
if (e.which == 13) { e.preventDefault(); |
edf69c107
|
105 106 107 108 |
if ($scope.submit_enabled) { $scope.pushCard(); listenForC = true; } |
a7ee2b64a
|
109 |
return false; |
88ca25463
|
110 |
} else { |
a7ee2b64a
|
111 112 |
} }); |
cf476b03e
|
113 |
$('button#blank-selected').click(function() { |
a7ee2b64a
|
114 115 |
console.log(window.getSelection()); document.execCommand('bold'); |
34a342819
|
116 |
}); |
88ca25463
|
117 |
$scope.newCardBlanks = []; |
cf476b03e
|
118 |
$scope.refreshNewCardInput = function() { |
88ca25463
|
119 120 |
$scope.newCardText = $('#new-card-input').text(); $scope.submit_enabled = $scope.newCardText.length >= 5 && $scope.newCardText.length <= 160; |
7a53b3dae
|
121 |
var i = 0; |
63f4d4ca5
|
122 |
$scope.newCardBlanks = []; |
cf476b03e
|
123 |
$('#new-card-input')[0].childNodes.forEach(function(node) { |
88ca25463
|
124 125 126 |
node = $(node)[0]; if (node.tagName == 'B') { var text = $(node).text(); |
88ca25463
|
127 128 129 130 |
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
|
131 |
console.log(leftspaces, text.length); |
88ca25463
|
132 133 134 |
if (leftspaces != text.length) $scope.newCardBlanks.push([i + leftspaces, i + text.length - rightspaces]); i += text.length; } else if (!node.data) { |
88ca25463
|
135 136 137 138 139 |
i += $(node).text().length; } else { i += node.data.length; } }); |
cf476b03e
|
140 |
$scope.newCardBlanks.sort(function(a, b) { |
7a53b3dae
|
141 142 |
return a[0] - b[0]; }); |
63f4d4ca5
|
143 |
i = 0; |
7a53b3dae
|
144 |
newtext = ''; |
cf476b03e
|
145 |
$scope.newCardBlanks.forEach(function(blank) { |
7a53b3dae
|
146 147 148 149 150 151 |
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
|
152 |
}; |
cf476b03e
|
153 154 155 |
$scope.shuffleCards = function() { $timeout(function() { (function(o) { |
684d730ca
|
156 157 158 159 160 |
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
|
161 |
$scope.$on('$destroy', function() { |
ee30a48eb
|
162 163 |
$scope.feed_ws.close(); }); |
dc454dbeb
|
164 |
return $http.get('/api/sections/' + sectionId + '/feed/'). |
20ed06e1b
|
165 166 167 168 169 170 171 172 173 174 175 176 |
success(function(data) { console.log(data); $scope.cards = data.map(function(card) { return new Flashcard(card, $scope.deck); }); $scope.refreshLayout().then(function() { $timeout($scope.refreshColumnWidth).then(function() { $scope.showGrid = true; }); console.log('layout done'); }); }); |
f0541be90
|
177 |
}); |