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 | |
Simon Hunt | 48e6167 | 2015-01-30 14:48:25 -0800 | [diff] [blame] | 125 | // 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 Hunt | 205099e | 2015-02-07 13:12:01 -0800 | [diff] [blame] | 140 | // 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 Cole | 08d08d4 | 2015-04-01 14:37:02 -0700 | [diff] [blame] | 166 | // return true if the object is empty, return false otherwise |
Bri Prebilic Cole | 19a32dd | 2015-03-26 18:00:03 -0700 | [diff] [blame] | 167 | function isEmptyObject(obj) { |
| 168 | var key; |
| 169 | for (key in obj) { |
| 170 | return false; |
| 171 | } |
| 172 | return true; |
| 173 | } |
| 174 | |
Simon Hunt | 27a5cc8 | 2015-02-19 14:49:55 -0800 | [diff] [blame] | 175 | // return the given string with the first character capitalized. |
| 176 | function cap(s) { |
Bri Prebilic Cole | 0feedc0 | 2015-04-09 14:17:37 -0700 | [diff] [blame] | 177 | return s.toLowerCase().replace(/^[a-z]/, function (m) { |
Simon Hunt | 27a5cc8 | 2015-02-19 14:49:55 -0800 | [diff] [blame] | 178 | return m.toUpperCase(); |
| 179 | }); |
| 180 | } |
| 181 | |
Bri Prebilic Cole | 08d08d4 | 2015-04-01 14:37:02 -0700 | [diff] [blame] | 182 | // return the parameter without a px suffix |
| 183 | function noPx(num) { |
| 184 | return Number(num.replace(/px$/, '')); |
| 185 | } |
| 186 | |
| 187 | // return an element's given style property without px suffix |
| 188 | function noPxStyle(elem, prop) { |
| 189 | return Number(elem.style(prop).replace(/px$/, '')); |
| 190 | } |
| 191 | |
Simon Hunt | 9664137 | 2015-06-02 15:53:25 -0700 | [diff] [blame] | 192 | function endsWith(str, suffix) { |
| 193 | return str.indexOf(suffix, str.length - suffix.length) !== -1; |
| 194 | } |
| 195 | |
| 196 | function parseBitRate(str) { |
| 197 | return Number(str.replace(/,/, '') |
| 198 | .replace(/\s+.bps/i, '') |
| 199 | .replace(/\.\d*/, '')); |
| 200 | } |
| 201 | |
Simon Hunt | 4deb0e8 | 2015-06-10 16:18:25 -0700 | [diff] [blame] | 202 | // return true if the given debug flag was specified in the query params |
| 203 | function debugOn(tag) { |
| 204 | return debugFlags[tag]; |
| 205 | } |
Simon Hunt | 9664137 | 2015-06-02 15:53:25 -0700 | [diff] [blame] | 206 | |
Simon Hunt | dc6362a | 2014-12-18 19:55:23 -0800 | [diff] [blame] | 207 | angular.module('onosUtil') |
Simon Hunt | 4deb0e8 | 2015-06-10 16:18:25 -0700 | [diff] [blame] | 208 | .factory('FnService', |
| 209 | ['$window', '$location', '$log', function (_$window_, $loc, _$log_) { |
Simon Hunt | a11b4eb | 2015-01-28 16:20:50 -0800 | [diff] [blame] | 210 | $window = _$window_; |
Simon Hunt | 4deb0e8 | 2015-06-10 16:18:25 -0700 | [diff] [blame] | 211 | $log = _$log_; |
| 212 | |
| 213 | _parseDebugFlags($loc.search().debug); |
Simon Hunt | a11b4eb | 2015-01-28 16:20:50 -0800 | [diff] [blame] | 214 | |
Simon Hunt | dc6362a | 2014-12-18 19:55:23 -0800 | [diff] [blame] | 215 | return { |
| 216 | isF: isF, |
| 217 | isA: isA, |
| 218 | isS: isS, |
| 219 | isO: isO, |
Simon Hunt | 51fc40b | 2015-01-06 13:56:12 -0800 | [diff] [blame] | 220 | contains: contains, |
Simon Hunt | a11b4eb | 2015-01-28 16:20:50 -0800 | [diff] [blame] | 221 | areFunctions: areFunctions, |
Simon Hunt | 48e6167 | 2015-01-30 14:48:25 -0800 | [diff] [blame] | 222 | areFunctionsNonStrict: areFunctionsNonStrict, |
| 223 | windowSize: windowSize, |
Simon Hunt | 31bb01f | 2015-03-31 16:50:41 -0700 | [diff] [blame] | 224 | isMobile: isMobile, |
Simon Hunt | 4deb0e8 | 2015-06-10 16:18:25 -0700 | [diff] [blame] | 225 | debugOn: debugOn, |
Simon Hunt | 205099e | 2015-02-07 13:12:01 -0800 | [diff] [blame] | 226 | find: find, |
| 227 | inArray: inArray, |
Simon Hunt | 27a5cc8 | 2015-02-19 14:49:55 -0800 | [diff] [blame] | 228 | removeFromArray: removeFromArray, |
Bri Prebilic Cole | 19a32dd | 2015-03-26 18:00:03 -0700 | [diff] [blame] | 229 | isEmptyObject: isEmptyObject, |
Bri Prebilic Cole | 08d08d4 | 2015-04-01 14:37:02 -0700 | [diff] [blame] | 230 | cap: cap, |
| 231 | noPx: noPx, |
Simon Hunt | 9664137 | 2015-06-02 15:53:25 -0700 | [diff] [blame] | 232 | noPxStyle: noPxStyle, |
| 233 | endsWith: endsWith, |
| 234 | parseBitRate: parseBitRate |
Simon Hunt | dc6362a | 2014-12-18 19:55:23 -0800 | [diff] [blame] | 235 | }; |
Simon Hunt | 1eecfa2 | 2014-12-16 14:46:29 -0800 | [diff] [blame] | 236 | }]); |
| 237 | |
Simon Hunt | dc6362a | 2014-12-18 19:55:23 -0800 | [diff] [blame] | 238 | }()); |