FeedController.js 7.82 KB
angular.module('flashy.FeedController', ['ui.router', 'ngAnimate', 'ngWebSocket', 'contenteditable']).
controller('FeedController', function($scope, $rootScope, $state, $http, $window, $timeout, $stateParams, $websocket, UserService) {
angular.module('flashy.CardGridController').controller.apply(this, arguments);
console.log('Hello from feed');
$scope.refreshCards = function() {
$http.get('/api/sections/' + $scope.sectionId + '/feed/').
success(function(data) {
console.log(data);
$scope.cards = data;
$scope.refreshLayout();
console.log('success in refresh cards...');
}).
error(function(err) {
console.log('refresh fail');
console.log(err);
});
};
$scope.sortAdd = function(card, array) {
console.log('sort add');
array.forEach(function(ele, i, ary) {
if (ele.score <= card.score) {
ary.splice(i, 0, card);
return;
}
});
};
$scope.hide = function(card) {
console.log('hiding card');
var found = -1;
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;
}
console.log('Error finding card to hide:');
console.log(card);
return -1;
};
$scope.update = function(id, new_score) {
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]));
}
};
$scope.feed_ws = $websocket($scope.ws_host + '/ws/feed/' + $scope.sectionId + '?subscribe-broadcast');
$scope.feed_ws.onOpen(function() {
console.log('deck ws open');
});
$scope.feed_ws.onMessage(function(e) {
data = JSON.parse(e.data);
console.log('got websocket message ' + e.data);
console.log(data);
if (data.event_type == 'new_card') {
$scope.add(data.flashcard);
} else if (data.event_type == 'score_change') {
$scope.update(data.flashcard_id, data.new_score);
}
});
$scope.pushCard = function() {
var i = 0;
var blanks = [];
$('#new-card-input')[0].childNodes.forEach(function(node) {
if (typeof node.data == 'undefined') {
console.log('undefined node');
}
node = $(node)[0];
if (node.tagName == 'B') {
var text = $(node).text();
console.log(text.length, text);
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++;
if (leftspaces != text.length) blanks.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;
}
});
var myCard = {
// we can't trim this string because it'd mess up the blanks. Something to fix.
'text': $('#new-card-input').text(),
'mask': blanks,
section: $scope.section.id
};
if (myCard.text == '') {
console.log('blank flashcard not pushed:' + myCard.text);
return closeNewCard();
}
$http.post('/api/flashcards/', myCard).
success(function(data) {
console.log('flashcard pushed: ' + myCard.text);
if (!UserService.hasVerifiedEmail()) {
Materialize.toast("<p>Thanks for contributing! However, others won't see your card until you verify your email address<p>", 4000);
}
}).
error(function(error) {
console.log('something went wrong pushing a card!');
});
return $scope.closeNewCardModal();
};
/* Key bindings for the whole feed window. Hotkey it up! */
var listenForC = true;
// 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
ready: function() {
$('#new-card-input').focus();
document.execCommand('selectAll', false, null);
}
};
$(document).keydown(function(e) {
var keyed = e.which;
if (keyed == 67 && listenForC) { // "c" for compose
$scope.openNewCardModal();
e.preventDefault();
return false;
} else if (keyed == 27) { // clear on ESC
$scope.closeNewCardModal();
}
});
$scope.openNewCardModal = function() {
$('#newCard').openModal(modal_options);
listenForC = false;
$('#new-card-input').html('Write a flashcard!');
};
$scope.closeNewCardModal = function() {
listenForC = true;
$('#new-card-input').html('').blur();
$('#newCard').closeModal(modal_options);
};
$('.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);
$('#new-card-input').on('keydown', function(e) {
if (e.which == 13) {
e.preventDefault();
$scope.pushCard();
listenForC = true;
return false;
}
});
$('button#blank-selected').click(function() {
console.log(window.getSelection());
document.execCommand('bold');
});
$scope.refreshCards();
$scope.shuffleCards = function() {
$timeout(function() {
(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);
return o;
})($scope.cardCols[0]);
});
};
$scope.$on('$destroy', function() {
$scope.feed_ws.close();
});
});