Commit 26c38d230f2cd431ee2a88435e27b8a8c7ec11f9

Authored by Andrew Buss
1 parent 07a06ca9da

super high quality fix, totally good forever

Showing 1 changed file with 3 additions and 0 deletions Inline Diff

scripts/FeedController.js View file @ 26c38d2
angular.module('flashy.FeedController', ['ui.router', 'ngAnimate', 'ngWebSocket']). 1 1 angular.module('flashy.FeedController', ['ui.router', 'ngAnimate', 'ngWebSocket']).
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
var resetModal = function() { 83 83 var resetModal = function() {
$('#new-card-input').html(''); 84 84 $('#new-card-input').html('');
$('#newCard').closeModal(modal_options); 85 85 $('#newCard').closeModal(modal_options);
}; 86 86 };
87 87
$scope.pushCard = function() { 88 88 $scope.pushCard = function() {
var i = 0; 89 89 var i = 0;
var blanks = []; 90 90 var blanks = [];
$('#new-card-input')[0].childNodes.forEach(function(node) { 91 91 $('#new-card-input')[0].childNodes.forEach(function(node) {
if (typeof node.data == 'undefined') { 92 92 if (typeof node.data == 'undefined') {
console.log('undefined node'); 93 93 console.log('undefined node');
//return resetModal(); 94 94 //return resetModal();
} 95 95 }
node = $(node)[0]; 96 96 node = $(node)[0];
if (node.tagName == 'B') { 97 97 if (node.tagName == 'B') {
var text = $(node).text(); 98 98 var text = $(node).text();
console.log(text.length, text); 99 99 console.log(text.length, text);
var leftspaces = 0, rightspaces = 0; 100 100 var leftspaces = 0, rightspaces = 0;
// awful way to find the first non-space character from the left or the right. thanks.js 101 101 // awful way to find the first non-space character from the left or the right. thanks.js
while (text[leftspaces] == ' ' || text[leftspaces] == '\xa0') leftspaces++; 102 102 while (text[leftspaces] == ' ' || text[leftspaces] == '\xa0') leftspaces++;
while (text[text.length - 1 - rightspaces] == ' ' || text[text.length - 1 - rightspaces] == '\xa0') rightspaces++; 103 103 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]); 104 104 if (leftspaces != text.length) blanks.push([i + leftspaces, i + text.length - rightspaces]);
i += text.length; 105 105 i += text.length;
106 } else if (!node.data) {
107 console.log('weird node', node);
108 i += $(node).text().length;
} else { 106 109 } else {
i += node.data.length; 107 110 i += node.data.length;
} 108 111 }
}); 109 112 });
var myCard = { 110 113 var myCard = {
// we can't trim this string because it'd mess up the blanks. Something to fix. 111 114 // we can't trim this string because it'd mess up the blanks. Something to fix.
'text': $('#new-card-input').text(), 112 115 'text': $('#new-card-input').text(),
'mask': blanks, 113 116 'mask': blanks,
section: $scope.section.id 114 117 section: $scope.section.id
}; 115 118 };
if (myCard.text == '') { 116 119 if (myCard.text == '') {
console.log('blank flashcard not pushed:' + myCard.text); 117 120 console.log('blank flashcard not pushed:' + myCard.text);
return resetModal(); 118 121 return resetModal();
} 119 122 }
$http.post('/api/flashcards/', myCard). 120 123 $http.post('/api/flashcards/', myCard).
success(function(data) { 121 124 success(function(data) {
console.log('flashcard pushed: ' + myCard.text); 122 125 console.log('flashcard pushed: ' + myCard.text);
if (!UserService.hasVerifiedEmail()) { 123 126 if (!UserService.hasVerifiedEmail()) {
Materialize.toast("<p>Thanks for contributing! However, others won't see your card until you verify your email address<p>", 4000); 124 127 Materialize.toast("<p>Thanks for contributing! However, others won't see your card until you verify your email address<p>", 4000);
} 125 128 }
126 129
}). 127 130 }).
error(function(error) { 128 131 error(function(error) {
console.log('something went wrong pushing a card!'); 129 132 console.log('something went wrong pushing a card!');
}); 130 133 });
return resetModal(); 131 134 return resetModal();
}; 132 135 };
133 136
/* Key bindings for the whole feed window. Hotkey it up! */ 134 137 /* Key bindings for the whole feed window. Hotkey it up! */
var listenForC = true; 135 138 var listenForC = true;
136 139
// Need to pass these options into openmodal and leanmodal, 137 140 // Need to pass these options into openmodal and leanmodal,
// otherwise the ready handler doesn't get called 138 141 // otherwise the ready handler doesn't get called
139 142
modal_options = { 140 143 modal_options = {
dismissible: true, // Modal can be dismissed by clicking outside of the modal 141 144 dismissible: true, // Modal can be dismissed by clicking outside of the modal
opacity: 0, // Opacity of modal background 142 145 opacity: 0, // Opacity of modal background
in_duration: 300, // Transition in duration 143 146 in_duration: 300, // Transition in duration
out_duration: 200, // Transition out duration 144 147 out_duration: 200, // Transition out duration
ready: function() { 145 148 ready: function() {
listenForC = false; 146 149 listenForC = false;
console.log('modal OPENING'); 147 150 console.log('modal OPENING');
$('#new-card-input').focus(); 148 151 $('#new-card-input').focus();
}, 149 152 },
complete: function() { 150 153 complete: function() {
listenForC = true; 151 154 listenForC = true;
console.log('modal done, closing'); 152 155 console.log('modal done, closing');
$('#new-card-input').blur(); 153 156 $('#new-card-input').blur();
} 154 157 }
}; 155 158 };
156 159
$(document).keydown(function(e) { 157 160 $(document).keydown(function(e) {
var keyed = e.which; 158 161 var keyed = e.which;
if (keyed == 67 && listenForC) { // "c" for compose 159 162 if (keyed == 67 && listenForC) { // "c" for compose
$('#newCard').openModal(modal_options); 160 163 $('#newCard').openModal(modal_options);
e.preventDefault(); 161 164 e.preventDefault();
listenForC = false; 162 165 listenForC = false;
return false; 163 166 return false;
} else if (keyed == 27) { // clear on ESC 164 167 } else if (keyed == 27) { // clear on ESC
listenForC = true; 165 168 listenForC = true;
document.getElementById('new-card-input').value = ''; 166 169 document.getElementById('new-card-input').value = '';
} 167 170 }
}); 168 171 });
$('.tooltipped').tooltip({delay: 50}); 169 172 $('.tooltipped').tooltip({delay: 50});
// the "href" attribute of .modal-trigger must specify the modal ID that wants to be triggered 170 173 // the "href" attribute of .modal-trigger must specify the modal ID that wants to be triggered
$('.modal-trigger').leanModal(modal_options); 171 174 $('.modal-trigger').leanModal(modal_options);
$('#new-card-input').on('keydown', function(e) { 172 175 $('#new-card-input').on('keydown', function(e) {
if (e.which == 13) { 173 176 if (e.which == 13) {
e.preventDefault(); 174 177 e.preventDefault();
$scope.pushCard(); 175 178 $scope.pushCard();
listenForC = true; 176 179 listenForC = true;
return false; 177 180 return false;
} 178 181 }
}); 179 182 });
$('button#blank-selected').click(function() { 180 183 $('button#blank-selected').click(function() {
console.log(window.getSelection()); 181 184 console.log(window.getSelection());
document.execCommand('bold'); 182 185 document.execCommand('bold');