author | cavaliet |
Thu, 09 Oct 2014 15:25:56 +0200 | |
changeset 7 | 813fec862542 |
parent 6 | c53e9b24f93f |
child 9 | ae8a74bd6add |
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 |
}) |
|
5
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
25 |
.controller('homeCtrl', function($scope, $location, dataModel, context){ |
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; |
|
48 |
|
|
5
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
49 |
|
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
50 |
|
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
51 |
|
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
52 |
// Socket management |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
53 |
var sock = null; |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
54 |
var ellog = null; |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
55 |
|
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
56 |
ellog = document.getElementById('log'); |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
57 |
function log(m) { |
6 | 58 |
if(ellog){ |
59 |
ellog.innerHTML += m + '\n'; |
|
60 |
ellog.scrollTop = ellog.scrollHeight; |
|
61 |
} |
|
5
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
62 |
} |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
63 |
|
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
64 |
var wsuri; |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
65 |
if (window.location.protocol === 'file:') { |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
66 |
wsuri = 'ws://127.0.0.1:8090/annot'; |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
67 |
} else { |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
68 |
wsuri = 'ws://' + window.location.hostname + ':8090/annot'; |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
69 |
} |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
70 |
wsuri = wsuri + '?event=test'; |
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 |
if ('WebSocket' in window) { |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
73 |
sock = new WebSocket(wsuri); |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
74 |
} else if ('MozWebSocket' in window) { |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
75 |
sock = new window.MozWebSocket(wsuri); |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
76 |
} else { |
6 | 77 |
if(context.logging===true){ |
78 |
log('Browser does not support WebSocket!'); |
|
79 |
} |
|
5
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
80 |
window.location = 'http://autobahn.ws/unsupportedbrowser'; |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
81 |
} |
6 | 82 |
console.log('socket in controller = ', sock); |
5
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
83 |
if (sock) { |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
84 |
sock.onopen = function() { |
6 | 85 |
if(context.logging===true){ |
86 |
log('Connected to ' + wsuri); |
|
87 |
} |
|
5
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 |
|
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
90 |
sock.onclose = function(e) { |
6 | 91 |
if(context.logging===true){ |
92 |
log('Connection closed (wasClean = ' + e.wasClean + ', code = ' + e.code + ', reason = ' + e.reason + ')'); |
|
93 |
} |
|
94 |
alert('Communication interrompue : la socket vient de se fermer.'); |
|
5
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
95 |
sock = null; |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
96 |
}; |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
97 |
|
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
98 |
sock.onmessage = function(e) { |
6 | 99 |
if(context.logging===true){ |
100 |
log('Got message: ' + e.data); |
|
101 |
} |
|
102 |
alert('Annotation bien reçue.'); |
|
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 |
} |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
105 |
|
1 | 106 |
$scope.sendAnnotation = function(eventCode){ |
107 |
if($scope.username==='' || typeof $scope.username==='undefined'){ |
|
108 |
alert('Vous devez indiquer un nom d\'utilisateur.'); |
|
109 |
return; |
|
110 |
} |
|
111 |
if(eventCode==='' || typeof eventCode==='undefined'){ |
|
112 |
alert('Vous devez indiquer un nom de catégorie.'); |
|
113 |
return; |
|
114 |
} |
|
115 |
// Send query |
|
5
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
116 |
if (sock) { |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
117 |
var new_annot = { |
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
118 |
categories : eventCode, |
6 | 119 |
user : $scope.username |
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 |
sock.send(JSON.stringify(new_annot)); |
6 | 122 |
if(context.logging===true){ |
123 |
log('Sent: ' + JSON.stringify(new_annot)); |
|
124 |
} |
|
5
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
125 |
} else { |
7 | 126 |
alert('La socket ne fonctionne pas.'); |
6 | 127 |
if(context.logging===true){ |
7 | 128 |
log('Not connected.'); |
6 | 129 |
} |
5
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
130 |
} |
1 | 131 |
}; |
132 |
|
|
5
90a7c431b979
first step of working annotation real annotation client
cavaliet
parents:
1
diff
changeset
|
133 |
// Interface management |
1 | 134 |
$scope.selectLevel = function(i, eventCode){ |
135 |
if(i===false){ |
|
136 |
$scope.returnVisStyle = {visibility:'hidden'}; |
|
137 |
$scope.selectedlevel = false; |
|
138 |
return; |
|
139 |
} |
|
140 |
if(typeof $scope.data.categories[i].subcategories!=='undefined' && $scope.data.categories[i].subcategories.length>0){ |
|
141 |
$scope.selectedlevel = $scope.data.categories[i].subcategories; |
|
142 |
$scope.returnVisStyle = {visibility:'show'}; |
|
143 |
} |
|
144 |
else{ |
|
145 |
// Send query |
|
146 |
$scope.sendAnnotation(eventCode); |
|
147 |
} |
|
148 |
}; |
|
149 |
|
|
150 |
}); |
|
151 |
||
152 |
})(); |