author | cavaliet |
Thu, 16 Oct 2014 10:24:38 +0200 | |
changeset 27 | 68b29e36c9a2 |
parent 23 | 16a1925df2df |
child 34 | dc21d793b5d4 |
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; |
23
16a1925df2df
add status to annotation creation. Improve error management for annotation creation
ymh <ymh.work@gmail.com>
parents:
11
diff
changeset
|
29 |
|
1 | 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 |
}); |
|
23
16a1925df2df
add status to annotation creation. Improve error management for annotation creation
ymh <ymh.work@gmail.com>
parents:
11
diff
changeset
|
46 |
|
1 | 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 = []; |
23
16a1925df2df
add status to annotation creation. Improve error management for annotation creation
ymh <ymh.work@gmail.com>
parents:
11
diff
changeset
|
66 |
|
16a1925df2df
add status to annotation creation. Improve error management for annotation creation
ymh <ymh.work@gmail.com>
parents:
11
diff
changeset
|
67 |
|
5
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; |
23
16a1925df2df
add status to annotation creation. Improve error management for annotation creation
ymh <ymh.work@gmail.com>
parents:
11
diff
changeset
|
72 |
|
5
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 |
} |
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 |
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) { |
23
16a1925df2df
add status to annotation creation. Improve error management for annotation creation
ymh <ymh.work@gmail.com>
parents:
11
diff
changeset
|
116 |
if(context.logging){ |
16a1925df2df
add status to annotation creation. Improve error management for annotation creation
ymh <ymh.work@gmail.com>
parents:
11
diff
changeset
|
117 |
console.log('Got message: ', e.data); |
6 | 118 |
} |
27 | 119 |
//var data_json = JSON.parse(e.data); |
10 | 120 |
//showAlert('Annotation bien reçue.', true); |
121 |
console.log('2 message', $scope.annotPile.length); |
|
122 |
if($scope.annotPile.length>0){ |
|
123 |
var c = $scope.annotPile.shift(); |
|
27 | 124 |
//console.log('3 message', c); |
10 | 125 |
var i; |
126 |
if(c===false){ |
|
127 |
//showAlert('Annotation envoyée.', true); |
|
128 |
$scope.sendBtnSuccess = true; |
|
129 |
i = $interval(function(){ $interval.cancel(i); $scope.sendBtnSuccess = false; }, 2000, 1); |
|
130 |
} |
|
131 |
else{ |
|
132 |
c.sendSuccess = true; |
|
133 |
i = $interval(function(){ $interval.cancel(i); c.sendSuccess = false; }, 2000, 1); |
|
134 |
} |
|
135 |
if(!$scope.$$phase) { |
|
136 |
$scope.$apply(); |
|
137 |
} |
|
138 |
} |
|
5
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 |
} |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
141 |
|
11 | 142 |
$scope.sendAnnotation = function(label, code, c){ |
1 | 143 |
if($scope.username==='' || typeof $scope.username==='undefined'){ |
9 | 144 |
showAlert('Vous devez indiquer un nom d\'utilisateur.', false); |
1 | 145 |
return; |
146 |
} |
|
11 | 147 |
if(label==='' || typeof label==='undefined'){ |
9 | 148 |
showAlert('Vous devez indiquer un nom de catégorie.', false); |
1 | 149 |
return; |
150 |
} |
|
151 |
// Send query |
|
5
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
152 |
if (sock) { |
11 | 153 |
if(typeof code==='undefined' || code===''){ |
154 |
code = window.S(label).slugify().s; |
|
155 |
} |
|
5
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
156 |
var new_annot = { |
11 | 157 |
category: {code: code, label: label}, |
6 | 158 |
user : $scope.username |
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 |
sock.send(JSON.stringify(new_annot)); |
6 | 161 |
if(context.logging===true){ |
162 |
log('Sent: ' + JSON.stringify(new_annot)); |
|
163 |
} |
|
10 | 164 |
if(typeof c==='undefined'){ |
165 |
$scope.annotPile.push(false); |
|
166 |
} |
|
167 |
else{ |
|
168 |
$scope.annotPile.push(c); |
|
169 |
} |
|
5
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
170 |
} else { |
9 | 171 |
showAlert('La socket ne fonctionne pas.', false); |
6 | 172 |
if(context.logging===true){ |
7 | 173 |
log('Not connected.'); |
6 | 174 |
} |
5
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
175 |
} |
1 | 176 |
}; |
23
16a1925df2df
add status to annotation creation. Improve error management for annotation creation
ymh <ymh.work@gmail.com>
parents:
11
diff
changeset
|
177 |
|
5
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
178 |
// Interface management |
11 | 179 |
$scope.selectLevel = function(label, code, c){ |
10 | 180 |
if(typeof c==='undefined'){ |
1 | 181 |
$scope.returnVisStyle = {visibility:'hidden'}; |
182 |
$scope.selectedlevel = false; |
|
183 |
return; |
|
184 |
} |
|
10 | 185 |
if(typeof c.subcategories!=='undefined' && c.subcategories.length>0){ |
186 |
$scope.selectedlevel = c.subcategories; |
|
1 | 187 |
$scope.returnVisStyle = {visibility:'show'}; |
188 |
} |
|
189 |
else{ |
|
190 |
// Send query |
|
10 | 191 |
console.log('send ntm', c); |
11 | 192 |
$scope.sendAnnotation(label, code, c); |
1 | 193 |
} |
194 |
}; |
|
23
16a1925df2df
add status to annotation creation. Improve error management for annotation creation
ymh <ymh.work@gmail.com>
parents:
11
diff
changeset
|
195 |
|
1 | 196 |
}); |
197 |
||
198 |
})(); |