Debian Codegraph @

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

raw symbols SCIP
1 /*
2 LemonLDAP::NG Viewer client
3
4 This is the main app file. Other are:
5 - struct.json and js/confTree.js that contains the full tree
6 - translate.json that contains the keywords translation
7
8 This file contains:
9 - the AngularJS controller
10 */
11 var llapp;
12
13 llapp = angular.module('llngViewer', ['ui.tree', 'ui.bootstrap', 'llApp', 'ngCookies']);
14
15 /*
16 Main AngularJS controller
17 */
18 llapp.controller('TreeCtrl', [
19 '$scope',
20 '$http',
21 '$location',
22 '$q',
23 '$uibModal',
24 '$translator',
25 '$cookies',
26 '$htmlParams',
27 '$timeout',
28 function($scope,
29 $http,
30 $location,
31 $q,
32 $uibModal,
33 $translator,
34 $cookies,
35 $htmlParams,
36 $timeout) {
37 var _download,
38 _stoggle,
39 c,
40 id,
41 pathEvent,
42 readError,
43 setHelp;
44 $scope.links = window.links;
45 $scope.menu = $htmlParams.menu;
46 $scope.menulinks = window.menulinks;
47 $scope.staticPrefix = window.staticPrefix;
48 $scope.formPrefix = window.formPrefix;
49 $scope.availableLanguages = window.availableLanguages;
50 $scope.waiting = true;
51 $scope.showM = false;
52 $scope.showT = false;
53 $scope.form = 'homeViewer';
54 $scope.currentCfg = {};
55 $scope.clipboardAvailable = Boolean(navigator.clipboard);
56 $scope.viewPrefix = window.viewPrefix;
57 $scope.allowDiff = window.allowDiff;
58 $scope.message = {};
59 $scope.result = '';
60 // Import translations functions
61 $scope.translateTitle = function(node) {
62 return $translator.translateField(node,
63 'title');
64 };
65 $scope.translateP = $translator.translateP;
66 $scope.translate = $translator.translate;
67 // HELP DISPLAY
68 $scope.helpUrl = 'start.html#configuration';
69 $scope.setShowHelp = function(val) {
70 var d;
71 if (val == null) {
72 val = !$scope.showH;
73 }
74 $scope.showH = val;
75 d = new Date(Date.now());
76 d.setFullYear(d.getFullYear() + 1);
77 return $cookies.put('showhelp',
78 (val ? 'true' : 'false'), {
79 "expires": d
80 });
81 };
82 $scope.showH = $cookies.get('showhelp') === 'false' ? false : true;
83 if ($scope.showH == null) {
84 $scope.setShowHelp(true);
85 }
86 // INTERCEPT AJAX ERRORS
87 readError = function(response) {
88 var e,
89 j;
90 e = response.status;
91 j = response.statusLine;
92 $scope.waiting = false;
93 if (e === 403) {
94 $scope.message = {
95 title: 'forbidden',
96 message: '',
97 items: []
98 };
99 } else if (e === 401) {
100 console.debug('Authentication needed');
101 $scope.message = {
102 title: 'authenticationNeeded',
103 message: '__waitOrF5__',
104 items: []
105 };
106 } else if (e === 400) {
107 $scope.message = {
108 title: 'badRequest',
109 message: j,
110 items: []
111 };
112 } else if (e > 0) {
113 $scope.message = {
114 title: 'badRequest',
115 message: j,
116 items: []
117 };
118 } else {
119 $scope.message = {
120 title: 'networkProblem',
121 message: '',
122 items: []
123 };
124 }
125 return $scope.showModal('message.html');
126 };
127 // Modal launcher
128 $scope.showModal = function(tpl,
129 init) {
130 var d,
131 modalInstance;
132 modalInstance = $uibModal.open({
133 templateUrl: tpl,
134 controller: 'ModalInstanceCtrl',
135 size: 'lg',
136 resolve: {
137 elem: function() {
138 return function(s) {
139 return $scope[s];
140 };
141 },
142 set: function() {
143 return function(f,
144 s) {
145 return $scope[f] = s;
146 };
147 },
148 init: function() {
149 return init;
150 }
151 }
152 });
153 d = $q.defer();
154 modalInstance.result.then(function(msgok) {
155 $scope.message = {
156 title: '',
157 message: '',
158 items: []
159 };
160 return d.resolve(msgok);
161 },
162 function(msgnok) {
163 $scope.message = {
164 title: '',
165 message: '',
166 items: []
167 };
168 return d.reject(msgnok);
169 });
170 return d.promise;
171 };
172 // FORM DISPLAY FUNCTIONS
173
174 // Function called when a menu item is selected. It launch function stored in
175 // "action" or "title"
176 $scope.menuClick = function(button) {
177 if (button.popup) {
178 window.open(button.popup);
179 } else {
180 if (!button.action) {
181 button.action = button.title;
182 }
183 switch (typeof button.action) {
184 case 'function':
185 button.action($scope.currentNode,
186 $scope);
187 break;
188 case 'string':
189 $scope[button.action]();
190 break;
191 default:
192 console.warn('Unknown action type', typeof button.action);
193 }
194 }
195 return $scope.showM = false;
196 };
197 // Display main form
198 $scope.home = function() {
199 $scope.form = 'homeViewer';
200 return $scope.showM = false;
201 };
202 // Download raw conf
203 $scope.downloadConf = function() {
204 return window.open($scope.viewPrefix + $scope.currentCfg.cfgNum + '?full=1');
205 };
206 // NODES MANAGEMENT
207 id = 1;
208 $scope._findContainer = function() {
209 return $scope._findScopeContainer().$modelValue;
210 };
211 $scope._findScopeContainer = function() {
212 var cs;
213 cs = $scope.currentScope;
214 while (!cs.$modelValue.type.match(/Container$/)) {
215 cs = cs.$parentNodeScope;
216 }
217 return cs;
218 };
219 $scope._findScopeByKey = function(k) {
220 var cs;
221 cs = $scope.currentScope;
222 while (!(cs.$modelValue.title === k)) {
223 cs = cs.$parentNodeScope;
224 }
225 return cs;
226 };
227 $scope.down = function() {
228 var i,
229 ind,
230 l,
231 len,
232 n,
233 p,
234 ref,
235 tmp;
236 id = $scope.currentNode.id;
237 p = $scope.currentScope.$parentNodeScope.$modelValue;
238 ind = p.nodes.length;
239 ref = p.nodes;
240 for (i = l = 0, len = ref.length; l < len; i = ++l) {
241 n = ref[i];
242 if (n.id === id) {
243 ind = i;
244 }
245 }
246 if (ind < p.nodes.length - 1) {
247 tmp = p.nodes[ind];
248 p.nodes[ind] = p.nodes[ind + 1];
249 p.nodes[ind + 1] = tmp;
250 }
251 return ind;
252 };
253 $scope.up = function() {
254 var i,
255 ind,
256 l,
257 len,
258 n,
259 p,
260 ref,
261 tmp;
262 id = $scope.currentNode.id;
263 p = $scope.currentScope.$parentNodeScope.$modelValue;
264 ind = -1;
265 ref = p.nodes;
266 for (i = l = 0, len = ref.length; l < len; i = ++l) {
267 n = ref[i];
268 if (n.id === id) {
269 ind = i;
270 }
271 }
272 if (ind > 0) {
273 tmp = p.nodes[ind];
274 p.nodes[ind] = p.nodes[ind - 1];
275 p.nodes[ind - 1] = tmp;
276 }
277 return ind;
278 };
279 // test if value is in select
280 $scope.inSelect = function(value) {
281 var l,
282 len,
283 n,
284 ref;
285 ref = $scope.currentNode.select;
286 for (l = 0, len = ref.length; l < len; l++) {
287 n = ref[l];
288 if (n.k === value) {
289 return true;
290 }
291 }
292 return false;
293 };
294 // This is for rule form: title = comment if defined, else title = re
295 $scope.changeRuleTitle = function(node) {
296 return node.title = node.comment.length > 0 ? node.comment : node.re;
297 };
298 // Node opening
299
300 // authParams mechanism: show used auth modules only (launched by stoggle)
301 $scope.filters = {};
302 $scope.execFilters = function(scope) {
303 var filter,
304 func,
305 ref;
306 scope = scope ? scope : $scope;
307 ref = $scope.filters;
308 for (filter in ref) {
309 func = ref[filter];
310 if ($scope.filters.hasOwnProperty(filter)) {
311 return window.filterFunctions[filter](scope,
312 $q,
313 func);
314 }
315 }
316 return false;
317 };
318 // To avoid binding all the tree, nodes are pushed in DOM only when opened
319 $scope.stoggle = function(scope) {
320 var node;
321 node = scope.$modelValue;
322 _stoggle(node);
323 return scope.toggle();
324 };
325 _stoggle = function(node) {
326 var a,
327 l,
328 len,
329 len1,
330 len2,
331 m,
332 n,
333 o,
334 ref,
335 ref1,
336 ref2;
337 ref = ['nodes', 'nodes_cond'];
338 for (l = 0, len = ref.length; l < len; l++) {
339 n = ref[l];
340 if (node[`_${n}`]) {
341 node[n] = [];
342 ref1 = node[`_${n}`];
343 for (m = 0, len1 = ref1.length; m < len1; m++) {
344 a = ref1[m];
345 node[n].push(a);
346 }
347 delete node[`_${n}`];
348 }
349 }
350 // Call execFilter for authParams
351 if (node._nodes_filter) {
352 if (node.nodes) {
353 ref2 = node.nodes;
354 for (o = 0, len2 = ref2.length; o < len2; o++) {
355 n = ref2[o];
356 n.onChange = $scope.execFilters;
357 }
358 }
359 $scope.filters[node._nodes_filter] = node;
360 return $scope.execFilters();
361 }
362 };
363 // Simple toggle management
364 $scope.toggle = function(scope) {
365 return scope.toggle();
366 };
367 // cnodes management: hash keys/values are loaded when parent node is opened
368 $scope.download = function(scope) {
369 var node;
370 node = scope.$modelValue;
371 return _download(node);
372 };
373 _download = function(node) {
374 var d,
375 uri;
376 d = $q.defer();
377 d.notify('Trying to get datas');
378 $scope.waiting = true;
379 console.debug(`Trying to get key ${node.cnodes}`);
380 uri = encodeURI(node.cnodes);
381 $http.get(`${window.viewPrefix}${$scope.currentCfg.cfgNum}/${uri}`).then(function(response) {
382 var a,
383 data,
384 l,
385 len;
386 data = response.data;
387 // Manage datas errors
388 if (!data) {
389 d.reject('Empty response from server');
390 } else if (data.error) {
391 if (data.error.match(/setDefault$/)) {
392 if (node['default']) {
393 node.nodes = node['default'].slice(0);
394 } else {
395 node.nodes = [];
396 }
397 delete node.cnodes;
398 d.resolve('Set data to default value');
399 } else {
400 d.reject(`Server return an error: ${data.error}`);
401 }
402 } else {
403 // Store datas
404 delete node.cnodes;
405 if (!node.type) {
406 node.type = 'keyTextContainer';
407 }
408 node.nodes = [];
409 // TODO: try/catch
410 for (l = 0, len = data.length; l < len; l++) {
411 a = data[l];
412 if (a.template) {
413 a._nodes = templates(a.template,
414 a.title);
415 }
416 node.nodes.push(a);
417 }
418 d.resolve('OK');
419 }
420 return $scope.waiting = false;
421 },
422 function(response) {
423 readError(response);
424 return d.reject('');
425 });
426 return d.promise;
427 };
428 $scope.openCnode = function(scope) {
429 return $scope.download(scope).then(function() {
430 return scope.toggle();
431 });
432 };
433 $scope.copyPath = function() {
434 var text = $scope.breadCrumb.join(" ยป ");
435 navigator.clipboard.writeText(text).then(function () {
436 $scope.$apply(function () {
437 $scope.copySuccess = true;
438 $timeout(function() {
439 $scope.copySuccess = false;
440 }, 400);
441 });
442 });
443 };
444 setHelp = function(scope) {
445 while (!scope.$modelValue.help && scope.$parentNodeScope) {
446 scope = scope.$parentNodeScope;
447 }
448 return $scope.helpUrl = scope.$modelValue.help || 'start.html#configuration';
449 };
450 // Form management
451
452 // `currentNode` contains the last select node
453
454 // method `diplayForm()`:
455 // - set the `form` property to the name of the form to download
456 // (`text` by default or `home` for node without `type` property)
457 // - launch getKeys to set `node.data`
458 // - hide tree when in XS size
459
460 $scope.getTrPath = function(scope) {
461 var path = [];
462 var trpath = [];
463 var current = scope;
464 var safetycount = 0;
465 while (current && current.$modelValue && safetycount < 100) {
466 safetycount = safetycount + 1;
467 if (current.$modelValue.title && current.$modelValue.title != path[0]) {
468 trpath.unshift(scope.translate(current.$modelValue.title));
469 path.unshift(current.$modelValue.title);
470 }
471 current = current.$parent;
472 }
473 return trpath;
474 };
475
476 $scope.displayForm = function(scope) {
477 var f,
478 l,
479 len,
480 n,
481 node,
482 ref;
483 node = scope.$modelValue;
484 if (node.cnodes) {
485 $scope.download(scope);
486 }
487 if (node._nodes) {
488 $scope.stoggle(scope);
489 }
490 $scope.currentNode = node;
491 $scope.currentScope = scope;
492 f = node.type ? node.type : 'text';
493 if (node.nodes || node._nodes || node.cnodes) {
494 $scope.form = f !== 'text' ? f : 'mini';
495 } else {
496 $scope.form = f;
497 // Get datas
498 $scope.getKey(node);
499 }
500 if (node.type && node.type === 'simpleInputContainer') {
501 ref = node.nodes;
502 for (l = 0, len = ref.length; l < len; l++) {
503 n = ref[l];
504 $scope.getKey(n);
505 }
506 }
507 $scope.showT = false;
508 $scope.breadCrumb = $scope.getTrPath(scope);
509 return setHelp(scope);
510 };
511 $scope.keyWritable = function(scope) {
512 var node;
513 node = scope.$modelValue;
514 // regexp-assemble of:
515 // authChoice
516 // cmbModule
517 // keyText
518 // menuApp
519 // menuCat
520 // rule
521 // samlAttribute
522 // samlIDPMetaDataNode
523 // samlSPMetaDataNode
524 // sfExtra
525 // virtualHost
526 if (node.type && node.type.match(/^(?:s(?:aml(?:(?:ID|S)PMetaDataNod|Attribut)e|fExtra)|(?:(?:cmbMod|r)ul|authChoic)e|(?:virtualHos|keyTex)t|menu(?:App|Cat))$/)) {
527 return true;
528 } else {
529 return false;
530 }
531 };
532 // method `getKey()`:
533 // - return a promise with the data:
534 // - from node when set
535 // - after downloading else
536
537 $scope.getKey = function(node) {
538 var d,
539 i,
540 l,
541 len,
542 n,
543 ref,
544 tmp,
545 uri;
546 d = $q.defer();
547 if (!node.data) {
548 $scope.waiting = true;
549 if (node.get && typeof node.get === 'object') {
550 node.data = [];
551 tmp = [];
552 ref = node.get;
553 for (i = l = 0, len = ref.length; l < len; i = ++l) {
554 n = ref[i];
555 node.data[i] = {
556 title: n.split("/").pop(),
557 get: n,
558 id: n
559 };
560 tmp.push($scope.getKey(node.data[i]));
561 }
562 $q.all(tmp).then(function() {
563 return d.resolve(node.data);
564 },
565 function(response) {
566 d.reject(response.statusLine);
567 return $scope.waiting = false;
568 });
569 } else {
570 uri = '';
571 if (node.get) {
572 console.debug(`Trying to get key ${node.get}`);
573 uri = encodeURI(node.get);
574 } else {
575 console.debug(`Trying to get title ${node.title}`);
576 }
577 $http.get(`${window.viewPrefix}${$scope.currentCfg.cfgNum}/${node.get ? uri : node.title}`).then(function(response) {
578 var data;
579 // Set default value if response is null or if asked by server
580 data = response.data;
581 if ((data.value === null || (data.error && data.error.match(/setDefault$/))) && node['default'] !== null) {
582 node.data = node['default'];
583 } else {
584 node.data = data.value;
585 }
586 if (node.data && node.data.toString().match(/_Hidden_$/)) {
587 node.type = 'text';
588 node.data = '######';
589 }
590 // Cast int as int (remember that booleans are int for Perl)
591 if (node.type && node.type.match(/^(bool|trool|boolOrExpr)$/)) {
592 if (typeof node.data === 'string' && node.data.match(/^(?:-1|0|1)$/)) {
593 node.data = parseInt(node.data,
594 10);
595 }
596 }
597 if (node.type && node.type.match(/^int$/)) {
598 node.data = parseInt(node.data,
599 10);
600 // Split SAML types
601 } else if (node.type && node.type.match(/^(saml(Service|Assertion)|blackWhiteList)$/) && !(typeof node.data === 'object')) {
602 node.data = node.data.split(';');
603 }
604 $scope.waiting = false;
605 return d.resolve(node.data);
606 },
607 function(response) {
608 readError(response);
609 return d.reject(response.status);
610 });
611 }
612 } else {
613 if (node.data.toString().match(/_Hidden_$/)) {
614 node.type = 'text';
615 node.data = '######';
616 }
617 d.resolve(node.data);
618 }
619 return d.promise;
620 };
621 // function `pathEvent(event, next; current)`:
622 // Called when $location.path() change, launch getCfg() with the new
623 // configuration number
624 pathEvent = function(event,
625 next,
626 current) {
627 var n;
628 n = next.match(new RegExp('#!?/view/(latest|[0-9]+)'));
629 if (n === null) {
630 return $location.path('/view/latest');
631 } else {
632 console.debug(`Trying to get cfg number ${n[1]}`);
633 return $scope.getCfg(n[1]);
634 }
635 };
636 $scope.$on('$locationChangeSuccess',
637 pathEvent);
638 // function `getCfg(n)`:
639 // Download configuration metadatas
640 $scope.getCfg = function(n) {
641 if ($scope.currentCfg.cfgNum !== n) {
642 return $http.get(`${window.viewPrefix}${n}`).then(function(response) {
643 var d;
644 $scope.currentCfg = response.data;
645 d = new Date($scope.currentCfg.cfgDate * 1000);
646 $scope.currentCfg.date = d.toLocaleString();
647 console.debug(`Metadatas of cfg ${n} loaded`);
648 $location.path(`/view/${n}`);
649 return $scope.init();
650 },
651 function(response) {
652 return readError(response).then(function() {
653 $scope.currentCfg.cfgNum = 0;
654 return $scope.init();
655 });
656 });
657 } else {
658 return $scope.waiting = false;
659 }
660 };
661 // method `getLanguage(lang)`
662 // Launch init() after setting current language
663 $scope.getLanguage = function(lang) {
664 $scope.lang = lang;
665 // Force reload home
666 $scope.form = 'white';
667 $scope.init();
668 return $scope.showM = false;
669 };
670 // Initialization
671
672 // Load JSON files:
673 // - struct.json: the main tree
674 // - languages/<lang>.json: the chosen language datas
675 $scope.init = function() {
676 var tmp;
677 tmp = null;
678 $scope.waiting = true;
679 $scope.breadCrumb = null;
680 $scope.data = [];
681 $scope.confirmNeeded = false;
682 $scope.forceSave = false;
683 $q.all([
684 $translator.init($scope.lang),
685 $http.get(`${window.staticPrefix}struct.json`).then(function(response) {
686 tmp = response.data;
687 console.debug("Structure loaded");
688 })
689 ]).then(function() {
690 console.debug("Starting structure binding");
691 $scope.data = tmp;
692 tmp = null;
693 if ($scope.currentCfg.cfgNum !== 0) {
694 setScopeVars($scope);
695 } else {
696 $scope.message = {
697 title: 'emptyConf',
698 message: '__zeroConfExplanations__'
699 };
700 $scope.showModal('message.html');
701 }
702 $scope.form = 'homeViewer';
703 return $scope.waiting = false;
704 },
705 readError);
706 // Colorized link
707 $scope.activeModule = "viewer";
708 return $scope.myStyle = {
709 color: '#ffb84d'
710 };
711 };
712 c = $location.path().match(new RegExp('^/view/(latest|[0-9]+)'));
713 if (!c) {
714 console.debug("Redirecting to /view/latest");
715 return $location.path('/view/latest');
716 }
717 }
718 ]);