Commit 7a53b3daec6b6b0206a21ec520f3f5752cd2a77e
1 parent
88ca25463b
Exists in
master
and in
1 other branch
fix blanks as you type except not really
Showing 1 changed file with 39 additions and 26 deletions Inline Diff
scripts/FeedController.js
View file @
7a53b3d
angular.module('flashy.FeedController', ['ui.router', 'ngAnimate', 'ngWebSocket', 'contenteditable']). | 1 | 1 | angular.module('flashy.FeedController', ['ui.router', 'ngAnimate', 'ngWebSocket', 'contenteditable']). | |
2 | 2 | |||
controller('FeedController', function($scope, $rootScope, $state, $http, $window, $timeout, $stateParams, $websocket, UserService) { | 3 | 3 | controller('FeedController', function ($scope, $rootScope, $state, $http, $window, $timeout, $stateParams, $websocket, UserService) { | |
angular.module('flashy.CardGridController').controller.apply(this, arguments); | 4 | 4 | angular.module('flashy.CardGridController').controller.apply(this, arguments); | |
console.log('Hello from feed'); | 5 | 5 | console.log('Hello from feed'); | |
6 | 6 | |||
$scope.refreshCards = function() { | 7 | 7 | $scope.refreshCards = function () { | |
$http.get('/api/sections/' + $scope.sectionId + '/feed/'). | 8 | 8 | $http.get('/api/sections/' + $scope.sectionId + '/feed/'). | |
success(function(data) { | 9 | 9 | success(function (data) { | |
console.log(data); | 10 | 10 | console.log(data); | |
$scope.cards = data; | 11 | 11 | $scope.cards = data; | |
$scope.refreshLayout(); | 12 | 12 | $scope.refreshLayout(); | |
console.log('success in refresh cards...'); | 13 | 13 | console.log('success in refresh cards...'); | |
}). | 14 | 14 | }). | |
error(function(err) { | 15 | 15 | error(function (err) { | |
console.log('refresh fail'); | 16 | 16 | console.log('refresh fail'); | |
console.log(err); | 17 | 17 | console.log(err); | |
}); | 18 | 18 | }); | |
}; | 19 | 19 | }; | |
20 | 20 | |||
$scope.sortAdd = function(card, array) { | 21 | 21 | $scope.sortAdd = function (card, array) { | |
console.log('sort add'); | 22 | 22 | console.log('sort add'); | |
array.forEach(function(ele, i, ary) { | 23 | 23 | array.forEach(function (ele, i, ary) { | |
if (ele.score <= card.score) { | 24 | 24 | if (ele.score <= card.score) { | |
ary.splice(i, 0, card); | 25 | 25 | ary.splice(i, 0, card); | |
return; | 26 | 26 | return; | |
} | 27 | 27 | } | |
}); | 28 | 28 | }); | |
}; | 29 | 29 | }; | |
30 | 30 | |||
$scope.hide = function(card) { | 31 | 31 | $scope.hide = function (card) { | |
console.log('hiding card'); | 32 | 32 | console.log('hiding card'); | |
var found = -1; | 33 | 33 | var found = -1; | |
col = $scope.cardTable[card.id].colNum; | 34 | 34 | col = $scope.cardTable[card.id].colNum; | |
found = $scope.cardCols[col].indexOf(card); | 35 | 35 | found = $scope.cardCols[col].indexOf(card); | |
if (found != -1) { | 36 | 36 | if (found != -1) { | |
$scope.cardCols[col].splice(found, 1); | 37 | 37 | $scope.cardCols[col].splice(found, 1); | |
console.log('card hidden'); | 38 | 38 | console.log('card hidden'); | |
return col; | 39 | 39 | return col; | |
} | 40 | 40 | } | |
console.log('Error finding card to hide:'); | 41 | 41 | console.log('Error finding card to hide:'); | |
console.log(card); | 42 | 42 | console.log(card); | |
return -1; | 43 | 43 | return -1; | |
}; | 44 | 44 | }; | |
45 | 45 | |||
$scope.update = function(id, new_score) { | 46 | 46 | $scope.update = function (id, new_score) { | |
card = $scope.cardTable[id].obj; | 47 | 47 | card = $scope.cardTable[id].obj; | |
if (Math.abs(new_score - card.score) < .0001) { | 48 | 48 | if (Math.abs(new_score - card.score) < .0001) { | |
console.log('score same, no update required'); | 49 | 49 | console.log('score same, no update required'); | |
return; | 50 | 50 | return; | |
} | 51 | 51 | } | |
console.log('updating'); | 52 | 52 | console.log('updating'); | |
console.log(card); | 53 | 53 | console.log(card); | |
var column = $scope.cardCols[$scope.cardTable[id].colNum]; | 54 | 54 | var column = $scope.cardCols[$scope.cardTable[id].colNum]; | |
var found = column.indexOf(card); | 55 | 55 | var found = column.indexOf(card); | |
var i = 0; | 56 | 56 | var i = 0; | |
for (; i < column.length; i++) | 57 | 57 | for (; i < column.length; i++) | |
if (column[i].score <= new_score) break; | 58 | 58 | if (column[i].score <= new_score) break; | |
card.score = new_score; | 59 | 59 | card.score = new_score; | |
if ($scope.$$phase) { // most of the time it is "$digest" | 60 | 60 | if ($scope.$$phase) { // most of the time it is "$digest" | |
column.splice(i, 0, column.splice(found, 1)[0]); | 61 | 61 | column.splice(i, 0, column.splice(found, 1)[0]); | |
} else { | 62 | 62 | } else { | |
$scope.$apply(column.splice(i, 0, column.splice(found, 1)[0])); | 63 | 63 | $scope.$apply(column.splice(i, 0, column.splice(found, 1)[0])); | |
} | 64 | 64 | } | |
}; | 65 | 65 | }; | |
66 | 66 | |||
$scope.feed_ws = $websocket($scope.ws_host + '/ws/feed/' + $scope.sectionId + '?subscribe-broadcast'); | 67 | 67 | $scope.feed_ws = $websocket($scope.ws_host + '/ws/feed/' + $scope.sectionId + '?subscribe-broadcast'); | |
$scope.feed_ws.onOpen(function() { | 68 | 68 | $scope.feed_ws.onOpen(function () { | |
console.log('deck ws open'); | 69 | 69 | console.log('deck ws open'); | |
}); | 70 | 70 | }); | |
71 | 71 | |||
$scope.feed_ws.onMessage(function(e) { | 72 | 72 | $scope.feed_ws.onMessage(function (e) { | |
data = JSON.parse(e.data); | 73 | 73 | data = JSON.parse(e.data); | |
console.log('got websocket message ' + e.data); | 74 | 74 | console.log('got websocket message ' + e.data); | |
console.log(data); | 75 | 75 | console.log(data); | |
if (data.event_type == 'new_card') { | 76 | 76 | if (data.event_type == 'new_card') { | |
$scope.add(data.flashcard); | 77 | 77 | $scope.add(data.flashcard); | |
} else if (data.event_type == 'score_change') { | 78 | 78 | } else if (data.event_type == 'score_change') { | |
$scope.update(data.flashcard_id, data.new_score); | 79 | 79 | $scope.update(data.flashcard_id, data.new_score); | |
} | 80 | 80 | } | |
}); | 81 | 81 | }); | |
82 | 82 | |||
$scope.pushCard = function() { | 83 | 83 | $scope.pushCard = function () { | |
var myCard = { | 84 | 84 | var myCard = { | |
// we can't trim this string because it'd mess up the blanks. Something to fix. | 85 | 85 | // we can't trim this string because it'd mess up the blanks. Something to fix. | |
'text': $('#new-card-input').text(), | 86 | 86 | 'text': $('#new-card-input').text(), | |
'mask': $scope.newCardBlanks, | 87 | 87 | 'mask': $scope.newCardBlanks, | |
section: $scope.section.id | 88 | 88 | section: $scope.section.id | |
}; | 89 | 89 | }; | |
if (myCard.text == '') { | 90 | 90 | if (myCard.text == '') { | |
console.log('blank flashcard not pushed:' + myCard.text); | 91 | 91 | console.log('blank flashcard not pushed:' + myCard.text); | |
return closeNewCard(); | 92 | 92 | return closeNewCard(); | |
} | 93 | 93 | } | |
$http.post('/api/flashcards/', myCard). | 94 | 94 | $http.post('/api/flashcards/', myCard). | |
success(function(data) { | 95 | 95 | success(function (data) { | |
console.log('flashcard pushed: ' + myCard.text); | 96 | 96 | console.log('flashcard pushed: ' + myCard.text); | |
if (!UserService.hasVerifiedEmail()) { | 97 | 97 | if (!UserService.hasVerifiedEmail()) { | |
Materialize.toast("<p>Thanks for contributing! However, others won't see your card until you verify your email address<p>", 4000); | 98 | 98 | Materialize.toast("<p>Thanks for contributing! However, others won't see your card until you verify your email address<p>", 4000); | |
} | 99 | 99 | } | |
}). | 100 | 100 | }). | |
error(function(error) { | 101 | 101 | error(function (error) { | |
console.log('something went wrong pushing a card!'); | 102 | 102 | console.log('something went wrong pushing a card!'); | |
}); | 103 | 103 | }); | |
return $scope.closeNewCardModal(); | 104 | 104 | return $scope.closeNewCardModal(); | |
}; | 105 | 105 | }; | |
106 | 106 | |||
/* Key bindings for the whole feed window. Hotkey it up! */ | 107 | 107 | /* Key bindings for the whole feed window. Hotkey it up! */ | |
var listenForC = true; | 108 | 108 | var listenForC = true; | |
109 | 109 | |||
// Need to pass these options into openmodal and leanmodal, | 110 | 110 | // Need to pass these options into openmodal and leanmodal, | |
// otherwise the ready handler doesn't get called | 111 | 111 | // otherwise the ready handler doesn't get called | |
112 | 112 | |||
modal_options = { | 113 | 113 | modal_options = { | |
dismissible: true, // Modal can be dismissed by clicking outside of the modal | 114 | 114 | dismissible: true, // Modal can be dismissed by clicking outside of the modal | |
opacity: 0, // Opacity of modal background | 115 | 115 | opacity: 0, // Opacity of modal background | |
in_duration: 300, // Transition in duration | 116 | 116 | in_duration: 300, // Transition in duration | |
out_duration: 200, // Transition out duration | 117 | 117 | out_duration: 200, // Transition out duration | |
ready: function() { | 118 | 118 | ready: function () { | |
$('#new-card-input').focus(); | 119 | 119 | $('#new-card-input').focus(); | |
document.execCommand('selectAll', false, null); | 120 | 120 | document.execCommand('selectAll', false, null); | |
} | 121 | 121 | } | |
}; | 122 | 122 | }; | |
123 | 123 | |||
$(document).keydown(function(e) { | 124 | 124 | $(document).keydown(function (e) { | |
var keyed = e.which; | 125 | 125 | var keyed = e.which; | |
if (keyed == 67 && listenForC) { // "c" for compose | 126 | 126 | if (keyed == 67 && listenForC) { // "c" for compose | |
$scope.openNewCardModal(); | 127 | 127 | $scope.openNewCardModal(); | |
e.preventDefault(); | 128 | 128 | e.preventDefault(); | |
return false; | 129 | 129 | return false; | |
} else if (keyed == 27) { // clear on ESC | 130 | 130 | } else if (keyed == 27) { // clear on ESC | |
$scope.closeNewCardModal(); | 131 | 131 | $scope.closeNewCardModal(); | |
} | 132 | 132 | } | |
}); | 133 | 133 | }); | |
134 | 134 | |||
$scope.openNewCardModal = function() { | 135 | 135 | $scope.openNewCardModal = function () { | |
$('#newCard').openModal(modal_options); | 136 | 136 | $('#newCard').openModal(modal_options); | |
listenForC = false; | 137 | 137 | listenForC = false; | |
$('#new-card-input').html('Write a flashcard!'); | 138 | 138 | $('#new-card-input').html('Write a flashcard!'); | |
}; | 139 | 139 | }; | |
140 | 140 | |||
$scope.closeNewCardModal = function() { | 141 | 141 | $scope.closeNewCardModal = function () { | |
listenForC = true; | 142 | 142 | listenForC = true; | |
$('#new-card-input').html('').blur(); | 143 | 143 | $('#new-card-input').html('').blur(); | |
$('#newCard').closeModal(modal_options); | 144 | 144 | $('#newCard').closeModal(modal_options); | |
}; | 145 | 145 | }; | |
146 | 146 | |||
$('.tooltipped').tooltip({delay: 50}); | 147 | 147 | $('.tooltipped').tooltip({delay: 50}); | |
// the "href" attribute of .modal-trigger must specify the modal ID that wants to be triggered | 148 | 148 | // the "href" attribute of .modal-trigger must specify the modal ID that wants to be triggered | |
$('.modal-trigger').leanModal(modal_options); | 149 | 149 | $('.modal-trigger').leanModal(modal_options); | |
$('#new-card-input').on('keydown', function(e) { | 150 | 150 | $('#new-card-input').on('keydown', function (e) { | |
if (e.which == 13) { | 151 | 151 | if (e.which == 13) { | |
e.preventDefault(); | 152 | 152 | e.preventDefault(); | |
$scope.pushCard(); | 153 | 153 | $scope.pushCard(); | |
listenForC = true; | 154 | 154 | listenForC = true; | |
return false; | 155 | 155 | return false; | |
} else { | 156 | 156 | } else { | |
157 | 157 | |||
} | 158 | 158 | } | |
}); | 159 | 159 | }); | |
$('button#blank-selected').click(function() { | 160 | 160 | $('button#blank-selected').click(function () { | |
console.log(window.getSelection()); | 161 | 161 | console.log(window.getSelection()); | |
document.execCommand('bold'); | 162 | 162 | document.execCommand('bold'); | |
}); | 163 | 163 | }); | |
$scope.refreshCards(); | 164 | 164 | $scope.refreshCards(); | |
$scope.newCardBlanks = []; | 165 | 165 | $scope.newCardBlanks = []; | |
$scope.refreshNewCardInput = function() { | 166 | 166 | $scope.refreshNewCardInput = function () { | |
$scope.newCardText = $('#new-card-input').text(); | 167 | 167 | $scope.newCardText = $('#new-card-input').text(); | |
$scope.submit_enabled = $scope.newCardText.length >= 5 && $scope.newCardText.length <= 160; | 168 | 168 | $scope.submit_enabled = $scope.newCardText.length >= 5 && $scope.newCardText.length <= 160; | |
$('#new-card-input')[0].childNodes.forEach(function(node) { | 169 | 169 | var i = 0; | |
170 | $('#new-card-input')[0].childNodes.forEach(function (node) { | |||
if (typeof node.data == 'undefined') { | 170 | 171 | if (typeof node.data == 'undefined') { | |
console.log('undefined node'); | 171 | 172 | console.log('undefined node'); | |
} | 172 | 173 | } | |
node = $(node)[0]; | 173 | 174 | node = $(node)[0]; | |
175 | $scope.newCardBlanks = []; | |||
if (node.tagName == 'B') { | 174 | 176 | if (node.tagName == 'B') { | |
var text = $(node).text(); | 175 | 177 | var text = $(node).text(); | |
console.log(text.length, text); | 176 | 178 | console.log(text.length, text); | |
var leftspaces = 0, rightspaces = 0; | 177 | 179 | var leftspaces = 0, rightspaces = 0; | |
// awful way to find the first non-space character from the left or the right. thanks.js | 178 | 180 | // awful way to find the first non-space character from the left or the right. thanks.js | |
while (text[leftspaces] == ' ' || text[leftspaces] == '\xa0') leftspaces++; | 179 | 181 | while (text[leftspaces] == ' ' || text[leftspaces] == '\xa0') leftspaces++; | |
while (text[text.length - 1 - rightspaces] == ' ' || text[text.length - 1 - rightspaces] == '\xa0') rightspaces++; | 180 | 182 | while (text[text.length - 1 - rightspaces] == ' ' || text[text.length - 1 - rightspaces] == '\xa0') rightspaces++; | |
if (leftspaces != text.length) $scope.newCardBlanks.push([i + leftspaces, i + text.length - rightspaces]); | 181 | 183 | if (leftspaces != text.length) $scope.newCardBlanks.push([i + leftspaces, i + text.length - rightspaces]); | |
i += text.length; | 182 | 184 | i += text.length; | |
} else if (!node.data) { | 183 | 185 | } else if (!node.data) { | |
console.log('weird node', node); | 184 | 186 | console.log('weird node', node); | |
i += $(node).text().length; | 185 | 187 | i += $(node).text().length; | |
} else { | 186 | 188 | } else { | |
i += node.data.length; | 187 | 189 | i += node.data.length; | |
} | 188 | 190 | } | |
}); | 189 | 191 | }); | |
190 | 192 | $scope.newCardBlanks.sort(function (a, b) { | ||
193 | return a[0] - b[0]; | |||
194 | }); | |||
195 | var i = 0; | |||
196 | newtext = ''; | |||
197 | $scope.newCardBlanks.forEach(function (blank) { | |||
198 | newtext += $scope.newCardText.slice(i, blank[0]); | |||
199 | newtext += '<b>' + $scope.newCardText.slice(blank[0], blank[1]) + '</b>'; | |||
200 | i = blank[1]; | |||
201 | }); | |||
202 | newtext += $scope.newCardText.slice(i); | |||
203 | //$scope.newCardFormattedText = newtext; | |||
}; | 191 | 204 | }; | |
$scope.shuffleCards = function() { | 192 | 205 | $scope.shuffleCards = function () { | |
$timeout(function() { | 193 | 206 | $timeout(function () { | |
(function(o) { | 194 | 207 | (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); | 195 | 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; | 196 | 209 | return o; | |
})($scope.cardCols[0]); | 197 | 210 | })($scope.cardCols[0]); | |
}); | 198 | 211 | }); | |
}; | 199 | 212 | }; | |
$scope.$on('$destroy', function() { | 200 | 213 | $scope.$on('$destroy', function () { | |
$scope.feed_ws.close(); | 201 | 214 | $scope.feed_ws.close(); | |
}); | 202 | 215 | }); | |
}); | 203 | 216 | }); | |
204 | 217 | |||