UNPKG

ionic

Version:

A tool for creating and developing Ionic Framework mobile apps.

184 lines (160 loc) 4.43 kB
/* * * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. * */ var utils = exports; /** * Defines a property getter / setter for obj[key]. */ utils.defineGetterSetter = function(obj, key, getFunc, opt_setFunc) { if (Object.defineProperty) { var desc = { get: getFunc, configurable: true }; if (opt_setFunc) { desc.set = opt_setFunc; } Object.defineProperty(obj, key, desc); } else { obj.__defineGetter__(key, getFunc); if (opt_setFunc) { obj.__defineSetter__(key, opt_setFunc); } } }; /** * Defines a property getter for obj[key]. */ utils.defineGetter = utils.defineGetterSetter; utils.arrayIndexOf = function(a, item) { if (a.indexOf) { return a.indexOf(item); } var len = a.length; for (var i = 0; i < len; ++i) { if (a[i] == item) { return i; } } return -1; }; /** * Returns whether the item was found in the array. */ utils.arrayRemove = function(a, item) { var index = utils.arrayIndexOf(a, item); if (index != -1) { a.splice(index, 1); } return index != -1; }; utils.typeName = function(val) { return Object.prototype.toString.call(val).slice(8, -1); }; /** * Returns an indication of whether the argument is an array or not */ utils.isArray = Array.isArray || function(a) {return utils.typeName(a) == 'Array';}; /** * Returns an indication of whether the argument is a Date or not */ utils.isDate = function(d) { return (d instanceof Date); }; /** * Does a deep clone of the object. */ utils.clone = function(obj) { if(!obj || typeof obj == 'function' || utils.isDate(obj) || typeof obj != 'object') { return obj; } var retVal, i; if(utils.isArray(obj)){ retVal = []; for(i = 0; i < obj.length; ++i){ retVal.push(utils.clone(obj[i])); } return retVal; } retVal = {}; for(i in obj){ if(!(i in retVal) || retVal[i] != obj[i]) { retVal[i] = utils.clone(obj[i]); } } return retVal; }; /** * Returns a wrapped version of the function */ utils.close = function(context, func, params) { return function() { var args = params || arguments; return func.apply(context, args); }; }; //------------------------------------------------------------------------------ function UUIDcreatePart(length) { var uuidpart = ""; for (var i=0; i<length; i++) { var uuidchar = parseInt((Math.random() * 256), 10).toString(16); if (uuidchar.length == 1) { uuidchar = "0" + uuidchar; } uuidpart += uuidchar; } return uuidpart; } /** * Create a UUID */ utils.createUUID = function() { return UUIDcreatePart(4) + '-' + UUIDcreatePart(2) + '-' + UUIDcreatePart(2) + '-' + UUIDcreatePart(2) + '-' + UUIDcreatePart(6); }; /** * Extends a child object from a parent object using classical inheritance * pattern. */ utils.extend = (function() { // proxy used to establish prototype chain var F = function() {}; // extend Child from Parent return function(Child, Parent) { F.prototype = Parent.prototype; Child.prototype = new F(); Child.__super__ = Parent.prototype; Child.prototype.constructor = Child; }; }()); /** * Alerts a message in any available way: alert or console.log. */ utils.alert = function(msg) { if (window.alert) { window.alert(msg); } else if (console && console.log) { console.log(msg); } };