blob: 9706478f8671f4ee299ea5999e12d3719e3b3d6c [file] [log] [blame]
Simon Hunt1eecfa22014-12-16 14:46:29 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
Simon Hunt1eecfa22014-12-16 14:46:29 -08003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/*
Simon Huntdc6362a2014-12-18 19:55:23 -080018 ONOS GUI -- Util -- General Purpose Functions
Simon Hunt1eecfa22014-12-16 14:46:29 -080019 */
Simon Huntdc6362a2014-12-18 19:55:23 -080020(function () {
Simon Hunt1eecfa22014-12-16 14:46:29 -080021 'use strict';
22
Simon Hunt4deb0e82015-06-10 16:18:25 -070023 // injected services
24 var $window, $log;
25
26 // internal state
27 var debugFlags = {};
28
Simon Huntb1d35e82016-01-26 13:54:06 -080029 // function references
30 var fcc = String.fromCharCode,
31 cca = String.prototype.charCodeAt;
Simon Hunt4deb0e82015-06-10 16:18:25 -070032
33 function _parseDebugFlags(dbgstr) {
34 var bits = dbgstr ? dbgstr.split(",") : [];
35 bits.forEach(function (key) {
36 debugFlags[key] = true;
37 });
38 $log.debug('Debug flags:', dbgstr);
39 }
Simon Hunta11b4eb2015-01-28 16:20:50 -080040
Simon Hunt59df0b22014-12-17 10:32:25 -080041 function isF(f) {
Simon Hunt404f6b22015-01-21 14:00:56 -080042 return typeof f === 'function' ? f : null;
Simon Hunt59df0b22014-12-17 10:32:25 -080043 }
44
45 function isA(a) {
Simon Hunt404f6b22015-01-21 14:00:56 -080046 // NOTE: Array.isArray() is part of EMCAScript 5.1
47 return Array.isArray(a) ? a : null;
Simon Hunt59df0b22014-12-17 10:32:25 -080048 }
49
50 function isS(s) {
51 return typeof s === 'string' ? s : null;
52 }
53
54 function isO(o) {
Simon Hunt404f6b22015-01-21 14:00:56 -080055 return (o && typeof o === 'object' && o.constructor === Object) ? o : null;
Simon Hunt59df0b22014-12-17 10:32:25 -080056 }
57
58 function contains(a, x) {
59 return isA(a) && a.indexOf(x) > -1;
60 }
61
Simon Hunt51fc40b2015-01-06 13:56:12 -080062 // Returns true if all names in the array are defined as functions
63 // on the given api object; false otherwise.
Simon Hunt48e61672015-01-30 14:48:25 -080064 // Also returns false if there are properties on the api that are NOT
65 // listed in the array of names.
Simon Hunt51fc40b2015-01-06 13:56:12 -080066 function areFunctions(api, fnNames) {
Simon Hunt48e61672015-01-30 14:48:25 -080067 var fnLookup = {},
68 extraFound = false;
69
70 if (!isA(fnNames)) {
71 return false;
72 }
73 var n = fnNames.length,
74 i, name;
75 for (i=0; i<n; i++) {
76 name = fnNames[i];
77 if (!isF(api[name])) {
78 return false;
79 }
80 fnLookup[name] = true;
81 }
82
83 // check for properties on the API that are not listed in the array,
84 angular.forEach(api, function (value, key) {
85 if (!fnLookup[key]) {
86 extraFound = true;
87 }
88 });
89 return !extraFound;
90 }
91
92 // Returns true if all names in the array are defined as functions
93 // on the given api object; false otherwise. This is a non-strict version
94 // that does not care about other properties on the api.
95 function areFunctionsNonStrict(api, fnNames) {
Simon Hunt51fc40b2015-01-06 13:56:12 -080096 if (!isA(fnNames)) {
97 return false;
98 }
99 var n = fnNames.length,
100 i, name;
101 for (i=0; i<n; i++) {
102 name = fnNames[i];
103 if (!isF(api[name])) {
104 return false;
105 }
106 }
107 return true;
108 }
109
Simon Hunta11b4eb2015-01-28 16:20:50 -0800110 // Returns width and height of window inner dimensions.
111 // offH, offW : offset width/height are subtracted, if present
112 function windowSize(offH, offW) {
113 var oh = offH || 0,
114 ow = offW || 0;
115 return {
116 height: $window.innerHeight - oh,
117 width: $window.innerWidth - ow
118 };
119 }
120
Simon Hunt31bb01f2015-03-31 16:50:41 -0700121 // Returns true if current browser determined to be a mobile device
122 function isMobile() {
123 var ua = $window.navigator.userAgent,
124 patt = /iPhone|iPod|iPad|Silk|Android|BlackBerry|Opera Mini|IEMobile/;
125 return patt.test(ua);
126 }
127
Bri Prebilic Cole55ee09b2015-08-04 14:34:07 -0700128 // Returns true if the current browser determined to be Chrome
129 function isChrome() {
130 var isChromium = $window.chrome,
131 vendorName = $window.navigator.vendor,
132 isOpera = $window.navigator.userAgent.indexOf("OPR") > -1;
133 return (isChromium !== null &&
134 isChromium !== undefined &&
135 vendorName === "Google Inc." &&
136 isOpera == false);
137 }
138
139 // Returns true if the current browser determined to be Safari
140 function isSafari() {
141 return ($window.navigator.userAgent.indexOf('Safari') !== -1 &&
142 $window.navigator.userAgent.indexOf('Chrome') === -1);
143 }
144
145 // Returns true if the current browser determined to be Firefox
146 function isFirefox() {
147 return typeof InstallTrigger !== 'undefined';
148 }
149
Simon Hunt48e61672015-01-30 14:48:25 -0800150 // search through an array of objects, looking for the one with the
151 // tagged property matching the given key. tag defaults to 'id'.
152 // returns the index of the matching object, or -1 for no match.
153 function find(key, array, tag) {
154 var _tag = tag || 'id',
155 idx, n, d;
156 for (idx = 0, n = array.length; idx < n; idx++) {
157 d = array[idx];
158 if (d[_tag] === key) {
159 return idx;
160 }
161 }
162 return -1;
163 }
164
Simon Hunt205099e2015-02-07 13:12:01 -0800165 // search through array to find (the first occurrence of) item,
166 // returning its index if found; otherwise returning -1.
167 function inArray(item, array) {
168 var i;
169 if (isA(array)) {
170 for (i=0; i<array.length; i++) {
171 if (array[i] === item) {
172 return i;
173 }
174 }
175 }
176 return -1;
177 }
178
179 // remove (the first occurrence of) the specified item from the given
180 // array, if any. Return true if the removal was made; false otherwise.
181 function removeFromArray(item, array) {
182 var found = false,
183 i = inArray(item, array);
184 if (i >= 0) {
185 array.splice(i, 1);
186 found = true;
187 }
188 return found;
189 }
190
Bri Prebilic Cole08d08d42015-04-01 14:37:02 -0700191 // return true if the object is empty, return false otherwise
Bri Prebilic Cole19a32dd2015-03-26 18:00:03 -0700192 function isEmptyObject(obj) {
193 var key;
194 for (key in obj) {
195 return false;
196 }
197 return true;
198 }
199
Bri Prebilic Cole0bc4de22015-07-20 17:07:55 -0700200 // returns true if the two objects have all the same properties
201 function sameObjProps(obj1, obj2) {
202 var key;
203 for (key in obj1) {
204 if (obj1.hasOwnProperty(key)) {
205 if (!(obj1[key] === obj2[key])) {
206 return false;
207 }
208 }
209 }
210 return true;
211 }
212
213 // returns true if the array contains the object
214 // does NOT use strict object reference equality,
215 // instead checks each property individually for equality
216 function containsObj(arr, obj) {
Bri Prebilic Cole70aacc42015-07-22 11:28:34 -0700217 var i,
218 len = arr.length;
219 for (i = 0; i < len; i++) {
Bri Prebilic Cole0bc4de22015-07-20 17:07:55 -0700220 if (sameObjProps(arr[i], obj)) {
221 return true;
222 }
223 }
224 return false;
225 }
226
Simon Hunt27a5cc82015-02-19 14:49:55 -0800227 // return the given string with the first character capitalized.
228 function cap(s) {
Matteo Scandolo209c6c62016-05-21 10:08:57 -0700229 return s ? s[0].toUpperCase() + s.slice(1).toLowerCase() : s;
Simon Hunt27a5cc82015-02-19 14:49:55 -0800230 }
231
Simon Huntb1d35e82016-01-26 13:54:06 -0800232 // return encoding structure for given parameters
233 function eecode(h, w) {
234 var m = 65,
235 x = 90,
236 d = x - m + 1,
237 s = x + m,
238 o = [],
239 n, i, c, e;
240
241 for (i = 0, n = w.length; i<n; i++) {
242 c = cca.call(w, i);
243 e = s - c + h;
244 e = e > x ? e - d : e;
245 o.push(e);
246 }
247 return {
248 o: w,
249 d: o.join(''),
250 e: fcc.apply(o, o)
251 };
252 }
253
Bri Prebilic Cole08d08d42015-04-01 14:37:02 -0700254 // return the parameter without a px suffix
255 function noPx(num) {
256 return Number(num.replace(/px$/, ''));
257 }
258
259 // return an element's given style property without px suffix
260 function noPxStyle(elem, prop) {
261 return Number(elem.style(prop).replace(/px$/, ''));
262 }
263
Simon Hunt96641372015-06-02 15:53:25 -0700264 function endsWith(str, suffix) {
265 return str.indexOf(suffix, str.length - suffix.length) !== -1;
266 }
267
Simon Hunt4deb0e82015-06-10 16:18:25 -0700268 // return true if the given debug flag was specified in the query params
269 function debugOn(tag) {
270 return debugFlags[tag];
271 }
Simon Hunt96641372015-06-02 15:53:25 -0700272
Simon Huntf90c18b2016-01-25 15:38:58 -0800273 // output debug message to console, if debug tag set...
274 // e.g. fs.debug('mytag', arg1, arg2, ...)
275 function debug(tag) {
276 var args;
277 if (debugOn(tag)) {
278 args = Array.prototype.slice.call(arguments, 1);
279 args.unshift('['+tag+']');
280 $log.debug.apply(this, args);
281 }
282 }
283
Simon Hunt8b28c6b2016-02-03 12:33:15 -0800284 // trie operation
285 function _trieOp(op, trie, word, data) {
286 var p = trie,
287 w = word.toUpperCase(),
288 s = w.split(''),
289 c = { p: p, s: s },
290 t = [],
291 x = 0,
292 f1 = op === '+' ? add : probe,
293 f2 = op === '+' ? insert : remove;
Simon Hunt737ba482016-01-30 16:11:13 -0800294
295 function add(c) {
296 var q = c.s.shift(),
297 np = c.p[q];
298
299 if (!np) {
300 c.p[q] = {};
301 np = c.p[q];
Simon Hunt8b28c6b2016-02-03 12:33:15 -0800302 x = 1;
Simon Hunt737ba482016-01-30 16:11:13 -0800303 }
Simon Hunt8b28c6b2016-02-03 12:33:15 -0800304 return { p: np, s: c.s }
Simon Hunt737ba482016-01-30 16:11:13 -0800305 }
306
Simon Hunt8b28c6b2016-02-03 12:33:15 -0800307 function probe(c) {
308 var q = c.s.shift(),
309 k = Object.keys(c.p).length,
310 np = c.p[q];
Simon Hunt737ba482016-01-30 16:11:13 -0800311
Simon Hunt8b28c6b2016-02-03 12:33:15 -0800312 t.push({ q:q, k:k, p:c.p });
313 if (!np) {
314 t = [];
315 return { s: [] };
Simon Hunt737ba482016-01-30 16:11:13 -0800316 }
Simon Hunt8b28c6b2016-02-03 12:33:15 -0800317 return { p: np, s: c.s }
318 }
Simon Hunt737ba482016-01-30 16:11:13 -0800319
Simon Hunt8b28c6b2016-02-03 12:33:15 -0800320 function insert() {
321 c.p._data = data;
322 return x ? 'added' : 'updated';
323 }
324
325 function remove() {
326 if (t.length) {
327 t = t.reverse();
328 while (t.length) {
329 c = t.shift();
330 delete c.p[c.q];
331 if (c.k > 1) {
332 t = [];
333 }
334 }
335 return 'removed';
336 }
337 return 'absent';
338 }
339
340 while (c.s.length) {
341 c = f1(c);
342 }
343 return f2();
Simon Hunt737ba482016-01-30 16:11:13 -0800344 }
345
Simon Hunt8b28c6b2016-02-03 12:33:15 -0800346 // add word to trie (word will be converted to uppercase)
347 // data associated with the word
348 // returns 'added' or 'updated'
349 function addToTrie(trie, word, data) {
350 return _trieOp('+', trie, word, data);
351 }
352
353 // remove word from trie (word will be converted to uppercase)
354 // returns 'removed' or 'absent'
355 function removeFromTrie(trie, word) {
356 return _trieOp('-', trie, word);
357 }
358
359 // lookup word (converted to uppercase) in trie
360 // returns:
361 // undefined if the word is not in the trie
362 // -1 for a partial match (word is a prefix to an existing word)
363 // data for the word for an exact match
364 function trieLookup(trie, word) {
365 var s = word.toUpperCase().split(''),
366 p = trie,
367 n;
368
369 while (s.length) {
370 n = s.shift();
371 p = p[n];
372 if (!p) {
373 return undefined;
374 }
375 }
376 if (p._data) {
377 return p._data;
378 }
379 return -1;
380 }
381
382
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100383 var hasOwn = {}.hasOwnProperty;
384
385 function classNames () {
386 var classes = [];
387
388 for (var i = 0; i < arguments.length; i++) {
389 var arg = arguments[i];
390 if (!arg) continue;
391
392 var argType = typeof arg;
393
394 if (argType === 'string' || argType === 'number') {
395 classes.push(arg);
396 } else if (Array.isArray(arg)) {
397 classes.push(classNames.apply(null, arg));
398 } else if (argType === 'object') {
399 for (var key in arg) {
400 if (hasOwn.call(arg, key) && arg[key]) {
401 classes.push(key);
402 }
403 }
404 }
405 }
406
407 return classes.join(' ');
408 }
409
Steven Burrows86af4352016-11-16 18:19:12 -0600410 function extend(protoProps, staticProps) {
411
412 var parent = this,
413 child;
414
415 child = function () {
416 return parent.apply(this, arguments);
417 };
418
419 angular.extend(child, parent, staticProps);
420
421 // Set the prototype chain to inherit from `parent`, without calling
422 // `parent`'s constructor function and add the prototype properties.
423 child.prototype = angular.extend({}, parent.prototype, protoProps);
424 child.prototype.constructor = child;
425
426 // Set a convenience property in case the parent's prototype is needed
427 // later.
428 child.__super__ = parent.prototype;
429
430 return child;
431 }
432
Simon Hunt0fe05d62017-05-17 16:42:32 -0700433 // -----------------------------------------------------------------
434 // The next section deals with sanitizing external strings destined
435 // to be loaded via a .html() function call.
436
437 var matcher = /<\/?([a-zA-Z0-9]+)*(.*?)\/?>/igm,
438 whitelist = ['b', 'i', 'p', 'em', 'strong'],
439 warnlist = ['script', 'style'];
440
441 // Returns true if the tag is in the warn list, (and is not an end-tag)
442 function inWarnList(tag) {
443 return (warnlist.indexOf(tag.name) !== -1 && tag.full.indexOf('/') === -1);
444 }
445
446 function analyze(html) {
447 html = String(html) || '';
448
449 var matches = [],
450 match;
451
452 // extract all tags
453 while ((match = matcher.exec(html)) !== null) {
454 matches.push({
455 full: match[0],
456 name: match[1]
457 // NOTE: ignoring attributes {match[2].split(' ')} for now
458 });
459 }
460
461 return matches;
462 }
463
464 function sanitize(html) {
465 html = String(html) || '';
466
467 var matches = analyze(html);
468
469 // do not allow script tags or style tags
470 html = html.replace(/<script(.*?)>(.*?[\r\n])*?(.*?)(.*?[\r\n])*?<\/script>/gim, '');
471 html = html.replace(/<style(.*?)>(.*?[\r\n])*?(.*?)(.*?[\r\n])*?<\/style>/gim, '');
472
473 // filter out all but whitelisted tag types
474 matches.forEach(function (tag) {
475 if (whitelist.indexOf(tag.name) === -1) {
476 html = html.replace(tag.full, '');
477 if (inWarnList(tag)) {
478 $log.warn('Unsanitary HTML input -- ' + tag.full + ' detected!');
479 }
480 }
481 });
482
483 // TODO: consider encoding HTML entities, e.g. '&' -> '&amp;'
484
485 return html;
486 }
487
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100488
Simon Huntdc6362a2014-12-18 19:55:23 -0800489 angular.module('onosUtil')
Simon Hunt4deb0e82015-06-10 16:18:25 -0700490 .factory('FnService',
491 ['$window', '$location', '$log', function (_$window_, $loc, _$log_) {
Simon Hunta11b4eb2015-01-28 16:20:50 -0800492 $window = _$window_;
Simon Hunt4deb0e82015-06-10 16:18:25 -0700493 $log = _$log_;
494
495 _parseDebugFlags($loc.search().debug);
Simon Hunta11b4eb2015-01-28 16:20:50 -0800496
Simon Huntdc6362a2014-12-18 19:55:23 -0800497 return {
498 isF: isF,
499 isA: isA,
500 isS: isS,
501 isO: isO,
Simon Hunt51fc40b2015-01-06 13:56:12 -0800502 contains: contains,
Simon Hunta11b4eb2015-01-28 16:20:50 -0800503 areFunctions: areFunctions,
Simon Hunt48e61672015-01-30 14:48:25 -0800504 areFunctionsNonStrict: areFunctionsNonStrict,
505 windowSize: windowSize,
Simon Hunt31bb01f2015-03-31 16:50:41 -0700506 isMobile: isMobile,
Bri Prebilic Cole55ee09b2015-08-04 14:34:07 -0700507 isChrome: isChrome,
508 isSafari: isSafari,
509 isFirefox: isFirefox,
Simon Hunt4deb0e82015-06-10 16:18:25 -0700510 debugOn: debugOn,
Simon Huntf90c18b2016-01-25 15:38:58 -0800511 debug: debug,
Simon Hunt205099e2015-02-07 13:12:01 -0800512 find: find,
513 inArray: inArray,
Simon Hunt27a5cc82015-02-19 14:49:55 -0800514 removeFromArray: removeFromArray,
Bri Prebilic Cole19a32dd2015-03-26 18:00:03 -0700515 isEmptyObject: isEmptyObject,
Bri Prebilic Cole0bc4de22015-07-20 17:07:55 -0700516 sameObjProps: sameObjProps,
517 containsObj: containsObj,
Bri Prebilic Cole08d08d42015-04-01 14:37:02 -0700518 cap: cap,
Simon Huntb1d35e82016-01-26 13:54:06 -0800519 eecode: eecode,
Bri Prebilic Cole08d08d42015-04-01 14:37:02 -0700520 noPx: noPx,
Simon Hunt96641372015-06-02 15:53:25 -0700521 noPxStyle: noPxStyle,
522 endsWith: endsWith,
Simon Hunt8b28c6b2016-02-03 12:33:15 -0800523 addToTrie: addToTrie,
524 removeFromTrie: removeFromTrie,
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100525 trieLookup: trieLookup,
Steven Burrows86af4352016-11-16 18:19:12 -0600526 classNames: classNames,
Simon Hunt0fe05d62017-05-17 16:42:32 -0700527 extend: extend,
528 sanitize: sanitize
Simon Huntdc6362a2014-12-18 19:55:23 -0800529 };
Simon Hunt1eecfa22014-12-16 14:46:29 -0800530 }]);
531
Simon Huntdc6362a2014-12-18 19:55:23 -0800532}());