author | ymh <ymh.work@gmail.com> |
Thu, 22 Jan 2015 08:07:09 +0100 | |
changeset 109 | 8546e2181a73 |
parent 107 | 6d41506f9482 |
child 110 | e4f0c105090d |
permissions | -rw-r--r-- |
1 | 1 |
(function(){ |
2 |
'use strict'; |
|
3 |
||
4 |
angular.module('mons', [ 'ngResource', 'ngRoute', 'autocomplete' ]) |
|
5 |
.config(function($routeProvider) { |
|
6 |
$routeProvider. |
|
7 |
when('/', { |
|
8 |
controller: 'homeCtrl' |
|
9 |
}). |
|
10 |
otherwise({ |
|
11 |
redirectTo: '/' |
|
12 |
}); |
|
13 |
}) |
|
14 |
.config(function($logProvider){ |
|
15 |
$logProvider.debugEnabled(true); |
|
16 |
}) |
|
17 |
.service('dataApi', function($resource, context) { |
|
34 | 18 |
//console.log('dataApi',$resource, context); |
1 | 19 |
this.dataResource = $resource(context.urls.dataUrl); |
20 |
}) |
|
44 | 21 |
.service('dataModel', function(dataApi, context) { |
1 | 22 |
//console.log('dataModel',this,dataApi); |
42
926f0426ce78
add event + event session + admin + category json management. Must rebuild database
ymh <ymh.work@gmail.com>
parents:
36
diff
changeset
|
23 |
if(typeof context.categories_json !== 'undefined' && context.categories_json) { |
926f0426ce78
add event + event session + admin + category json management. Must rebuild database
ymh <ymh.work@gmail.com>
parents:
36
diff
changeset
|
24 |
this.data = JSON.parse(context.categories_json); |
926f0426ce78
add event + event session + admin + category json management. Must rebuild database
ymh <ymh.work@gmail.com>
parents:
36
diff
changeset
|
25 |
} |
926f0426ce78
add event + event session + admin + category json management. Must rebuild database
ymh <ymh.work@gmail.com>
parents:
36
diff
changeset
|
26 |
else { |
926f0426ce78
add event + event session + admin + category json management. Must rebuild database
ymh <ymh.work@gmail.com>
parents:
36
diff
changeset
|
27 |
this.data = dataApi.dataResource.get(); |
926f0426ce78
add event + event session + admin + category json management. Must rebuild database
ymh <ymh.work@gmail.com>
parents:
36
diff
changeset
|
28 |
} |
1 | 29 |
}) |
9 | 30 |
.controller('homeCtrl', function($scope, $location, dataModel, context, $interval){ |
42
926f0426ce78
add event + event session + admin + category json management. Must rebuild database
ymh <ymh.work@gmail.com>
parents:
36
diff
changeset
|
31 |
|
34 | 32 |
function getURLParameter(name) { |
33 |
return decodeURI( |
|
34 |
(new RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1] |
|
35 |
); |
|
36 |
} |
|
42
926f0426ce78
add event + event session + admin + category json management. Must rebuild database
ymh <ymh.work@gmail.com>
parents:
36
diff
changeset
|
37 |
|
109
8546e2181a73
correct color codes sent by client
ymh <ymh.work@gmail.com>
parents:
107
diff
changeset
|
38 |
function colorToHex(c) { |
8546e2181a73
correct color codes sent by client
ymh <ymh.work@gmail.com>
parents:
107
diff
changeset
|
39 |
var m = /rgba?\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)/.exec(c); |
8546e2181a73
correct color codes sent by client
ymh <ymh.work@gmail.com>
parents:
107
diff
changeset
|
40 |
return m ? '#' + (1 << 24 | m[1] << 16 | m[2] << 8 | m[3]).toString(16).substr(1) : c; |
107 | 41 |
} |
42 |
||
1 | 43 |
$scope.data = dataModel.data; |
23
16a1925df2df
add status to annotation creation. Improve error management for annotation creation
ymh <ymh.work@gmail.com>
parents:
11
diff
changeset
|
44 |
|
42
926f0426ce78
add event + event session + admin + category json management. Must rebuild database
ymh <ymh.work@gmail.com>
parents:
36
diff
changeset
|
45 |
var process_categories = function(data) { |
1 | 46 |
if(typeof data.categories!=='undefined' && data.categories.length>0){ |
47 |
var cats = []; |
|
48 |
var nbCat = data.categories.length; |
|
49 |
for(var i=0;i<nbCat;i++){ |
|
50 |
cats.push(data.categories[i].label); |
|
51 |
if(typeof data.categories[i].subcategories!=='undefined' && data.categories[i].subcategories.length>0){ |
|
52 |
var nbSubCat = data.categories[i].subcategories.length; |
|
53 |
for(var j=0;j<nbSubCat;j++){ |
|
54 |
cats.push(data.categories[i].subcategories[j].label); |
|
55 |
} |
|
56 |
} |
|
57 |
} |
|
107 | 58 |
if(typeof data.autocomplete === 'undefined' || data.autocomplete.length === 0) { |
59 |
$scope.allCatLabels = cats; |
|
60 |
} |
|
61 |
else { |
|
62 |
$scope.allCatLabels = data.autocomplete; |
|
63 |
} |
|
1 | 64 |
} |
42
926f0426ce78
add event + event session + admin + category json management. Must rebuild database
ymh <ymh.work@gmail.com>
parents:
36
diff
changeset
|
65 |
}; |
926f0426ce78
add event + event session + admin + category json management. Must rebuild database
ymh <ymh.work@gmail.com>
parents:
36
diff
changeset
|
66 |
|
926f0426ce78
add event + event session + admin + category json management. Must rebuild database
ymh <ymh.work@gmail.com>
parents:
36
diff
changeset
|
67 |
if (typeof dataModel.data.$promise !== 'undefined') { |
926f0426ce78
add event + event session + admin + category json management. Must rebuild database
ymh <ymh.work@gmail.com>
parents:
36
diff
changeset
|
68 |
dataModel.data.$promise.then(process_categories); |
926f0426ce78
add event + event session + admin + category json management. Must rebuild database
ymh <ymh.work@gmail.com>
parents:
36
diff
changeset
|
69 |
} |
926f0426ce78
add event + event session + admin + category json management. Must rebuild database
ymh <ymh.work@gmail.com>
parents:
36
diff
changeset
|
70 |
else { |
926f0426ce78
add event + event session + admin + category json management. Must rebuild database
ymh <ymh.work@gmail.com>
parents:
36
diff
changeset
|
71 |
process_categories(dataModel.data); |
926f0426ce78
add event + event session + admin + category json management. Must rebuild database
ymh <ymh.work@gmail.com>
parents:
36
diff
changeset
|
72 |
} |
23
16a1925df2df
add status to annotation creation. Improve error management for annotation creation
ymh <ymh.work@gmail.com>
parents:
11
diff
changeset
|
73 |
|
1 | 74 |
$scope.selectedlevel = false; |
9 | 75 |
|
76 |
$scope.currentInterval = false; |
|
77 |
$scope.showSuccessAlert = false; |
|
78 |
$scope.showAlertDiv = false; |
|
79 |
function showAlert(m, success){ |
|
80 |
$scope.alertMessage = m; |
|
81 |
$scope.showSuccessAlert = success; |
|
82 |
$scope.showAlertDiv = true; |
|
83 |
if(!$scope.$$phase) { |
|
84 |
$scope.$apply(); |
|
85 |
} |
|
86 |
if($scope.currentInterval){ |
|
87 |
$interval.cancel($scope.currentInterval); |
|
88 |
$scope.currentInterval = false; |
|
89 |
} |
|
10 | 90 |
$scope.currentInterval = $interval(function(){ $interval.cancel($scope.currentInterval); $scope.showAlertDiv = false; }, 2000, 1); |
9 | 91 |
} |
10 | 92 |
$scope.annotPile = []; |
23
16a1925df2df
add status to annotation creation. Improve error management for annotation creation
ymh <ymh.work@gmail.com>
parents:
11
diff
changeset
|
93 |
|
16a1925df2df
add status to annotation creation. Improve error management for annotation creation
ymh <ymh.work@gmail.com>
parents:
11
diff
changeset
|
94 |
|
5
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
95 |
|
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
96 |
// Socket management |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
97 |
var sock = null; |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
98 |
var ellog = null; |
23
16a1925df2df
add status to annotation creation. Improve error management for annotation creation
ymh <ymh.work@gmail.com>
parents:
11
diff
changeset
|
99 |
|
5
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
100 |
ellog = document.getElementById('log'); |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
101 |
function log(m) { |
6 | 102 |
if(ellog){ |
103 |
ellog.innerHTML += m + '\n'; |
|
104 |
ellog.scrollTop = ellog.scrollHeight; |
|
105 |
} |
|
5
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
106 |
} |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
107 |
|
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
108 |
var wsuri; |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
109 |
if (window.location.protocol === 'file:') { |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
110 |
wsuri = 'ws://127.0.0.1:8090/annot'; |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
111 |
} else { |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
112 |
wsuri = 'ws://' + window.location.hostname + ':8090/annot'; |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
113 |
} |
107 | 114 |
|
44 | 115 |
var eventCode = context.event_code; |
34 | 116 |
if(typeof eventCode==='undefined' || eventCode===''){ |
44 | 117 |
eventCode = $location.search().event; |
34 | 118 |
if(typeof eventCode==='undefined' || eventCode===''){ |
44 | 119 |
eventCode = getURLParameter('event'); |
120 |
if(typeof eventCode==='undefined' || eventCode===''){ |
|
121 |
alert('le code de l\'événement doit être indiqué dans un paramètre de template u dans l\'url selon ?event=CODE_EVENEMENT.'); |
|
122 |
return; |
|
123 |
} |
|
34 | 124 |
} |
125 |
} |
|
126 |
wsuri = wsuri + '?event=' + eventCode; |
|
5
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
127 |
|
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
128 |
if ('WebSocket' in window) { |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
129 |
sock = new WebSocket(wsuri); |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
130 |
} else if ('MozWebSocket' in window) { |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
131 |
sock = new window.MozWebSocket(wsuri); |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
132 |
} else { |
6 | 133 |
if(context.logging===true){ |
134 |
log('Browser does not support WebSocket!'); |
|
135 |
} |
|
5
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
136 |
window.location = 'http://autobahn.ws/unsupportedbrowser'; |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
137 |
} |
23
16a1925df2df
add status to annotation creation. Improve error management for annotation creation
ymh <ymh.work@gmail.com>
parents:
11
diff
changeset
|
138 |
|
5
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
139 |
if (sock) { |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
140 |
sock.onopen = function() { |
6 | 141 |
if(context.logging===true){ |
142 |
log('Connected to ' + wsuri); |
|
143 |
} |
|
5
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
144 |
}; |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
145 |
|
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
146 |
sock.onclose = function(e) { |
6 | 147 |
if(context.logging===true){ |
148 |
log('Connection closed (wasClean = ' + e.wasClean + ', code = ' + e.code + ', reason = ' + e.reason + ')'); |
|
149 |
} |
|
9 | 150 |
showAlert('Communication interrompue : la socket vient de se fermer.', false); |
5
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
151 |
sock = null; |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
152 |
}; |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
153 |
|
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
154 |
sock.onmessage = function(e) { |
36 | 155 |
var data_json = JSON.parse(e.data); |
156 |
//console.log('1', data_json); |
|
23
16a1925df2df
add status to annotation creation. Improve error management for annotation creation
ymh <ymh.work@gmail.com>
parents:
11
diff
changeset
|
157 |
if(context.logging){ |
36 | 158 |
log('Got message: ' + e.data); |
6 | 159 |
} |
10 | 160 |
//showAlert('Annotation bien reçue.', true); |
34 | 161 |
//console.log('2 message', $scope.annotPile.length); |
10 | 162 |
if($scope.annotPile.length>0){ |
163 |
var c = $scope.annotPile.shift(); |
|
27 | 164 |
//console.log('3 message', c); |
36 | 165 |
//console.log('2',data_json.status,data_json.status==='OK'); |
166 |
var ok = data_json.status==='OK'; |
|
10 | 167 |
var i; |
168 |
if(c===false){ |
|
169 |
//showAlert('Annotation envoyée.', true); |
|
36 | 170 |
$scope.sendBtnSuccess = ok; |
171 |
$scope.sendBtnError = !ok; |
|
172 |
i = $interval(function(){ $interval.cancel(i); $scope.sendBtnSuccess = false; $scope.sendBtnError = false; }, 2000, 1); |
|
10 | 173 |
} |
174 |
else{ |
|
36 | 175 |
c.sendSuccess = ok; |
176 |
c.sendError = !ok; |
|
177 |
i = $interval(function(){ $interval.cancel(i); c.sendSuccess = false; c.sendError = false; }, 2000, 1); |
|
10 | 178 |
} |
179 |
if(!$scope.$$phase) { |
|
180 |
$scope.$apply(); |
|
181 |
} |
|
182 |
} |
|
5
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
183 |
}; |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
184 |
} |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
185 |
|
107 | 186 |
$scope.sendFreeAnnotation = function(label, text) { |
187 |
$scope.sendAnnotation(label, window.S(label).slugify().s, label, text, $scope.data.defaultColor || "#536991"); |
|
188 |
}; |
|
189 |
||
190 |
$scope.sendAnnotation = function(label, code, freeLabel, freetext, color, c){ |
|
191 |
||
1 | 192 |
if($scope.username==='' || typeof $scope.username==='undefined'){ |
9 | 193 |
showAlert('Vous devez indiquer un nom d\'utilisateur.', false); |
1 | 194 |
return; |
195 |
} |
|
11 | 196 |
if(label==='' || typeof label==='undefined'){ |
9 | 197 |
showAlert('Vous devez indiquer un nom de catégorie.', false); |
1 | 198 |
return; |
199 |
} |
|
200 |
// Send query |
|
5
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
201 |
if (sock) { |
11 | 202 |
if(typeof code==='undefined' || code===''){ |
203 |
code = window.S(label).slugify().s; |
|
204 |
} |
|
107 | 205 |
|
206 |
var hexc; |
|
207 |
if(color.substring(0, 4) === 'rgb(') { |
|
109
8546e2181a73
correct color codes sent by client
ymh <ymh.work@gmail.com>
parents:
107
diff
changeset
|
208 |
hexc = colorToHex(color); |
107 | 209 |
} |
210 |
else { |
|
211 |
hexc = color; |
|
212 |
} |
|
213 |
||
214 |
||
5
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
215 |
var new_annot = { |
11 | 216 |
category: {code: code, label: label}, |
107 | 217 |
text: freetext, |
218 |
color: hexc, |
|
6 | 219 |
user : $scope.username |
5
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
220 |
}; |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
221 |
sock.send(JSON.stringify(new_annot)); |
109
8546e2181a73
correct color codes sent by client
ymh <ymh.work@gmail.com>
parents:
107
diff
changeset
|
222 |
if(context.logging===true){ |
6 | 223 |
log('Sent: ' + JSON.stringify(new_annot)); |
224 |
} |
|
10 | 225 |
if(typeof c==='undefined'){ |
226 |
$scope.annotPile.push(false); |
|
227 |
} |
|
228 |
else{ |
|
229 |
$scope.annotPile.push(c); |
|
230 |
} |
|
107 | 231 |
$scope.catText = ""; |
232 |
$scope.catLabel = ""; |
|
5
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
233 |
} else { |
9 | 234 |
showAlert('La socket ne fonctionne pas.', false); |
6 | 235 |
if(context.logging===true){ |
7 | 236 |
log('Not connected.'); |
6 | 237 |
} |
5
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
238 |
} |
1 | 239 |
}; |
23
16a1925df2df
add status to annotation creation. Improve error management for annotation creation
ymh <ymh.work@gmail.com>
parents:
11
diff
changeset
|
240 |
|
5
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
241 |
// Interface management |
107 | 242 |
$scope.selectLevel = function(label, code, freelabel, freetext, color, c){ |
10 | 243 |
if(typeof c==='undefined'){ |
1 | 244 |
$scope.returnVisStyle = {visibility:'hidden'}; |
245 |
$scope.selectedlevel = false; |
|
246 |
return; |
|
247 |
} |
|
10 | 248 |
if(typeof c.subcategories!=='undefined' && c.subcategories.length>0){ |
249 |
$scope.selectedlevel = c.subcategories; |
|
1 | 250 |
$scope.returnVisStyle = {visibility:'show'}; |
251 |
} |
|
252 |
else{ |
|
253 |
// Send query |
|
34 | 254 |
//console.log('send ntm', c); |
107 | 255 |
$scope.sendAnnotation(label, code, freelabel, freetext, color, c); |
1 | 256 |
} |
257 |
}; |
|
23
16a1925df2df
add status to annotation creation. Improve error management for annotation creation
ymh <ymh.work@gmail.com>
parents:
11
diff
changeset
|
258 |
|
1 | 259 |
}); |
260 |
||
261 |
})(); |