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