Debian Codegraph @

lemonldap-ng-manager/site/htdocs/static/js/notifications.js

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