Blame view

scripts/FeedController.js 8.69 KB
4a8b43927   Andrew Buss   start improving c...
1
  angular.module('flashy.FeedController', ['ui.router', 'ngAnimate', 'ngWebSocket', 'contenteditable']).
9c420b35c   Rachel Lee   Separating all cu...
2

cf476b03e   Andrew Buss   properly check wh...
3
      controller('FeedController', function($scope, $rootScope, $state, $http, $window, $timeout, $stateParams, $websocket, UserService) {
ee30a48eb   Andrew Buss   further refactori...
4
          angular.module('flashy.CardGridController').controller.apply(this, arguments);
bbba33088   Andrew Buss   Added addclass st...
5
          console.log('Hello from feed');
fe2c7edc6   Andrew Buss   fix flashcard sca...
6

cf476b03e   Andrew Buss   properly check wh...
7
          $scope.refreshCards = function() {
c3350cb1f   Andrew Buss   refactor common f...
8
              $http.get('/api/sections/' + $scope.sectionId + '/feed/').
cf476b03e   Andrew Buss   properly check wh...
9
                  success(function(data) {
5ce17b918   Tetranoir   feed controller, ...
10
                      console.log(data);
9822fe0ff   Andrew Buss   to properly apply...
11
                      $scope.cards = data;
425c32c56   Tetranoir   columns basically...
12
                      $scope.refreshLayout();
5ce17b918   Tetranoir   feed controller, ...
13
14
                      console.log('success in refresh cards...');
                  }).
cf476b03e   Andrew Buss   properly check wh...
15
                  error(function(err) {
5ce17b918   Tetranoir   feed controller, ...
16
                      console.log('refresh fail');
107013870   Andrew Buss   ui tweaks, check ...
17
                      console.log(err);
5ce17b918   Tetranoir   feed controller, ...
18
19
                  });
          };
fe2c7edc6   Andrew Buss   fix flashcard sca...
20

cf476b03e   Andrew Buss   properly check wh...
21
          $scope.sortAdd = function(card, array) {
7cf67e561   Andrew Buss   merge proper
22
              console.log('sort add');
cf476b03e   Andrew Buss   properly check wh...
23
              array.forEach(function(ele, i, ary) {
7cf67e561   Andrew Buss   merge proper
24
25
26
27
28
29
                  if (ele.score <= card.score) {
                      ary.splice(i, 0, card);
                      return;
                  }
              });
          };
fe2c7edc6   Andrew Buss   fix flashcard sca...
30

cf476b03e   Andrew Buss   properly check wh...
31
          $scope.hide = function(card) {
fe2c7edc6   Andrew Buss   fix flashcard sca...
32
              console.log('hiding card');
fe2c7edc6   Andrew Buss   fix flashcard sca...
33
              var found = -1;
7cf67e561   Andrew Buss   merge proper
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   Andrew Buss   fix flashcard sca...
41
42
              console.log('Error finding card to hide:');
              console.log(card);
7cf67e561   Andrew Buss   merge proper
43
44
              return -1;
          };
0c65d0c07   Andrew Buss   try to fix behavi...
45

cf476b03e   Andrew Buss   properly check wh...
46
          $scope.update = function(id, new_score) {
c3350cb1f   Andrew Buss   refactor common f...
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   Andrew Buss   fix flashcard sca...
66

ee30a48eb   Andrew Buss   further refactori...
67
          $scope.feed_ws = $websocket($scope.ws_host + '/ws/feed/' + $scope.sectionId + '?subscribe-broadcast');
cf476b03e   Andrew Buss   properly check wh...
68
          $scope.feed_ws.onOpen(function() {
ee30a48eb   Andrew Buss   further refactori...
69
70
              console.log('deck ws open');
          });
235b8e0ec   Andrew Buss   realtime is hard,...
71

cf476b03e   Andrew Buss   properly check wh...
72
          $scope.feed_ws.onMessage(function(e) {
7cf67e561   Andrew Buss   merge proper
73
              data = JSON.parse(e.data);
235b8e0ec   Andrew Buss   realtime is hard,...
74
              console.log('got websocket message ' + e.data);
7cf67e561   Andrew Buss   merge proper
75
              console.log(data);
f71cb843b   Andrew Buss   tweaks; email ver...
76
              if (data.event_type == 'new_card') {
425c32c56   Tetranoir   columns basically...
77
                  $scope.add(data.flashcard);
517adf2f1   Tetranoir   listening for sco...
78
              } else if (data.event_type == 'score_change') {
7cf67e561   Andrew Buss   merge proper
79
80
                  $scope.update(data.flashcard_id, data.new_score);
              }
ee30a48eb   Andrew Buss   further refactori...
81
          });
107013870   Andrew Buss   ui tweaks, check ...
82

cf476b03e   Andrew Buss   properly check wh...
83
          $scope.pushCard = function() {
1167ab44a   Rachel Lee   Feed submit is go...
84
              var myCard = {
a7ee2b64a   Andrew Buss   new flashcard cre...
85
86
                  // we can't trim this string because it'd mess up the blanks. Something to fix.
                  'text': $('#new-card-input').text(),
88ca25463   Andrew Buss   fixed broken auth...
87
                  'mask': $scope.newCardBlanks,
63d40a399   Andrew Buss   unbreak card push
88
                  section: $scope.section.id
2f199068c   Andrew Buss   focus input on ca...
89
              };
107013870   Andrew Buss   ui tweaks, check ...
90
91
              if (myCard.text == '') {
                  console.log('blank flashcard not pushed:' + myCard.text);
4a8b43927   Andrew Buss   start improving c...
92
                  return closeNewCard();
107013870   Andrew Buss   ui tweaks, check ...
93
              }
1167ab44a   Rachel Lee   Feed submit is go...
94
              $http.post('/api/flashcards/', myCard).
cf476b03e   Andrew Buss   properly check wh...
95
                  success(function(data) {
065f0cdac   Tetranoir   node.data error a...
96
                      console.log('flashcard pushed: ' + myCard.text);
f71cb843b   Andrew Buss   tweaks; email ver...
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   Andrew Buss   more on userservi...
100
                  }).
cf476b03e   Andrew Buss   properly check wh...
101
                  error(function(error) {
807399a02   Andrew Buss   push dialog hopef...
102
                      console.log('something went wrong pushing a card!');
8fa323ef6   Andrew Buss   more on userservi...
103
                  });
4a8b43927   Andrew Buss   start improving c...
104
              return $scope.closeNewCardModal();
32b3331d8   Melody   Fixed fancy butto...
105
          };
2ad496102   Rachel Lee   Tweaking hotkey a...
106
          /* Key bindings for the whole feed window. Hotkey it up! */
b83f46393   Andrew Buss   only listen for c...
107
          var listenForC = true;
807399a02   Andrew Buss   push dialog hopef...
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   Andrew Buss   properly check wh...
117
              ready: function() {
807399a02   Andrew Buss   push dialog hopef...
118
                  $('#new-card-input').focus();
4a8b43927   Andrew Buss   start improving c...
119
                  document.execCommand('selectAll', false, null);
807399a02   Andrew Buss   push dialog hopef...
120
121
              }
          };
cf476b03e   Andrew Buss   properly check wh...
122
          $(document).keydown(function(e) {
456616544   Rachel Lee   Hotkeyed compose ...
123
              var keyed = e.which;
c6ac1ffcf   Andrew Buss   refactor; $timeou...
124
              if (keyed == 67 && listenForC) { // "c" for compose
4a8b43927   Andrew Buss   start improving c...
125
                  $scope.openNewCardModal();
807399a02   Andrew Buss   push dialog hopef...
126
                  e.preventDefault();
b83f46393   Andrew Buss   only listen for c...
127
                  return false;
c6ac1ffcf   Andrew Buss   refactor; $timeou...
128
              } else if (keyed == 27) { // clear on ESC
4a8b43927   Andrew Buss   start improving c...
129
                  $scope.closeNewCardModal();
456616544   Rachel Lee   Hotkeyed compose ...
130
131
              }
          });
4a8b43927   Andrew Buss   start improving c...
132

cf476b03e   Andrew Buss   properly check wh...
133
          $scope.openNewCardModal = function() {
4a8b43927   Andrew Buss   start improving c...
134
135
136
              $('#newCard').openModal(modal_options);
              listenForC = false;
              $('#new-card-input').html('Write a flashcard!');
4a8b43927   Andrew Buss   start improving c...
137
          };
cf476b03e   Andrew Buss   properly check wh...
138
          $scope.closeNewCardModal = function() {
4a8b43927   Andrew Buss   start improving c...
139
140
141
142
              listenForC = true;
              $('#new-card-input').html('').blur();
              $('#newCard').closeModal(modal_options);
          };
a7ee2b64a   Andrew Buss   new flashcard cre...
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   Andrew Buss   properly check wh...
146
          $('#new-card-input').on('keydown', function(e) {
a7ee2b64a   Andrew Buss   new flashcard cre...
147
148
149
150
151
              if (e.which == 13) {
                  e.preventDefault();
                  $scope.pushCard();
                  listenForC = true;
                  return false;
88ca25463   Andrew Buss   fixed broken auth...
152
              } else {
a7ee2b64a   Andrew Buss   new flashcard cre...
153
154
              }
          });
cf476b03e   Andrew Buss   properly check wh...
155
          $('button#blank-selected').click(function() {
a7ee2b64a   Andrew Buss   new flashcard cre...
156
157
              console.log(window.getSelection());
              document.execCommand('bold');
34a342819   Melody   Nicer new flashca...
158
          });
c6ac1ffcf   Andrew Buss   refactor; $timeou...
159
          $scope.refreshCards();
88ca25463   Andrew Buss   fixed broken auth...
160
          $scope.newCardBlanks = [];
cf476b03e   Andrew Buss   properly check wh...
161
          $scope.refreshNewCardInput = function() {
88ca25463   Andrew Buss   fixed broken auth...
162
163
              $scope.newCardText = $('#new-card-input').text();
              $scope.submit_enabled = $scope.newCardText.length >= 5 && $scope.newCardText.length <= 160;
7a53b3dae   Andrew Buss   fix blanks as you...
164
              var i = 0;
63f4d4ca5   Andrew Buss   unbreak blanks
165
              $scope.newCardBlanks = [];
cf476b03e   Andrew Buss   properly check wh...
166
              $('#new-card-input')[0].childNodes.forEach(function(node) {
88ca25463   Andrew Buss   fixed broken auth...
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   Andrew Buss   fixed broken auth...
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   Andrew Buss   unbreak blanks
177
                      console.log(leftspaces, text.length);
88ca25463   Andrew Buss   fixed broken auth...
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   Andrew Buss   properly check wh...
187
              $scope.newCardBlanks.sort(function(a, b) {
7a53b3dae   Andrew Buss   fix blanks as you...
188
189
                  return a[0] - b[0];
              });
63f4d4ca5   Andrew Buss   unbreak blanks
190
              i = 0;
7a53b3dae   Andrew Buss   fix blanks as you...
191
              newtext = '';
63f4d4ca5   Andrew Buss   unbreak blanks
192
              console.log($scope.newCardBlanks);
cf476b03e   Andrew Buss   properly check wh...
193
              $scope.newCardBlanks.forEach(function(blank) {
7a53b3dae   Andrew Buss   fix blanks as you...
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   Andrew Buss   fixed broken auth...
200
          };
cf476b03e   Andrew Buss   properly check wh...
201
202
203
          $scope.shuffleCards = function() {
              $timeout(function() {
                  (function(o) {
684d730ca   Andrew Buss   refactor; trying ...
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   Andrew Buss   properly check wh...
209
          $scope.$on('$destroy', function() {
ee30a48eb   Andrew Buss   further refactori...
210
211
              $scope.feed_ws.close();
          });
f0541be90   Andrew Buss   navbar icons!
212
      });