Commit d6f6a5fcde235c8e0f6a72c36e5c9a5a6a638964
1 parent
688e5fe71b
Exists in
master
and in
1 other branch
correctly add card to empty col
Showing 1 changed file with 1 additions and 1 deletions Inline Diff
scripts/FeedController.js
View file @
d6f6a5f
angular.module('flashy.FeedController', ['ui.router']). | 1 | 1 | angular.module('flashy.FeedController', ['ui.router']). | |
2 | 2 | |||
controller('FeedController', function($scope, $rootScope, $stateParams, $state, $http, $window, $timeout, UserService) { | 3 | 3 | controller('FeedController', function($scope, $rootScope, $stateParams, $state, $http, $window, $timeout, UserService) { | |
console.log('Hello from feed'); | 4 | 4 | console.log('Hello from feed'); | |
sectionId = $stateParams.sectionId; | 5 | 5 | sectionId = $stateParams.sectionId; | |
$rootScope.currentSection = $rootScope.SectionResource.get({sectionId: sectionId}); | 6 | 6 | $rootScope.currentSection = $rootScope.SectionResource.get({sectionId: sectionId}); | |
$scope.cards = false; | 7 | 7 | $scope.cards = false; | |
$scope.cardCols = []; // organized data | 8 | 8 | $scope.cardCols = []; // organized data | |
$scope.numCols = 0; | 9 | 9 | $scope.numCols = 0; | |
10 | 10 | |||
function calculate_cols() { | 11 | 11 | function calculate_cols() { | |
var avail = $window.innerWidth - 17; | 12 | 12 | var avail = $window.innerWidth - 17; | |
return Math.max(1, Math.floor(avail / 250)); | 13 | 13 | return Math.max(1, Math.floor(avail / 250)); | |
} | 14 | 14 | } | |
15 | 15 | |||
function refreshColumnWidth() { | 16 | 16 | function refreshColumnWidth() { | |
avail = $window.innerWidth - 17; | 17 | 17 | avail = $window.innerWidth - 17; | |
width = Math.floor(avail / Math.floor(avail / 250)); | 18 | 18 | width = Math.floor(avail / Math.floor(avail / 250)); | |
$('.cardColumn').css({ | 19 | 19 | $('.cardColumn').css({ | |
width: width + 'px', | 20 | 20 | width: width + 'px', | |
'font-size': 100 * width / 250 + '%' | 21 | 21 | 'font-size': 100 * width / 250 + '%' | |
}); | 22 | 22 | }); | |
$('.cardColumn .card.flashy').css({ | 23 | 23 | $('.cardColumn .card.flashy').css({ | |
width: width - 12 + 'px', | 24 | 24 | width: width - 12 + 'px', | |
height: (width * 3 / 5) + 'px' | 25 | 25 | height: (width * 3 / 5) + 'px' | |
}); | 26 | 26 | }); | |
} | 27 | 27 | } | |
28 | 28 | |||
$scope.refreshLayout = function() { | 29 | 29 | $scope.refreshLayout = function() { | |
refreshColumnWidth(); | 30 | 30 | refreshColumnWidth(); | |
// check if we actually need to refresh the whole layout | 31 | 31 | // check if we actually need to refresh the whole layout | |
if (calculate_cols() == $scope.numCols) return; | 32 | 32 | if (calculate_cols() == $scope.numCols) return; | |
$scope.numCols = calculate_cols(); | 33 | 33 | $scope.numCols = calculate_cols(); | |
console.log('refreshing layout for ' + $scope.numCols + ' columns'); | 34 | 34 | console.log('refreshing layout for ' + $scope.numCols + ' columns'); | |
$scope.cardCols = []; | 35 | 35 | $scope.cardCols = []; | |
var cols = []; | 36 | 36 | var cols = []; | |
for (i = 0; i < $scope.numCols; i++) cols.push([]); | 37 | 37 | for (i = 0; i < $scope.numCols; i++) cols.push([]); | |
$scope.cards.forEach(function(card, i) { | 38 | 38 | $scope.cards.forEach(function(card, i) { | |
cols[i % $scope.numCols].push(card); | 39 | 39 | cols[i % $scope.numCols].push(card); | |
}); | 40 | 40 | }); | |
// wait until the next digest cycle to update cardCols | 41 | 41 | // wait until the next digest cycle to update cardCols | |
$timeout(function() { | 42 | 42 | $timeout(function() { | |
$scope.cardCols = cols; | 43 | 43 | $scope.cardCols = cols; | |
}); | 44 | 44 | }); | |
}; | 45 | 45 | }; | |
46 | 46 | |||
angular.element($window).bind('resize', $scope.refreshLayout); | 47 | 47 | angular.element($window).bind('resize', $scope.refreshLayout); | |
48 | 48 | |||
$scope.refreshCards = function() { | 49 | 49 | $scope.refreshCards = function() { | |
$http.get('/api/sections/' + sectionId + '/feed/'). | 50 | 50 | $http.get('/api/sections/' + sectionId + '/feed/'). | |
success(function(data) { | 51 | 51 | success(function(data) { | |
console.log(data); | 52 | 52 | console.log(data); | |
$scope.cards = data; | 53 | 53 | $scope.cards = data; | |
$scope.refreshLayout(); | 54 | 54 | $scope.refreshLayout(); | |
console.log('success in refresh cards...'); | 55 | 55 | console.log('success in refresh cards...'); | |
}). | 56 | 56 | }). | |
error(function(err) { | 57 | 57 | error(function(err) { | |
console.log('refresh fail'); | 58 | 58 | console.log('refresh fail'); | |
}); | 59 | 59 | }); | |
}; | 60 | 60 | }; | |
61 | 61 | |||
$scope.add = function(card) { | 62 | 62 | $scope.add = function(card) { | |
console.log('adding card'); | 63 | 63 | console.log('adding card'); | |
for (i = 0; i < $scope.cardCols.length; i++) { | 64 | 64 | for (i = 0; i < $scope.cardCols.length; i++) { | |
if ($scope.cardCols.length == 0) { | 65 | 65 | if ($scope.cardCols.length == 0) { | |
$scope.cardCols.unshift(card); | 66 | 66 | $scope.cardCols[i].unshift(card); | |
return; | 67 | 67 | return; | |
} | 68 | 68 | } | |
} | 69 | 69 | } | |
var colNum = Math.floor(Math.random() * $scope.numCols); | 70 | 70 | var colNum = Math.floor(Math.random() * $scope.numCols); | |
$scope.cardCols[colNum].unshift(card); | 71 | 71 | $scope.cardCols[colNum].unshift(card); | |
}; | 72 | 72 | }; | |
73 | 73 | |||
$scope.hide = function(card) { | 74 | 74 | $scope.hide = function(card) { | |
console.log('hiding card'); | 75 | 75 | console.log('hiding card'); | |
console.log(card); | 76 | 76 | console.log(card); | |
var found = -1; | 77 | 77 | var found = -1; | |
for (i = 0; i < $scope.cardCols.length; i++) { | 78 | 78 | for (i = 0; i < $scope.cardCols.length; i++) { | |
found = $scope.cardCols[i].indexOf(card); | 79 | 79 | found = $scope.cardCols[i].indexOf(card); | |
if (found != -1) { | 80 | 80 | if (found != -1) { | |
$scope.cardCols[i].splice(found, 1); | 81 | 81 | $scope.cardCols[i].splice(found, 1); | |
return; | 82 | 82 | return; | |
} | 83 | 83 | } | |
} | 84 | 84 | } | |
console.log('Error finding card to hide:'); | 85 | 85 | console.log('Error finding card to hide:'); | |
console.log(card); | 86 | 86 | console.log(card); | |
}; | 87 | 87 | }; | |
88 | 88 | |||
/* Instance creation */ | 89 | 89 | /* Instance creation */ | |
$http.get('/api/sections/' + sectionId + '/feed/'). | 90 | 90 | $http.get('/api/sections/' + sectionId + '/feed/'). | |
success(function(data) { | 91 | 91 | success(function(data) { | |
console.log(data); | 92 | 92 | console.log(data); | |
$scope.cards = data; | 93 | 93 | $scope.cards = data; | |
$scope.refreshLayout(); | 94 | 94 | $scope.refreshLayout(); | |
console.log('success in refresh cards...'); | 95 | 95 | console.log('success in refresh cards...'); | |
}). | 96 | 96 | }). | |
error(function(err) { | 97 | 97 | error(function(err) { | |
console.log('pulling feed failed'); | 98 | 98 | console.log('pulling feed failed'); | |
}); | 99 | 99 | }); | |
100 | 100 | |||
var loc = window.location, new_uri; | 101 | 101 | var loc = window.location, new_uri; | |
if (loc.protocol === 'https:') { | 102 | 102 | if (loc.protocol === 'https:') { | |
new_uri = 'wss:'; | 103 | 103 | new_uri = 'wss:'; | |
} else { | 104 | 104 | } else { | |
new_uri = 'ws:'; | 105 | 105 | new_uri = 'ws:'; | |
} | 106 | 106 | } | |
new_uri += '//' + loc.host; | 107 | 107 | new_uri += '//' + loc.host; | |
var ws = new WebSocket(new_uri + '/ws/feed/' + sectionId + '?subscribe-broadcast'); | 108 | 108 | var ws = new WebSocket(new_uri + '/ws/feed/' + sectionId + '?subscribe-broadcast'); | |
109 | 109 | |||
ws.onopen = function() { | 110 | 110 | ws.onopen = function() { | |
console.log('websocket connected'); | 111 | 111 | console.log('websocket connected'); | |
}; | 112 | 112 | }; | |
ws.onmessage = function(e) { | 113 | 113 | ws.onmessage = function(e) { | |
console.log('got websocket message ' + e.data); | 114 | 114 | console.log('got websocket message ' + e.data); | |
data = JSON.parse(e.data); | 115 | 115 | data = JSON.parse(e.data); | |
if (data.event_type == 'new_card') { | 116 | 116 | if (data.event_type == 'new_card') { | |
$scope.add(data.flashcard); | 117 | 117 | $scope.add(data.flashcard); | |
} | 118 | 118 | } | |
}; | 119 | 119 | }; | |
ws.onerror = function(e) { | 120 | 120 | ws.onerror = function(e) { | |
console.error(e); | 121 | 121 | console.error(e); | |
}; | 122 | 122 | }; | |
ws.onclose = function(e) { | 123 | 123 | ws.onclose = function(e) { | |
console.log('connection closed'); | 124 | 124 | console.log('connection closed'); | |
}; | 125 | 125 | }; | |
126 | 126 | |||
$scope.pushCard = function() { | 127 | 127 | $scope.pushCard = function() { | |
var i = 0; | 128 | 128 | var i = 0; | |
var blanks = []; | 129 | 129 | var blanks = []; | |
$('#new-card-input')[0].childNodes.forEach(function(node) { | 130 | 130 | $('#new-card-input')[0].childNodes.forEach(function(node) { | |
node = $(node)[0]; | 131 | 131 | node = $(node)[0]; | |
console.log(node); | 132 | 132 | console.log(node); | |
if (node.tagName == 'B') { | 133 | 133 | if (node.tagName == 'B') { | |
text = $(node).text(); | 134 | 134 | text = $(node).text(); | |
blanks.push([i, i + text.length]); | 135 | 135 | blanks.push([i, i + text.length]); | |
i += text.length; | 136 | 136 | i += text.length; | |
} else { | 137 | 137 | } else { | |
i += node.data.length; | 138 | 138 | i += node.data.length; | |
} | 139 | 139 | } | |
}); | 140 | 140 | }); | |
text = $('#new-card-input').text(); | 141 | 141 | text = $('#new-card-input').text(); | |
var myCard = { | 142 | 142 | var myCard = { | |
'text': text, | 143 | 143 | 'text': text, | |
'mask': blanks, | 144 | 144 | 'mask': blanks, | |
section: sectionId | 145 | 145 | section: sectionId | |
}; | 146 | 146 | }; | |
$http.post('/api/flashcards/', myCard). | 147 | 147 | $http.post('/api/flashcards/', myCard). | |
success(function(data) { | 148 | 148 | success(function(data) { | |
console.log('flashcard push succeeded'); | 149 | 149 | console.log('flashcard push succeeded'); | |
if (!UserService.hasVerifiedEmail()) { | 150 | 150 | if (!UserService.hasVerifiedEmail()) { | |
Materialize.toast("<p>Thanks for contributing! However, others won't see your card until you verify your email address<p>", 4000); | 151 | 151 | Materialize.toast("<p>Thanks for contributing! However, others won't see your card until you verify your email address<p>", 4000); | |
} | 152 | 152 | } | |
}). | 153 | 153 | }). | |
error(function(error) { | 154 | 154 | error(function(error) { | |
console.log('something went wrong pushing a card!'); | 155 | 155 | console.log('something went wrong pushing a card!'); | |
}); | 156 | 156 | }); | |
listenForC = true; | 157 | 157 | listenForC = true; | |
$('#new-card-input').html(''); | 158 | 158 | $('#new-card-input').html(''); | |
}; | 159 | 159 | }; | |
160 | 160 | |||
/* Key bindings for the whole feed window. Hotkey it up! */ | 161 | 161 | /* Key bindings for the whole feed window. Hotkey it up! */ | |
var listenForC = true; | 162 | 162 | var listenForC = true; | |
163 | 163 | |||
// Need to pass these options into openmodal and leanmodal, | 164 | 164 | // Need to pass these options into openmodal and leanmodal, | |
// otherwise the ready handler doesn't get called | 165 | 165 | // otherwise the ready handler doesn't get called | |
166 | 166 | |||
modal_options = { | 167 | 167 | modal_options = { | |
dismissible: true, // Modal can be dismissed by clicking outside of the modal | 168 | 168 | dismissible: true, // Modal can be dismissed by clicking outside of the modal | |
opacity: 0, // Opacity of modal background | 169 | 169 | opacity: 0, // Opacity of modal background | |
in_duration: 300, // Transition in duration | 170 | 170 | in_duration: 300, // Transition in duration | |
out_duration: 200, // Transition out duration | 171 | 171 | out_duration: 200, // Transition out duration | |
ready: function() { | 172 | 172 | ready: function() { | |
listenForC = false; | 173 | 173 | listenForC = false; | |
console.log('modal OPENING'); | 174 | 174 | console.log('modal OPENING'); | |
$('#new-card-input').focus(); | 175 | 175 | $('#new-card-input').focus(); | |
}, | 176 | 176 | }, | |
complete: function() { | 177 | 177 | complete: function() { | |
listenForC = true; | 178 | 178 | listenForC = true; | |
console.log('modal done, closing'); | 179 | 179 | console.log('modal done, closing'); | |
$('#new-card-input').blur(); | 180 | 180 | $('#new-card-input').blur(); | |
} | 181 | 181 | } | |
}; | 182 | 182 | }; | |
183 | 183 | |||
$(document).keydown(function(e) { | 184 | 184 | $(document).keydown(function(e) { | |
var keyed = e.which; | 185 | 185 | var keyed = e.which; | |
if (keyed == 67 && listenForC) { // "c" or "C" for compose | 186 | 186 | if (keyed == 67 && listenForC) { // "c" or "C" for compose | |
$('#newCard').openModal(modal_options); | 187 | 187 | $('#newCard').openModal(modal_options); | |
e.preventDefault(); | 188 | 188 | e.preventDefault(); | |
listenForC = false; | 189 | 189 | listenForC = false; | |
return false; | 190 | 190 | return false; | |
} else if (keyed == 27) { // enter or esc, respectively | 191 | 191 | } else if (keyed == 27) { // enter or esc, respectively | |
listenForC = true; | 192 | 192 | listenForC = true; | |
document.getElementById('new-card-input').value = ''; | 193 | 193 | document.getElementById('new-card-input').value = ''; | |
} | 194 | 194 | } | |
}); | 195 | 195 | }); | |
$scope.flashcard = ''; | 196 | 196 | $scope.flashcard = ''; | |
$scope.text = ''; | 197 | 197 | $scope.text = ''; | |
var selected_start = 0; | 198 | 198 | var selected_start = 0; | |
var selected_end = 0; | 199 | 199 | var selected_end = 0; | |
$(document).ready(function() { | 200 | 200 | $(document).ready(function() { | |
$('.tooltipped').tooltip({delay: 50}); | 201 | 201 | $('.tooltipped').tooltip({delay: 50}); | |
// the "href" attribute of .modal-trigger must specify the modal ID that wants to be triggered | 202 | 202 | // the "href" attribute of .modal-trigger must specify the modal ID that wants to be triggered | |
$('.modal-trigger').leanModal(modal_options); | 203 | 203 | $('.modal-trigger').leanModal(modal_options); | |
$('#new-card-input').on('keydown', function(e) { | 204 | 204 | $('#new-card-input').on('keydown', function(e) { | |
if (e.which == 13) { | 205 | 205 | if (e.which == 13) { | |
e.preventDefault(); | 206 | 206 | e.preventDefault(); | |
$scope.pushCard(); | 207 | 207 | $scope.pushCard(); | |
$('#newCard').closeModal(modal_options); | 208 | 208 | $('#newCard').closeModal(modal_options); | |
return false; | 209 | 209 | return false; | |
} | 210 | 210 | } | |
}); | 211 | 211 | }); | |
$('#new-card-input').on('mouseup', function() { | 212 | 212 | $('#new-card-input').on('mouseup', function() { |