Debian Codegraph @

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

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