Debian Codegraph @

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

raw symbols SCIP
1 /*
2 LemonLDAP::NG base app module
3
4 This file contains:
5 - 3 AngularJS directives (HTML attributes):
6 * `on-read-file` to get file content
7 * `resizer` to resize HTML div
8 * `trspan` to set translated message in HTML content
9 - a AngularJS factory to handle 401 Ajax responses
10 */
11 var llapp;
12
13 llapp = angular.module('llApp', ['ngAria']);
14
15 // TRANSLATION SYSTEM
16
17 // It provides:
18 // - 3 functions to translate:
19 // * translate(word)
20 // * translateP(paragraph): only __words__ are translated
21 // * translateField(object, property)
22 // - an HTML attribute called 'trspan'. Example: <h3 trspan="portal"/>
23
24 // $translator provider
25 llapp.provider('$translator', function() {
26 var al, c, j, k, langs, langs2, len, len1, nl, nlangs, re, ref, res;
27 res = {};
28 // Language detection
29 c = decodeURIComponent(document.cookie);
30 if (c.match(/llnglanguage=(\w+)/)) {
31 res.lang = RegExp.$1;
32 } else if (navigator) {
33 langs = [];
34 langs2 = [];
35 nlangs = [navigator.language];
36 if (navigator.languages) {
37 nlangs = navigator.languages;
38 }
39 for (j = 0, len = nlangs.length; j < len; j++) {
40 nl = nlangs[j];
41 console.debug('Navigator lang', nl);
42 ref = window.availableLanguages;
43 for (k = 0, len1 = ref.length; k < len1; k++) {
44 al = ref[k];
45 console.debug(' Available lang', al);
46 re = new RegExp('^' + al + '-?');
47 if (nl.match(re)) {
48 console.debug(' Matching lang =', al);
49 langs.push(al);
50 } else if (al.substring(0, 1) === nl.substring(0, 1)) {
51 langs2.push(al);
52 }
53 }
54 }
55 res.lang = langs[0] ? langs[0] : langs2[0] ? langs2[0] : 'en';
56 } else {
57 res.lang = 'en';
58 }
59 console.debug('Selected lang ->', res.lang);
60 // Internal properties
61 res.deferredTr = [];
62 res.translationFields = {};
63 // Translation methods
64 // 1. word translation
65 res.translate = function(s) {
66 if (res.translationFields[s]) {
67 s = res.translationFields[s];
68 }
69 return s;
70 };
71 // 2. node field translation
72 res.translateField = function(node, field) {
73 return res.translate(node[field]);
74 };
75 // 3. paragraph translation (verify that json is available
76 res.translateP = function(s) {
77 if (s && res.translationFields.portal) {
78 s = s.replace(/__(\w+)__/g, function(match, w) {
79 return res.translate(w);
80 });
81 }
82 return s;
83 };
84 // Initialization
85 this.$get = [
86 '$q',
87 '$http',
88 function($q,
89 $http) {
90 res.last = '';
91 res.init = function(lang) {
92 var d;
93 if (!lang) {
94 lang = res.lang;
95 }
96 d = new Date();
97 d.setTime(d.getTime() + 30 * 86400000);
98 document.cookie = `llnglanguage=${lang}; expires=${d.toUTCString()}; path=/`;
99 d = $q.defer();
100 if (res.last !== lang) {
101 res.last = lang;
102 $http.get(`${window.staticPrefix}languages/${lang}.json`).then(function(response) {
103 var h,
104 l,
105 len2,
106 ref1;
107 res.translationFields = response.data;
108 ref1 = res.deferredTr;
109 for (l = 0, len2 = ref1.length; l < len2; l++) {
110 h = ref1[l];
111 h.e[h.f](res.translationFields[h.m]);
112 }
113 res.deferredTr = [];
114 return d.resolve("Translation files loaded");
115 },
116 function(response) {
117 return d.reject('');
118 });
119 } else {
120 d.resolve("No change");
121 }
122 return d.promise;
123 };
124 return res;
125 }
126 ];
127 return this;
128 });
129
130 // Translation directive (HTML trspan tag)
131 llapp.directive('trspan', [
132 '$translator',
133 function($translator) {
134 return {
135 restrict: 'A',
136 replace: false,
137 transclude: true,
138 scope: {
139 trspan: "@"
140 },
141 link: function(scope,
142 elem,
143 attr) {
144 if ($translator.translationFields.portal) {
145 attr.trspan = $translator.translate(attr.trspan);
146 } else {
147 // Deferred translations will be done after JSON download
148 $translator.deferredTr.push({
149 e: elem,
150 f: 'text',
151 m: attr.trspan
152 });
153 }
154 return elem.text(attr.trspan);
155 },
156 template: ''
157 };
158 }
159 ]);
160
161 // Form menu management
162
163 // Two parts:
164 // - $htmlParams: used to store values inserted as <script type="text/menu">.
165 // It provides menu() method to get them
166 // - HTML "script" element handler
167 llapp.provider('$htmlParams', function() {
168 this.$get = function() {
169 var params;
170 params = {};
171 return {
172 set: function(key, obj) {
173 return params[key] = obj;
174 },
175 menu: function() {
176 return params.menu;
177 },
178 // To be used later
179 params: function() {
180 return params.params;
181 }
182 };
183 };
184 return this;
185 });
186
187 llapp.directive('script', [
188 '$htmlParams',
189 function($htmlParams) {
190 return {
191 restrict: 'E',
192 terminal: true,
193 compile: function(element, attr) {
194 var e, t;
195 if (attr.type && (t = attr.type.match(/text\/(menu|parameters)/))) {
196 try {
197 return $htmlParams.set(t[1], JSON.parse(element[0].text));
198 } catch (error) {
199 e = error;
200 console.error("Parsing error:", e);
201 }
202 }
203 }
204 };
205 }
206 ]);
207
208 // Modal controller used to display messages
209 llapp.controller('ModalInstanceCtrl', [
210 '$scope',
211 '$uibModalInstance',
212 'elem',
213 'set',
214 'init',
215 function($scope,
216 $uibModalInstance,
217 elem,
218 set,
219 init) {
220 var currentNode,
221 oldValue;
222 $scope.elem = elem;
223 $scope.set = set;
224 $scope.result = init;
225 $scope.staticPrefix = window.staticPrefix;
226 currentNode = elem('currentNode');
227 $scope.translateP = elem('translateP');
228 if (currentNode) {
229 oldValue = currentNode.data;
230 $scope.currentNode = currentNode;
231 }
232 $scope.ok = function() {
233 set('result',
234 $scope.result);
235 return $uibModalInstance.close(true);
236 };
237 $scope.cancel = function() {
238 if (currentNode) {
239 $scope.currentNode.data = oldValue;
240 }
241 return $uibModalInstance.dismiss('cancel');
242 };
243 // test if value is in select
244 return $scope.inSelect = function(value) {
245 var i,
246 j,
247 len,
248 ref;
249 ref = $scope.currentNode.select;
250 for (j = 0, len = ref.length; j < len; j++) {
251 i = ref[j];
252 if (i.k === value) {
253 return true;
254 }
255 }
256 return false;
257 };
258 }
259 ]);
260
261 // File reader directive
262
263 // Add "onReadFile" HTML attribute to be used in a "file" input
264 // The content off attribute will be launched.
265
266 // Example:
267 // <input type="file" on-read-file="replaceContent($fileContent)"/>
268 llapp.directive('onReadFile', [
269 '$parse',
270 function($parse) {
271 return {
272 restrict: 'A',
273 scope: false,
274 link: function(scope,
275 element,
276 attrs) {
277 var fn;
278 fn = $parse(attrs.onReadFile);
279 return element.on('change',
280 function(onChangeEvent) {
281 var reader;
282 reader = new FileReader();
283 reader.onload = function(onLoadEvent) {
284 return scope.$apply(function() {
285 return fn(scope, {
286 $fileContent: onLoadEvent.target.result
287 });
288 });
289 };
290 return reader.readAsText((onChangeEvent.srcElement || onChangeEvent.target).files[0]);
291 });
292 }
293 };
294 }
295 ]);
296
297 // Resize system
298
299 // Add a "resizer" HTML attribute
300 llapp.directive('resizer', [
301 '$document',
302 function($document) {
303 var hsize,
304 rsize;
305 hsize = null;
306 rsize = null;
307 return function($scope,
308 $element,
309 $attrs) {
310 var mousemove,
311 mouseup;
312 $element.on('mousedown',
313 function(event) {
314 if ($attrs.resizer === 'vertical') {
315 rsize = $($attrs.resizerRight).width() + $($attrs.resizerLeft).width();
316 } else {
317 hsize = $($attrs.resizerTop).height() + $($attrs.resizerBottom).height();
318 }
319 event.preventDefault();
320 $document.on('mousemove',
321 mousemove);
322 return $document.on('mouseup',
323 mouseup);
324 });
325 mousemove = function(event) {
326 var x,
327 y;
328 // Handle vertical resizer
329 if ($attrs.resizer === 'vertical') {
330 x = event.pageX;
331 if ($attrs.resizerMax && x > $attrs.resizerMax) {
332 x = parseInt($attrs.resizerMax);
333 }
334 $($attrs.resizerLeft).css({
335 width: `${x}px`
336 });
337 return $($attrs.resizerRight).css({
338 width: `${rsize - x}px`
339 });
340 } else {
341 // Handle horizontal resizer
342 y = event.pageY - $('#navbar').height();
343 $($attrs.resizerTop).css({
344 height: `${y}px`
345 });
346 return $($attrs.resizerBottom).css({
347 height: `${hsize - y}px`
348 });
349 }
350 };
351 return mouseup = function() {
352 $document.unbind('mousemove',
353 mousemove);
354 return $document.unbind('mouseup',
355 mouseup);
356 };
357 };
358 }
359 ]);
360
361 /*
362 * Authentication system
363 *
364 * If a 401 code is returned and if "Authorization" header contains an url,
365 * user is redirected to this url (but target is replaced by location.href
366 */
367 llapp.factory('$lmhttp', [
368 '$q',
369 '$location',
370 function($q,
371 $location) {
372 return {
373 responseError: function(rejection) {
374 if (rejection.status === 401 && window.portal) {
375 return window.location = `${window.portal}?url=` + window.btoa(window.location).replace(/\//,
376 '_');
377 } else {
378 return $q.reject(rejection);
379 }
380 }
381 };
382 }
383 ]);
384
385 llapp.config([
386 '$httpProvider',
387 function($httpProvider) {
388 return $httpProvider.interceptors.push('$lmhttp');
389 }
390 ]);