UNPKG

inet-core

Version:
1,214 lines (1,213 loc) 161 kB
import * as i0 from '@angular/core'; import { Injectable, Input, Directive, Pipe, HostListener, inject, provideAppInitializer, NgModule } from '@angular/core'; import { combineLatest, of, Observable, fromEvent, Subject, forkJoin } from 'rxjs'; import { map, tap, shareReplay, catchError } from 'rxjs/operators'; import * as i1 from '@angular/common/http'; import { HttpClient, HttpHeaders, HttpParams, provideHttpClient, HttpRequest } from '@angular/common/http'; import { DatePipe, CommonModule } from '@angular/common'; (function (global, factory) { "use strict"; if (typeof module === "object" && typeof module.exports === "object") { // For CommonJS and CommonJS-like environments where a proper `window` // is present, execute the factory and get iNet. // For environments that do not have a `window` with a `document` // (such as Node.js), expose a factory as module.exports. // This accentuates the need for the creation of a real `window`. // e.g. var iNet = require("iNet")(window); module.exports = global.document ? factory(global, true) : function (w) { return factory(w); }; } else { factory(global); } // Pass this if window is not defined yet })(typeof window !== "undefined" ? window : this, function (window, noGlobal) { /*-------------------------------------------- | C O N S T R U C T O R S | ============================================*/ /** * Define the iNet instance. */ /** * iNet core utilities and functions. * * The iNet namespace (global object) encapsulates all classes, singletons, and utility methods provided by iNet Solution's libraries. * @class iNet * @singleton */ var iNet = { /** * @property {String} * @readonly * The version of the framework * @type String Major[.Minor[.Patch[.Build[Release]]]] * */ version: '6.0.0.1', /** * The date release the framework * @property {String} * @readonly */ build_date: '14/06/2022', /** * @property {String} BLANK_IMAGE_URL * URL to a 1x1 transparent gif image used by iNet to create inline icons with * CSS background images. */ BLANK_IMAGE_URL: 'data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==' }; /*-------------------------------------------- | M E T H O D S | ============================================*/ /** * Copies all the properties of config to obj. * Example: * * var a = {a:1,b:2,c:5}; * var b = {b:3,d:7}; * iNet.apply(a, b); * // a ={ a: 1, b: 3, c: 5, d: 7}; * * @param {Object} obj The receiver of the properties * @param {Object} config The source of the properties * @param {Object} defaults A different object that will also be applied for default values * @return {Object} returns obj * @member iNet apply */ iNet.apply = function (o, c, defaults) { // no "this" reference for friendly out of scope calls if (defaults) { iNet.apply(o, defaults); } if (o && c && typeof c == 'object') { for (var p in c) { o[p] = c[p]; } } return o; }; (function () { var ua = navigator.userAgent.toLowerCase(), toString = Object.prototype.toString, check = function (r) { return r.test(ua); }, DOC = document, isStrict = DOC.compatMode == "CSS1Compat", docMode = document.documentMode, isOpera = check(/opera/), isChrome = check(/chrome/), isWebKit = check(/webkit/), isSafari = !isChrome && check(/safari/), isSafari2 = isSafari && check(/applewebkit\/4/), // unique to Safari 2 isSafari3 = isSafari && check(/version\/3/), isSafari4 = isSafari && check(/version\/4/), isIE = !isOpera && check(/msie/), isIE7 = isIE && (check(/msie 7/) || docMode == 7), isIE8 = isIE && (check(/msie 8/) && docMode != 7 && docMode != 9 && docMode != 10 || docMode == 8), isIE9 = isIE && (check(/msie 9/) && docMode != 7 && docMode != 8 && docMode != 10 || docMode == 9), isIE10 = isIE && (check(/msie 10/) && docMode != 7 && docMode != 8 && docMode != 9 || docMode == 10), isIE6 = isIE && !isIE7 && !isIE8 && !isIE9 && !isIE10, isSupport = (isIE && (docMode && !(docMode >= 7))) || !isIE, isGecko = !isWebKit && check(/gecko/), isGecko2 = isGecko && check(/rv:1\.8/), isGecko3 = isGecko && check(/rv:1\.9/), isGecko4 = isGecko && check(/rv:2\.0/), isBorderBox = isIE && !isStrict, isWindows = check(/windows|win32/), isMac = check(/macintosh|mac os x/), isAir = check(/adobeair/), isLinux = check(/linux/), isSecure = /^https/i.test(window.location.protocol), propertyNameSplitRe = /[,;\s]+/; iNet.apply(iNet, { /** * Returns true if the passed object is a JavaScript Function, otherwise false. * @param {Object} object The object to test * @return {Boolean} */ isFunction: function (v) { return toString.apply(v) === '[object Function]'; }, /** * Returns the current context path. * * @return {String} The current context path */ getContextPath: function () { var pathname = window.location.pathname; if (pathname == '/') { return '/'; } var position = pathname.indexOf('/', 1); if (position == 0) { position = pathname.length; } return pathname.substr(0, position); }, /** * return a new MediaQueryList object. * @param {String} media Is a string mediaQueryString * @return MediaQueryList object */ matchMedia: function (media) { return window.matchMedia(media); }, /** * Returns the true if viewport is match with width value, otherwise false. * @param {Number} v The width to test * @returns {Boolean} */ matchWidth: function (v) { return iNet.matchMedia("(min-width:" + v + "px)").matches; }, /** * Returns the full url from the given relative path. * * @param {String} relativePath (required) The given relative path. * * @return {String} The string URL */ getUrl: function (relativePath) { relativePath = relativePath || ''; var url = window.location.protocol + '//'; url += window.location.hostname; var __port = window.location.port; if (!iNet.isEmpty(__port)) { if (isSecure && (__port != 443)) { url += ':' + __port; } else if (!isSecure && __port != 80) { url += ':' + __port; } } // append the context path. var pathname = window.location.pathname; if (pathname.length > 1) { var position = pathname.indexOf('/', 1); if (position == 0) position = pathname.length; url += pathname.substr(0, position); } var prefix = iNet.prefix || 'smartcloud'; var extension = iNet.extension || '.cpx'; if (relativePath.indexOf('/') == 0) { return url + '/' + prefix + relativePath + extension; } else { return url + '/' + prefix + '/' + relativePath + extension; } }, /** * Returns full url form given relative path with pattern * @param {String} url The URL * @param {String} pattern (optional) The pattern of module * @return {String} The string URL */ getXUrl: function (url, pattern) { var __pattern = !iNet.isEmpty(pattern) ? pattern : iNet.pattern; if (!iNet.isEmpty(__pattern)) { return iNet.getUrl(__pattern + "/" + url); } return iNet.getUrl(url); }, /** * Returns full url form given relative path with firm prefix * @param {String} path (required) The URL * @return {String} The string URL */ getPUrl: function (path) { path = path || ''; var url = window.location.protocol + '//'; url += window.location.hostname; var __port = window.location.port; if (!iNet.isEmpty(__port)) { if (isSecure && (__port != 443)) { url += ':' + __port; } else if (!isSecure && __port != 80) { url += ':' + __port; } } // append the context path. var pathname = window.location.pathname; if (pathname.length > 1) { var position = pathname.indexOf('/', 1); if (position == 0) position = pathname.length; url += pathname.substr(0, position); } var prefix = iNet.firmPrefix || iNet.prefix || 'smartcloud'; var extension = iNet.extension || '.cpx'; if (prefix === 'icloud') { prefix = 'smartcloud'; } if (path.indexOf('/') == 0) { return url + '/' + prefix + path + extension; } else { return url + '/' + prefix + '/' + path + extension; } }, /** * Returns the SSO redirect url from the given url path. * * @param {String} url (required) The given url. * @param {String} app (option) The given application. * * @return {String} The string SSO redirect URL */ getSSOUrl: function (url, app) { if (iNet.isEmpty(url)) { return ''; } return iNet.urlAppend(iNet.getUrl('ssorequest/redirect'), (!iNet.isEmpty(app) ? 'application=' + app + '&' : '') + 'url=' + encodeURIComponent(url)); }, /** * Returns the Discovery redirect url from the given url path. * * @param {String} url (required) The given url. * @param {String} app (option) The given application. * * @return {String} The string SSO Discovery redirect URL */ getSSODiscoveryUrl: function (url, app) { if (iNet.isEmpty(url)) { return ''; } return iNet.urlAppend(iNet.getUrl('ssodiscovery/redirect'), (!iNet.isEmpty(app) ? 'application=' + app + '&' : '') + 'url=' + encodeURIComponent(url)); }, /** * Returns the Discovery redirect url from the given url path. * * @param {String} app (option) The given application. * @param {Object} params (option) The given params. * * @return {String} The string SSO Callback redirect URL */ getSSOCallback: function (app, params) { var __params = params || {}; if (!iNet.isEmpty(app)) { __params.application = app; } var __pairs = Object.keys(__params).map(function (k) { return encodeURIComponent(k) + "=" + encodeURIComponent(__params[k]); }).join('&'); return iNet.urlAppend(iNet.getPUrl('ssocallback/redirect'), __pairs); }, /** * Appends content to the query string of a URL, handling logic for whether to place * a question mark or ampersand. * @param {String} url The URL to append to. * @param {String} s The content to append to the URL. * @return (String) The resulting URL */ urlAppend: function (url, s) { if (!iNet.isEmpty(s)) { return url + (url.indexOf('?') === -1 ? '?' : '&') + s; } return url; }, /** * Clone simple variables including array, {}-like objects, DOM nodes and Date without keeping the old reference. * A reference for the object itself is returned if it's not a direct descendant of Object. * * @param {Object} obj The object to clone * @return {Object} The new object */ clone: function (obj) { if (null == obj || "object" != typeof obj) { return obj; } var copy = obj.constructor(); if (copy) { for (var attr in obj) { if (obj.hasOwnProperty(attr)) { copy[attr] = obj[attr]; } } } return copy; }, /** * @method copy * @member iNet * Copies a set of named properties fom the source object to the destination object. * * Example: * * var foo = { a: 1, b: 2, c: 3 }; * var bar = iNet.copy({}, foo, 'a,c'); * // bar = { a: 1, c: 3 }; * * @param {Object} dest The destination object. * @param {Object} source The source object. * @param {String/String[]} names Either an Array of property names, or a comma-delimited list * of property names to copy. * @param {Boolean} [usePrototypeKeys=false] Pass `true` to copy keys off of the * prototype as well as the instance. * @return {Object} The `dest` object. */ copy: function (dest, source, names, usePrototypeKeys) { if (typeof names === 'string') { names = names.split(propertyNameSplitRe); } for (var name, i = 0, n = names ? names.length : 0; i < n; i++) { name = names[i]; // Only copy a property if the source actually *has* that property. // If we are including prototype properties, then ensure that a property of // that name can be found *somewhere* in the prototype chain (otherwise we'd be copying undefined in which may break things) if (source.hasOwnProperty(name) || (usePrototypeKeys && name in source)) { dest[name] = source[name]; } } return dest; }, /** * @method copyToIf * @member iNet * Copies a set of named properties fom the source object to the destination object * if the destination object does not already have them. * * Example: * * var foo = { a: 1, b: 2, c: 3 }; * var bar = iNet.copyIf({ a:115 }, foo, 'a,c'); * // bar = { a: 115, c: 3 }; * * @param {Object} destination The destination object. * @param {Object} source The source object. * @param {String/String[]} names Either an Array of property names, or a single string * with a list of property names separated by ",", ";" or spaces. * @return {Object} The `dest` object. */ copyIf: function (destination, source, names) { if (typeof names === 'string') { names = names.split(propertyNameSplitRe); } for (var name, i = 0, n = names ? names.length : 0; i < n; i++) { name = names[i]; if (destination[name] === undefined) { destination[name] = source[name]; } } return destination; }, /** * Returns true if the passed object is a JavaScript Object, otherwise false. * * @param {Object} v The object to test * @return {Boolean} */ isObject: function (v) { return v && typeof v == "object"; }, /** * Returns true if the passed object is a string. * * @param {Object} v The object to test * @return {Boolean} */ isString: function (v) { return typeof v === 'string'; }, /** * Returns true if the passed object is a number. Returns false for * non-finite numbers. * * @param {Object} v The object to test * @return {Boolean} */ isNumber: function (v) { return typeof v === 'number' && isFinite(v); }, /** * Returns true if the passed object is a boolean. * * @param {Object} v The object to test * @return {Boolean} */ isBoolean: function (v) { return typeof v === 'boolean'; }, /** * Returns true if the passed object is a JavaScript array, otherwise false. * * @param {Object} v The object to test * @return {Boolean} */ isArray: function (v) { return toString.apply(v) === '[object Array]'; }, /** * Returns true if the passed object is not undefined. * * @param {Object} v The object to test * @return {Boolean} */ isDefined: function (v) { return typeof v !== 'undefined'; }, /** * Returns true if the passed value is iterable, false otherwise * @param {Mixed} value The value to test * @return {Boolean} */ isIterable: function (value) { return (value && typeof value !== 'string') ? value.length !== undefined : false; }, /** * Returns true if the passed value is empty. The value is deemed to be * empty if it is * null * undefined * an empty array * a zero length * an empty object * string (Unless the allowBlank parameter is true) * * @param {Mixed} value The value to test * @param {Boolean} allowBlank (optional) true to allow empty strings (defaults to false) * @return {Boolean} */ isEmpty: function (v, allowBlank) { return v === null || v === undefined || ((iNet.isArray(v) && !v.length)) || (!allowBlank ? v === '' : false) || iNet.isEmptyObject(v); }, /** * Returns the given value itself if it's not empty, as described in {@link iNet#isEmpty}; returns the default * value (second argument) otherwise. * * @param {Object} value The value to test. * @param {Object} defaultValue The value to return if the original value is empty. * @param {Boolean} [allowBlank=false] `true` to allow zero length strings to qualify as non-empty. * @return {Object} value, if non-empty, else defaultValue. */ valueFrom: function (value, defaultValue, allowBlank) { return iNet.isEmpty(value, allowBlank) ? defaultValue : value; }, /** * Return true if passed object is empty. * @param {Mixed} value The value to test * @returns {boolean} */ isEmptyObject: function (v) { if (!iNet.isObject(v)) { return false; } for (var key in v) { return false; } return true; }, /** * Returns true if the passed object is a email. * * @param {Object} v The object to test * @return {Boolean} */ isEmail: function (v) { return /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(v); }, /** * Returns true if the passed value is a JavaScript Date object, false otherwise. * @param {Object} v The object to test * @return {Boolean} */ isDate: function (v) { return toString.call(v) === '[object Date]'; }, /** * Returns true if the passed value is a JavaScript 'primitive', a string, number or boolean. * @param {Mixed} value The value to test * @return {Boolean} */ isPrimitive: function (value) { var type = typeof value; return type === 'string' || type === 'number' || type === 'boolean'; }, /** * Returns `true` if the passed value is an HTMLElement * @param {Object} value The value to test. * @return {Boolean} */ isElement: function (value) { return value ? value.nodeType === 1 : false; }, /** * True if the detected browser is Opera. * * @type Boolean */ isOpera: isOpera, /** * True if the detected browser uses WebKit. * * @type Boolean */ isWebKit: isWebKit, /** * True if the detected browser is Chrome. * * @type Boolean */ isChrome: isChrome, /** * True if the detected browser is Safari. * * @type Boolean */ isSafari: isSafari, /** * True if the detected browser is Safari 3.x. * * @type Boolean */ isSafari3: isSafari3, /** * True if the detected browser is Safari 4.x. * * @type Boolean */ isSafari4: isSafari4, /** * True if the detected browser is Safari 2.x. * * @type Boolean */ isSafari2: isSafari2, /** * True if the detected browser is Internet Explorer. * * @type Boolean */ isIE: isIE, /** * True if the detected browser is Internet Explorer 6.x. * * @type Boolean */ isIE6: isIE6, /** * True if the detected browser is Internet Explorer 7.x. * * @type Boolean */ isIE7: isIE7, /** * True if the detected browser is Internet Explorer 8.x. * * @type Boolean */ isIE8: isIE8, /** * True if the detected browser is Internet Explorer 9.x. * * @type Boolean */ isIE9: isIE9, /** * True if the detected browser is Internet Explorer 10.x. * * @type Boolean */ isIE10: isIE10, /** * True if the detected browser uses the Gecko layout engine (e.g. Mozilla, * Firefox). * * @type Boolean */ isGecko: isGecko, /** * True if the detected browser uses a pre-Gecko 1.9 layout engine (e.g. * Firefox 2.x). * * @type Boolean */ isGecko2: isGecko2, /** * True if the detected browser uses a Gecko 1.9+ layout engine (e.g. * Firefox 3.x). * * @type Boolean */ isGecko3: isGecko3, /** * True if the detected browser uses a Gecko 1.9+ layout engine (e.g. * Firefox 4.x). * * @type Boolean */ isGecko4: isGecko4, /** * True if the detected browser is Internet Explorer running in non-strict * mode. * * @type Boolean */ isBorderBox: isBorderBox, /** * True if the detected platform is Linux. * * @type Boolean */ isLinux: isLinux, /** * True if the detected platform is Windows. * * @type Boolean */ isWindows: isWindows, /** * True if the detected platform is Mac OS. * * @type Boolean */ isMac: isMac, /** * True if the detected platform is Adobe Air. * * @type Boolean */ isAir: isAir, /** * True if the detected browser is Support. * * @type Boolean */ isSupport: isSupport, /** * Converts any iterable (numeric indices and a length property) into a * true array Don't use this on strings. IE doesn't support "abc"[0] * which this implementation depends on. For strings, use this instead: * "abc".match(/./g) => [a,b,c]; * * @param {Iterable} the iterable object to be turned into a true Array. * @return (Array) array */ toArray: function () { return isIE ? function (a, i, j, res) { res = []; iNet.each(a, function (v) { res.push(v); }); return res.slice(i || 0, j || res.length); } : function (a, i, j) { return Array.prototype.slice.call(a, i || 0, j || a.length); }; }(), /** * Copies all the properties of config to obj if they don't already exist. * * @param {Object} obj The receiver of the properties * @param {Object} config The source of the properties * @return {Object} returns obj */ applyIf: function (o, c) { if (o) { for (var p in c) { if (iNet.isEmpty(o[p])) { o[p] = c[p]; } } } return o; }, /** * Iterates an array or an iterable value and invoke the given callback function for each item. * * @param {Object} array The item at the current index in the passed array * @param {Object} fn The callback function * @param {Object} scope (optional) The scope (this reference) in which the specified function is executed. */ each: function (array, fn, scope) { if (iNet.isEmpty(array, true)) { return; } for (var i = 0, len = array.length; i < len; i++) { if (fn.call(scope || array[i], array[i], i, array) === false) { return i; } } }, /** * Creates namespaces to be used for scoping variables and classes so that they are not global. Specifying the last node of a namespace implicitly creates all other nodes. * Usage: * * iNet.namespace('iNet.ui', 'iNet.ui.form'); * * // equivalent and preferable to the above syntax * iNet.ns('iNet.ui',, 'iNet.ui.form'); * * iNet.ui.form.Control = function() { ... }; * * @param {String...} namespaces * @return {Object} The (last) namespace object created. * @member iNet * @method namespace */ namespace: function () { var o, d; iNet.each(arguments, function (v) { d = v.split("."); o = window[d[0]] = window[d[0]] || {}; iNet.each(d.slice(1), function (v2) { o = o[v2] = o[v2] || {}; }); }); return o; }, /** * Get parameter from URL * * @param {String} name The parameter name * @param {String} url (optional) The URL * * @return {String} Returns the value of param */ getParam: function (name, url) { if (!!window['ctx_' + name]) { return window['ctx_' + name]; } name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]"); var regexS = "[\\?&]" + name + "=([^&#]*)"; var regex = new RegExp(regexS); var results = regex.exec(url || window.location.href); if (!results) { return ""; } else { return results[1]; } }, /** * Parser html to string * * @param {String} v the given value * @return {String} The new string */ parserHtml: function (v) { if (v === undefined || v === null || v.length === undefined) { return ''; } v.replace(/</g, "&lt;"); v.replace(/>/g, "&gt;"); v.replace(/\n/g, '<br />'); return v; }, /** * Generate the identifier. * @return {String} The string */ generateId: function () { var sid = ""; for (var i = 0; i < 24; i++) { sid += Math.floor(Math.random() * 0xF).toString(0xF); } return sid; }, /** * Generate the UUID. * @return {String} The string UUID */ generateUUID: function () { var d = new Date().getTime(); var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { var r = (d + Math.random() * 16) % 16 | 0; d = Math.floor(d / 16); return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16); }); return uuid; }, /** * Generate the identifier as alphabet. * @return {String} The string */ alphaGenerateId: function () { var __id = ''; var __alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; var len = __alphabet.length; for (var index = 0; index < len; index++) { __id += __alphabet.charAt(Math.floor(Math.random() * len)); } // return the identifier 24 character return __id.substr(len - 24); }, /** * Returns the current timestamp * @param {Number} time (optional) the timestamp * @return {Number} The current timestamp */ getDate: function (time) { var __time = time || new Date().getTime(); try { return (new Date(__time)).getTime(); } catch (e) { return (new Date()).getTime(); } }, /** * Get layout of framework * @returns {window} The given window document */ getLayout: function () { return window.parent; }, /** * Get current Application * @return {iNet.app.Application} The current application */ getApp: function () { var layout = iNet.getLayout(); var app = null; if (layout && layout.iNet) { app = layout.iNet.APP; } return app; }, /** * Return number item of object * @param {Object} v The object to get size * @return {Number} */ getSize: function (v) { var size = 0, key; if (iNet.isArray(v)) { size = v.length; } else if (iNet.isObject(v)) { for (key in v) { if (v.hasOwnProperty(key)) { size++; } } } else { size = v.length; } return size; }, /** * Return currency with currency code * @deprecated 5.6 * @param {Number} v The number of currency * @returns {String} */ getCurrency: function (v) { var __lang = iNet.lang || iNet.language; var __numberArr = v.toString().split('.'); __numberArr[0] = __numberArr[0].replace(/(\d)(?=(\d\d\d)+(?!\d))/g, '$1,'); var __result = __numberArr.join('.'); switch (__lang) { case 'en': return String.format('{0} USD', __result); break; default: return String.format('{0} VND', __result); } }, /** * Returns the given value itself if it's not empty, as described in {@link iNet#isEmpty}; returns the default * value (second argument) otherwise. * * @param {Object} value The value to test. * @param {Object} defaultValue The value to return if the original value is empty. * @param {Boolean} [allowBlank=false] `true` to allow zero length strings to qualify as non-empty. * @return {Object} value, if non-empty, else defaultValue. */ valueFrom: function (value, defaultValue, allowBlank) { return iNet.isEmpty(value, allowBlank) ? defaultValue : value; }, /** * @property {Function} * A reusable empty function. */ emptyFn: function () { }, /** * Create a new function from the provided `fn`, the arguments of which are pre-set to `args`. * New arguments passed to the newly created callback when it's invoked are appended after the pre-set ones. * This is especially useful when creating callbacks. * @param {Function} fn The original function. * @param {Array} args The arguments to pass to new callback. * @param {Object} scope (optional) The scope (`this` reference) in which the function is executed. * @return {Function} The new callback function. */ pass: function (fn, args, scope) { if (!iNet.isArray(args)) { if (iNet.isIterable(args)) { args = iNet.Array.clone(args); } else { args = args !== undefined ? [args] : []; } } return function () { var fnArgs = args.slice(); fnArgs.push.apply(fnArgs, arguments); return fn.apply(scope || this, fnArgs); }; }, /** * Overrides members of the specified target with the given values. * @param {Object} origclass The class to override * @param {Object} overrides The list of functions to add to origClass. * This should be specified as an object literal containing one or more methods. * @method override */ override: function (origclass, overrides) { if (overrides) { var p = origclass.prototype; iNet.apply(p, overrides); if (iNet.isIE && overrides.toString != origclass.toString) { p.toString = overrides.toString; } } }, /** * Defines a class or override * @param {Function} subclass The class inheriting the functionality * @param {Function} superclass The class being extended * @param {Object} overrides (optional) A literal with members which are copied * into the subclass's prototype, and are therefore shared between all instances of the new class. * @return {Function} The subclass constructor. * @method extend */ extend: function () { // inline overrides var io = function (o) { for (var m in o) { this[m] = o[m]; } }; var oc = Object.prototype.constructor; return function (sb, sp, overrides) { if (iNet.isObject(sp)) { overrides = sp; sp = sb; sb = overrides.constructor != oc ? overrides.constructor : function () { sp.apply(this, arguments); }; } var F = function () { }, sbp, spp = sp.prototype; F.prototype = spp; sbp = sb.prototype = new F(); sbp.constructor = sb; sb.superclass = spp; if (spp.constructor == oc) { spp.constructor = sp; } sb.override = function (o) { iNet.override(sb, o); }; sbp.superclass = sbp.supr = (function () { return spp; }); sbp.override = io; iNet.override(sb, overrides); sb.extend = function (o) { iNet.extend(sb, o); }; return sb; }; }(), getIconByAppName: function (appName, domain) { if (iNet.isEmpty(domain) || iNet.isEmpty(appName)) throw new Error("Domain or application name must not empty"); var url = new URL(domain); var __iconFile = appName.toLowerCase() + '.png'; return url.origin + url.pathname.substring(0, url.pathname.indexOf("/", 2)) + '/images/' + __iconFile; }, /* * Load JavaScript file (asynchronously) * @param {String} src The URL of an Javascript * @return {Function} onLoad The new callback function. */ loadScript: function (src, onLoad) { var script = document.createElement('script'); script.type = 'text/javascript'; script.src = src; if (script.readyState) { script.onreadystatechange = function () { var state = this.readyState; if (state === 'loaded' || state === 'complete') { script.onreadystatechange = null; onLoad(); } }; } else { script.onload = onLoad; } document.getElementsByTagName('head')[0].appendChild(script); } }); /** * Convenient alias for {@link iNet#namespace iNet.namespace}. * @inheritdoc iNet#namespace * @member iNet * @method ns */ iNet.ns = iNet.namespace; })(); /** * Support URL decode, encode. * * @class iNet.Url * */ iNet.Url = { /** * URL encoding strings * @param string The string to encode * @returns {String} The new String */ encode: function (string) { return escape(this._utf8_encode(string)); }, /** * URL decoding to string * @param string the String to decode * @returns {String} The new String */ decode: function (string) { return this._utf8_decode(unescape(string)); }, // /** * private method for UTF-8 encoding * @param string The string to encode * @returns {string} The new String * @private */ _utf8_encode: function (string) { string = string.replace(/\r\n/g, "\n"); var utftext = ""; for (var n = 0; n < string.length; n++) { var c = string.charCodeAt(n); if (c < 128) { utftext += String.fromCharCode(c); } else if ((c > 127) && (c < 2048)) { utftext += String.fromCharCode((c >> 6) | 192); utftext += String.fromCharCode((c & 63) | 128); } else { utftext += String.fromCharCode((c >> 12) | 224); utftext += String.fromCharCode(((c >> 6) & 63) | 128); utftext += String.fromCharCode((c & 63) | 128); } } return utftext; }, /** * private method for UTF-8 decoding * @param utftext The string to decode * @returns {string} The new String * @private */ _utf8_decode: function (utftext) { var string = ""; var i = 0; var c = 0, c1 = 0, c2 = 0, c3 = 0; while (i < utftext.length) { c = utftext.charCodeAt(i); if (c < 128) { string += String.fromCharCode(c); i++; } else if ((c > 191) && (c < 224)) { c2 = utftext.charCodeAt(i + 1); string += String.fromCharCode(((c & 31) << 6) | (c2 & 63)); i += 2; } else { c2 = utftext.charCodeAt(i + 1); c3 = utftext.charCodeAt(i + 2); string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63)); i += 3; } } return string; } }; iNet.Template = { parse: function (templateId, obj) { var templateElement = document.getElementById(templateId); var converter = function (data) { return data; }; function getTemplateText(element) { return element.innerHTML.toString(); } if (iNet.isFunction(templateElement['converter'])) converter = templateElement['converter']; var item = converter(obj); return getTemplateText(templateElement).replace(/{(\w+)}/g, function (str, key) { return (typeof item[key] !== 'undefined') ? item[key] : ''; }); } }; /*-------------------------------------------- | S C O P E F U N C T I O N | ============================================*/ /** * These functions are available on every Function object (any JavaScript function). * @class Function */ iNet.apply(Function.prototype, { /** * Creates an interceptor function. The passed fcn is called before the * original one. If it returns false, the original one is not called. The * resulting function returns the results of the original function. The passed * fcn is called with the parameters of the original function. * * @param {Function} fcn The function to call before the original * @param {Object} scope (optional) The scope of the passed fcn (Defaults to scope of original function or window) * @return {Function} The new function */ createInterceptor: function (fcn, scope) { var method = this; return !iNet.isFunction(fcn) ? this : function () { var me = this, args = arguments; fcn.target = me; fcn.method = method; return (fcn.apply(scope || me || window, args) !== false) ? method.apply(me || window, args) : null; }; }, /** * Creates a callback that passes arguments[0], arguments[1], arguments[2], * ... Call directly on any function. Example: * myFunction.createCallback(arg1, arg2); * //Will create a function that is bound to those 2 args. executes in the window scope. * * @return {Function} The new function */ createCallback: function () { // make args available, in function below var args = arguments, method = this; return function () { return method.apply(window, args); }; }, /** * Creates a delegate (callback) that sets the scope to obj. Call directly * on any function. * Example: * * this.myFunction.createDelegate(this, [arg1, arg2]); * //Will create a function that is automatically scoped to obj so that the <tt>this</tt> variable inside the callback points to obj. * * @param {Object} obj (optional) The object for which the scope is set * @param {Array} args (optional) Overrides arguments for the call. (Defaults to the arguments passed by the caller) * @param {Boolean/Number} appendArgs (optional) if True args are appended to call args instead of overriding, * if a number the args are inserted at the specified position * @return {Function} The new function */ createDelegate: function (obj, args, appendArgs) { var method = this; return function () { var callArgs = args || arguments; if (appendArgs === true) { callArgs = Array.prototype.slice.call(arguments, 0); callArgs = callArgs.concat(args); } else if (iNet.isNumber(appendArgs)) { callArgs = Array.prototype.slice.call(arguments, 0); // copy arguments first var applyArgs = [appendArgs, 0].concat(args); // create method call params Array.prototype.splice.apply(callArgs, applyArgs); // splice them in } return method.apply(obj || window, callArgs); }; }, /** * Calls this function after the number of millseconds specified, optionally * in a specific scope. Example usage: * * var sayHi = function(name){ * alert('Hi, ' + name); * } * * // executes immediately: * sayHi('Fred'); * * // executes after 2 seconds: * Function.defer(sayHi, 2000, this, ['Fred']); * * // this syntax is sometimes useful for deferring * // execution of an anonymous function: * Function.defer(function(){ * alert('Anonymous'); * }, 100); * * @param {Number} millis The number of milliseconds for the setTimeout call