Commit a276a49eeba1cc4b7089be45f464520b77b40a79

Authored by Tetranoir
1 parent 3918ba7a5f

fixed slopy commit, not breaking things, but isnt perfect

Showing 1 changed file with 4 additions and 4 deletions Inline Diff

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