blob: 56e105bf1b372b71df7354db556b2f0654710285 [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 Hunta11b4eb2015-01-28 16:20:50 -080023 var $window;
24
Simon Hunt59df0b22014-12-17 10:32:25 -080025 function isF(f) {
Simon Hunt404f6b22015-01-21 14:00:56 -080026 return typeof f === 'function' ? f : null;
Simon Hunt59df0b22014-12-17 10:32:25 -080027 }
28
29 function isA(a) {
Simon Hunt404f6b22015-01-21 14:00:56 -080030 // NOTE: Array.isArray() is part of EMCAScript 5.1
31 return Array.isArray(a) ? a : null;
Simon Hunt59df0b22014-12-17 10:32:25 -080032 }
33
34 function isS(s) {
35 return typeof s === 'string' ? s : null;
36 }
37
38 function isO(o) {
Simon Hunt404f6b22015-01-21 14:00:56 -080039 return (o && typeof o === 'object' && o.constructor === Object) ? o : null;
Simon Hunt59df0b22014-12-17 10:32:25 -080040 }
41
42 function contains(a, x) {
43 return isA(a) && a.indexOf(x) > -1;
44 }
45
Simon Hunt51fc40b2015-01-06 13:56:12 -080046 // Returns true if all names in the array are defined as functions
47 // on the given api object; false otherwise.
Simon Hunt48e61672015-01-30 14:48:25 -080048 // Also returns false if there are properties on the api that are NOT
49 // listed in the array of names.
Simon Hunt51fc40b2015-01-06 13:56:12 -080050 function areFunctions(api, fnNames) {
Simon Hunt48e61672015-01-30 14:48:25 -080051 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 Hunt51fc40b2015-01-06 13:56:12 -080080 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 Hunta11b4eb2015-01-28 16:20:50 -080094 // 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 Hunt31bb01f2015-03-31 16:50:41 -0700105 // Returns true if current browser determined to be a mobile device
106 function isMobile() {
107 var ua = $window.navigator.userAgent,
108 patt = /iPhone|iPod|iPad|Silk|Android|BlackBerry|Opera Mini|IEMobile/;
109 return patt.test(ua);
110 }
111
Simon Hunt48e61672015-01-30 14:48:25 -0800112 // search through an array of objects, looking for the one with the
113 // tagged property matching the given key. tag defaults to 'id'.
114 // returns the index of the matching object, or -1 for no match.
115 function find(key, array, tag) {
116 var _tag = tag || 'id',
117 idx, n, d;
118 for (idx = 0, n = array.length; idx < n; idx++) {
119 d = array[idx];
120 if (d[_tag] === key) {
121 return idx;
122 }
123 }
124 return -1;
125 }
126
Simon Hunt205099e2015-02-07 13:12:01 -0800127 // search through array to find (the first occurrence of) item,
128 // returning its index if found; otherwise returning -1.
129 function inArray(item, array) {
130 var i;
131 if (isA(array)) {
132 for (i=0; i<array.length; i++) {
133 if (array[i] === item) {
134 return i;
135 }
136 }
137 }
138 return -1;
139 }
140
141 // remove (the first occurrence of) the specified item from the given
142 // array, if any. Return true if the removal was made; false otherwise.
143 function removeFromArray(item, array) {
144 var found = false,
145 i = inArray(item, array);
146 if (i >= 0) {
147 array.splice(i, 1);
148 found = true;
149 }
150 return found;
151 }
152
Bri Prebilic Cole08d08d42015-04-01 14:37:02 -0700153 // return true if the object is empty, return false otherwise
Bri Prebilic Cole19a32dd2015-03-26 18:00:03 -0700154 function isEmptyObject(obj) {
155 var key;
156 for (key in obj) {
157 return false;
158 }
159 return true;
160 }
161
Simon Hunt27a5cc82015-02-19 14:49:55 -0800162 // return the given string with the first character capitalized.
163 function cap(s) {
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700164 return s.toLowerCase().replace(/^[a-z]/, function (m) {
Simon Hunt27a5cc82015-02-19 14:49:55 -0800165 return m.toUpperCase();
166 });
167 }
168
Bri Prebilic Cole08d08d42015-04-01 14:37:02 -0700169 // return the parameter without a px suffix
170 function noPx(num) {
171 return Number(num.replace(/px$/, ''));
172 }
173
174 // return an element's given style property without px suffix
175 function noPxStyle(elem, prop) {
176 return Number(elem.style(prop).replace(/px$/, ''));
177 }
178
Simon Huntdc6362a2014-12-18 19:55:23 -0800179 angular.module('onosUtil')
Simon Hunta11b4eb2015-01-28 16:20:50 -0800180 .factory('FnService', ['$window', function (_$window_) {
181 $window = _$window_;
182
Simon Huntdc6362a2014-12-18 19:55:23 -0800183 return {
184 isF: isF,
185 isA: isA,
186 isS: isS,
187 isO: isO,
Simon Hunt51fc40b2015-01-06 13:56:12 -0800188 contains: contains,
Simon Hunta11b4eb2015-01-28 16:20:50 -0800189 areFunctions: areFunctions,
Simon Hunt48e61672015-01-30 14:48:25 -0800190 areFunctionsNonStrict: areFunctionsNonStrict,
191 windowSize: windowSize,
Simon Hunt31bb01f2015-03-31 16:50:41 -0700192 isMobile: isMobile,
Simon Hunt205099e2015-02-07 13:12:01 -0800193 find: find,
194 inArray: inArray,
Simon Hunt27a5cc82015-02-19 14:49:55 -0800195 removeFromArray: removeFromArray,
Bri Prebilic Cole19a32dd2015-03-26 18:00:03 -0700196 isEmptyObject: isEmptyObject,
Bri Prebilic Cole08d08d42015-04-01 14:37:02 -0700197 cap: cap,
198 noPx: noPx,
199 noPxStyle: noPxStyle
Simon Huntdc6362a2014-12-18 19:55:23 -0800200 };
Simon Hunt1eecfa22014-12-16 14:46:29 -0800201 }]);
202
Simon Huntdc6362a2014-12-18 19:55:23 -0800203}());