Commit 7a53b3daec6b6b0206a21ec520f3f5752cd2a77e

Authored by Andrew Buss
1 parent 88ca25463b

fix blanks as you type except not really

Showing 1 changed file with 39 additions and 26 deletions Side-by-side Diff

scripts/FeedController.js View file @ 7a53b3d
1 1 angular.module('flashy.FeedController', ['ui.router', 'ngAnimate', 'ngWebSocket', 'contenteditable']).
2 2  
3   - controller('FeedController', function($scope, $rootScope, $state, $http, $window, $timeout, $stateParams, $websocket, UserService) {
  3 + controller('FeedController', function ($scope, $rootScope, $state, $http, $window, $timeout, $stateParams, $websocket, UserService) {
4 4 angular.module('flashy.CardGridController').controller.apply(this, arguments);
5 5 console.log('Hello from feed');
6 6  
7   - $scope.refreshCards = function() {
  7 + $scope.refreshCards = function () {
8 8 $http.get('/api/sections/' + $scope.sectionId + '/feed/').
9   - success(function(data) {
  9 + success(function (data) {
10 10 console.log(data);
11 11 $scope.cards = data;
12 12 $scope.refreshLayout();
13 13 console.log('success in refresh cards...');
14 14 }).
15   - error(function(err) {
  15 + error(function (err) {
16 16 console.log('refresh fail');
17 17 console.log(err);
18 18 });
19 19 };
20 20  
21   - $scope.sortAdd = function(card, array) {
  21 + $scope.sortAdd = function (card, array) {
22 22 console.log('sort add');
23   - array.forEach(function(ele, i, ary) {
  23 + array.forEach(function (ele, i, ary) {
24 24 if (ele.score <= card.score) {
25 25 ary.splice(i, 0, card);
26 26 return;
... ... @@ -28,7 +28,7 @@
28 28 });
29 29 };
30 30  
31   - $scope.hide = function(card) {
  31 + $scope.hide = function (card) {
32 32 console.log('hiding card');
33 33 var found = -1;
34 34 col = $scope.cardTable[card.id].colNum;
... ... @@ -43,7 +43,7 @@
43 43 return -1;
44 44 };
45 45  
46   - $scope.update = function(id, new_score) {
  46 + $scope.update = function (id, new_score) {
47 47 card = $scope.cardTable[id].obj;
48 48 if (Math.abs(new_score - card.score) < .0001) {
49 49 console.log('score same, no update required');
50 50  
... ... @@ -65,11 +65,11 @@
65 65 };
66 66  
67 67 $scope.feed_ws = $websocket($scope.ws_host + '/ws/feed/' + $scope.sectionId + '?subscribe-broadcast');
68   - $scope.feed_ws.onOpen(function() {
  68 + $scope.feed_ws.onOpen(function () {
69 69 console.log('deck ws open');
70 70 });
71 71  
72   - $scope.feed_ws.onMessage(function(e) {
  72 + $scope.feed_ws.onMessage(function (e) {
73 73 data = JSON.parse(e.data);
74 74 console.log('got websocket message ' + e.data);
75 75 console.log(data);
... ... @@ -80,7 +80,7 @@
80 80 }
81 81 });
82 82  
83   - $scope.pushCard = function() {
  83 + $scope.pushCard = function () {
84 84 var myCard = {
85 85 // we can't trim this string because it'd mess up the blanks. Something to fix.
86 86 'text': $('#new-card-input').text(),
87 87  
... ... @@ -92,13 +92,13 @@
92 92 return closeNewCard();
93 93 }
94 94 $http.post('/api/flashcards/', myCard).
95   - success(function(data) {
  95 + success(function (data) {
96 96 console.log('flashcard pushed: ' + myCard.text);
97 97 if (!UserService.hasVerifiedEmail()) {
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 }).
101   - error(function(error) {
  101 + error(function (error) {
102 102 console.log('something went wrong pushing a card!');
103 103 });
104 104 return $scope.closeNewCardModal();
105 105  
... ... @@ -115,13 +115,13 @@
115 115 opacity: 0, // Opacity of modal background
116 116 in_duration: 300, // Transition in duration
117 117 out_duration: 200, // Transition out duration
118   - ready: function() {
  118 + ready: function () {
119 119 $('#new-card-input').focus();
120 120 document.execCommand('selectAll', false, null);
121 121 }
122 122 };
123 123  
124   - $(document).keydown(function(e) {
  124 + $(document).keydown(function (e) {
125 125 var keyed = e.which;
126 126 if (keyed == 67 && listenForC) { // "c" for compose
127 127 $scope.openNewCardModal();
128 128  
... ... @@ -132,13 +132,13 @@
132 132 }
133 133 });
134 134  
135   - $scope.openNewCardModal = function() {
  135 + $scope.openNewCardModal = function () {
136 136 $('#newCard').openModal(modal_options);
137 137 listenForC = false;
138 138 $('#new-card-input').html('Write a flashcard!');
139 139 };
140 140  
141   - $scope.closeNewCardModal = function() {
  141 + $scope.closeNewCardModal = function () {
142 142 listenForC = true;
143 143 $('#new-card-input').html('').blur();
144 144 $('#newCard').closeModal(modal_options);
... ... @@ -147,7 +147,7 @@
147 147 $('.tooltipped').tooltip({delay: 50});
148 148 // the "href" attribute of .modal-trigger must specify the modal ID that wants to be triggered
149 149 $('.modal-trigger').leanModal(modal_options);
150   - $('#new-card-input').on('keydown', function(e) {
  150 + $('#new-card-input').on('keydown', function (e) {
151 151 if (e.which == 13) {
152 152 e.preventDefault();
153 153 $scope.pushCard();
154 154  
155 155  
156 156  
... ... @@ -157,20 +157,22 @@
157 157  
158 158 }
159 159 });
160   - $('button#blank-selected').click(function() {
  160 + $('button#blank-selected').click(function () {
161 161 console.log(window.getSelection());
162 162 document.execCommand('bold');
163 163 });
164 164 $scope.refreshCards();
165 165 $scope.newCardBlanks = [];
166   - $scope.refreshNewCardInput = function() {
  166 + $scope.refreshNewCardInput = function () {
167 167 $scope.newCardText = $('#new-card-input').text();
168 168 $scope.submit_enabled = $scope.newCardText.length >= 5 && $scope.newCardText.length <= 160;
169   - $('#new-card-input')[0].childNodes.forEach(function(node) {
  169 + var i = 0;
  170 + $('#new-card-input')[0].childNodes.forEach(function (node) {
170 171 if (typeof node.data == 'undefined') {
171 172 console.log('undefined node');
172 173 }
173 174 node = $(node)[0];
  175 + $scope.newCardBlanks = [];
174 176 if (node.tagName == 'B') {
175 177 var text = $(node).text();
176 178 console.log(text.length, text);
177 179  
178 180  
... ... @@ -187,17 +189,28 @@
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 };
192   - $scope.shuffleCards = function() {
193   - $timeout(function() {
194   - (function(o) {
  205 + $scope.shuffleCards = function () {
  206 + $timeout(function () {
  207 + (function (o) {
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);
196 209 return o;
197 210 })($scope.cardCols[0]);
198 211 });
199 212 };
200   - $scope.$on('$destroy', function() {
  213 + $scope.$on('$destroy', function () {
201 214 $scope.feed_ws.close();
202 215 });
203 216 });