Simon Hunt | 1eecfa2 | 2014-12-16 14:46:29 -0800 | [diff] [blame] | 1 | /* |
Simon Hunt | 8ead3a2 | 2015-01-06 11:00:15 -0800 | [diff] [blame] | 2 | * Copyright 2014,2015 Open Networking Laboratory |
Simon Hunt | 1eecfa2 | 2014-12-16 14:46:29 -0800 | [diff] [blame] | 3 | * |
| 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 Hunt | dc6362a | 2014-12-18 19:55:23 -0800 | [diff] [blame] | 18 | ONOS GUI -- Util -- General Purpose Functions |
Simon Hunt | 1eecfa2 | 2014-12-16 14:46:29 -0800 | [diff] [blame] | 19 | */ |
Simon Hunt | dc6362a | 2014-12-18 19:55:23 -0800 | [diff] [blame] | 20 | (function () { |
Simon Hunt | 1eecfa2 | 2014-12-16 14:46:29 -0800 | [diff] [blame] | 21 | 'use strict'; |
| 22 | |
Simon Hunt | 4deb0e8 | 2015-06-10 16:18:25 -0700 | [diff] [blame] | 23 | // 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 Hunt | a11b4eb | 2015-01-28 16:20:50 -0800 | [diff] [blame] | 37 | |
Simon Hunt | 59df0b2 | 2014-12-17 10:32:25 -0800 | [diff] [blame] | 38 | function isF(f) { |
Simon Hunt | 404f6b2 | 2015-01-21 14:00:56 -0800 | [diff] [blame] | 39 | return typeof f === 'function' ? f : null; |
Simon Hunt | 59df0b2 | 2014-12-17 10:32:25 -0800 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | function isA(a) { |
Simon Hunt | 404f6b2 | 2015-01-21 14:00:56 -0800 | [diff] [blame] | 43 | // NOTE: Array.isArray() is part of EMCAScript 5.1 |
| 44 | return Array.isArray(a) ? a : null; |
Simon Hunt | 59df0b2 | 2014-12-17 10:32:25 -0800 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | function isS(s) { |
| 48 | return typeof s === 'string' ? s : null; |
| 49 | } |
| 50 | |
| 51 | function isO(o) { |
Simon Hunt | 404f6b2 | 2015-01-21 14:00:56 -0800 | [diff] [blame] | 52 | return (o && typeof o === 'object' && o.constructor === Object) ? o : null; |
Simon Hunt | 59df0b2 | 2014-12-17 10:32:25 -0800 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | function contains(a, x) { |
| 56 | return isA(a) && a.indexOf(x) > -1; |
| 57 | } |
| 58 | |
Simon Hunt | 51fc40b | 2015-01-06 13:56:12 -0800 | [diff] [blame] | 59 | // Returns true if all names in the array are defined as functions |
| 60 | // on the given api object; false otherwise. |
Simon Hunt | 48e6167 | 2015-01-30 14:48:25 -0800 | [diff] [blame] | 61 | // Also returns false if there are properties on the api that are NOT |
| 62 | // listed in the array of names. |
Simon Hunt | 51fc40b | 2015-01-06 13:56:12 -0800 | [diff] [blame] | 63 | function areFunctions(api, fnNames) { |
Simon Hunt | 48e6167 | 2015-01-30 14:48:25 -0800 | [diff] [blame] | 64 | 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 Hunt | 51fc40b | 2015-01-06 13:56:12 -0800 | [diff] [blame] | 93 | 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 Hunt | a11b4eb | 2015-01-28 16:20:50 -0800 | [diff] [blame] | 107 | // 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 Hunt | 31bb01f | 2015-03-31 16:50:41 -0700 | [diff] [blame] | 118 | // 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 Cole | 55ee09b | 2015-08-04 14:34:07 -0700 | [diff] [blame] | 125 | // 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 Hunt | 48e6167 | 2015-01-30 14:48:25 -0800 | [diff] [blame] | 147 | // 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 Hunt | 205099e | 2015-02-07 13:12:01 -0800 | [diff] [blame] | 162 | // 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 Cole | 08d08d4 | 2015-04-01 14:37:02 -0700 | [diff] [blame] | 188 | // return true if the object is empty, return false otherwise |
Bri Prebilic Cole | 19a32dd | 2015-03-26 18:00:03 -0700 | [diff] [blame] | 189 | function isEmptyObject(obj) { |
| 190 | var key; |
| 191 | for (key in obj) { |
| 192 | return false; |
| 193 | } |
| 194 | return true; |
| 195 | } |
| 196 | |
Bri Prebilic Cole | 0bc4de2 | 2015-07-20 17:07:55 -0700 | [diff] [blame] | 197 | // 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 Cole | 70aacc4 | 2015-07-22 11:28:34 -0700 | [diff] [blame] | 214 | var i, |
| 215 | len = arr.length; |
| 216 | for (i = 0; i < len; i++) { |
Bri Prebilic Cole | 0bc4de2 | 2015-07-20 17:07:55 -0700 | [diff] [blame] | 217 | if (sameObjProps(arr[i], obj)) { |
| 218 | return true; |
| 219 | } |
| 220 | } |
| 221 | return false; |
| 222 | } |
| 223 | |
Simon Hunt | 27a5cc8 | 2015-02-19 14:49:55 -0800 | [diff] [blame] | 224 | // return the given string with the first character capitalized. |
| 225 | function cap(s) { |
Simon Hunt | 4092733 | 2016-01-22 15:29:47 -0800 | [diff] [blame] | 226 | return s ? s[0].toUpperCase() + s.slice(1) : s; |
Simon Hunt | 27a5cc8 | 2015-02-19 14:49:55 -0800 | [diff] [blame] | 227 | } |
| 228 | |
Bri Prebilic Cole | 08d08d4 | 2015-04-01 14:37:02 -0700 | [diff] [blame] | 229 | // 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 Hunt | 9664137 | 2015-06-02 15:53:25 -0700 | [diff] [blame] | 239 | 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 Hunt | 4deb0e8 | 2015-06-10 16:18:25 -0700 | [diff] [blame] | 249 | // return true if the given debug flag was specified in the query params |
| 250 | function debugOn(tag) { |
| 251 | return debugFlags[tag]; |
| 252 | } |
Simon Hunt | 9664137 | 2015-06-02 15:53:25 -0700 | [diff] [blame] | 253 | |
Simon Hunt | f90c18b | 2016-01-25 15:38:58 -0800 | [diff] [blame^] | 254 | // 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 Hunt | dc6362a | 2014-12-18 19:55:23 -0800 | [diff] [blame] | 265 | angular.module('onosUtil') |
Simon Hunt | 4deb0e8 | 2015-06-10 16:18:25 -0700 | [diff] [blame] | 266 | .factory('FnService', |
| 267 | ['$window', '$location', '$log', function (_$window_, $loc, _$log_) { |
Simon Hunt | a11b4eb | 2015-01-28 16:20:50 -0800 | [diff] [blame] | 268 | $window = _$window_; |
Simon Hunt | 4deb0e8 | 2015-06-10 16:18:25 -0700 | [diff] [blame] | 269 | $log = _$log_; |
| 270 | |
| 271 | _parseDebugFlags($loc.search().debug); |
Simon Hunt | a11b4eb | 2015-01-28 16:20:50 -0800 | [diff] [blame] | 272 | |
Simon Hunt | dc6362a | 2014-12-18 19:55:23 -0800 | [diff] [blame] | 273 | return { |
| 274 | isF: isF, |
| 275 | isA: isA, |
| 276 | isS: isS, |
| 277 | isO: isO, |
Simon Hunt | 51fc40b | 2015-01-06 13:56:12 -0800 | [diff] [blame] | 278 | contains: contains, |
Simon Hunt | a11b4eb | 2015-01-28 16:20:50 -0800 | [diff] [blame] | 279 | areFunctions: areFunctions, |
Simon Hunt | 48e6167 | 2015-01-30 14:48:25 -0800 | [diff] [blame] | 280 | areFunctionsNonStrict: areFunctionsNonStrict, |
| 281 | windowSize: windowSize, |
Simon Hunt | 31bb01f | 2015-03-31 16:50:41 -0700 | [diff] [blame] | 282 | isMobile: isMobile, |
Bri Prebilic Cole | 55ee09b | 2015-08-04 14:34:07 -0700 | [diff] [blame] | 283 | isChrome: isChrome, |
| 284 | isSafari: isSafari, |
| 285 | isFirefox: isFirefox, |
Simon Hunt | 4deb0e8 | 2015-06-10 16:18:25 -0700 | [diff] [blame] | 286 | debugOn: debugOn, |
Simon Hunt | f90c18b | 2016-01-25 15:38:58 -0800 | [diff] [blame^] | 287 | debug: debug, |
Simon Hunt | 205099e | 2015-02-07 13:12:01 -0800 | [diff] [blame] | 288 | find: find, |
| 289 | inArray: inArray, |
Simon Hunt | 27a5cc8 | 2015-02-19 14:49:55 -0800 | [diff] [blame] | 290 | removeFromArray: removeFromArray, |
Bri Prebilic Cole | 19a32dd | 2015-03-26 18:00:03 -0700 | [diff] [blame] | 291 | isEmptyObject: isEmptyObject, |
Bri Prebilic Cole | 0bc4de2 | 2015-07-20 17:07:55 -0700 | [diff] [blame] | 292 | sameObjProps: sameObjProps, |
| 293 | containsObj: containsObj, |
Bri Prebilic Cole | 08d08d4 | 2015-04-01 14:37:02 -0700 | [diff] [blame] | 294 | cap: cap, |
| 295 | noPx: noPx, |
Simon Hunt | 9664137 | 2015-06-02 15:53:25 -0700 | [diff] [blame] | 296 | noPxStyle: noPxStyle, |
| 297 | endsWith: endsWith, |
| 298 | parseBitRate: parseBitRate |
Simon Hunt | dc6362a | 2014-12-18 19:55:23 -0800 | [diff] [blame] | 299 | }; |
Simon Hunt | 1eecfa2 | 2014-12-16 14:46:29 -0800 | [diff] [blame] | 300 | }]); |
| 301 | |
Simon Hunt | dc6362a | 2014-12-18 19:55:23 -0800 | [diff] [blame] | 302 | }()); |