author | cavaliet |
Fri, 10 Oct 2014 12:03:01 +0200 | |
changeset 9 | ae8a74bd6add |
parent 7 | 813fec862542 |
child 10 | 90918426079c |
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 |
} |
|
63 |
$scope.currentInterval = $interval(function(){ console.log('fin interval !'); $interval.cancel($scope.currentInterval); $scope.showAlertDiv = false; }, 2000, 1); |
|
64 |
} |
|
5
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
65 |
|
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 |
// Socket management |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
69 |
var sock = null; |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
70 |
var ellog = null; |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
71 |
|
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
72 |
ellog = document.getElementById('log'); |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
73 |
function log(m) { |
6 | 74 |
if(ellog){ |
75 |
ellog.innerHTML += m + '\n'; |
|
76 |
ellog.scrollTop = ellog.scrollHeight; |
|
77 |
} |
|
5
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
78 |
} |
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 |
var wsuri; |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
81 |
if (window.location.protocol === 'file:') { |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
82 |
wsuri = 'ws://127.0.0.1:8090/annot'; |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
83 |
} else { |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
84 |
wsuri = 'ws://' + window.location.hostname + ':8090/annot'; |
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 |
wsuri = wsuri + '?event=test'; |
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 |
if ('WebSocket' in window) { |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
89 |
sock = new WebSocket(wsuri); |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
90 |
} else if ('MozWebSocket' in window) { |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
91 |
sock = new window.MozWebSocket(wsuri); |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
92 |
} else { |
6 | 93 |
if(context.logging===true){ |
94 |
log('Browser does not support WebSocket!'); |
|
95 |
} |
|
5
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
96 |
window.location = 'http://autobahn.ws/unsupportedbrowser'; |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
97 |
} |
9 | 98 |
|
5
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
99 |
if (sock) { |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
100 |
sock.onopen = function() { |
6 | 101 |
if(context.logging===true){ |
102 |
log('Connected to ' + wsuri); |
|
103 |
} |
|
5
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
104 |
}; |
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 |
sock.onclose = function(e) { |
6 | 107 |
if(context.logging===true){ |
108 |
log('Connection closed (wasClean = ' + e.wasClean + ', code = ' + e.code + ', reason = ' + e.reason + ')'); |
|
109 |
} |
|
9 | 110 |
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
|
111 |
sock = null; |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
112 |
}; |
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 |
sock.onmessage = function(e) { |
6 | 115 |
if(context.logging===true){ |
116 |
log('Got message: ' + e.data); |
|
117 |
} |
|
9 | 118 |
showAlert('Annotation bien reçue.', true); |
5
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
119 |
}; |
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 |
|
1 | 122 |
$scope.sendAnnotation = function(eventCode){ |
123 |
if($scope.username==='' || typeof $scope.username==='undefined'){ |
|
9 | 124 |
showAlert('Vous devez indiquer un nom d\'utilisateur.', false); |
1 | 125 |
return; |
126 |
} |
|
127 |
if(eventCode==='' || typeof eventCode==='undefined'){ |
|
9 | 128 |
showAlert('Vous devez indiquer un nom de catégorie.', false); |
1 | 129 |
return; |
130 |
} |
|
131 |
// Send query |
|
5
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
132 |
if (sock) { |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
133 |
var new_annot = { |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
134 |
categories : eventCode, |
6 | 135 |
user : $scope.username |
5
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
136 |
}; |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
137 |
sock.send(JSON.stringify(new_annot)); |
6 | 138 |
if(context.logging===true){ |
139 |
log('Sent: ' + JSON.stringify(new_annot)); |
|
140 |
} |
|
5
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
141 |
} else { |
9 | 142 |
showAlert('La socket ne fonctionne pas.', false); |
6 | 143 |
if(context.logging===true){ |
7 | 144 |
log('Not connected.'); |
6 | 145 |
} |
5
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
146 |
} |
1 | 147 |
}; |
148 |
|
|
5
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
149 |
// Interface management |
1 | 150 |
$scope.selectLevel = function(i, eventCode){ |
151 |
if(i===false){ |
|
152 |
$scope.returnVisStyle = {visibility:'hidden'}; |
|
153 |
$scope.selectedlevel = false; |
|
154 |
return; |
|
155 |
} |
|
156 |
if(typeof $scope.data.categories[i].subcategories!=='undefined' && $scope.data.categories[i].subcategories.length>0){ |
|
157 |
$scope.selectedlevel = $scope.data.categories[i].subcategories; |
|
158 |
$scope.returnVisStyle = {visibility:'show'}; |
|
159 |
} |
|
160 |
else{ |
|
161 |
// Send query |
|
162 |
$scope.sendAnnotation(eventCode); |
|
163 |
} |
|
164 |
}; |
|
165 |
|
|
166 |
}); |
|
167 |
||
168 |
})(); |