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. |
| 48 | function areFunctions(api, fnNames) { |
| 49 | if (!isA(fnNames)) { |
| 50 | return false; |
| 51 | } |
| 52 | var n = fnNames.length, |
| 53 | i, name; |
| 54 | for (i=0; i<n; i++) { |
| 55 | name = fnNames[i]; |
| 56 | if (!isF(api[name])) { |
| 57 | return false; |
| 58 | } |
| 59 | } |
| 60 | return true; |
| 61 | } |
| 62 | |
Simon Hunt | a11b4eb | 2015-01-28 16:20:50 -0800 | [diff] [blame] | 63 | // Returns width and height of window inner dimensions. |
| 64 | // offH, offW : offset width/height are subtracted, if present |
| 65 | function windowSize(offH, offW) { |
| 66 | var oh = offH || 0, |
| 67 | ow = offW || 0; |
| 68 | return { |
| 69 | height: $window.innerHeight - oh, |
| 70 | width: $window.innerWidth - ow |
| 71 | }; |
| 72 | } |
| 73 | |
Simon Hunt | dc6362a | 2014-12-18 19:55:23 -0800 | [diff] [blame] | 74 | angular.module('onosUtil') |
Simon Hunt | a11b4eb | 2015-01-28 16:20:50 -0800 | [diff] [blame] | 75 | .factory('FnService', ['$window', function (_$window_) { |
| 76 | $window = _$window_; |
| 77 | |
Simon Hunt | dc6362a | 2014-12-18 19:55:23 -0800 | [diff] [blame] | 78 | return { |
| 79 | isF: isF, |
| 80 | isA: isA, |
| 81 | isS: isS, |
| 82 | isO: isO, |
Simon Hunt | 51fc40b | 2015-01-06 13:56:12 -0800 | [diff] [blame] | 83 | contains: contains, |
Simon Hunt | a11b4eb | 2015-01-28 16:20:50 -0800 | [diff] [blame] | 84 | areFunctions: areFunctions, |
| 85 | windowSize: windowSize |
Simon Hunt | dc6362a | 2014-12-18 19:55:23 -0800 | [diff] [blame] | 86 | }; |
Simon Hunt | 1eecfa2 | 2014-12-16 14:46:29 -0800 | [diff] [blame] | 87 | }]); |
| 88 | |
Simon Hunt | dc6362a | 2014-12-18 19:55:23 -0800 | [diff] [blame] | 89 | }()); |