blob: 77c2b96a8bfa83c141234a2fed71d5a5de0e0cac [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
268 function parseBitRate(str) {
269 return Number(str.replace(/,/, '')
270 .replace(/\s+.bps/i, '')
271 .replace(/\.\d*/, ''));
272 }
273
Simon Hunt4deb0e82015-06-10 16:18:25 -0700274 // return true if the given debug flag was specified in the query params
275 function debugOn(tag) {
276 return debugFlags[tag];
277 }
Simon Hunt96641372015-06-02 15:53:25 -0700278
Simon Huntf90c18b2016-01-25 15:38:58 -0800279 // output debug message to console, if debug tag set...
280 // e.g. fs.debug('mytag', arg1, arg2, ...)
281 function debug(tag) {
282 var args;
283 if (debugOn(tag)) {
284 args = Array.prototype.slice.call(arguments, 1);
285 args.unshift('['+tag+']');
286 $log.debug.apply(this, args);
287 }
288 }
289
Simon Hunt8b28c6b2016-02-03 12:33:15 -0800290 // trie operation
291 function _trieOp(op, trie, word, data) {
292 var p = trie,
293 w = word.toUpperCase(),
294 s = w.split(''),
295 c = { p: p, s: s },
296 t = [],
297 x = 0,
298 f1 = op === '+' ? add : probe,
299 f2 = op === '+' ? insert : remove;
Simon Hunt737ba482016-01-30 16:11:13 -0800300
301 function add(c) {
302 var q = c.s.shift(),
303 np = c.p[q];
304
305 if (!np) {
306 c.p[q] = {};
307 np = c.p[q];
Simon Hunt8b28c6b2016-02-03 12:33:15 -0800308 x = 1;
Simon Hunt737ba482016-01-30 16:11:13 -0800309 }
Simon Hunt8b28c6b2016-02-03 12:33:15 -0800310 return { p: np, s: c.s }
Simon Hunt737ba482016-01-30 16:11:13 -0800311 }
312
Simon Hunt8b28c6b2016-02-03 12:33:15 -0800313 function probe(c) {
314 var q = c.s.shift(),
315 k = Object.keys(c.p).length,
316 np = c.p[q];
Simon Hunt737ba482016-01-30 16:11:13 -0800317
Simon Hunt8b28c6b2016-02-03 12:33:15 -0800318 t.push({ q:q, k:k, p:c.p });
319 if (!np) {
320 t = [];
321 return { s: [] };
Simon Hunt737ba482016-01-30 16:11:13 -0800322 }
Simon Hunt8b28c6b2016-02-03 12:33:15 -0800323 return { p: np, s: c.s }
324 }
Simon Hunt737ba482016-01-30 16:11:13 -0800325
Simon Hunt8b28c6b2016-02-03 12:33:15 -0800326 function insert() {
327 c.p._data = data;
328 return x ? 'added' : 'updated';
329 }
330
331 function remove() {
332 if (t.length) {
333 t = t.reverse();
334 while (t.length) {
335 c = t.shift();
336 delete c.p[c.q];
337 if (c.k > 1) {
338 t = [];
339 }
340 }
341 return 'removed';
342 }
343 return 'absent';
344 }
345
346 while (c.s.length) {
347 c = f1(c);
348 }
349 return f2();
Simon Hunt737ba482016-01-30 16:11:13 -0800350 }
351
Simon Hunt8b28c6b2016-02-03 12:33:15 -0800352 // add word to trie (word will be converted to uppercase)
353 // data associated with the word
354 // returns 'added' or 'updated'
355 function addToTrie(trie, word, data) {
356 return _trieOp('+', trie, word, data);
357 }
358
359 // remove word from trie (word will be converted to uppercase)
360 // returns 'removed' or 'absent'
361 function removeFromTrie(trie, word) {
362 return _trieOp('-', trie, word);
363 }
364
365 // lookup word (converted to uppercase) in trie
366 // returns:
367 // undefined if the word is not in the trie
368 // -1 for a partial match (word is a prefix to an existing word)
369 // data for the word for an exact match
370 function trieLookup(trie, word) {
371 var s = word.toUpperCase().split(''),
372 p = trie,
373 n;
374
375 while (s.length) {
376 n = s.shift();
377 p = p[n];
378 if (!p) {
379 return undefined;
380 }
381 }
382 if (p._data) {
383 return p._data;
384 }
385 return -1;
386 }
387
388
Simon Huntdc6362a2014-12-18 19:55:23 -0800389 angular.module('onosUtil')
Simon Hunt4deb0e82015-06-10 16:18:25 -0700390 .factory('FnService',
391 ['$window', '$location', '$log', function (_$window_, $loc, _$log_) {
Simon Hunta11b4eb2015-01-28 16:20:50 -0800392 $window = _$window_;
Simon Hunt4deb0e82015-06-10 16:18:25 -0700393 $log = _$log_;
394
395 _parseDebugFlags($loc.search().debug);
Simon Hunta11b4eb2015-01-28 16:20:50 -0800396
Simon Huntdc6362a2014-12-18 19:55:23 -0800397 return {
398 isF: isF,
399 isA: isA,
400 isS: isS,
401 isO: isO,
Simon Hunt51fc40b2015-01-06 13:56:12 -0800402 contains: contains,
Simon Hunta11b4eb2015-01-28 16:20:50 -0800403 areFunctions: areFunctions,
Simon Hunt48e61672015-01-30 14:48:25 -0800404 areFunctionsNonStrict: areFunctionsNonStrict,
405 windowSize: windowSize,
Simon Hunt31bb01f2015-03-31 16:50:41 -0700406 isMobile: isMobile,
Bri Prebilic Cole55ee09b2015-08-04 14:34:07 -0700407 isChrome: isChrome,
408 isSafari: isSafari,
409 isFirefox: isFirefox,
Simon Hunt4deb0e82015-06-10 16:18:25 -0700410 debugOn: debugOn,
Simon Huntf90c18b2016-01-25 15:38:58 -0800411 debug: debug,
Simon Hunt205099e2015-02-07 13:12:01 -0800412 find: find,
413 inArray: inArray,
Simon Hunt27a5cc82015-02-19 14:49:55 -0800414 removeFromArray: removeFromArray,
Bri Prebilic Cole19a32dd2015-03-26 18:00:03 -0700415 isEmptyObject: isEmptyObject,
Bri Prebilic Cole0bc4de22015-07-20 17:07:55 -0700416 sameObjProps: sameObjProps,
417 containsObj: containsObj,
Bri Prebilic Cole08d08d42015-04-01 14:37:02 -0700418 cap: cap,
Simon Huntb1d35e82016-01-26 13:54:06 -0800419 eecode: eecode,
Bri Prebilic Cole08d08d42015-04-01 14:37:02 -0700420 noPx: noPx,
Simon Hunt96641372015-06-02 15:53:25 -0700421 noPxStyle: noPxStyle,
422 endsWith: endsWith,
Simon Hunt737ba482016-01-30 16:11:13 -0800423 parseBitRate: parseBitRate,
Simon Hunt8b28c6b2016-02-03 12:33:15 -0800424 addToTrie: addToTrie,
425 removeFromTrie: removeFromTrie,
426 trieLookup: trieLookup
Simon Huntdc6362a2014-12-18 19:55:23 -0800427 };
Simon Hunt1eecfa22014-12-16 14:46:29 -0800428 }]);
429
Simon Huntdc6362a2014-12-18 19:55:23 -0800430}());