blob: ad441cd8f9d0eec28abc3054115c61e0254c5817 [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
Simon Hunt48e61672015-01-30 14:48:25 -0800125 // search through an array of objects, looking for the one with the
126 // tagged property matching the given key. tag defaults to 'id'.
127 // returns the index of the matching object, or -1 for no match.
128 function find(key, array, tag) {
129 var _tag = tag || 'id',
130 idx, n, d;
131 for (idx = 0, n = array.length; idx < n; idx++) {
132 d = array[idx];
133 if (d[_tag] === key) {
134 return idx;
135 }
136 }
137 return -1;
138 }
139
Simon Hunt205099e2015-02-07 13:12:01 -0800140 // search through array to find (the first occurrence of) item,
141 // returning its index if found; otherwise returning -1.
142 function inArray(item, array) {
143 var i;
144 if (isA(array)) {
145 for (i=0; i<array.length; i++) {
146 if (array[i] === item) {
147 return i;
148 }
149 }
150 }
151 return -1;
152 }
153
154 // remove (the first occurrence of) the specified item from the given
155 // array, if any. Return true if the removal was made; false otherwise.
156 function removeFromArray(item, array) {
157 var found = false,
158 i = inArray(item, array);
159 if (i >= 0) {
160 array.splice(i, 1);
161 found = true;
162 }
163 return found;
164 }
165
Bri Prebilic Cole08d08d42015-04-01 14:37:02 -0700166 // return true if the object is empty, return false otherwise
Bri Prebilic Cole19a32dd2015-03-26 18:00:03 -0700167 function isEmptyObject(obj) {
168 var key;
169 for (key in obj) {
170 return false;
171 }
172 return true;
173 }
174
Bri Prebilic Cole0bc4de22015-07-20 17:07:55 -0700175 // returns true if the two objects have all the same properties
176 function sameObjProps(obj1, obj2) {
177 var key;
178 for (key in obj1) {
179 if (obj1.hasOwnProperty(key)) {
180 if (!(obj1[key] === obj2[key])) {
181 return false;
182 }
183 }
184 }
185 return true;
186 }
187
188 // returns true if the array contains the object
189 // does NOT use strict object reference equality,
190 // instead checks each property individually for equality
191 function containsObj(arr, obj) {
192 var i;
193 for (i = 0; i < arr.length; i++) {
194 if (sameObjProps(arr[i], obj)) {
195 return true;
196 }
197 }
198 return false;
199 }
200
Simon Hunt27a5cc82015-02-19 14:49:55 -0800201 // return the given string with the first character capitalized.
202 function cap(s) {
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700203 return s.toLowerCase().replace(/^[a-z]/, function (m) {
Simon Hunt27a5cc82015-02-19 14:49:55 -0800204 return m.toUpperCase();
205 });
206 }
207
Bri Prebilic Cole08d08d42015-04-01 14:37:02 -0700208 // return the parameter without a px suffix
209 function noPx(num) {
210 return Number(num.replace(/px$/, ''));
211 }
212
213 // return an element's given style property without px suffix
214 function noPxStyle(elem, prop) {
215 return Number(elem.style(prop).replace(/px$/, ''));
216 }
217
Simon Hunt96641372015-06-02 15:53:25 -0700218 function endsWith(str, suffix) {
219 return str.indexOf(suffix, str.length - suffix.length) !== -1;
220 }
221
222 function parseBitRate(str) {
223 return Number(str.replace(/,/, '')
224 .replace(/\s+.bps/i, '')
225 .replace(/\.\d*/, ''));
226 }
227
Simon Hunt4deb0e82015-06-10 16:18:25 -0700228 // return true if the given debug flag was specified in the query params
229 function debugOn(tag) {
230 return debugFlags[tag];
231 }
Simon Hunt96641372015-06-02 15:53:25 -0700232
Simon Huntdc6362a2014-12-18 19:55:23 -0800233 angular.module('onosUtil')
Simon Hunt4deb0e82015-06-10 16:18:25 -0700234 .factory('FnService',
235 ['$window', '$location', '$log', function (_$window_, $loc, _$log_) {
Simon Hunta11b4eb2015-01-28 16:20:50 -0800236 $window = _$window_;
Simon Hunt4deb0e82015-06-10 16:18:25 -0700237 $log = _$log_;
238
239 _parseDebugFlags($loc.search().debug);
Simon Hunta11b4eb2015-01-28 16:20:50 -0800240
Simon Huntdc6362a2014-12-18 19:55:23 -0800241 return {
242 isF: isF,
243 isA: isA,
244 isS: isS,
245 isO: isO,
Simon Hunt51fc40b2015-01-06 13:56:12 -0800246 contains: contains,
Simon Hunta11b4eb2015-01-28 16:20:50 -0800247 areFunctions: areFunctions,
Simon Hunt48e61672015-01-30 14:48:25 -0800248 areFunctionsNonStrict: areFunctionsNonStrict,
249 windowSize: windowSize,
Simon Hunt31bb01f2015-03-31 16:50:41 -0700250 isMobile: isMobile,
Simon Hunt4deb0e82015-06-10 16:18:25 -0700251 debugOn: debugOn,
Simon Hunt205099e2015-02-07 13:12:01 -0800252 find: find,
253 inArray: inArray,
Simon Hunt27a5cc82015-02-19 14:49:55 -0800254 removeFromArray: removeFromArray,
Bri Prebilic Cole19a32dd2015-03-26 18:00:03 -0700255 isEmptyObject: isEmptyObject,
Bri Prebilic Cole0bc4de22015-07-20 17:07:55 -0700256 sameObjProps: sameObjProps,
257 containsObj: containsObj,
Bri Prebilic Cole08d08d42015-04-01 14:37:02 -0700258 cap: cap,
259 noPx: noPx,
Simon Hunt96641372015-06-02 15:53:25 -0700260 noPxStyle: noPxStyle,
261 endsWith: endsWith,
262 parseBitRate: parseBitRate
Simon Huntdc6362a2014-12-18 19:55:23 -0800263 };
Simon Hunt1eecfa22014-12-16 14:46:29 -0800264 }]);
265
Simon Huntdc6362a2014-12-18 19:55:23 -0800266}());