Commit 5a0a5435a44903d1461b61c20825fcb47ed0604c

Authored by Rohan Rangray

Merge branch 'master' of git.ucsd.edu:110swag/flashy-frontend

merigin

Showing 2 changed files Inline Diff

scripts/CardGridController.js View file @ 5a0a543
angular.module('flashy.CardGridController', ['ui.router', 'ngAnimate', 'ngWebSocket']).CardGridController = 1 1 angular.module('flashy.CardGridController', ['ui.router', 'ngAnimate', 'ngWebSocket']).CardGridController =
function($scope, $rootScope, $state, $http, $window, $timeout, $stateParams, $websocket, UserService, Flashcard) { 2 2 function($scope, $rootScope, $state, $http, $window, $timeout, $stateParams, $websocket, UserService, Flashcard) {
$scope.cards = []; 3 3 $scope.cards = [];
$scope.deck = []; 4 4 $scope.deck = [];
$scope.cardCols = []; // organized data 5 5 $scope.cardCols = []; // organized data
$scope.numCols = 0; 6 6 $scope.numCols = 0;
$scope.cardTable = {}; // look up table of cards, {'colNum':col, 'obj':card} 7 7 $scope.cardTable = {}; // look up table of cards, {'colNum':col, 'obj':card}
$scope.sectionId = parseInt($stateParams.sectionId); 8 8 $scope.sectionId = parseInt($stateParams.sectionId);
$scope.section = $rootScope.SectionResource.get({sectionId: $scope.sectionId}); 9 9 $scope.section = $rootScope.SectionResource.get({sectionId: $scope.sectionId});
$scope.showGrid = false; 10 10 $scope.showGrid = false;
$scope.moveQueue = []; 11 11 $scope.moveQueue = [];
$rootScope.currentSection = $scope.section; 12 12 $rootScope.currentSection = $scope.section;
13 13
if (!UserService.isInSection($scope.sectionId)) { 14 14 if (!UserService.isInSection($scope.sectionId)) {
console.log('user is not enrolled in ' + $scope.sectionId); 15 15 console.log('user is not enrolled in ' + $scope.sectionId);
$state.go('addclass'); 16 16 $state.go('addclass');
} 17 17 }
18 18
$scope.refreshColumnWidth = function() { 19 19 $scope.refreshColumnWidth = function() {
avail = $window.innerWidth - 17; 20 20 avail = $window.innerWidth - 17;
width = Math.floor(avail / Math.floor(avail / 250)); 21 21 width = Math.floor(avail / Math.floor(avail / 250));
$('.cardColumn').css({ 22 22 $('.cardColumn').css({
width: width + 'px', 23 23 width: width + 'px',
'font-size': 100 * width / 250 + '%' 24 24 'font-size': 100 * width / 250 + '%'
}); 25 25 });
$('.cardColumn .card.flashy').css({ 26 26 $('.cardColumn .card.flashy').css({
width: width - 12 + 'px', 27 27 width: width - 12 + 'px',
height: (width * 3 / 5) + 'px' 28 28 height: (width * 3 / 5) + 'px'
}); 29 29 });
}; 30 30 };
31
$scope.refreshLayout = function() { 32 31 $scope.refreshLayout = function() {
numCols = Math.max(1, Math.floor(($window.innerWidth - 17) / 250)); 33 32 numCols = Math.max(1, Math.floor(($window.innerWidth - 17) / 250));
34 33
// check if we actually need to refresh the whole layout 35 34 // check if we actually need to refresh the whole layout
if (numCols == $scope.numCols) return $scope.refreshColumnWidth(); 36 35 if (numCols == $scope.numCols) return $scope.refreshColumnWidth();
$scope.numCols = numCols; 37 36 $scope.numCols = numCols;
console.log('refreshing layout for ' + $scope.numCols + ' columns'); 38 37 console.log('refreshing layout for ' + numCols + ' columns');
$scope.cardCols = []; 39 38 $scope.cardCols = [];
var cols = []; 40 39 var cols = [];
for (i = 0; i < $scope.numCols; i++) cols.push([]); 41 40 for (var i = 0; i < numCols; i++) cols.push([]);
$scope.cards.forEach(function(card, i) { 42 41 var n = 0;
card.colNum = i % $scope.numCols; 43 42 $scope.cards.forEach(function(card, j) {
43 card.colNum = n++ % numCols;
cols[card.colNum].push(card); 44 44 cols[card.colNum].push(card);
}); 45 45 });
for (i in cols) $scope.updateColRanks(cols[i]); 46 46 for (i in cols) $scope.updateColRanks(cols[i]);
console.log(cols); 47 47 console.log(cols);
return $timeout(function() { 48 48 return $timeout(function() {
$scope.cardCols = cols; 49 49 $scope.cardCols = cols;
$timeout($scope.refreshColumnWidth); 50 50 $timeout($scope.refreshColumnWidth);
}); 51 51 });
52 52
}; 53 53 };
54 54
angular.element($window).bind('resize', $scope.refreshLayout); 55 55 angular.element($window).bind('resize', $scope.refreshLayout);
56 56
$scope.ws_host = window.location.origin.replace('http', 'ws'); 57 57 $scope.ws_host = window.location.origin.replace('http', 'ws');
$scope.deck_ws = $websocket($scope.ws_host + '/ws/deck/' + $scope.sectionId + '?subscribe-user'); 58 58 $scope.deck_ws = $websocket($scope.ws_host + '/ws/deck/' + $scope.sectionId + '?subscribe-user');
$scope.deck_ws.onOpen(function() { 59 59 $scope.deck_ws.onOpen(function() {
console.log('deck ws open'); 60 60 console.log('deck ws open');
}); 61 61 });
62 62
$scope.deck_ws.onMessage(function(message) { 63 63 $scope.deck_ws.onMessage(function(message) {
data = JSON.parse(message.data); 64 64 data = JSON.parse(message.data);
console.log('message', data); 65 65 console.log('message', data);
card = new Flashcard(data.flashcard.id); 66 66 card = new Flashcard(data.flashcard.id);
if (data.event_type == 'card_pulled') { 67 67 if (data.event_type == 'card_pulled') {
$scope.deck[card.id] = card; 68 68 $scope.deck[card.id] = card;
} 69 69 }
if (data.event_type == 'card_unpulled') { 70 70 if (data.event_type == 'card_unpulled') {
$scope.deck[card.id] = undefined; 71 71 $scope.deck[card.id] = undefined;
} 72 72 }
if (data.event_type == 'card_hidden') { 73 73 if (data.event_type == 'card_hidden') {
$scope.hideCardFromGrid(card); 74 74 $scope.hideCardFromGrid(card);
} 75 75 }
}); 76 76 });
77 77
$scope.cardInDeck = function(id) { 78 78 $scope.cardInDeck = function(id) {
return $scope.deck[id]; 79 79 return $scope.deck[id];
}; 80 80 };
$scope.addCardToGrid = function(card) { 81 81 $scope.addCardToGrid = function(card) {
var colNum = 0; 82 82 var colNum = 0;
var lowestCol = $scope.cardCols[0]; 83 83 var lowestCol = $scope.cardCols[0];
var lowestColNum = 0; 84 84 var lowestColNum = 0;
while (colNum < $scope.numCols) { 85 85 while (colNum < $scope.numCols) {
if ($scope.cardCols[colNum].length == 0) { 86 86 if ($scope.cardCols[colNum].length == 0) {
lowestCol = $scope.cardCols[colNum]; 87 87 lowestCol = $scope.cardCols[colNum];
break; 88 88 break;
} else if ($scope.cardCols[colNum].length < lowestCol.length) { 89 89 } else if ($scope.cardCols[colNum].length < lowestCol.length) {
lowestCol = $scope.cardCols[colNum]; 90 90 lowestCol = $scope.cardCols[colNum];
lowestColNum = colNum; 91 91 lowestColNum = colNum;
lowestColLen = $scope.cardCols[colNum].length; 92 92 lowestColLen = $scope.cardCols[colNum].length;
} 93 93 }
colNum++; 94 94 colNum++;
} 95 95 }
console.log(card); 96 96 console.log(card);
$scope.cards.push(data); 97 97 $scope.cards.push(data);
lowestCol.unshift(card); 98 98 lowestCol.unshift(card);
card.colNum = lowestColNum; 99 99 card.colNum = lowestColNum;
$scope.updateColRanks(lowestCol); 100 100 $scope.updateColRanks(lowestCol);
$timeout($scope.refreshColumnWidth); 101 101 $timeout($scope.refreshColumnWidth);
102 102
}; 103 103 };
104 104
$scope.updateColRanks = function(col) { 105 105 $scope.updateColRanks = function(col) {
for (i in col) 106 106 for (i in col)
col[i].colRank = parseInt(i); 107 107 col[i].colRank = parseInt(i);
}; 108 108 };
109 109
$scope.hideCardFromGrid = function(card) { 110 110 $scope.hideCardFromGrid = function(card) {
console.log('hiding', card); 111 111 console.log('hiding', card);
$scope.cardCols[card.colNum].splice(card.colRank, 1); 112 112 $scope.cardCols[card.colNum].splice(card.colRank, 1);
$scope.updateColRanks($scope.cardCols[card.colNum]); 113 113 $scope.updateColRanks($scope.cardCols[card.colNum]);
console.log($scope.cardCols); 114 114 console.log($scope.cardCols);
}; 115 115 };
116 116
$scope.$on('$destroy', function() { 117 117 $scope.$on('$destroy', function() {
$scope.deck_ws.close(); 118 118 $scope.deck_ws.close();
scripts/FeedController.js View file @ 5a0a543
angular.module('flashy.FeedController', ['ui.router', 'ngAnimate', 'ngWebSocket', 'contenteditable']).controller('FeedController', 1 1 angular.module('flashy.FeedController', ['ui.router', 'ngAnimate', 'ngWebSocket', 'contenteditable']).controller('FeedController',
function($scope, $rootScope, $state, $http, $window, $timeout, $stateParams, $websocket, UserService, Flashcard) { 2 2 function($scope, $rootScope, $state, $http, $window, $timeout, $stateParams, $websocket, UserService, Flashcard) {
angular.module('flashy.CardGridController').CardGridController.apply(this, arguments); 3 3 angular.module('flashy.CardGridController').CardGridController.apply(this, arguments);
4 4
$scope.sortAdd = function(card, array) { 5 5 $scope.sortAdd = function(card, array) {
console.log('sort add'); 6 6 console.log('sort add');
array.forEach(function(ele, i, ary) { 7 7 array.forEach(function(ele, i, ary) {
if (ele.score <= card.score) { 8 8 if (ele.score <= card.score) {
ary.splice(i, 0, card); 9 9 ary.splice(i, 0, card);
return; 10 10 return;
} 11 11 }
}); 12 12 });
}; 13 13 };
14 14
$scope.updateCardScore = function(card) { 15 15 $scope.updateCardScore = function(card) {
console.log($scope.cardCols, card); 16 16 console.log($scope.cardCols, card);
// if no colNum is attached, then this doesn't exist on the feed yet 17 17 // if no colNum is attached, then this doesn't exist on the feed yet
if (!card.colNum) return; 18 18 if (!card.colNum) return;
$scope.cardCols[card.colNum].sort(function(a, b) { 19 19 $scope.cardCols[card.colNum].sort(function(a, b) {
return b.score - a.score; 20 20 return b.score - a.score;
}); 21 21 });
$scope.updateColRanks($scope.cardCols[card.colNum]); 22 22 $scope.updateColRanks($scope.cardCols[card.colNum]);
}; 23 23 };
24 24
$scope.feed_ws = $websocket($scope.ws_host + '/ws/feed/' + $scope.sectionId + '?subscribe-broadcast'); 25 25 $scope.feed_ws = $websocket($scope.ws_host + '/ws/feed/' + $scope.sectionId + '?subscribe-broadcast');
$scope.feed_ws.onMessage(function(e) { 26 26 $scope.feed_ws.onMessage(function(e) {
data = JSON.parse(e.data); 27 27 data = JSON.parse(e.data);
console.log('message', data); 28 28 console.log('message', data);
if (data.event_type == 'new_card') { 29 29 if (data.event_type == 'new_card') {
$scope.addCardToGrid(new Flashcard(data.flashcard, $scope.deck)); 30 30 $scope.addCardToGrid(new Flashcard(data.flashcard, $scope.deck));
} else if (data.event_type == 'score_change') { 31 31 } else if (data.event_type == 'score_change') {
card = new Flashcard(data.flashcard); 32 32 card = new Flashcard(data.flashcard);
card.score = data.flashcard.score; 33 33 card.score = data.flashcard.score;
$scope.updateCardScore(card); 34 34 $scope.updateCardScore(card);
} 35 35 }
}); 36 36 });
37 37
$scope.pushCard = function() { 38 38 $scope.pushCard = function() {
var myCard = { 39 39 var myCard = {
// we can't trim this string because it'd mess up the blanks. Something to fix. 40 40 // we can't trim this string because it'd mess up the blanks. Something to fix.
'text': $('#new-card-input').text(), 41 41 'text': $('#new-card-input').text(),
'mask': $scope.newCardBlanks, 42 42 'mask': $scope.newCardBlanks,
section: $scope.section.id 43 43 section: $scope.section.id
}; 44 44 };
if (myCard.text == '') { 45 45 if (myCard.text == '') {
console.log('blank flashcard not pushed:' + myCard.text); 46 46 console.log('blank flashcard not pushed:' + myCard.text);
return closeNewCard(); 47 47 return closeNewCard();
} 48 48 }
$http.post('/api/flashcards/', myCard). 49 49 $http.post('/api/flashcards/', myCard).
success(function(data) { 50 50 success(function(data) {
console.log('flashcard pushed: ' + myCard.text); 51 51 console.log('flashcard pushed: ' + myCard.text);
if (!UserService.hasVerifiedEmail()) { 52 52 if (!UserService.hasVerifiedEmail()) {
Materialize.toast("<p>Thanks for contributing! However, others won't see your card until you verify your email address<p>", 4000); 53 53 Materialize.toast("<p>Thanks for contributing! However, others won't see your card until you verify your email address<p>", 4000);
} 54 54 }
}); 55 55 });
return $scope.closeNewCardModal(); 56 56 return $scope.closeNewCardModal();
}; 57 57 };
58 58
/* Key bindings for the whole feed window. Hotkey it up! */ 59 59 /* Key bindings for the whole feed window. Hotkey it up! */
var listenForC = true; 60 60 var listenForC = true;
61 61
// Need to pass these options into openmodal and leanmodal, 62 62 // Need to pass these options into openmodal and leanmodal,
// otherwise the ready handler doesn't get called 63 63 // otherwise the ready handler doesn't get called
64 64
modal_options = { 65 65 modal_options = {
dismissible: true, // Modal can be dismissed by clicking outside of the modal 66 66 dismissible: true, // Modal can be dismissed by clicking outside of the modal
opacity: 0, // Opacity of modal background 67 67 opacity: 0, // Opacity of modal background
in_duration: 300, // Transition in duration 68 68 in_duration: 300, // Transition in duration
out_duration: 200, // Transition out duration 69 69 out_duration: 200, // Transition out duration
ready: function() { 70 70 ready: function() {
$('#new-card-input').focus(); 71 71 $('#new-card-input').focus();
document.execCommand('selectAll', false, null); 72 72 document.execCommand('selectAll', false, null);
} 73 73 }
}; 74 74 };
75 75
$(document).keydown(function(e) { 76 76 $(document).keydown(function(e) {
var keyed = e.which; 77 77 var keyed = e.which;
if (keyed == 67 && listenForC) { // "c" for compose 78 78 if (keyed == 67 && listenForC) { // "c" for compose
$scope.openNewCardModal(); 79 79 $scope.openNewCardModal();
e.preventDefault(); 80 80 e.preventDefault();
return false; 81 81 return false;
} else if (keyed == 27) { // clear on ESC 82 82 } else if (keyed == 27) { // clear on ESC
$scope.closeNewCardModal(); 83 83 $scope.closeNewCardModal();
} 84 84 }
}); 85 85 });
86 86
$scope.openNewCardModal = function() { 87 87 $scope.openNewCardModal = function() {
$('#newCard').openModal(modal_options); 88 88 $('#newCard').openModal(modal_options);
listenForC = false; 89 89 listenForC = false;
$('#new-card-input').html('Write a flashcard!'); 90 90 $('#new-card-input').html('Write a flashcard!');
}; 91 91 };
92 92
$scope.closeNewCardModal = function() { 93 93 $scope.closeNewCardModal = function() {
listenForC = true; 94 94 listenForC = true;
$('#new-card-input').html('').blur(); 95 95 $('#new-card-input').html('').blur();
$('#newCard').closeModal(modal_options); 96 96 $('#newCard').closeModal(modal_options);
}; 97 97 };
98 98
$('.tooltipped').tooltip({delay: 50}); 99 99 $('.tooltipped').tooltip({delay: 50});
// the "href" attribute of .modal-trigger must specify the modal ID that wants to be triggered 100 100 // the "href" attribute of .modal-trigger must specify the modal ID that wants to be triggered
$('.modal-trigger').leanModal(modal_options); 101 101 $('.modal-trigger').leanModal(modal_options);
$('#new-card-input').on('keydown', function(e) { 102 102 $('#new-card-input').on('keydown', function(e) {
if (e.which == 13) { 103 103 if (e.which == 13) {
e.preventDefault(); 104 104 e.preventDefault();
if ($scope.submit_enabled) { 105 105 if ($scope.submit_enabled) {
$scope.pushCard(); 106 106 $scope.pushCard();
listenForC = true; 107 107 listenForC = true;
} 108 108 }
return false; 109 109 return false;
} else { 110 110 } else {
111 111
} 112 112 }
}); 113 113 });
$('button#blank-selected').click(function() { 114 114 $('button#blank-selected').click(function() {
console.log(window.getSelection()); 115 115 console.log(window.getSelection());
document.execCommand('bold'); 116 116 document.execCommand('bold');
}); 117 117 });
$scope.refreshCards(); 118
$scope.newCardBlanks = []; 119 118 $scope.newCardBlanks = [];
$scope.refreshNewCardInput = function() { 120 119 $scope.refreshNewCardInput = function() {
$scope.newCardText = $('#new-card-input').text(); 121 120 $scope.newCardText = $('#new-card-input').text();
$scope.submit_enabled = $scope.newCardText.length >= 5 && $scope.newCardText.length <= 160; 122 121 $scope.submit_enabled = $scope.newCardText.length >= 5 && $scope.newCardText.length <= 160;
var i = 0; 123 122 var i = 0;
$scope.newCardBlanks = []; 124 123 $scope.newCardBlanks = [];
$('#new-card-input')[0].childNodes.forEach(function(node) { 125 124 $('#new-card-input')[0].childNodes.forEach(function(node) {
node = $(node)[0]; 126 125 node = $(node)[0];
if (node.tagName == 'B') { 127 126 if (node.tagName == 'B') {
var text = $(node).text(); 128 127 var text = $(node).text();
var leftspaces = 0, rightspaces = 0; 129 128 var leftspaces = 0, rightspaces = 0;
// awful way to find the first non-space character from the left or the right. thanks.js 130 129 // awful way to find the first non-space character from the left or the right. thanks.js
while (text[leftspaces] == ' ' || text[leftspaces] == '\xa0') leftspaces++; 131 130 while (text[leftspaces] == ' ' || text[leftspaces] == '\xa0') leftspaces++;
while (text[text.length - 1 - rightspaces] == ' ' || text[text.length - 1 - rightspaces] == '\xa0') rightspaces++; 132 131 while (text[text.length - 1 - rightspaces] == ' ' || text[text.length - 1 - rightspaces] == '\xa0') rightspaces++;
console.log(leftspaces, text.length); 133 132 console.log(leftspaces, text.length);
if (leftspaces != text.length) $scope.newCardBlanks.push([i + leftspaces, i + text.length - rightspaces]); 134 133 if (leftspaces != text.length) $scope.newCardBlanks.push([i + leftspaces, i + text.length - rightspaces]);
i += text.length; 135 134 i += text.length;
} else if (!node.data) { 136 135 } else if (!node.data) {
i += $(node).text().length; 137 136 i += $(node).text().length;
} else { 138 137 } else {
i += node.data.length; 139 138 i += node.data.length;
} 140 139 }
}); 141 140 });
$scope.newCardBlanks.sort(function(a, b) { 142 141 $scope.newCardBlanks.sort(function(a, b) {
return a[0] - b[0]; 143 142 return a[0] - b[0];
}); 144 143 });
i = 0; 145 144 i = 0;
newtext = ''; 146 145 newtext = '';
$scope.newCardBlanks.forEach(function(blank) { 147 146 $scope.newCardBlanks.forEach(function(blank) {
newtext += $scope.newCardText.slice(i, blank[0]); 148 147 newtext += $scope.newCardText.slice(i, blank[0]);
newtext += '<b>' + $scope.newCardText.slice(blank[0], blank[1]) + '</b>'; 149 148 newtext += '<b>' + $scope.newCardText.slice(blank[0], blank[1]) + '</b>';
i = blank[1]; 150 149 i = blank[1];
}); 151 150 });
newtext += $scope.newCardText.slice(i); 152 151 newtext += $scope.newCardText.slice(i);
//$scope.newCardFormattedText = newtext; 153 152 //$scope.newCardFormattedText = newtext;
}; 154 153 };
$scope.shuffleCards = function() { 155 154 $scope.shuffleCards = function() {
$timeout(function() { 156 155 $timeout(function() {
(function(o) { 157 156 (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); 158 157 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; 159 158 return o;
})($scope.cardCols[0]); 160 159 })($scope.cardCols[0]);
}); 161 160 });
}; 162 161 };
$scope.$on('$destroy', function() { 163 162 $scope.$on('$destroy', function() {
$scope.feed_ws.close(); 164 163 $scope.feed_ws.close();
}); 165 164 });
return $http.get('/api/sections/' + $scope.sectionId + '/feed/'). 166 165 return $http.get('/api/sections/' + $scope.sectionId + '/feed/').
success(function(data) { 167 166 success(function(data) {