FeedController.js 5.71 KB
angular.module('flashy.FeedController', ['ui.router']).
controller('FeedController', function($scope, $rootScope, $stateParams, $state, $http) {
console.log('Hello from feed');
sectionId = $stateParams.sectionId;
$rootScope.currentSection = $rootScope.SectionResource.get({sectionId: sectionId});
$scope.cards = [];
var loc = window.location, new_uri;
if (loc.protocol === 'https:') {
new_uri = 'wss:';
} else {
new_uri = 'ws:';
}
new_uri += '//' + loc.host;
var ws = new WebSocket(new_uri + '/ws/feed/' + sectionId + '?subscribe-broadcast');
ws.onopen = function() {
console.log('websocket connected');
};
ws.onmessage = function(e) {
console.log('got websocket message ' + e.data);
$scope.refreshCards();
};
ws.onerror = function(e) {
console.error(e);
};
ws.onclose = function(e) {
console.log('connection closed');
};
$http.get('/api/sections/' + sectionId + '/feed/').
success(function(data) {
console.log(data);
$scope.cards = data;
}).
error(function(err) {
console.log('pulling feed failed');
});
$scope.viewDeck = function() {
$state.go('deck', {sectionId: sectionId});
console.log('go to deck');
};
$scope.pushCard = function() {
var pushed = new Date(Date.now());
var i = 0;
var blanks = [];
$('#new-card-input')[0].childNodes.forEach(function(node) {
node = $(node)[0];
console.log(node);
if (node.tagName == 'B') {
text = $(node).text();
blanks.push([i, i + text.length]);
i += text.length;
} else {
i += node.data.length;
}
});
console.log(blanks);
text = $('#new-card-input').text();
var myCard = {
'text': text,
'material_date': pushed,
'mask': blanks,
section: sectionId
};
$http.post('/api/flashcards/', myCard).
success(function(data) {
console.log('pushed a card!');
listenForC = true;
}).
error(function(error) {
console.log('something went wrong pushing a card!');
});
$('#new-card-input').html('');
};
$scope.refreshCards = function() {
$http.get('/api/sections/' + sectionId + '/feed/').
success(function(data) {
console.log(data);
$scope.cards = data;
/*if (data.length == 0) {
return true;
} else {
return false;
}*/
console.log('success in refresh cards...');
}).
error(function(err) {
console.log('refresh fail');
});
};
/* 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() {
listenForC = false;
console.log('modal OPENING');
$('#new-card-input').focus();
},
complete: function() {
listenForC = true;
console.log('modal done, closing');
$('#new-card-input').blur();
}
};
$(document).keydown(function(e) {
console.log(e.which);
var keyed = e.which;
if (keyed == 67 && listenForC) { // "c" or "C" for compose
$('#newCard').openModal(modal_options);
e.preventDefault();
listenForC = false;
return false;
} else if (keyed == 27) { // enter or esc, respectively
listenForC = true;
document.getElementById('new-card-input').value = '';
}
});
$scope.flashcard = '';
$scope.text = '';
var selected_start = 0;
var selected_end = 0;
$(document).ready(function() {
// 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();
$('#newCard').closeModal(modal_options);
return false;
}
});
$('#new-card-input').on('mouseup', function() {
console.log('got selection: ' + selected_start);
});
$('button#blank-selected').click(function() {
console.log(window.getSelection());
document.execCommand('bold');
});
});
$scope.$on('$destroy', function() {
ws.close();
$rootScope.currentSection = {};
$(document).off('keydown');
});
});