blob: 2b2ba0e9392bd530b61789f079db86009e8c1c6a [file] [log] [blame]
Simon Hunt1eecfa22014-12-16 14:46:29 -08001/*
Simon Hunt8ead3a22015-01-06 11:00:15 -08002 * Copyright 2014,2015 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
29
30 function _parseDebugFlags(dbgstr) {
31 var bits = dbgstr ? dbgstr.split(",") : [];
32 bits.forEach(function (key) {
33 debugFlags[key] = true;
34 });
35 $log.debug('Debug flags:', dbgstr);
36 }
Simon Hunta11b4eb2015-01-28 16:20:50 -080037
Simon Hunt59df0b22014-12-17 10:32:25 -080038 function isF(f) {
Simon Hunt404f6b22015-01-21 14:00:56 -080039 return typeof f === 'function' ? f : null;
Simon Hunt59df0b22014-12-17 10:32:25 -080040 }
41
42 function isA(a) {
Simon Hunt404f6b22015-01-21 14:00:56 -080043 // NOTE: Array.isArray() is part of EMCAScript 5.1
44 return Array.isArray(a) ? a : null;
Simon Hunt59df0b22014-12-17 10:32:25 -080045 }
46
47 function isS(s) {
48 return typeof s === 'string' ? s : null;
49 }
50
51 function isO(o) {
Simon Hunt404f6b22015-01-21 14:00:56 -080052 return (o && typeof o === 'object' && o.constructor === Object) ? o : null;
Simon Hunt59df0b22014-12-17 10:32:25 -080053 }
54
55 function contains(a, x) {
56 return isA(a) && a.indexOf(x) > -1;
57 }
58
Simon Hunt51fc40b2015-01-06 13:56:12 -080059 // Returns true if all names in the array are defined as functions
60 // on the given api object; false otherwise.
Simon Hunt48e61672015-01-30 14:48:25 -080061 // Also returns false if there are properties on the api that are NOT
62 // listed in the array of names.
Simon Hunt51fc40b2015-01-06 13:56:12 -080063 function areFunctions(api, fnNames) {
Simon Hunt48e61672015-01-30 14:48:25 -080064 var fnLookup = {},
65 extraFound = false;
66
67 if (!isA(fnNames)) {
68 return false;
69 }
70 var n = fnNames.length,
71 i, name;
72 for (i=0; i<n; i++) {
73 name = fnNames[i];
74 if (!isF(api[name])) {
75 return false;
76 }
77 fnLookup[name] = true;
78 }
79
80 // check for properties on the API that are not listed in the array,
81 angular.forEach(api, function (value, key) {
82 if (!fnLookup[key]) {
83 extraFound = true;
84 }
85 });
86 return !extraFound;
87 }
88
89 // Returns true if all names in the array are defined as functions
90 // on the given api object; false otherwise. This is a non-strict version
91 // that does not care about other properties on the api.
92 function areFunctionsNonStrict(api, fnNames) {
Simon Hunt51fc40b2015-01-06 13:56:12 -080093 if (!isA(fnNames)) {
94 return false;
95 }
96 var n = fnNames.length,
97 i, name;
98 for (i=0; i<n; i++) {
99 name = fnNames[i];
100 if (!isF(api[name])) {
101 return false;
102 }
103 }
104 return true;
105 }
106
Simon Hunta11b4eb2015-01-28 16:20:50 -0800107 // Returns width and height of window inner dimensions.
108 // offH, offW : offset width/height are subtracted, if present
109 function windowSize(offH, offW) {
110 var oh = offH || 0,
111 ow = offW || 0;
112 return {
113 height: $window.innerHeight - oh,
114 width: $window.innerWidth - ow
115 };
116 }
117
Simon Hunt31bb01f2015-03-31 16:50:41 -0700118 // Returns true if current browser determined to be a mobile device
119 function isMobile() {
120 var ua = $window.navigator.userAgent,
121 patt = /iPhone|iPod|iPad|Silk|Android|BlackBerry|Opera Mini|IEMobile/;
122 return patt.test(ua);
123 }
124
Bri Prebilic Cole55ee09b2015-08-04 14:34:07 -0700125 // Returns true if the current browser determined to be Chrome
126 function isChrome() {
127 var isChromium = $window.chrome,
128 vendorName = $window.navigator.vendor,
129 isOpera = $window.navigator.userAgent.indexOf("OPR") > -1;
130 return (isChromium !== null &&
131 isChromium !== undefined &&
132 vendorName === "Google Inc." &&
133 isOpera == false);
134 }
135
136 // Returns true if the current browser determined to be Safari
137 function isSafari() {
138 return ($window.navigator.userAgent.indexOf('Safari') !== -1 &&
139 $window.navigator.userAgent.indexOf('Chrome') === -1);
140 }
141
142 // Returns true if the current browser determined to be Firefox
143 function isFirefox() {
144 return typeof InstallTrigger !== 'undefined';
145 }
146
Simon Hunt48e61672015-01-30 14:48:25 -0800147 // search through an array of objects, looking for the one with the
148 // tagged property matching the given key. tag defaults to 'id'.
149 // returns the index of the matching object, or -1 for no match.
150 function find(key, array, tag) {
151 var _tag = tag || 'id',
152 idx, n, d;
153 for (idx = 0, n = array.length; idx < n; idx++) {
154 d = array[idx];
155 if (d[_tag] === key) {
156 return idx;
157 }
158 }
159 return -1;
160 }
161
Simon Hunt205099e2015-02-07 13:12:01 -0800162 // search through array to find (the first occurrence of) item,
163 // returning its index if found; otherwise returning -1.
164 function inArray(item, array) {
165 var i;
166 if (isA(array)) {
167 for (i=0; i<array.length; i++) {
168 if (array[i] === item) {
169 return i;
170 }
171 }
172 }
173 return -1;
174 }
175
176 // remove (the first occurrence of) the specified item from the given
177 // array, if any. Return true if the removal was made; false otherwise.
178 function removeFromArray(item, array) {
179 var found = false,
180 i = inArray(item, array);
181 if (i >= 0) {
182 array.splice(i, 1);
183 found = true;
184 }
185 return found;
186 }
187
Bri Prebilic Cole08d08d42015-04-01 14:37:02 -0700188 // return true if the object is empty, return false otherwise
Bri Prebilic Cole19a32dd2015-03-26 18:00:03 -0700189 function isEmptyObject(obj) {
190 var key;
191 for (key in obj) {
192 return false;
193 }
194 return true;
195 }
196
Bri Prebilic Cole0bc4de22015-07-20 17:07:55 -0700197 // returns true if the two objects have all the same properties
198 function sameObjProps(obj1, obj2) {
199 var key;
200 for (key in obj1) {
201 if (obj1.hasOwnProperty(key)) {
202 if (!(obj1[key] === obj2[key])) {
203 return false;
204 }
205 }
206 }
207 return true;
208 }
209
210 // returns true if the array contains the object
211 // does NOT use strict object reference equality,
212 // instead checks each property individually for equality
213 function containsObj(arr, obj) {
Bri Prebilic Cole70aacc42015-07-22 11:28:34 -0700214 var i,
215 len = arr.length;
216 for (i = 0; i < len; i++) {
Bri Prebilic Cole0bc4de22015-07-20 17:07:55 -0700217 if (sameObjProps(arr[i], obj)) {
218 return true;
219 }
220 }
221 return false;
222 }
223
Simon Hunt27a5cc82015-02-19 14:49:55 -0800224 // return the given string with the first character capitalized.
225 function cap(s) {
Simon Hunt40927332016-01-22 15:29:47 -0800226 return s ? s[0].toUpperCase() + s.slice(1) : s;
Simon Hunt27a5cc82015-02-19 14:49:55 -0800227 }
228
Bri Prebilic Cole08d08d42015-04-01 14:37:02 -0700229 // return the parameter without a px suffix
230 function noPx(num) {
231 return Number(num.replace(/px$/, ''));
232 }
233
234 // return an element's given style property without px suffix
235 function noPxStyle(elem, prop) {
236 return Number(elem.style(prop).replace(/px$/, ''));
237 }
238
Simon Hunt96641372015-06-02 15:53:25 -0700239 function endsWith(str, suffix) {
240 return str.indexOf(suffix, str.length - suffix.length) !== -1;
241 }
242
243 function parseBitRate(str) {
244 return Number(str.replace(/,/, '')
245 .replace(/\s+.bps/i, '')
246 .replace(/\.\d*/, ''));
247 }
248
Simon Hunt4deb0e82015-06-10 16:18:25 -0700249 // return true if the given debug flag was specified in the query params
250 function debugOn(tag) {
251 return debugFlags[tag];
252 }
Simon Hunt96641372015-06-02 15:53:25 -0700253
Simon Huntf90c18b2016-01-25 15:38:58 -0800254 // output debug message to console, if debug tag set...
255 // e.g. fs.debug('mytag', arg1, arg2, ...)
256 function debug(tag) {
257 var args;
258 if (debugOn(tag)) {
259 args = Array.prototype.slice.call(arguments, 1);
260 args.unshift('['+tag+']');
261 $log.debug.apply(this, args);
262 }
263 }
264
Simon Huntdc6362a2014-12-18 19:55:23 -0800265 angular.module('onosUtil')
Simon Hunt4deb0e82015-06-10 16:18:25 -0700266 .factory('FnService',
267 ['$window', '$location', '$log', function (_$window_, $loc, _$log_) {
Simon Hunta11b4eb2015-01-28 16:20:50 -0800268 $window = _$window_;
Simon Hunt4deb0e82015-06-10 16:18:25 -0700269 $log = _$log_;
270
271 _parseDebugFlags($loc.search().debug);
Simon Hunta11b4eb2015-01-28 16:20:50 -0800272
Simon Huntdc6362a2014-12-18 19:55:23 -0800273 return {
274 isF: isF,
275 isA: isA,
276 isS: isS,
277 isO: isO,
Simon Hunt51fc40b2015-01-06 13:56:12 -0800278 contains: contains,
Simon Hunta11b4eb2015-01-28 16:20:50 -0800279 areFunctions: areFunctions,
Simon Hunt48e61672015-01-30 14:48:25 -0800280 areFunctionsNonStrict: areFunctionsNonStrict,
281 windowSize: windowSize,
Simon Hunt31bb01f2015-03-31 16:50:41 -0700282 isMobile: isMobile,
Bri Prebilic Cole55ee09b2015-08-04 14:34:07 -0700283 isChrome: isChrome,
284 isSafari: isSafari,
285 isFirefox: isFirefox,
Simon Hunt4deb0e82015-06-10 16:18:25 -0700286 debugOn: debugOn,
Simon Huntf90c18b2016-01-25 15:38:58 -0800287 debug: debug,
Simon Hunt205099e2015-02-07 13:12:01 -0800288 find: find,
289 inArray: inArray,
Simon Hunt27a5cc82015-02-19 14:49:55 -0800290 removeFromArray: removeFromArray,
Bri Prebilic Cole19a32dd2015-03-26 18:00:03 -0700291 isEmptyObject: isEmptyObject,
Bri Prebilic Cole0bc4de22015-07-20 17:07:55 -0700292 sameObjProps: sameObjProps,
293 containsObj: containsObj,
Bri Prebilic Cole08d08d42015-04-01 14:37:02 -0700294 cap: cap,
295 noPx: noPx,
Simon Hunt96641372015-06-02 15:53:25 -0700296 noPxStyle: noPxStyle,
297 endsWith: endsWith,
298 parseBitRate: parseBitRate
Simon Huntdc6362a2014-12-18 19:55:23 -0800299 };
Simon Hunt1eecfa22014-12-16 14:46:29 -0800300 }]);
301
Simon Huntdc6362a2014-12-18 19:55:23 -0800302}());