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 | a11b4eb | 2015-01-28 16:20:50 -0800 | [diff] [blame] | 23 | var $window; |
| 24 | |
Simon Hunt | 59df0b2 | 2014-12-17 10:32:25 -0800 | [diff] [blame] | 25 | function isF(f) { |
Simon Hunt | 404f6b2 | 2015-01-21 14:00:56 -0800 | [diff] [blame] | 26 | return typeof f === 'function' ? f : null; |
Simon Hunt | 59df0b2 | 2014-12-17 10:32:25 -0800 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | function isA(a) { |
Simon Hunt | 404f6b2 | 2015-01-21 14:00:56 -0800 | [diff] [blame] | 30 | // NOTE: Array.isArray() is part of EMCAScript 5.1 |
| 31 | return Array.isArray(a) ? a : null; |
Simon Hunt | 59df0b2 | 2014-12-17 10:32:25 -0800 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | function isS(s) { |
| 35 | return typeof s === 'string' ? s : null; |
| 36 | } |
| 37 | |
| 38 | function isO(o) { |
Simon Hunt | 404f6b2 | 2015-01-21 14:00:56 -0800 | [diff] [blame] | 39 | return (o && typeof o === 'object' && o.constructor === Object) ? o : null; |
Simon Hunt | 59df0b2 | 2014-12-17 10:32:25 -0800 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | function contains(a, x) { |
| 43 | return isA(a) && a.indexOf(x) > -1; |
| 44 | } |
| 45 | |
Simon Hunt | 51fc40b | 2015-01-06 13:56:12 -0800 | [diff] [blame] | 46 | // Returns true if all names in the array are defined as functions |
| 47 | // on the given api object; false otherwise. |
Simon Hunt | 48e6167 | 2015-01-30 14:48:25 -0800 | [diff] [blame] | 48 | // Also returns false if there are properties on the api that are NOT |
| 49 | // listed in the array of names. |
Simon Hunt | 51fc40b | 2015-01-06 13:56:12 -0800 | [diff] [blame] | 50 | function areFunctions(api, fnNames) { |
Simon Hunt | 48e6167 | 2015-01-30 14:48:25 -0800 | [diff] [blame] | 51 | var fnLookup = {}, |
| 52 | extraFound = false; |
| 53 | |
| 54 | if (!isA(fnNames)) { |
| 55 | return false; |
| 56 | } |
| 57 | var n = fnNames.length, |
| 58 | i, name; |
| 59 | for (i=0; i<n; i++) { |
| 60 | name = fnNames[i]; |
| 61 | if (!isF(api[name])) { |
| 62 | return false; |
| 63 | } |
| 64 | fnLookup[name] = true; |
| 65 | } |
| 66 | |
| 67 | // check for properties on the API that are not listed in the array, |
| 68 | angular.forEach(api, function (value, key) { |
| 69 | if (!fnLookup[key]) { |
| 70 | extraFound = true; |
| 71 | } |
| 72 | }); |
| 73 | return !extraFound; |
| 74 | } |
| 75 | |
| 76 | // Returns true if all names in the array are defined as functions |
| 77 | // on the given api object; false otherwise. This is a non-strict version |
| 78 | // that does not care about other properties on the api. |
| 79 | function areFunctionsNonStrict(api, fnNames) { |
Simon Hunt | 51fc40b | 2015-01-06 13:56:12 -0800 | [diff] [blame] | 80 | if (!isA(fnNames)) { |
| 81 | return false; |
| 82 | } |
| 83 | var n = fnNames.length, |
| 84 | i, name; |
| 85 | for (i=0; i<n; i++) { |
| 86 | name = fnNames[i]; |
| 87 | if (!isF(api[name])) { |
| 88 | return false; |
| 89 | } |
| 90 | } |
| 91 | return true; |
| 92 | } |
| 93 | |
Simon Hunt | a11b4eb | 2015-01-28 16:20:50 -0800 | [diff] [blame] | 94 | // Returns width and height of window inner dimensions. |
| 95 | // offH, offW : offset width/height are subtracted, if present |
| 96 | function windowSize(offH, offW) { |
| 97 | var oh = offH || 0, |
| 98 | ow = offW || 0; |
| 99 | return { |
| 100 | height: $window.innerHeight - oh, |
| 101 | width: $window.innerWidth - ow |
| 102 | }; |
| 103 | } |
| 104 | |
Simon Hunt | 48e6167 | 2015-01-30 14:48:25 -0800 | [diff] [blame] | 105 | // search through an array of objects, looking for the one with the |
| 106 | // tagged property matching the given key. tag defaults to 'id'. |
| 107 | // returns the index of the matching object, or -1 for no match. |
| 108 | function find(key, array, tag) { |
| 109 | var _tag = tag || 'id', |
| 110 | idx, n, d; |
| 111 | for (idx = 0, n = array.length; idx < n; idx++) { |
| 112 | d = array[idx]; |
| 113 | if (d[_tag] === key) { |
| 114 | return idx; |
| 115 | } |
| 116 | } |
| 117 | return -1; |
| 118 | } |
| 119 | |
Simon Hunt | 205099e | 2015-02-07 13:12:01 -0800 | [diff] [blame] | 120 | // search through array to find (the first occurrence of) item, |
| 121 | // returning its index if found; otherwise returning -1. |
| 122 | function inArray(item, array) { |
| 123 | var i; |
| 124 | if (isA(array)) { |
| 125 | for (i=0; i<array.length; i++) { |
| 126 | if (array[i] === item) { |
| 127 | return i; |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | return -1; |
| 132 | } |
| 133 | |
| 134 | // remove (the first occurrence of) the specified item from the given |
| 135 | // array, if any. Return true if the removal was made; false otherwise. |
| 136 | function removeFromArray(item, array) { |
| 137 | var found = false, |
| 138 | i = inArray(item, array); |
| 139 | if (i >= 0) { |
| 140 | array.splice(i, 1); |
| 141 | found = true; |
| 142 | } |
| 143 | return found; |
| 144 | } |
| 145 | |
Simon Hunt | 27a5cc8 | 2015-02-19 14:49:55 -0800 | [diff] [blame^] | 146 | // return the given string with the first character capitalized. |
| 147 | function cap(s) { |
| 148 | return s.replace(/^[a-z]/, function (m) { |
| 149 | return m.toUpperCase(); |
| 150 | }); |
| 151 | } |
| 152 | |
Simon Hunt | dc6362a | 2014-12-18 19:55:23 -0800 | [diff] [blame] | 153 | angular.module('onosUtil') |
Simon Hunt | a11b4eb | 2015-01-28 16:20:50 -0800 | [diff] [blame] | 154 | .factory('FnService', ['$window', function (_$window_) { |
| 155 | $window = _$window_; |
| 156 | |
Simon Hunt | dc6362a | 2014-12-18 19:55:23 -0800 | [diff] [blame] | 157 | return { |
| 158 | isF: isF, |
| 159 | isA: isA, |
| 160 | isS: isS, |
| 161 | isO: isO, |
Simon Hunt | 51fc40b | 2015-01-06 13:56:12 -0800 | [diff] [blame] | 162 | contains: contains, |
Simon Hunt | a11b4eb | 2015-01-28 16:20:50 -0800 | [diff] [blame] | 163 | areFunctions: areFunctions, |
Simon Hunt | 48e6167 | 2015-01-30 14:48:25 -0800 | [diff] [blame] | 164 | areFunctionsNonStrict: areFunctionsNonStrict, |
| 165 | windowSize: windowSize, |
Simon Hunt | 205099e | 2015-02-07 13:12:01 -0800 | [diff] [blame] | 166 | find: find, |
| 167 | inArray: inArray, |
Simon Hunt | 27a5cc8 | 2015-02-19 14:49:55 -0800 | [diff] [blame^] | 168 | removeFromArray: removeFromArray, |
| 169 | cap: cap |
Simon Hunt | dc6362a | 2014-12-18 19:55:23 -0800 | [diff] [blame] | 170 | }; |
Simon Hunt | 1eecfa2 | 2014-12-16 14:46:29 -0800 | [diff] [blame] | 171 | }]); |
| 172 | |
Simon Hunt | dc6362a | 2014-12-18 19:55:23 -0800 | [diff] [blame] | 173 | }()); |