| author | cavaliet |
| Fri, 20 Sep 2013 15:44:11 +0200 | |
| changeset 112 | ba6056f58516 |
| parent 111 | e2ecb345b58f |
| child 117 | 0a4e7d6ebe80 |
| permissions | -rw-r--r-- |
| 63 | 1 |
"use strict"; |
| 26 | 2 |
// initialize the app |
3 |
||
| 47 | 4 |
var app = angular.module("recordApp", ['ngResource', 'ngRoute', 'pascalprecht.translate']) |
| 26 | 5 |
|
|
33
945b144d68c1
put context variables in a single object
ymh <ymh.work@gmail.com>
parents:
28
diff
changeset
|
6 |
app.service("Api", function($resource, context) { |
| 47 | 7 |
this.record = $resource(context.urls.record_api, |
8 |
{}, |
|
9 |
{ |
|
10 |
get: { |
|
11 |
method: "GET", |
|
12 |
isArray: false |
|
13 |
}, |
|
14 |
save:{ |
|
15 |
method:"PUT", |
|
16 |
isArray:false, |
|
|
111
e2ecb345b58f
thesaurus add button enabled only when uri is correct
cavaliet
parents:
88
diff
changeset
|
17 |
headers:{'X-CSRFToken':context.csrf_token} |
| 47 | 18 |
} |
19 |
}); |
|
| 26 | 20 |
}); |
21 |
||
|
111
e2ecb345b58f
thesaurus add button enabled only when uri is correct
cavaliet
parents:
88
diff
changeset
|
22 |
app.service("RecordModel", function(Api, context) { |
| 49 | 23 |
this.record = Api.record.get({recordId: context.record_id}); |
24 |
this.uriLabels = context.uri_labels; |
|
25 |
}); |
|
26 |
||
| 54 | 27 |
|
28 |
app.directive('objectDisp', ['$compile', '$http', '$templateCache', 'context', function($compile, $http, $templateCache, context) { |
|
29 |
||
30 |
var getTemplate = function(templateName) { |
|
31 |
var templateLoader, |
|
32 |
templateUrl = context.urls.base_static+'p4l/templates/'+templateName+'.html'; |
|
33 |
templateLoader = $http.get(templateUrl, {cache: $templateCache}); |
|
34 |
||
35 |
return templateLoader; |
|
36 |
} |
|
37 |
||
38 |
var linker = function(scope, element, attrs) { |
|
39 |
||
40 |
var loader = getTemplate(scope.dispTemplate); |
|
41 |
||
42 |
var promise = loader.success(function(html) { |
|
43 |
element.html(html); |
|
44 |
}).then(function (response) { |
|
45 |
element.replaceWith($compile(element.html())(scope)); |
|
46 |
}); |
|
47 |
} |
|
48 |
||
49 |
return { |
|
50 |
restrict: 'E', |
|
51 |
scope: { |
|
52 |
dispTemplate: "=", |
|
53 |
obj: "=" |
|
54 |
}, |
|
55 |
link: linker |
|
56 |
}; |
|
57 |
}]); |
|
58 |
||
59 |
||
60 |
app.directive('objectList', function(RecordModel, context) { |
|
| 50 | 61 |
return { |
62 |
restrict: 'E', |
|
63 |
replace: true, |
|
64 |
transclude: true, |
|
65 |
scope: { |
|
| 54 | 66 |
list:"=objectList", |
67 |
dispTemplate: "@dispTemplate", |
|
68 |
formTemplate: "@formTemplate", |
|
| 79 | 69 |
objectFields: "@objectFields", |
70 |
labelFields: "@labelFields", |
|
71 |
sizeFields: "@sizeFields", |
|
72 |
table: "@table" |
|
| 54 | 73 |
}, |
74 |
controller: function($scope, $element, $attrs, $transclude) { |
|
| 79 | 75 |
|
| 54 | 76 |
$scope.getStaticTemplateUrl = function(templateName) { |
77 |
return context.urls.base_static+'p4l/templates/'+templateName+".html"; |
|
78 |
} |
|
79 |
$scope.getEmptyObjectFromList = function(fieldList) { |
|
80 |
var res = {}; |
|
81 |
for ( var field in fieldList) { |
|
82 |
res[field] = ""; |
|
83 |
} |
|
84 |
return res; |
|
85 |
} |
|
| 79 | 86 |
}, |
87 |
templateUrl: function(tElement, tAttrs){ |
|
88 |
if(tAttrs.table=="true"){ |
|
89 |
return context.urls.base_static+'p4l/templates/objectListTable.html' |
|
90 |
} |
|
91 |
else{ |
|
92 |
return context.urls.base_static+'p4l/templates/objectList.html' |
|
93 |
} |
|
| 50 | 94 |
}, |
| 54 | 95 |
link: function($scope, $element, $attrs) { |
|
88
1753fcef095a
Improve object list behaviour in edit form
ymh <ymh.work@gmail.com>
parents:
80
diff
changeset
|
96 |
|
|
1753fcef095a
Improve object list behaviour in edit form
ymh <ymh.work@gmail.com>
parents:
80
diff
changeset
|
97 |
// Setup divs table parameters |
|
1753fcef095a
Improve object list behaviour in edit form
ymh <ymh.work@gmail.com>
parents:
80
diff
changeset
|
98 |
$scope.headFields = angular.fromJson($scope.objectFields); |
|
1753fcef095a
Improve object list behaviour in edit form
ymh <ymh.work@gmail.com>
parents:
80
diff
changeset
|
99 |
$scope.headLabels = angular.fromJson($scope.labelFields); |
|
1753fcef095a
Improve object list behaviour in edit form
ymh <ymh.work@gmail.com>
parents:
80
diff
changeset
|
100 |
$scope.headSizes = angular.fromJson($scope.sizeFields); |
|
1753fcef095a
Improve object list behaviour in edit form
ymh <ymh.work@gmail.com>
parents:
80
diff
changeset
|
101 |
if($scope.headSizes==undefined){ |
|
1753fcef095a
Improve object list behaviour in edit form
ymh <ymh.work@gmail.com>
parents:
80
diff
changeset
|
102 |
$scope.headSizes = []; |
|
1753fcef095a
Improve object list behaviour in edit form
ymh <ymh.work@gmail.com>
parents:
80
diff
changeset
|
103 |
var n = $scope.headFields.length; |
|
1753fcef095a
Improve object list behaviour in edit form
ymh <ymh.work@gmail.com>
parents:
80
diff
changeset
|
104 |
for(var i=0;i<n;i++){ |
|
1753fcef095a
Improve object list behaviour in edit form
ymh <ymh.work@gmail.com>
parents:
80
diff
changeset
|
105 |
$scope.headSizes.push(2); |
|
1753fcef095a
Improve object list behaviour in edit form
ymh <ymh.work@gmail.com>
parents:
80
diff
changeset
|
106 |
} |
|
1753fcef095a
Improve object list behaviour in edit form
ymh <ymh.work@gmail.com>
parents:
80
diff
changeset
|
107 |
} |
|
1753fcef095a
Improve object list behaviour in edit form
ymh <ymh.work@gmail.com>
parents:
80
diff
changeset
|
108 |
if($scope.headLabels==undefined){ |
|
1753fcef095a
Improve object list behaviour in edit form
ymh <ymh.work@gmail.com>
parents:
80
diff
changeset
|
109 |
$scope.headLabels = $scope.headFields; |
|
1753fcef095a
Improve object list behaviour in edit form
ymh <ymh.work@gmail.com>
parents:
80
diff
changeset
|
110 |
} |
| 79 | 111 |
|
| 54 | 112 |
$scope.editedObj = null; |
|
88
1753fcef095a
Improve object list behaviour in edit form
ymh <ymh.work@gmail.com>
parents:
80
diff
changeset
|
113 |
$scope.editedIndex = -1; |
| 54 | 114 |
|
115 |
$scope.getEmptyObject = function() { |
|
| 79 | 116 |
return $scope.getEmptyObjectFromList($scope.headFields); |
| 54 | 117 |
}; |
118 |
||
|
88
1753fcef095a
Improve object list behaviour in edit form
ymh <ymh.work@gmail.com>
parents:
80
diff
changeset
|
119 |
$scope.setEditedObject = function(obj, index) { |
|
1753fcef095a
Improve object list behaviour in edit form
ymh <ymh.work@gmail.com>
parents:
80
diff
changeset
|
120 |
|
|
1753fcef095a
Improve object list behaviour in edit form
ymh <ymh.work@gmail.com>
parents:
80
diff
changeset
|
121 |
var newObj = obj; |
|
1753fcef095a
Improve object list behaviour in edit form
ymh <ymh.work@gmail.com>
parents:
80
diff
changeset
|
122 |
if(index>=0) { |
|
1753fcef095a
Improve object list behaviour in edit form
ymh <ymh.work@gmail.com>
parents:
80
diff
changeset
|
123 |
newObj = angular.copy(obj); |
|
1753fcef095a
Improve object list behaviour in edit form
ymh <ymh.work@gmail.com>
parents:
80
diff
changeset
|
124 |
} |
|
1753fcef095a
Improve object list behaviour in edit form
ymh <ymh.work@gmail.com>
parents:
80
diff
changeset
|
125 |
$scope.editedObj = newObj; |
|
1753fcef095a
Improve object list behaviour in edit form
ymh <ymh.work@gmail.com>
parents:
80
diff
changeset
|
126 |
$scope.editedIndex = index; |
| 54 | 127 |
} |
128 |
|
|
129 |
$scope.newEditedObject = function() { |
|
130 |
var newObj = $scope.getEmptyObject(); |
|
|
88
1753fcef095a
Improve object list behaviour in edit form
ymh <ymh.work@gmail.com>
parents:
80
diff
changeset
|
131 |
$scope.setEditedObject(newObj, -1); |
| 54 | 132 |
}; |
133 |
|
|
|
88
1753fcef095a
Improve object list behaviour in edit form
ymh <ymh.work@gmail.com>
parents:
80
diff
changeset
|
134 |
$scope.removeFromList = function(index) { |
|
1753fcef095a
Improve object list behaviour in edit form
ymh <ymh.work@gmail.com>
parents:
80
diff
changeset
|
135 |
if(index>=0){ |
|
1753fcef095a
Improve object list behaviour in edit form
ymh <ymh.work@gmail.com>
parents:
80
diff
changeset
|
136 |
$scope.list.splice(index, 1); |
| 54 | 137 |
} |
|
88
1753fcef095a
Improve object list behaviour in edit form
ymh <ymh.work@gmail.com>
parents:
80
diff
changeset
|
138 |
$scope.setEditedObject(null, -1); |
| 54 | 139 |
} |
140 |
|
|
141 |
$scope.onOk = function() { |
|
|
88
1753fcef095a
Improve object list behaviour in edit form
ymh <ymh.work@gmail.com>
parents:
80
diff
changeset
|
142 |
|
|
1753fcef095a
Improve object list behaviour in edit form
ymh <ymh.work@gmail.com>
parents:
80
diff
changeset
|
143 |
if($scope.editedIndex >= 0) { |
|
1753fcef095a
Improve object list behaviour in edit form
ymh <ymh.work@gmail.com>
parents:
80
diff
changeset
|
144 |
$scope.list[$scope.editedIndex] = $scope.editedObj; |
|
1753fcef095a
Improve object list behaviour in edit form
ymh <ymh.work@gmail.com>
parents:
80
diff
changeset
|
145 |
} |
|
1753fcef095a
Improve object list behaviour in edit form
ymh <ymh.work@gmail.com>
parents:
80
diff
changeset
|
146 |
else { |
|
1753fcef095a
Improve object list behaviour in edit form
ymh <ymh.work@gmail.com>
parents:
80
diff
changeset
|
147 |
$scope.list.push($scope.editedObj); |
|
1753fcef095a
Improve object list behaviour in edit form
ymh <ymh.work@gmail.com>
parents:
80
diff
changeset
|
148 |
} |
|
1753fcef095a
Improve object list behaviour in edit form
ymh <ymh.work@gmail.com>
parents:
80
diff
changeset
|
149 |
|
|
1753fcef095a
Improve object list behaviour in edit form
ymh <ymh.work@gmail.com>
parents:
80
diff
changeset
|
150 |
$scope.setEditedObject(null, -1); |
|
1753fcef095a
Improve object list behaviour in edit form
ymh <ymh.work@gmail.com>
parents:
80
diff
changeset
|
151 |
|
| 54 | 152 |
} |
| 50 | 153 |
|
|
88
1753fcef095a
Improve object list behaviour in edit form
ymh <ymh.work@gmail.com>
parents:
80
diff
changeset
|
154 |
$scope.onCancel = function() { |
|
1753fcef095a
Improve object list behaviour in edit form
ymh <ymh.work@gmail.com>
parents:
80
diff
changeset
|
155 |
$scope.setEditedObject(null, -1); |
|
1753fcef095a
Improve object list behaviour in edit form
ymh <ymh.work@gmail.com>
parents:
80
diff
changeset
|
156 |
} |
| 50 | 157 |
} |
158 |
}; |
|
159 |
}); |
|
160 |
||
| 54 | 161 |
|
| 60 | 162 |
app.directive('addSemUri', function(RecordModel, context, $timeout){ |
| 51 | 163 |
return { |
164 |
restrict: 'E', |
|
165 |
replace: true, |
|
166 |
transclude: true, |
|
167 |
scope: { |
|
| 57 | 168 |
listname:"@", |
169 |
list:"=", |
|
| 61 | 170 |
placeholder:"@", |
| 51 | 171 |
}, |
172 |
templateUrl: function(tElement, tAttrs) { |
|
173 |
return context.urls.base_static+'p4l/templates/addSemanticUriForm.html'; |
|
174 |
}, |
|
| 52 | 175 |
link: function($scope, $element, $attrs) { |
176 |
// Get queries attributes from $scope listname and context query dict |
|
| 63 | 177 |
var attr_dict = context.query_dicts[$scope.listname]; |
| 52 | 178 |
for (var k in attr_dict){ |
| 51 | 179 |
if (attr_dict.hasOwnProperty(k)) { |
| 52 | 180 |
$scope[k] = attr_dict[k]; |
| 51 | 181 |
} |
| 52 | 182 |
} |
| 67 | 183 |
$scope.formVisible = false; |
| 52 | 184 |
// initalize autocomplete and browse thesaurus events |
| 60 | 185 |
// We have to timeout because init_browse needs the real ids and not {{ $id }} |
186 |
// NB : scope.apply generates bug |
|
187 |
$timeout(function(){ |
|
188 |
init_autocomplete(); |
|
189 |
init_browse(); |
|
190 |
}, 0); |
|
| 52 | 191 |
}, |
192 |
controller: function($scope, $element, $attrs, $transclude, RecordModel){ |
|
193 |
$scope.record = RecordModel.record; |
|
194 |
$scope.uriLabels = RecordModel.uriLabels; |
|
195 |
$scope.addUriText = ''; |
|
|
111
e2ecb345b58f
thesaurus add button enabled only when uri is correct
cavaliet
parents:
88
diff
changeset
|
196 |
$scope.uriInDict = false; |
| 52 | 197 |
|
198 |
$scope.addUriToList = function() { |
|
| 57 | 199 |
$scope.list.push($scope.addUriText); |
| 52 | 200 |
$scope.addUriText = ''; |
201 |
}; |
|
| 60 | 202 |
$scope.removeFromList = function(obj) { |
203 |
var i = $scope.list.indexOf(obj); |
|
204 |
if(i>=0){ |
|
205 |
$scope.list.splice(i, 1); |
|
206 |
} |
|
207 |
} |
|
| 52 | 208 |
|
209 |
$scope.updateUriLabelDict = function(k,v) { |
|
210 |
$scope.uriLabels[k] = v; |
|
211 |
}; |
|
|
111
e2ecb345b58f
thesaurus add button enabled only when uri is correct
cavaliet
parents:
88
diff
changeset
|
212 |
$scope.$watch("addUriText", function(newValue, oldValue){ |
|
e2ecb345b58f
thesaurus add button enabled only when uri is correct
cavaliet
parents:
88
diff
changeset
|
213 |
$scope.uriInDict = (($scope.dataquery!='') && ($scope.addUriText in $scope.uriLabels)); |
|
e2ecb345b58f
thesaurus add button enabled only when uri is correct
cavaliet
parents:
88
diff
changeset
|
214 |
}); |
| 51 | 215 |
} |
216 |
} |
|
| 55 | 217 |
}); |
218 |
||
| 63 | 219 |
app.directive('simpleSemUri', function(RecordModel, context, $timeout) { |
220 |
return { |
|
221 |
restrict: 'E', |
|
222 |
replace: true, |
|
223 |
transclude: true, |
|
224 |
scope: { |
|
225 |
listname:"@", |
|
226 |
val:"=", |
|
227 |
placeholder:"@", |
|
228 |
}, |
|
229 |
templateUrl: function(tElement, tAttrs) { |
|
230 |
return context.urls.base_static+'p4l/templates/simpleSemanticUriForm.html'; |
|
231 |
}, |
|
232 |
link: function($scope, $element, $attrs) { |
|
233 |
// Get queries attributes from $scope listname and context query dict |
|
234 |
var attr_dict = context.query_dicts[$scope.listname]; |
|
235 |
for (var k in attr_dict){ |
|
236 |
if (attr_dict.hasOwnProperty(k)) { |
|
237 |
$scope[k] = attr_dict[k]; |
|
238 |
} |
|
239 |
} |
|
| 67 | 240 |
$scope.formVisible = false; |
| 63 | 241 |
// initalize autocomplete and browse thesaurus events |
242 |
// We have to timeout because init_browse needs the real ids and not {{ $id }} |
|
243 |
// NB : scope.apply generates bug |
|
244 |
$timeout(function(){ |
|
245 |
init_autocomplete(); |
|
246 |
init_browse(); |
|
247 |
}, 0); |
|
248 |
}, |
|
249 |
controller: function($scope, $element, $attrs, $transclude, RecordModel) { |
|
250 |
$scope.record = RecordModel.record; |
|
251 |
$scope.uriLabels = RecordModel.uriLabels; |
|
252 |
$scope.addUriText = ''; |
|
|
111
e2ecb345b58f
thesaurus add button enabled only when uri is correct
cavaliet
parents:
88
diff
changeset
|
253 |
$scope.uriInDict = false; |
| 63 | 254 |
|
255 |
$scope.updateVal = function() { |
|
256 |
$scope.val = $scope.addUriText; |
|
257 |
}; |
|
258 |
||
259 |
$scope.updateUriLabelDict = function(k,v) { |
|
260 |
$scope.uriLabels[k] = v; |
|
261 |
}; |
|
|
111
e2ecb345b58f
thesaurus add button enabled only when uri is correct
cavaliet
parents:
88
diff
changeset
|
262 |
$scope.$watch("addUriText", function(newValue, oldValue){ |
|
e2ecb345b58f
thesaurus add button enabled only when uri is correct
cavaliet
parents:
88
diff
changeset
|
263 |
$scope.uriInDict = (($scope.dataquery!='') && ($scope.addUriText in $scope.uriLabels)); |
|
e2ecb345b58f
thesaurus add button enabled only when uri is correct
cavaliet
parents:
88
diff
changeset
|
264 |
}); |
| 63 | 265 |
} |
266 |
}; |
|
267 |
}); |
|
|
59
a0ef3043b1d2
Merge with cae920b543068c169d5d09fdbb2fd7c4eac7e147 and enhance add sem directive
cavaliet
diff
changeset
|
268 |
|
| 70 | 269 |
app.directive('languagesListInput', function(RecordModel, context) { |
270 |
return { |
|
271 |
restrict: 'E', |
|
272 |
replace: true, |
|
273 |
transclude: true, |
|
274 |
scope: { |
|
275 |
obj:"=", |
|
276 |
}, |
|
277 |
templateUrl: function(tElement, tAttrs) { |
|
278 |
return context.urls.base_static+'p4l/templates/languagesListInput.html'; |
|
279 |
}, |
|
280 |
link: function($scope, $element, $attrs) { |
|
281 |
// Get list from context languages_list |
|
282 |
$scope.list = context.languages_list; |
|
283 |
} |
|
284 |
}; |
|
285 |
}); |
|
286 |
||
| 112 | 287 |
app.controller("RecordCtrl", function($translate, $scope, RecordModel, context){ |
| 55 | 288 |
|
289 |
$scope.record = RecordModel.record; |
|
| 71 | 290 |
$scope.uriLabels = RecordModel.uriLabels; |
291 |
|
|
292 |
$scope.saving = false; |
|
| 55 | 293 |
|
294 |
$scope.submitRecord = function() { |
|
| 71 | 295 |
$scope.saving = true; |
| 112 | 296 |
$scope.record.$save( |
297 |
{ recordId: context.record_id }, |
|
298 |
function(){ |
|
299 |
//console.log("SUCCESS"); |
|
300 |
}, |
|
301 |
function(){ |
|
302 |
alert($translate("An error occured. Somes datas may be incorrect or incomplete.")); |
|
303 |
$scope.saving=false; |
|
304 |
}) |
|
| 74 | 305 |
.then(function(response) { |
306 |
$scope.saving=false; |
|
307 |
}); |
|
| 55 | 308 |
} |
309 |
|
|
| 74 | 310 |
$scope.getPreviousUrl = function() { |
311 |
return context.urls.previous || context.urls.home; |
|
| 80 | 312 |
} |
| 55 | 313 |
|
314 |
}); |
|
| 51 | 315 |
|
| 26 | 316 |
app.config(['$routeProvider', function($routeProvider) { |
317 |
// $routeProvider.when('/', {controller: 'RecordCtrl', templateUrl: 'partials/record.html'}); |
|
318 |
// $routeProvider.otherwise({redirectTo: '/'}); |
|
319 |
}]); |
|
|
28
f26426e9360b
update view simplification. Baby step towards complete form.
ymh <ymh.work@gmail.com>
parents:
26
diff
changeset
|
320 |