UNPKG

uicore-ts

Version:

UICore is a library to build native-like user interfaces using pure Typescript. No HTML is needed at all. Components are described as TS classes and all user interactions are handled explicitly. This library is strongly inspired by the UIKit framework tha

417 lines (416 loc) 12.8 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var __async = (__this, __arguments, generator) => { return new Promise((resolve, reject) => { var fulfilled = (value) => { try { step(generator.next(value)); } catch (e) { reject(e); } }; var rejected = (value) => { try { step(generator.throw(value)); } catch (e) { reject(e); } }; var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected); step((generator = generator.apply(__this, __arguments)).next()); }); }; var UICoreExtensions_exports = {}; module.exports = __toCommonJS(UICoreExtensions_exports); var import_UICoreExtensionValueObject = require("./UICoreExtensionValueObject"); var import_UIObject = require("./UIObject"); const YES = true; const NO = false; if ("removeElementAtIndex" in Array.prototype == NO) { Array.prototype.removeElementAtIndex = function(index) { if (index >= 0 && index < this.length) { this.splice(index, 1); } }; } if ("removeElement" in Array.prototype == NO) { Array.prototype.removeElement = function(element) { this.removeElementAtIndex(this.indexOf(element)); }; } if ("insertElementAtIndex" in Array.prototype == NO) { Array.prototype.insertElementAtIndex = function(index, element) { if (index >= 0 && index <= this.length) { this.splice(index, 0, element); } }; } if ("replaceElementAtIndex" in Array.prototype == NO) { Array.prototype.replaceElementAtIndex = function(index, element) { this.removeElementAtIndex(index); this.insertElementAtIndex(index, element); }; } if ("contains" in Array.prototype == NO) { Array.prototype.contains = function(element) { return this.indexOf(element) != -1; }; } if ("containsAny" in Array.prototype == NO) { Array.prototype.containsAny = function(elements) { return this.anyMatch((element) => elements.contains(element)); }; } if ("anyMatch" in Array.prototype == NO) { Array.prototype.anyMatch = function(functionToCall) { return this.findIndex(functionToCall) > -1; }; } if ("noneMatch" in Array.prototype == NO) { Array.prototype.noneMatch = function(functionToCall) { return this.findIndex(functionToCall) == -1; }; } if ("allMatch" in Array.prototype == NO) { Array.prototype.allMatch = function(functionToCall) { function reversedFunction(value, index, array) { return !functionToCall(value, index, array); } return this.findIndex(reversedFunction) == -1; }; } if ("findAsyncSequential" in Array.prototype == NO) { Array.prototype.findAsyncSequential = function(functionToCall) { function findAsyncSequential(array, predicate) { return __async(this, null, function* () { for (const t of array) { if (yield predicate(t)) { return t; } } return void 0; }); } return findAsyncSequential(this, functionToCall); }; } if ("groupedBy" in Array.prototype == NO) { Array.prototype.groupedBy = function(funcProp) { return this.reduce(function(acc, val) { (acc[funcProp(val)] = acc[funcProp(val)] || []).push(val); return acc; }, {}); }; } if ("uniqueMap" in Array.prototype == NO) { Array.prototype.uniqueMap = function(funcProp) { const result = []; for (let i = 0; i < this.length; i++) { const element = this[i]; const elementResult = funcProp(element); if (!result.contains(elementResult)) { result.push(elementResult); } } return result; }; } if ("firstElement" in Array.prototype == NO) { Object.defineProperty(Array.prototype, "firstElement", { get: function firstElement() { return this[0]; }, set: function(element) { if (this.length == 0) { this.push(element); return; } this[0] = element; } }); } if ("lastElement" in Array.prototype == NO) { Object.defineProperty(Array.prototype, "lastElement", { get: function lastElement() { return this[this.length - 1]; }, set: function(element) { if (this.length == 0) { this.push(element); return; } this[this.length - 1] = element; } }); } if ("everyElement" in Array.prototype == NO) { Object.defineProperty(Array.prototype, "everyElement", { get: function everyElement() { const valueKeys = []; const targetFunction = (objects) => { return this.map((element) => { var _a; const thisObject = import_UIObject.UIObject.valueForKeyPath( valueKeys.arrayByTrimmingToLengthIfLonger(valueKeys.length - 1).join("."), element ) || element; const elementFunction = (_a = import_UIObject.UIObject.valueForKeyPath(valueKeys.join("."), element)) == null ? void 0 : _a.bind( thisObject, objects ); return elementFunction == null ? void 0 : elementFunction(); }); }; const result = new Proxy( targetFunction, { get: (target, key, _receiver) => { if (key == "UI_elementValues") { return this.map((element) => import_UIObject.UIObject.valueForKeyPath( valueKeys.join("."), element )); } valueKeys.push(key); return result; }, set: (target, key, value, _receiver) => { valueKeys.push(key); this.forEach((element) => import_UIObject.UIObject.setValueForKeyPath(valueKeys.join("."), value, element, YES)); return true; } } ); return result; }, set: function(element) { for (let i = 0; i < this.length; ++i) { this[i] = element; } } }); } if ("copy" in Array.prototype == NO) { Array.prototype.copy = function() { return this.slice(0); }; } if ("arrayByRepeating" in Array.prototype == NO) { Array.prototype.arrayByRepeating = function(numberOfRepetitions) { const result = []; for (let i = 0; i < numberOfRepetitions; i++) { this.forEach((element) => result.push(element)); } return result; }; } if ("arrayByTrimmingToLengthIfLonger" in Array.prototype == NO) { Array.prototype.arrayByTrimmingToLengthIfLonger = function(maxLength) { const result = []; for (let i = 0; i < maxLength && i < this.length; i++) { result.push(this[i]); } return result; }; } if ("summedValue" in Array.prototype == NO) { Object.defineProperty(Array.prototype, "summedValue", { get: function summedValue() { return this.reduce(function(a, b) { return a + b; }, 0); } }); } Array.prototype.max = function() { return Math.max.apply(null, this); }; Array.prototype.min = function() { return Math.min.apply(null, this); }; if (!Array.prototype.average) { Array.prototype.average = function() { if (this.length == 0) { return 0; } const sum = this.reduce((a, b) => a + b, 0); return sum / this.length; }; } if ("isEqualToArray" in Array.prototype == NO) { Array.prototype.isEqualToArray = function(array, keyPath) { if (!array) { return false; } if (this.length != array.length) { return false; } var i = 0; const l = this.length; for (; i < l; i++) { if (this[i] instanceof Array && array[i] instanceof Array && !keyPath) { if (!this[i].isEqualToArray(array[i])) { return false; } } else if (keyPath && import_UIObject.UIObject.valueForKeyPath(keyPath, this[i]) != import_UIObject.UIObject.valueForKeyPath( keyPath, array[i] )) { return false; } else if (this[i] != array[i]) { return false; } } return true; }; Object.defineProperty(Array.prototype, "isEqualToArray", { enumerable: false }); } if ("forEach" in Object.prototype == NO) { Object.prototype.forEach = function(callbackFunction) { const keys = Object.keys(this); let shouldStopLooping = NO; function stopLooping() { shouldStopLooping = YES; } keys.anyMatch((key) => { callbackFunction(this[key], key, stopLooping); return shouldStopLooping; }); }; Object.defineProperty(Object.prototype, "forEach", { enumerable: false }); } if ("allValues" in Object.prototype == NO) { Object.defineProperty(Object.prototype, "allValues", { get: function() { const values = []; this.forEach((value) => { values.push(value); }); return values; }, enumerable: NO }); } if ("allKeys" in Object.prototype == NO) { Object.defineProperty(Object.prototype, "allKeys", { get: function() { return Object.keys(this); }, enumerable: NO }); } if ("objectByCopyingValuesRecursivelyFromObject" in Object.prototype == NO) { Object.prototype.objectByCopyingValuesRecursivelyFromObject = function(object) { function isAnObject(item) { return item && typeof item === "object" && !Array.isArray(item); } function mergeRecursively(target, source) { const output = Object.assign({}, target); if (isAnObject(target) && isAnObject(source)) { Object.keys(source).forEach(function(key) { var _a; if (isAnObject(source[key])) { output[key] = mergeRecursively((_a = target[key]) != null ? _a : {}, source[key]); } else { Object.assign(output, { [key]: source[key] }); } }); } return output; } return mergeRecursively(this, object); }; Object.defineProperty(Object.prototype, "objectByCopyingValuesRecursivelyFromObject", { enumerable: false }); } if ("asValueObject" in Object.prototype == NO) { Object.prototype.asValueObject = function() { return new import_UICoreExtensionValueObject.UICoreExtensionValueObject(this); }; Object.defineProperty(Object.prototype, "asValueObject", { enumerable: false }); } if ("contains" in String.prototype == NO) { String.prototype.contains = function(string) { return this.indexOf(string) != -1; }; Object.defineProperty(Object.prototype, "contains", { enumerable: false }); } if ("capitalizedString" in String.prototype == NO) { Object.defineProperty(Object.prototype, "capitalizedString", { get: function() { return this.charAt(0).toUpperCase() + this.slice(1).toLowerCase(); }, enumerable: NO }); } if ("numericalValue" in String.prototype == NO) { Object.defineProperty(String.prototype, "numericalValue", { get: function numericalValue() { return Number(this); } }); } if ("integerValue" in String.prototype == NO) { Object.defineProperty(String.prototype, "integerValue", { get: function integerValue() { return Number(this).integerValue; } }); } if ("isAString" in String.prototype == NO) { String.prototype.isAString = YES; } if ("isANumber" in Number.prototype == NO) { Number.prototype.isANumber = YES; } if ("integerValue" in Number.prototype == NO) { Object.defineProperty(Number.prototype, "integerValue", { get: function() { return parseInt("" + (Math.round(this) + 0.5)); }, enumerable: NO }); } if ("constrainedValue" in Number.prototype == NO) { Number.prototype.constrainedValue = function(min, max) { if (this < min) { return min; } if (this > max) { return max; } return this; }; Object.defineProperty(Number.prototype, "constrainedValue", { enumerable: NO }); } if ("integerValue" in Boolean.prototype == NO) { Object.defineProperty(Boolean.prototype, "integerValue", { get: function() { if (this == true) { return 1; } return 0; } }); } if ("dateString" in Date.prototype == NO) { Object.defineProperty(Date.prototype, "dateString", { get: function dateString() { return ("0" + this.getDate()).slice(-2) + "-" + ("0" + (this.getMonth() + 1)).slice(-2) + "-" + this.getFullYear() + " " + ("0" + this.getHours()).slice(-2) + ":" + ("0" + this.getMinutes()).slice(-2); } }); } //# sourceMappingURL=UICoreExtensions.js.map