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