Commit da678ed382c398188c14a7abb4b5075695ff06b2
1 parent
4e372b10c9
Exists in
master
and in
1 other branch
clean up no-cards message
Showing 1 changed file with 0 additions and 15 deletions Inline Diff
scripts/FeedController.js
View file @
da678ed
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 | ||||
$scope.noCards = function () { | 36 | |||
37 | ||||
38 | ||||
/*if (data.length == 0) { | 39 | |||
return true; | 40 | |||
} else { | 41 | |||
return false; | 42 | |||
}*/ | 43 | |||
44 | ||||
} | 45 | |||
46 | ||||
47 | ||||
48 | ||||
49 | ||||
}). | 50 | 35 | }). | |
error(function(err) { | 51 | 36 | error(function(err) { | |
console.log('pulling feed failed'); | 52 | 37 | console.log('pulling feed failed'); | |
}); | 53 | 38 | }); | |
54 | 39 | |||
$scope.viewDeck = function() { | 55 | 40 | $scope.viewDeck = function() { | |
$state.go('deck', {sectionId: sectionId}); | 56 | 41 | $state.go('deck', {sectionId: sectionId}); | |
console.log('go to deck'); | 57 | 42 | console.log('go to deck'); | |
}; | 58 | 43 | }; | |
59 | 44 | |||
$scope.pushCard = function() { | 60 | 45 | $scope.pushCard = function() { | |
var pushed = new Date(Date.now()); | 61 | 46 | var pushed = new Date(Date.now()); | |
var i = 0; | 62 | 47 | var i = 0; | |
var blanks = []; | 63 | 48 | var blanks = []; | |
$('#new-card-input')[0].childNodes.forEach(function(node) { | 64 | 49 | $('#new-card-input')[0].childNodes.forEach(function(node) { | |
node = $(node)[0]; | 65 | 50 | node = $(node)[0]; | |
console.log(node); | 66 | 51 | console.log(node); | |
if (node.tagName == 'B') { | 67 | 52 | if (node.tagName == 'B') { | |
text = $(node).text(); | 68 | 53 | text = $(node).text(); | |
blanks.push([i, i + text.length]); | 69 | 54 | blanks.push([i, i + text.length]); | |
i += text.length; | 70 | 55 | i += text.length; | |
} else { | 71 | 56 | } else { | |
i += node.data.length; | 72 | 57 | i += node.data.length; | |
} | 73 | 58 | } | |
}); | 74 | 59 | }); | |
console.log(blanks); | 75 | 60 | console.log(blanks); | |
text = $('#new-card-input').text(); | 76 | 61 | text = $('#new-card-input').text(); | |
var myCard = { | 77 | 62 | var myCard = { | |
'text': text, | 78 | 63 | 'text': text, | |
'material_date': pushed, | 79 | 64 | 'material_date': pushed, | |
'mask': blanks, | 80 | 65 | 'mask': blanks, | |
section: sectionId | 81 | 66 | section: sectionId | |
}; | 82 | 67 | }; | |
$http.post('/api/flashcards/', myCard). | 83 | 68 | $http.post('/api/flashcards/', myCard). | |
success(function(data) { | 84 | 69 | success(function(data) { | |
console.log('pushed a card!'); | 85 | 70 | console.log('pushed a card!'); | |
listenForC = true; | 86 | 71 | listenForC = true; | |
}). | 87 | 72 | }). | |
error(function(error) { | 88 | 73 | error(function(error) { | |
console.log('something went wrong pushing a card!'); | 89 | 74 | console.log('something went wrong pushing a card!'); | |
}); | 90 | 75 | }); | |
91 | 76 | |||
$('#new-card-input').html(''); | 92 | 77 | $('#new-card-input').html(''); | |
}; | 93 | 78 | }; | |
94 | 79 | |||
$scope.refreshCards = function() { | 95 | 80 | $scope.refreshCards = function() { | |
$http.get('/api/sections/' + sectionId + '/feed/'). | 96 | 81 | $http.get('/api/sections/' + sectionId + '/feed/'). | |
success(function(data) { | 97 | 82 | success(function(data) { | |
console.log(data); | 98 | 83 | console.log(data); | |
$scope.cards = data; | 99 | 84 | $scope.cards = data; | |
100 | 85 | |||
/*if (data.length == 0) { | 101 | 86 | /*if (data.length == 0) { | |
return true; | 102 | 87 | return true; | |
} else { | 103 | 88 | } else { | |
return false; | 104 | 89 | return false; | |
}*/ | 105 | 90 | }*/ | |
106 | 91 | |||
console.log('success in refresh cards...'); | 107 | 92 | console.log('success in refresh cards...'); | |
}). | 108 | 93 | }). | |
error(function(err) { | 109 | 94 | error(function(err) { | |
console.log('refresh fail'); | 110 | 95 | console.log('refresh fail'); | |
}); | 111 | 96 | }); | |
}; | 112 | 97 | }; | |
113 | 98 | |||
/* Key bindings for the whole feed window. Hotkey it up! */ | 114 | 99 | /* Key bindings for the whole feed window. Hotkey it up! */ | |
var listenForC = true; | 115 | 100 | var listenForC = true; | |
116 | 101 | |||
// Need to pass these options into openmodal and leanmodal, | 117 | 102 | // Need to pass these options into openmodal and leanmodal, | |
// otherwise the ready handler doesn't get called | 118 | 103 | // otherwise the ready handler doesn't get called | |
119 | 104 | |||
modal_options = { | 120 | 105 | modal_options = { | |
dismissible: true, // Modal can be dismissed by clicking outside of the modal | 121 | 106 | dismissible: true, // Modal can be dismissed by clicking outside of the modal | |
opacity: 0, // Opacity of modal background | 122 | 107 | opacity: 0, // Opacity of modal background | |
in_duration: 300, // Transition in duration | 123 | 108 | in_duration: 300, // Transition in duration | |
out_duration: 200, // Transition out duration | 124 | 109 | out_duration: 200, // Transition out duration | |
ready: function() { | 125 | 110 | ready: function() { | |
listenForC = false; | 126 | 111 | listenForC = false; | |
console.log('modal OPENING'); | 127 | 112 | console.log('modal OPENING'); | |
$('#new-card-input').focus(); | 128 | 113 | $('#new-card-input').focus(); | |
}, | 129 | 114 | }, | |
complete: function() { | 130 | 115 | complete: function() { | |
listenForC = true; | 131 | 116 | listenForC = true; | |
console.log('modal done, closing'); | 132 | 117 | console.log('modal done, closing'); | |
$('#new-card-input').blur(); | 133 | 118 | $('#new-card-input').blur(); | |
} | 134 | 119 | } | |
}; | 135 | 120 | }; | |
136 | 121 | |||
$(document).keydown(function(e) { | 137 | 122 | $(document).keydown(function(e) { | |
console.log(e.which); | 138 | 123 | console.log(e.which); | |
var keyed = e.which; | 139 | 124 | var keyed = e.which; | |
if (keyed == 67 && listenForC) { // "c" or "C" for compose | 140 | 125 | if (keyed == 67 && listenForC) { // "c" or "C" for compose | |
$('#newCard').openModal(modal_options); | 141 | 126 | $('#newCard').openModal(modal_options); | |
e.preventDefault(); | 142 | 127 | e.preventDefault(); | |
listenForC = false; | 143 | 128 | listenForC = false; | |
return false; | 144 | 129 | return false; | |
} else if (keyed == 27) { // enter or esc, respectively | 145 | 130 | } else if (keyed == 27) { // enter or esc, respectively | |
listenForC = true; | 146 | 131 | listenForC = true; | |
document.getElementById('new-card-input').value = ''; | 147 | 132 | document.getElementById('new-card-input').value = ''; | |
} | 148 | 133 | } | |
}); | 149 | 134 | }); | |
$scope.flashcard = ''; | 150 | 135 | $scope.flashcard = ''; | |
$scope.text = ''; | 151 | 136 | $scope.text = ''; | |
var selected_start = 0; | 152 | 137 | var selected_start = 0; | |
var selected_end = 0; | 153 | 138 | var selected_end = 0; | |
$(document).ready(function() { | 154 | 139 | $(document).ready(function() { | |
// the "href" attribute of .modal-trigger must specify the modal ID that wants to be triggered | 155 | 140 | // the "href" attribute of .modal-trigger must specify the modal ID that wants to be triggered | |
$('.modal-trigger').leanModal(modal_options); | 156 | 141 | $('.modal-trigger').leanModal(modal_options); | |
$('#new-card-input').on('keydown', function(e) { | 157 | 142 | $('#new-card-input').on('keydown', function(e) { |