Debian Codegraph @

lemonldap-ng-manager/site/js-src/notifications.js

raw symbols SCIP
1 /*
2 * LemonLDAP::NG Notifications Explorer client
3 */
4 var llapp, max, menu, overScheme, scheme;
5
6 // Max number of notifications to display (see overScheme)
7 max = 25;
8
9 scheme = [
10 function(v) {
11 return "groupBy=substr(uid,1)";
12 },
13 function(v) {
14 return `uid=${v}*&groupBy=uid`;
15 },
16 function(v) {
17 return `uid=${v}`;
18 }
19 ];
20
21 // When number of children nodes exceeds "max" value
22 // and does not return "null", a level is added. See
23 // "$scope.updateTree" method
24 overScheme = function(v, level, over) {
25 // "v.length > over" avoids a loop if one user opened more than "max"
26 // notifications
27 console.debug('overScheme => level', level, 'over', over);
28 if (level === 1 && v.length > over) {
29 return `uid=${v}*&groupBy=substr(uid,${level + over + 1})`;
30 } else {
31 return null;
32 }
33 };
34
35 // Session menu
36 menu = {
37 actives: [{
38 title: 'markAsDone',
39 icon: 'check'
40 }],
41 done: [{
42 title: 'deleteNotification',
43 icon: 'trash'
44 }],
45 new: [{
46 title: 'save',
47 icon: 'save'
48 }],
49 home: []
50 };
51
52 // AngularJS application
53 llapp = angular.module('llngNotificationsExplorer', ['ui.tree', 'ui.bootstrap', 'llApp']);
54
55 // Main controller
56 llapp.controller('NotificationsExplorerCtrl', [
57 '$scope',
58 '$translator',
59 '$location',
60 '$q',
61 '$http',
62 '$uibModal',
63 function($scope,
64 $translator,
65 $location,
66 $q,
67 $http,
68 $uibModal) {
69 var autoId,
70 c,
71 dateToString;
72 $scope.links = links;
73 $scope.menulinks = menulinks;
74 $scope.staticPrefix = staticPrefix;
75 $scope.scriptname = scriptname;
76 $scope.formPrefix = formPrefix;
77 $scope.availableLanguages = availableLanguages;
78 $scope.waiting = true;
79 $scope.showM = false;
80 $scope.showT = true;
81 $scope.showForm = false;
82 $scope.data = [];
83 $scope.form = {};
84 $scope.formPost = {};
85 $scope.currentScope = null;
86 $scope.currentNotification = null;
87 $scope.menu = menu;
88 // Import translation functions
89 $scope.translateP = $translator.translateP;
90 $scope.translate = $translator.translate;
91 $scope.translateTitle = function(node) {
92 return $translator.translateField(node,
93 'title');
94 };
95 // Handler menu items
96 $scope.menuClick = function(button) {
97 if (button.popup) {
98 window.open(button.popup);
99 } else {
100 button.action || (button.action = button.title);
101 switch (typeof button.action) {
102 case 'function':
103 button.action($scope.currentNode,
104 $scope);
105 break;
106 case 'string':
107 $scope[button.action]();
108 break;
109 default:
110 console.warn('Unknown action type', typeof button.action);
111 }
112 }
113 return $scope.showM = false;
114 };
115 // Notification management
116 $scope.markAsDone = function() {
117 $scope.waiting = true;
118 return $http.put(`${scriptname}notifications/${$scope.type}/${$scope.currentNotification.uid}_${$scope.currentNotification.reference}`, {
119 done: 1
120 }).then(function(response) {
121 $scope.currentNotification = null;
122 $scope.currentScope.remove();
123 $scope.message = {
124 title: 'notificationDeleted'
125 };
126 $scope.showModal("alert.html");
127 $scope.waiting = false;
128 return $scope.init();
129 },
130 function(response) {
131 $scope.message = {
132 title: 'notificationNotDeleted',
133 message: response.statusText
134 };
135 $scope.showModal("alert.html");
136 $scope.waiting = false;
137 return $scope.init();
138 });
139 };
140 $scope.deleteNotification = function() {
141 $scope.waiting = true;
142 return $http['delete'](`${scriptname}notifications/${$scope.type}/${$scope.currentNotification.uid}_${$scope.currentNotification.reference}_${$scope.currentNotification.done}`).then(function(response) {
143 $scope.currentNotification = null;
144 $scope.currentScope.remove();
145 $scope.message = {
146 title: 'notificationPurged'
147 };
148 $scope.showModal("alert.html");
149 $scope.waiting = false;
150 return $scope.init();
151 },
152 function(response) {
153 $scope.message = {
154 title: 'notificationNotPurged',
155 message: response.statusText
156 };
157 $scope.showModal("alert.html");
158 $scope.waiting = false;
159 return $scope.init();
160 });
161 };
162 // Open node
163 $scope.stoggle = function(scope) {
164 var node;
165 node = scope.$modelValue;
166 if (node.nodes.length === 0) {
167 $scope.updateTree(node.value,
168 node.nodes,
169 node.level,
170 node.over,
171 node.query,
172 node.count);
173 }
174 return scope.toggle();
175 };
176 $scope.notifDate = function(s) {
177 var d;
178 if (s != null) {
179 if (s.match(/(\d{4})-(\d{2})-(\d{2})/)) {
180 s = s.substr(0,
181 4) + s.substr(5,
182 2) + s.substr(8,
183 2);
184 }
185 d = new Date(s.substr(0,
186 4),
187 s.substr(4,
188 2) - 1,
189 s.substr(6,
190 2));
191 return d.toLocaleDateString();
192 }
193 return '';
194 };
195 $scope.getLanguage = function(lang) {
196 $scope.lang = lang;
197 if ($scope.form.date) {
198 $scope.form.date = new Date();
199 } else {
200 $scope.form = 'white';
201 }
202 $scope.init();
203 return $scope.showM = false;
204 };
205 $scope.$on('$locationChangeSuccess',
206 function(event,
207 next,
208 current) {
209 var n;
210 n = next.match(/#!?\/(\w+)/);
211 $scope.type = n != null ? n[1] : 'actives';
212 if ($scope.type === 'new') {
213 return $scope.displayCreateForm();
214 } else {
215 $scope.showForm = false;
216 return $scope.init();
217 }
218 });
219 autoId = 0;
220 $scope.updateTree = function(value,
221 node,
222 level,
223 over,
224 currentQuery,
225 count) {
226 var query,
227 tmp;
228 $scope.waiting = true;
229 query = scheme[level](value,
230 currentQuery);
231 // If number of notifications exceeds "max", call it
232 if (count > max) {
233 if (tmp = overScheme(value,
234 level,
235 over)) {
236 over++;
237 query = tmp;
238 level = level - 1;
239 } else {
240 over = 0;
241 }
242 } else {
243 over = 0;
244 }
245 // Launch HTTP query
246 if ($scope.type === 'done' || $scope.type === 'actives') {
247 $http.get(`${scriptname}notifications/${$scope.type}?${query}`).then(function(response) {
248 var data,
249 i,
250 len,
251 n,
252 ref;
253 data = response.data;
254 if (data.result) {
255 ref = data.values;
256 for (i = 0, len = ref.length; i < len; i++) {
257 n = ref[i];
258 autoId++;
259 n.id = `node${autoId}`;
260 if (level < scheme.length - 1) {
261 n.nodes = [];
262 n.level = level + 1;
263 n.query = query;
264 n.over = over;
265 }
266 node.push(n);
267 }
268 if (value === '') {
269 $scope.total = data.total;
270 }
271 }
272 return $scope.waiting = false;
273 },
274 function(resp) {
275 return $scope.waiting = false;
276 });
277 }
278 // Highlight current selection
279 console.debug("Selection", $scope.type);
280 $scope.activesStyle = {
281 color: '#777'
282 };
283 $scope.doneStyle = {
284 color: '#777'
285 };
286 $scope.newStyle = {
287 color: '#777'
288 };
289 if ($scope.type === 'actives') {
290 $scope.activesStyle = {
291 color: '#333'
292 };
293 }
294 if ($scope.type === 'done') {
295 return $scope.doneStyle = {
296 color: '#333'
297 };
298 }
299 };
300 $scope.displayNotification = function(scope) {
301 var node,
302 notificationId;
303 $scope.waiting = true;
304 $scope.currentScope = scope;
305 node = scope.$modelValue;
306 notificationId = node.notification.replace(/#/g,
307 '_');
308 if ($scope.type === 'actives') {
309 notificationId = `${node.uid}_${node.reference}`;
310 }
311 $http.get(`${scriptname}notifications/${$scope.type}/${notificationId}`).then(function(response) {
312 var notif;
313 $scope.currentNotification = {
314 uid: node.uid,
315 reference: node.reference
316 };
317 if ($scope.type === 'done') {
318 $scope.currentNotification.done = response.data.done;
319 }
320 try {
321 console.debug("Try to parse a JSON formated notification...");
322 notif = JSON.parse(response.data.notifications);
323 $scope.currentNotification.date = $scope.notifDate(notif.date);
324 $scope.currentNotification.condition = notif.condition;
325 $scope.currentNotification.text = notif.text;
326 $scope.currentNotification.title = notif.title;
327 $scope.currentNotification.subtitle = notif.subtitle;
328 $scope.currentNotification.check = notif.check;
329 } catch (error) {
330 console.error("Unable to parse JSON");
331 $scope.currentNotification.notifications = response.data.notifications;
332 }
333 return $scope.waiting = false;
334 },
335 function(resp) {
336 return $scope.waiting = false;
337 });
338 return $scope.showT = false;
339 };
340 // Modal launcher
341 $scope.showModal = function(tpl,
342 init) {
343 var d,
344 modalInstance;
345 modalInstance = $uibModal.open({
346 templateUrl: tpl,
347 controller: 'ModalInstanceCtrl',
348 size: 'lg',
349 resolve: {
350 elem: function() {
351 return function(s) {
352 return $scope[s];
353 };
354 },
355 set: function() {
356 return function(f,
357 s) {
358 return $scope[f] = s;
359 };
360 },
361 init: function() {
362 return init;
363 }
364 }
365 });
366 d = $q.defer();
367 return modalInstance.result.then(function(msgok) {
368 $scope.message = {
369 title: '',
370 message: ''
371 };
372 return d.resolve(msgok);
373 },
374 function(msgnok) {
375 $scope.message = {
376 title: '',
377 message: ''
378 };
379 return d.reject(msgnok);
380 });
381 };
382 $scope.save = function() {
383 if ($scope.form.uid && $scope.form.reference && $scope.form.xml) {
384 $scope.waiting = true;
385 $scope.formPost.uid = $scope.form.uid;
386 if ($scope.form.date) {
387 $scope.formPost.date = dateToString($scope.form.date);
388 }
389 $scope.formPost.reference = $scope.form.reference;
390 $scope.formPost.condition = $scope.form.condition;
391 $scope.formPost.xml = $scope.form.xml;
392 $http.post('notifications/actives',
393 $scope.formPost).then(function(response) {
394 var data;
395 data = response.data;
396 $scope.form = {};
397 if (data.result === 1) {
398 $scope.message = {
399 title: 'notificationCreated'
400 };
401 } else {
402 $scope.message = {
403 title: 'notificationNotCreated',
404 message: data.error
405 };
406 }
407 $scope.showModal("alert.html");
408 $scope.waiting = false;
409 return $scope.form.date = new Date();
410 },
411 function(response) {
412 $scope.message = {
413 title: 'notificationNotCreated',
414 message: response.statusText
415 };
416 $scope.showModal("alert.html");
417 $scope.waiting = false;
418 return $scope.form.date = new Date();
419 });
420 } else {
421 $scope.message = {
422 title: 'incompleteForm'
423 };
424 $scope.showModal("alert.html");
425 }
426 return $scope.form.date = new Date();
427 };
428 $scope.init = function() {
429 $scope.waiting = true;
430 $scope.showM = false;
431 $scope.showT = false;
432 $scope.data = [];
433 $scope.currentScope = null;
434 $scope.currentNotification = null;
435 $q.all([$translator.init($scope.lang),
436 $scope.updateTree('',
437 $scope.data,
438 0,
439 0)
440 ]).then(function() {
441 return $scope.waiting = false;
442 },
443 function(resp) {
444 return $scope.waiting = false;
445 });
446 // Colorized link
447 $scope.activeModule = "notifications";
448 return $scope.myStyle = {
449 color: '#ffb84d'
450 };
451 };
452 $scope.displayCreateForm = function() {
453 $scope.activesStyle = {
454 color: '#777'
455 };
456 $scope.doneStyle = {
457 color: '#777'
458 };
459 $scope.newStyle = {
460 color: '#333'
461 };
462 $scope.waiting = true;
463 return $translator.init($scope.lang).then(function() {
464 $scope.currentNotification = null;
465 $scope.showForm = true;
466 $scope.data = [];
467 $scope.waiting = false;
468 return $scope.form.date = new Date();
469 });
470 };
471 c = $location.path().match(/^\/(\w+)/);
472 $scope.type = c ? c[1] : 'actives';
473 // Datepicker
474 $scope.popupopen = function() {
475 return $scope.popup.opened = true;
476 };
477 $scope.dateOptions = {
478 startingDay: 1,
479 minDate: new Date()
480 };
481 $scope.popup = {
482 opened: false
483 };
484 // Date conversion
485 return dateToString = function(dt) {
486 var day,
487 month,
488 year;
489 year = dt.getFullYear();
490 month = dt.getMonth() + 1;
491 if (month < 10) {
492 month = `0${month}`;
493 }
494 day = dt.getDate();
495 if (day < 10) {
496 day = `0${day}`;
497 }
498 return `${year}-${month}-${day}`;
499 };
500 }
501 ]);