author | cavaliet |
Fri, 17 Oct 2014 13:51:39 +0200 | |
changeset 34 | dc21d793b5d4 |
parent 27 | 68b29e36c9a2 |
child 36 | 5d392dea4f49 |
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===''){ |
|
98 |
alert('le code de l\'événement doit être indiqué dans l\'url selon "?event=CODE_EVENEMENT".'); |
|
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) { |
23
16a1925df2df
add status to annotation creation. Improve error management for annotation creation
ymh <ymh.work@gmail.com>
parents:
11
diff
changeset
|
131 |
if(context.logging){ |
34 | 132 |
log('Got message: ', e.data); |
6 | 133 |
} |
27 | 134 |
//var data_json = JSON.parse(e.data); |
10 | 135 |
//showAlert('Annotation bien reçue.', true); |
34 | 136 |
//console.log('2 message', $scope.annotPile.length); |
10 | 137 |
if($scope.annotPile.length>0){ |
138 |
var c = $scope.annotPile.shift(); |
|
27 | 139 |
//console.log('3 message', c); |
10 | 140 |
var i; |
141 |
if(c===false){ |
|
142 |
//showAlert('Annotation envoyée.', true); |
|
143 |
$scope.sendBtnSuccess = true; |
|
144 |
i = $interval(function(){ $interval.cancel(i); $scope.sendBtnSuccess = false; }, 2000, 1); |
|
145 |
} |
|
146 |
else{ |
|
147 |
c.sendSuccess = true; |
|
148 |
i = $interval(function(){ $interval.cancel(i); c.sendSuccess = false; }, 2000, 1); |
|
149 |
} |
|
150 |
if(!$scope.$$phase) { |
|
151 |
$scope.$apply(); |
|
152 |
} |
|
153 |
} |
|
5
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
154 |
}; |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
155 |
} |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
156 |
|
11 | 157 |
$scope.sendAnnotation = function(label, code, c){ |
1 | 158 |
if($scope.username==='' || typeof $scope.username==='undefined'){ |
9 | 159 |
showAlert('Vous devez indiquer un nom d\'utilisateur.', false); |
1 | 160 |
return; |
161 |
} |
|
11 | 162 |
if(label==='' || typeof label==='undefined'){ |
9 | 163 |
showAlert('Vous devez indiquer un nom de catégorie.', false); |
1 | 164 |
return; |
165 |
} |
|
166 |
// Send query |
|
5
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
167 |
if (sock) { |
11 | 168 |
if(typeof code==='undefined' || code===''){ |
169 |
code = window.S(label).slugify().s; |
|
170 |
} |
|
5
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
171 |
var new_annot = { |
11 | 172 |
category: {code: code, label: label}, |
6 | 173 |
user : $scope.username |
5
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
174 |
}; |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
175 |
sock.send(JSON.stringify(new_annot)); |
6 | 176 |
if(context.logging===true){ |
177 |
log('Sent: ' + JSON.stringify(new_annot)); |
|
178 |
} |
|
10 | 179 |
if(typeof c==='undefined'){ |
180 |
$scope.annotPile.push(false); |
|
181 |
} |
|
182 |
else{ |
|
183 |
$scope.annotPile.push(c); |
|
184 |
} |
|
5
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
185 |
} else { |
9 | 186 |
showAlert('La socket ne fonctionne pas.', false); |
6 | 187 |
if(context.logging===true){ |
7 | 188 |
log('Not connected.'); |
6 | 189 |
} |
5
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
190 |
} |
1 | 191 |
}; |
23
16a1925df2df
add status to annotation creation. Improve error management for annotation creation
ymh <ymh.work@gmail.com>
parents:
11
diff
changeset
|
192 |
|
5
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
193 |
// Interface management |
11 | 194 |
$scope.selectLevel = function(label, code, c){ |
10 | 195 |
if(typeof c==='undefined'){ |
1 | 196 |
$scope.returnVisStyle = {visibility:'hidden'}; |
197 |
$scope.selectedlevel = false; |
|
198 |
return; |
|
199 |
} |
|
10 | 200 |
if(typeof c.subcategories!=='undefined' && c.subcategories.length>0){ |
201 |
$scope.selectedlevel = c.subcategories; |
|
1 | 202 |
$scope.returnVisStyle = {visibility:'show'}; |
203 |
} |
|
204 |
else{ |
|
205 |
// Send query |
|
34 | 206 |
//console.log('send ntm', c); |
11 | 207 |
$scope.sendAnnotation(label, code, c); |
1 | 208 |
} |
209 |
}; |
|
23
16a1925df2df
add status to annotation creation. Improve error management for annotation creation
ymh <ymh.work@gmail.com>
parents:
11
diff
changeset
|
210 |
|
1 | 211 |
}); |
212 |
||
213 |
})(); |