author | ymh <ymh.work@gmail.com> |
Sun, 19 Oct 2014 12:52:38 +0200 | |
changeset 42 | 926f0426ce78 |
parent 36 | 5d392dea4f49 |
child 44 | 76f2fa4eb4f7 |
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 |
}) |
|
42
926f0426ce78
add event + event session + admin + category json management. Must rebuild database
ymh <ymh.work@gmail.com>
parents:
36
diff
changeset
|
21 |
.service('dataModel', function(dataApi, context, $q) { |
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 |
|
1 | 38 |
$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
|
39 |
|
42
926f0426ce78
add event + event session + admin + category json management. Must rebuild database
ymh <ymh.work@gmail.com>
parents:
36
diff
changeset
|
40 |
var process_categories = function(data) { |
1 | 41 |
if(typeof data.categories!=='undefined' && data.categories.length>0){ |
42 |
var cats = []; |
|
43 |
var nbCat = data.categories.length; |
|
44 |
for(var i=0;i<nbCat;i++){ |
|
45 |
cats.push(data.categories[i].label); |
|
46 |
if(typeof data.categories[i].subcategories!=='undefined' && data.categories[i].subcategories.length>0){ |
|
47 |
var nbSubCat = data.categories[i].subcategories.length; |
|
48 |
for(var j=0;j<nbSubCat;j++){ |
|
49 |
cats.push(data.categories[i].subcategories[j].label); |
|
50 |
} |
|
51 |
} |
|
52 |
} |
|
53 |
$scope.allCatLabels = cats; |
|
54 |
} |
|
42
926f0426ce78
add event + event session + admin + category json management. Must rebuild database
ymh <ymh.work@gmail.com>
parents:
36
diff
changeset
|
55 |
}; |
926f0426ce78
add event + event session + admin + category json management. Must rebuild database
ymh <ymh.work@gmail.com>
parents:
36
diff
changeset
|
56 |
|
926f0426ce78
add event + event session + admin + category json management. Must rebuild database
ymh <ymh.work@gmail.com>
parents:
36
diff
changeset
|
57 |
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
|
58 |
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
|
59 |
} |
926f0426ce78
add event + event session + admin + category json management. Must rebuild database
ymh <ymh.work@gmail.com>
parents:
36
diff
changeset
|
60 |
else { |
926f0426ce78
add event + event session + admin + category json management. Must rebuild database
ymh <ymh.work@gmail.com>
parents:
36
diff
changeset
|
61 |
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
|
62 |
} |
23
16a1925df2df
add status to annotation creation. Improve error management for annotation creation
ymh <ymh.work@gmail.com>
parents:
11
diff
changeset
|
63 |
|
1 | 64 |
$scope.selectedlevel = false; |
9 | 65 |
|
66 |
$scope.currentInterval = false; |
|
67 |
$scope.showSuccessAlert = false; |
|
68 |
$scope.showAlertDiv = false; |
|
69 |
function showAlert(m, success){ |
|
70 |
$scope.alertMessage = m; |
|
71 |
$scope.showSuccessAlert = success; |
|
72 |
$scope.showAlertDiv = true; |
|
73 |
if(!$scope.$$phase) { |
|
74 |
$scope.$apply(); |
|
75 |
} |
|
76 |
if($scope.currentInterval){ |
|
77 |
$interval.cancel($scope.currentInterval); |
|
78 |
$scope.currentInterval = false; |
|
79 |
} |
|
10 | 80 |
$scope.currentInterval = $interval(function(){ $interval.cancel($scope.currentInterval); $scope.showAlertDiv = false; }, 2000, 1); |
9 | 81 |
} |
10 | 82 |
$scope.annotPile = []; |
23
16a1925df2df
add status to annotation creation. Improve error management for annotation creation
ymh <ymh.work@gmail.com>
parents:
11
diff
changeset
|
83 |
|
16a1925df2df
add status to annotation creation. Improve error management for annotation creation
ymh <ymh.work@gmail.com>
parents:
11
diff
changeset
|
84 |
|
5
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
85 |
|
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
86 |
// Socket management |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
87 |
var sock = null; |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
88 |
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
|
89 |
|
5
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
90 |
ellog = document.getElementById('log'); |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
91 |
function log(m) { |
6 | 92 |
if(ellog){ |
93 |
ellog.innerHTML += m + '\n'; |
|
94 |
ellog.scrollTop = ellog.scrollHeight; |
|
95 |
} |
|
5
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
96 |
} |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
97 |
|
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
98 |
var wsuri; |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
99 |
if (window.location.protocol === 'file:') { |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
100 |
wsuri = 'ws://127.0.0.1:8090/annot'; |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
101 |
} else { |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
102 |
wsuri = 'ws://' + window.location.hostname + ':8090/annot'; |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
103 |
} |
34 | 104 |
var eventCode = $location.search().event; |
105 |
if(typeof eventCode==='undefined' || eventCode===''){ |
|
106 |
eventCode = getURLParameter('event'); |
|
107 |
if(typeof eventCode==='undefined' || eventCode===''){ |
|
36 | 108 |
alert('le code de l\'événement doit être indiqué dans l\'url selon ?event=CODE_EVENEMENT.'); |
34 | 109 |
return; |
110 |
} |
|
111 |
} |
|
112 |
wsuri = wsuri + '?event=' + eventCode; |
|
5
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
113 |
|
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
114 |
if ('WebSocket' in window) { |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
115 |
sock = new WebSocket(wsuri); |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
116 |
} else if ('MozWebSocket' in window) { |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
117 |
sock = new window.MozWebSocket(wsuri); |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
118 |
} else { |
6 | 119 |
if(context.logging===true){ |
120 |
log('Browser does not support WebSocket!'); |
|
121 |
} |
|
5
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
122 |
window.location = 'http://autobahn.ws/unsupportedbrowser'; |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
123 |
} |
23
16a1925df2df
add status to annotation creation. Improve error management for annotation creation
ymh <ymh.work@gmail.com>
parents:
11
diff
changeset
|
124 |
|
5
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
125 |
if (sock) { |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
126 |
sock.onopen = function() { |
6 | 127 |
if(context.logging===true){ |
128 |
log('Connected to ' + wsuri); |
|
129 |
} |
|
5
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
130 |
}; |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
131 |
|
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
132 |
sock.onclose = function(e) { |
6 | 133 |
if(context.logging===true){ |
134 |
log('Connection closed (wasClean = ' + e.wasClean + ', code = ' + e.code + ', reason = ' + e.reason + ')'); |
|
135 |
} |
|
9 | 136 |
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
|
137 |
sock = null; |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
138 |
}; |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
139 |
|
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
140 |
sock.onmessage = function(e) { |
36 | 141 |
var data_json = JSON.parse(e.data); |
142 |
//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
|
143 |
if(context.logging){ |
36 | 144 |
log('Got message: ' + e.data); |
6 | 145 |
} |
10 | 146 |
//showAlert('Annotation bien reçue.', true); |
34 | 147 |
//console.log('2 message', $scope.annotPile.length); |
10 | 148 |
if($scope.annotPile.length>0){ |
149 |
var c = $scope.annotPile.shift(); |
|
27 | 150 |
//console.log('3 message', c); |
36 | 151 |
//console.log('2',data_json.status,data_json.status==='OK'); |
152 |
var ok = data_json.status==='OK'; |
|
10 | 153 |
var i; |
154 |
if(c===false){ |
|
155 |
//showAlert('Annotation envoyée.', true); |
|
36 | 156 |
$scope.sendBtnSuccess = ok; |
157 |
$scope.sendBtnError = !ok; |
|
158 |
i = $interval(function(){ $interval.cancel(i); $scope.sendBtnSuccess = false; $scope.sendBtnError = false; }, 2000, 1); |
|
10 | 159 |
} |
160 |
else{ |
|
36 | 161 |
c.sendSuccess = ok; |
162 |
c.sendError = !ok; |
|
163 |
i = $interval(function(){ $interval.cancel(i); c.sendSuccess = false; c.sendError = false; }, 2000, 1); |
|
10 | 164 |
} |
165 |
if(!$scope.$$phase) { |
|
166 |
$scope.$apply(); |
|
167 |
} |
|
168 |
} |
|
5
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
169 |
}; |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
170 |
} |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
171 |
|
11 | 172 |
$scope.sendAnnotation = function(label, code, c){ |
1 | 173 |
if($scope.username==='' || typeof $scope.username==='undefined'){ |
9 | 174 |
showAlert('Vous devez indiquer un nom d\'utilisateur.', false); |
1 | 175 |
return; |
176 |
} |
|
11 | 177 |
if(label==='' || typeof label==='undefined'){ |
9 | 178 |
showAlert('Vous devez indiquer un nom de catégorie.', false); |
1 | 179 |
return; |
180 |
} |
|
181 |
// Send query |
|
5
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
182 |
if (sock) { |
11 | 183 |
if(typeof code==='undefined' || code===''){ |
184 |
code = window.S(label).slugify().s; |
|
185 |
} |
|
5
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
186 |
var new_annot = { |
11 | 187 |
category: {code: code, label: label}, |
6 | 188 |
user : $scope.username |
5
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
189 |
}; |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
190 |
sock.send(JSON.stringify(new_annot)); |
6 | 191 |
if(context.logging===true){ |
192 |
log('Sent: ' + JSON.stringify(new_annot)); |
|
193 |
} |
|
10 | 194 |
if(typeof c==='undefined'){ |
195 |
$scope.annotPile.push(false); |
|
196 |
} |
|
197 |
else{ |
|
198 |
$scope.annotPile.push(c); |
|
199 |
} |
|
5
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
200 |
} else { |
9 | 201 |
showAlert('La socket ne fonctionne pas.', false); |
6 | 202 |
if(context.logging===true){ |
7 | 203 |
log('Not connected.'); |
6 | 204 |
} |
5
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
205 |
} |
1 | 206 |
}; |
23
16a1925df2df
add status to annotation creation. Improve error management for annotation creation
ymh <ymh.work@gmail.com>
parents:
11
diff
changeset
|
207 |
|
5
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
208 |
// Interface management |
11 | 209 |
$scope.selectLevel = function(label, code, c){ |
10 | 210 |
if(typeof c==='undefined'){ |
1 | 211 |
$scope.returnVisStyle = {visibility:'hidden'}; |
212 |
$scope.selectedlevel = false; |
|
213 |
return; |
|
214 |
} |
|
10 | 215 |
if(typeof c.subcategories!=='undefined' && c.subcategories.length>0){ |
216 |
$scope.selectedlevel = c.subcategories; |
|
1 | 217 |
$scope.returnVisStyle = {visibility:'show'}; |
218 |
} |
|
219 |
else{ |
|
220 |
// Send query |
|
34 | 221 |
//console.log('send ntm', c); |
11 | 222 |
$scope.sendAnnotation(label, code, c); |
1 | 223 |
} |
224 |
}; |
|
23
16a1925df2df
add status to annotation creation. Improve error management for annotation creation
ymh <ymh.work@gmail.com>
parents:
11
diff
changeset
|
225 |
|
1 | 226 |
}); |
227 |
||
228 |
})(); |