FeedController.js
5.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
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');
});
});