Commit a15945bde1a9adceed883c846c68b0a79d2b2fcf
Exists in
master
and in
1 other branch
Merge branch 'master' of git.ucsd.edu:110swag/flashy-frontend
Showing 1 changed file Inline Diff
scripts/FeedController.js
View file @
a15945b
angular.module('flashy.FeedController', ['ui.router']). | 1 | 1 | angular.module('flashy.FeedController', ['ui.router']). | |
controller('FeedController', function($scope, $rootScope, $stateParams, $state, $http) { | 2 | 2 | controller('FeedController', function($scope, $rootScope, $stateParams, $state, $http) { | |
console.log('Hello from feed'); | 3 | 3 | console.log('Hello from feed'); | |
sectionId = $stateParams.sectionId; | 4 | 4 | sectionId = $stateParams.sectionId; | |
$rootScope.currentSection = $rootScope.SectionResource.get({sectionId: sectionId}); | 5 | 5 | $rootScope.currentSection = $rootScope.SectionResource.get({sectionId: sectionId}); | |
$scope.cards = []; | 6 | 6 | $scope.cards = []; | |
7 | 7 | |||
var loc = window.location, new_uri; | 8 | 8 | var loc = window.location, new_uri; | |
if (loc.protocol === 'https:') { | 9 | 9 | if (loc.protocol === 'https:') { | |
new_uri = 'wss:'; | 10 | 10 | new_uri = 'wss:'; | |
} else { | 11 | 11 | } else { | |
new_uri = 'ws:'; | 12 | 12 | new_uri = 'ws:'; | |
} | 13 | 13 | } | |
new_uri += '//' + loc.host; | 14 | 14 | new_uri += '//' + loc.host; | |
var ws = new WebSocket(new_uri + '/ws/feed/' + sectionId + '?subscribe-broadcast'); | 15 | 15 | var ws = new WebSocket(new_uri + '/ws/feed/' + sectionId + '?subscribe-broadcast'); | |
16 | 16 | |||
ws.onopen = function() { | 17 | 17 | ws.onopen = function() { | |
console.log('websocket connected'); | 18 | 18 | console.log('websocket connected'); | |
}; | 19 | 19 | }; | |
ws.onmessage = function(e) { | 20 | 20 | ws.onmessage = function(e) { | |
console.log('got websocket message ' + e.data); | 21 | 21 | console.log('got websocket message ' + e.data); | |
$scope.refreshCards(); | 22 | 22 | $scope.refreshCards(); | |
}; | 23 | 23 | }; | |
ws.onerror = function(e) { | 24 | 24 | ws.onerror = function(e) { | |
console.error(e); | 25 | 25 | console.error(e); | |
}; | 26 | 26 | }; | |
ws.onclose = function(e) { | 27 | 27 | ws.onclose = function(e) { | |
console.log('connection closed'); | 28 | 28 | console.log('connection closed'); | |
}; | 29 | 29 | }; | |
30 | 30 | |||
$http.get('/api/sections/' + sectionId + '/feed/'). | 31 | 31 | $http.get('/api/sections/' + sectionId + '/feed/'). | |
success(function(data) { | 32 | 32 | success(function(data) { | |
console.log(data); | 33 | 33 | console.log(data); | |
$scope.cards = data; | 34 | 34 | $scope.cards = data; | |
35 | 35 | |||
$scope.noCards = function () { | 36 | 36 | $scope.noCards = function () { | |
37 | 37 | |||
alert(data.length); | 38 | 38 | alert(data.length); | |
39 | 39 | |||
if (data.length == 0) { | 40 | 40 | if (data.length == 0) { | |
return true; | 41 | 41 | return true; | |
} else { | 42 | 42 | } else { | |
return false; | 43 | 43 | return false; | |
} | 44 | 44 | } | |
45 | 45 | |||
} | 46 | 46 | } | |
47 | 47 | |||
48 | 48 | |||
49 | 49 | |||
50 | 50 | |||
}). | 51 | 51 | }). | |
error(function(err) { | 52 | 52 | error(function(err) { | |
console.log('pulling feed failed'); | 53 | 53 | console.log('pulling feed failed'); | |
}); | 54 | 54 | }); | |
55 | 55 | |||
$scope.viewDeck = function() { | 56 | 56 | $scope.viewDeck = function() { | |
$state.go('deck', {sectionId: sectionId}); | 57 | 57 | $state.go('deck', {sectionId: sectionId}); | |
console.log('go to deck'); | 58 | 58 | console.log('go to deck'); | |
}; | 59 | 59 | }; | |
60 | 60 | |||
$scope.pushCard = function() { | 61 | 61 | $scope.pushCard = function() { | |
var pushed = new Date(Date.now()); | 62 | 62 | var pushed = new Date(Date.now()); | |
var i = 0; | 63 | 63 | var i = 0; | |
var blanks = []; | 64 | 64 | var blanks = []; | |
$('#new-card-input')[0].childNodes.forEach(function(node) { | 65 | 65 | $('#new-card-input')[0].childNodes.forEach(function(node) { | |
node = $(node)[0]; | 66 | 66 | node = $(node)[0]; | |
console.log(node); | 67 | 67 | console.log(node); | |
if (node.tagName == 'B') { | 68 | 68 | if (node.tagName == 'B') { | |
text = $(node).text(); | 69 | 69 | text = $(node).text(); | |
blanks.push([i, i + text.length]); | 70 | 70 | blanks.push([i, i + text.length]); | |
i += text.length; | 71 | 71 | i += text.length; | |
} else { | 72 | 72 | } else { | |
i += node.data.length; | 73 | 73 | i += node.data.length; | |
} | 74 | 74 | } | |
}); | 75 | 75 | }); | |
console.log(blanks); | 76 | 76 | console.log(blanks); | |
text = $('#new-card-input').text(); | 77 | 77 | text = $('#new-card-input').text(); | |
var myCard = { | 78 | 78 | var myCard = { | |
'text': text, | 79 | 79 | 'text': text, | |
'material_date': pushed, | 80 | 80 | 'material_date': pushed, | |
'mask': JSON.stringify(blanks), | 81 | 81 | 'mask': blanks, | |
section: sectionId | 82 | 82 | section: sectionId | |
}; | 83 | 83 | }; | |
$http.post('/api/flashcards/', myCard). | 84 | 84 | $http.post('/api/flashcards/', myCard). | |
success(function(data) { | 85 | 85 | success(function(data) { | |
console.log('pushed a card!'); | 86 | 86 | console.log('pushed a card!'); | |
listenForC = true; | 87 | 87 | listenForC = true; | |
}). | 88 | 88 | }). | |
error(function(error) { | 89 | 89 | error(function(error) { | |
console.log('something went wrong pushing a card!'); | 90 | 90 | console.log('something went wrong pushing a card!'); | |
}); | 91 | 91 | }); | |
92 | 92 | |||
$('#new-card-input').html(''); | 93 | 93 | $('#new-card-input').html(''); | |
}; | 94 | 94 | }; | |
95 | 95 | |||
$scope.refreshCards = function() { | 96 | 96 | $scope.refreshCards = function() { | |
$http.get('/api/sections/' + sectionId + '/feed/'). | 97 | 97 | $http.get('/api/sections/' + sectionId + '/feed/'). | |
success(function(data) { | 98 | 98 | success(function(data) { | |
console.log(data); | 99 | 99 | console.log(data); | |
$scope.cards = data; | 100 | 100 | $scope.cards = data; | |
console.log('success in refresh cards...'); | 101 | 101 | console.log('success in refresh cards...'); | |
}). | 102 | 102 | }). | |
error(function(err) { | 103 | 103 | error(function(err) { | |
console.log('refresh fail'); | 104 | 104 | console.log('refresh fail'); | |
}); | 105 | 105 | }); | |
}; | 106 | 106 | }; | |
107 | 107 | |||
/* Key bindings for the whole feed window. Hotkey it up! */ | 108 | 108 | /* Key bindings for the whole feed window. Hotkey it up! */ | |
var listenForC = true; | 109 | 109 | var listenForC = true; | |
110 | 110 | |||
// Need to pass these options into openmodal and leanmodal, | 111 | 111 | // Need to pass these options into openmodal and leanmodal, | |
// otherwise the ready handler doesn't get called | 112 | 112 | // otherwise the ready handler doesn't get called | |
113 | 113 | |||
modal_options = { | 114 | 114 | modal_options = { | |
dismissible: true, // Modal can be dismissed by clicking outside of the modal | 115 | 115 | dismissible: true, // Modal can be dismissed by clicking outside of the modal | |
opacity: 0, // Opacity of modal background | 116 | 116 | opacity: 0, // Opacity of modal background | |
in_duration: 300, // Transition in duration | 117 | 117 | in_duration: 300, // Transition in duration | |
out_duration: 200, // Transition out duration | 118 | 118 | out_duration: 200, // Transition out duration | |
ready: function() { | 119 | 119 | ready: function() { | |
listenForC = false; | 120 | 120 | listenForC = false; | |
console.log('modal OPENING'); | 121 | 121 | console.log('modal OPENING'); | |
$('#new-card-input').focus(); | 122 | 122 | $('#new-card-input').focus(); | |
}, | 123 | 123 | }, | |
complete: function() { | 124 | 124 | complete: function() { | |
listenForC = true; | 125 | 125 | listenForC = true; | |
console.log('modal done, closing'); | 126 | 126 | console.log('modal done, closing'); | |
$('#new-card-input').blur(); | 127 | 127 | $('#new-card-input').blur(); | |
} | 128 | 128 | } | |
}; | 129 | 129 | }; | |
130 | 130 | |||
$(document).keydown(function(e) { | 131 | 131 | $(document).keydown(function(e) { | |
console.log(e.which); | 132 | 132 | console.log(e.which); | |
var keyed = e.which; | 133 | 133 | var keyed = e.which; | |
if (keyed == 67 && listenForC) { // "c" or "C" for compose | 134 | 134 | if (keyed == 67 && listenForC) { // "c" or "C" for compose | |
$('#newCard').openModal(modal_options); | 135 | 135 | $('#newCard').openModal(modal_options); | |
e.preventDefault(); | 136 | 136 | e.preventDefault(); | |
listenForC = false; | 137 | 137 | listenForC = false; | |
return false; | 138 | 138 | return false; | |
} else if (keyed == 27) { // enter or esc, respectively | 139 | 139 | } else if (keyed == 27) { // enter or esc, respectively | |
listenForC = true; | 140 | 140 | listenForC = true; | |
document.getElementById('new-card-input').value = ''; | 141 | 141 | document.getElementById('new-card-input').value = ''; | |
} | 142 | 142 | } | |
}); | 143 | 143 | }); | |
$scope.flashcard = ''; | 144 | 144 | $scope.flashcard = ''; | |
$scope.text = ''; | 145 | 145 | $scope.text = ''; | |
var selected_start = 0; | 146 | 146 | var selected_start = 0; | |
var selected_end = 0; | 147 | 147 | var selected_end = 0; | |
$(document).ready(function() { | 148 | 148 | $(document).ready(function() { | |
// the "href" attribute of .modal-trigger must specify the modal ID that wants to be triggered | 149 | 149 | // the "href" attribute of .modal-trigger must specify the modal ID that wants to be triggered | |
$('.modal-trigger').leanModal(modal_options); | 150 | 150 | $('.modal-trigger').leanModal(modal_options); | |
$('#new-card-input').on('keydown', function(e) { | 151 | 151 | $('#new-card-input').on('keydown', function(e) { | |
if (e.which == 13) { | 152 | 152 | if (e.which == 13) { | |
e.preventDefault(); | 153 | 153 | e.preventDefault(); | |
$scope.pushCard(); | 154 | 154 | $scope.pushCard(); |