Blame view

scripts/FeedController.js 7.5 KB
70c2390ab   Andrew Buss   unbreak deck, car...
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   Andrew Buss   make cardgridcont...
8
          angular.module('flashy.CardGridController').CardGridController.apply(this, arguments);
80b664e09   Andrew Buss   switched back to ...
9

70c2390ab   Andrew Buss   unbreak deck, car...
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   Andrew Buss   switched back to ...
18

180aa08bb   Andrew Buss   hide now working
19
          $scope.updateCardScore = function(card) {
5673511eb   Andrew Buss   more feed refacto...
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   Andrew Buss   more refactoring;...
23
              $scope.cardCols[card.colNum].sort(function(a, b) {
bf0941f00   Andrew Buss   continue refactor...
24
25
                  return b.score - a.score;
              });
0adf8f867   Andrew Buss   nice hide animations
26
              $scope.updateColRanks($scope.cardCols[card.colNum]);
c3350cb1f   Andrew Buss   refactor common f...
27
          };
fe2c7edc6   Andrew Buss   fix flashcard sca...
28

dc454dbeb   Andrew Buss   card list vaguely...
29
          $scope.feed_ws = $websocket($scope.ws_host + '/ws/feed/' + sectionId + '?subscribe-broadcast');
cf476b03e   Andrew Buss   properly check wh...
30
          $scope.feed_ws.onMessage(function(e) {
7cf67e561   Andrew Buss   merge proper
31
              data = JSON.parse(e.data);
bf0941f00   Andrew Buss   continue refactor...
32
              console.log('message', data);
f71cb843b   Andrew Buss   tweaks; email ver...
33
              if (data.event_type == 'new_card') {
5673511eb   Andrew Buss   more feed refacto...
34
                  $scope.addCardToGrid(new Flashcard(data.flashcard, $scope.deck));
517adf2f1   Tetranoir   listening for sco...
35
              } else if (data.event_type == 'score_change') {
5673511eb   Andrew Buss   more feed refacto...
36
                  card = new Flashcard(data.flashcard);
0dfc72586   Andrew Buss   more refactoring;...
37
38
                  card.score = data.flashcard.score;
                  $scope.updateCardScore(card);
7cf67e561   Andrew Buss   merge proper
39
              }
ee30a48eb   Andrew Buss   further refactori...
40
          });
107013870   Andrew Buss   ui tweaks, check ...
41

cf476b03e   Andrew Buss   properly check wh...
42
          $scope.pushCard = function() {
1167ab44a   Rachel Lee   Feed submit is go...
43
              var myCard = {
a7ee2b64a   Andrew Buss   new flashcard cre...
44
45
                  // 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...
46
                  'mask': $scope.newCardBlanks,
63d40a399   Andrew Buss   unbreak card push
47
                  section: $scope.section.id
2f199068c   Andrew Buss   focus input on ca...
48
              };
107013870   Andrew Buss   ui tweaks, check ...
49
50
              if (myCard.text == '') {
                  console.log('blank flashcard not pushed:' + myCard.text);
4a8b43927   Andrew Buss   start improving c...
51
                  return closeNewCard();
107013870   Andrew Buss   ui tweaks, check ...
52
              }
1167ab44a   Rachel Lee   Feed submit is go...
53
              $http.post('/api/flashcards/', myCard).
cf476b03e   Andrew Buss   properly check wh...
54
                  success(function(data) {
065f0cdac   Tetranoir   node.data error a...
55
                      console.log('flashcard pushed: ' + myCard.text);
f71cb843b   Andrew Buss   tweaks; email ver...
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   Andrew Buss   more on userservi...
59
                  });
4a8b43927   Andrew Buss   start improving c...
60
              return $scope.closeNewCardModal();
32b3331d8   Melody   Fixed fancy butto...
61
          };
2ad496102   Rachel Lee   Tweaking hotkey a...
62
          /* Key bindings for the whole feed window. Hotkey it up! */
b83f46393   Andrew Buss   only listen for c...
63
          var listenForC = true;
807399a02   Andrew Buss   push dialog hopef...
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   Andrew Buss   properly check wh...
73
              ready: function() {
807399a02   Andrew Buss   push dialog hopef...
74
                  $('#new-card-input').focus();
4a8b43927   Andrew Buss   start improving c...
75
                  document.execCommand('selectAll', false, null);
807399a02   Andrew Buss   push dialog hopef...
76
77
              }
          };
cf476b03e   Andrew Buss   properly check wh...
78
          $(document).keydown(function(e) {
456616544   Rachel Lee   Hotkeyed compose ...
79
              var keyed = e.which;
c6ac1ffcf   Andrew Buss   refactor; $timeou...
80
              if (keyed == 67 && listenForC) { // "c" for compose
4a8b43927   Andrew Buss   start improving c...
81
                  $scope.openNewCardModal();
807399a02   Andrew Buss   push dialog hopef...
82
                  e.preventDefault();
b83f46393   Andrew Buss   only listen for c...
83
                  return false;
c6ac1ffcf   Andrew Buss   refactor; $timeou...
84
              } else if (keyed == 27) { // clear on ESC
4a8b43927   Andrew Buss   start improving c...
85
                  $scope.closeNewCardModal();
456616544   Rachel Lee   Hotkeyed compose ...
86
87
              }
          });
4a8b43927   Andrew Buss   start improving c...
88

cf476b03e   Andrew Buss   properly check wh...
89
          $scope.openNewCardModal = function() {
4a8b43927   Andrew Buss   start improving c...
90
91
92
              $('#newCard').openModal(modal_options);
              listenForC = false;
              $('#new-card-input').html('Write a flashcard!');
4a8b43927   Andrew Buss   start improving c...
93
          };
cf476b03e   Andrew Buss   properly check wh...
94
          $scope.closeNewCardModal = function() {
4a8b43927   Andrew Buss   start improving c...
95
96
97
98
              listenForC = true;
              $('#new-card-input').html('').blur();
              $('#newCard').closeModal(modal_options);
          };
a7ee2b64a   Andrew Buss   new flashcard cre...
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   Andrew Buss   properly check wh...
102
          $('#new-card-input').on('keydown', function(e) {
a7ee2b64a   Andrew Buss   new flashcard cre...
103
104
              if (e.which == 13) {
                  e.preventDefault();
edf69c107   Andrew Buss   actually disable ...
105
106
107
108
                  if ($scope.submit_enabled) {
                      $scope.pushCard();
                      listenForC = true;
                  }
a7ee2b64a   Andrew Buss   new flashcard cre...
109
                  return false;
88ca25463   Andrew Buss   fixed broken auth...
110
              } else {
a7ee2b64a   Andrew Buss   new flashcard cre...
111
112
              }
          });
cf476b03e   Andrew Buss   properly check wh...
113
          $('button#blank-selected').click(function() {
a7ee2b64a   Andrew Buss   new flashcard cre...
114
115
              console.log(window.getSelection());
              document.execCommand('bold');
34a342819   Melody   Nicer new flashca...
116
          });
88ca25463   Andrew Buss   fixed broken auth...
117
          $scope.newCardBlanks = [];
cf476b03e   Andrew Buss   properly check wh...
118
          $scope.refreshNewCardInput = function() {
88ca25463   Andrew Buss   fixed broken auth...
119
120
              $scope.newCardText = $('#new-card-input').text();
              $scope.submit_enabled = $scope.newCardText.length >= 5 && $scope.newCardText.length <= 160;
7a53b3dae   Andrew Buss   fix blanks as you...
121
              var i = 0;
63f4d4ca5   Andrew Buss   unbreak blanks
122
              $scope.newCardBlanks = [];
cf476b03e   Andrew Buss   properly check wh...
123
              $('#new-card-input')[0].childNodes.forEach(function(node) {
88ca25463   Andrew Buss   fixed broken auth...
124
125
126
                  node = $(node)[0];
                  if (node.tagName == 'B') {
                      var text = $(node).text();
88ca25463   Andrew Buss   fixed broken auth...
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   Andrew Buss   unbreak blanks
131
                      console.log(leftspaces, text.length);
88ca25463   Andrew Buss   fixed broken auth...
132
133
134
                      if (leftspaces != text.length) $scope.newCardBlanks.push([i + leftspaces, i + text.length - rightspaces]);
                      i += text.length;
                  } else if (!node.data) {
88ca25463   Andrew Buss   fixed broken auth...
135
136
137
138
139
                      i += $(node).text().length;
                  } else {
                      i += node.data.length;
                  }
              });
cf476b03e   Andrew Buss   properly check wh...
140
              $scope.newCardBlanks.sort(function(a, b) {
7a53b3dae   Andrew Buss   fix blanks as you...
141
142
                  return a[0] - b[0];
              });
63f4d4ca5   Andrew Buss   unbreak blanks
143
              i = 0;
7a53b3dae   Andrew Buss   fix blanks as you...
144
              newtext = '';
cf476b03e   Andrew Buss   properly check wh...
145
              $scope.newCardBlanks.forEach(function(blank) {
7a53b3dae   Andrew Buss   fix blanks as you...
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   Andrew Buss   fixed broken auth...
152
          };
cf476b03e   Andrew Buss   properly check wh...
153
154
155
          $scope.shuffleCards = function() {
              $timeout(function() {
                  (function(o) {
684d730ca   Andrew Buss   refactor; trying ...
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   Andrew Buss   properly check wh...
161
          $scope.$on('$destroy', function() {
ee30a48eb   Andrew Buss   further refactori...
162
163
              $scope.feed_ws.close();
          });
dc454dbeb   Andrew Buss   card list vaguely...
164
          return $http.get('/api/sections/' + sectionId + '/feed/').
20ed06e1b   Andrew Buss   make cardgridcont...
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   Andrew Buss   navbar icons!
177
      });