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

502 lines (501 loc) 15.6 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; 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 UIObject_exports = {}; __export(UIObject_exports, { CALL: () => CALL, EXTEND: () => EXTEND, FIRST: () => FIRST, FIRST_OR_NIL: () => FIRST_OR_NIL, IF: () => IF, IS: () => IS, IS_AN_EMAIL_ADDRESS: () => IS_AN_EMAIL_ADDRESS, IS_DEFINED: () => IS_DEFINED, IS_LIKE_NULL: () => IS_LIKE_NULL, IS_NIL: () => IS_NIL, IS_NOT: () => IS_NOT, IS_NOT_LIKE_NULL: () => IS_NOT_LIKE_NULL, IS_NOT_NIL: () => IS_NOT_NIL, IS_UNDEFINED: () => IS_UNDEFINED, LAZY_VALUE: () => LAZY_VALUE, MAKE_ID: () => MAKE_ID, NO: () => NO, RETURNER: () => RETURNER, UIFunctionCall: () => UIFunctionCall, UIFunctionExtender: () => UIFunctionExtender, UILazyPropertyValue: () => UILazyPropertyValue, UIObject: () => UIObject, YES: () => YES, nil: () => nil, wrapInNil: () => wrapInNil }); module.exports = __toCommonJS(UIObject_exports); var import_UICoreExtensionValueObject = require("./UICoreExtensionValueObject"); var import_UITimer = require("./UITimer"); function NilFunction() { return nil; } var nil = new Proxy(Object.assign(NilFunction, { "class": null, "className": "Nil" }), { get(target, name) { if (name == Symbol.toPrimitive) { return function(hint) { if (hint == "number") { return 0; } if (hint == "string") { return ""; } return false; }; } if (name == "toString") { return function toString() { return ""; }; } return NilFunction(); }, set() { return NilFunction(); } }); window.nil = nil; function wrapInNil(object) { let result = FIRST_OR_NIL(object); if (object instanceof Object && !(object instanceof Function)) { result = new Proxy(object, { get(target, name) { if (name == "wrapped_nil_target") { return target; } const value = Reflect.get(target, name); if (typeof value === "object") { return wrapInNil(value); } if (IS_NOT_LIKE_NULL(value)) { return value; } return nil; }, set(target, name, value) { if (IS(target)) { target[name] = value; } return YES; } }); } return result; } const YES = true; const NO = false; function IS(object) { if (object && object !== nil) { return YES; } return NO; } function IS_NOT(object) { return !IS(object); } function IS_DEFINED(object) { if (object != void 0) { return YES; } return NO; } function IS_UNDEFINED(object) { return !IS_DEFINED(object); } function IS_NIL(object) { if (object === nil) { return YES; } return NO; } function IS_NOT_NIL(object) { return !IS_NIL(object); } function IS_LIKE_NULL(object) { return IS_UNDEFINED(object) || IS_NIL(object) || object == null; } function IS_NOT_LIKE_NULL(object) { return !IS_LIKE_NULL(object); } function IS_AN_EMAIL_ADDRESS(email) { const re = /\S+@\S+\.\S+/; return re.test(email); } function FIRST_OR_NIL(...objects) { const result = objects.find((object) => IS(object)); return result || nil; } function FIRST(...objects) { const result = objects.find((object) => IS(object)); return result || IF(IS_DEFINED(objects.lastElement))(RETURNER(objects.lastElement))(); } function MAKE_ID(randomPartLength = 15) { let result = ""; const characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; for (let i = 0; i < randomPartLength; i++) { result = result + characters.charAt(Math.floor(Math.random() * characters.length)); } result = result + Date.now(); return result; } function RETURNER(value) { return (..._objects) => value; } function IF(value) { let thenFunction = nil; let elseFunction = nil; const result = function(functionToCall) { thenFunction = functionToCall; return result.evaluateConditions; }; result.evaluateConditions = function() { if (IS(value)) { return thenFunction(); } return elseFunction(); }; result.evaluateConditions.ELSE_IF = function(otherValue) { const functionResult = IF(otherValue); elseFunction = functionResult.evaluateConditions; const functionResultEvaluateConditionsFunction = function() { return result.evaluateConditions(); }; functionResultEvaluateConditionsFunction.ELSE_IF = functionResult.evaluateConditions.ELSE_IF; functionResultEvaluateConditionsFunction.ELSE = functionResult.evaluateConditions.ELSE; functionResult.evaluateConditions = functionResultEvaluateConditionsFunction; return functionResult; }; result.evaluateConditions.ELSE = function(functionToCall) { elseFunction = functionToCall; return result.evaluateConditions(); }; return result; } class UIFunctionCall { constructor(...parameters) { this.isAUIFunctionCallObject = YES; this.parameters = parameters; } callFunction(functionToCall) { const parameters = this.parameters; functionToCall(...parameters); } } function CALL(...objects) { return new UIFunctionCall(...objects); } class UIFunctionExtender { constructor(extendingFunction) { this.isAUIFunctionExtenderObject = YES; this.extendingFunction = extendingFunction; } static functionByExtendingFunction(functionToExtend, extendingFunction) { return EXTEND(extendingFunction).extendedFunction(functionToExtend); } extendedFunction(functionToExtend) { const extendingFunction = this.extendingFunction; function extendedFunction(...objects) { const boundFunctionToExtend = functionToExtend.bind(this); boundFunctionToExtend(...objects); const boundExtendingFunction = extendingFunction.bind(this); boundExtendingFunction(...objects); } extendedFunction.extendedFunction = functionToExtend; return extendedFunction; } } function EXTEND(extendingFunction) { return new UIFunctionExtender(extendingFunction); } class UILazyPropertyValue { constructor(initFunction) { this.isAUILazyPropertyValueObject = YES; this.initFunction = initFunction; } setLazyPropertyValue(key, target) { let isValueInitialized = NO; let _value = nil; const initValue = () => { _value = this.initFunction(); isValueInitialized = YES; this.initFunction = nil; }; if (delete target[key]) { Object.defineProperty(target, key, { get: function() { if (IS_NOT(isValueInitialized)) { initValue(); } return _value; }, set: function(newValue) { _value = newValue; }, enumerable: true, configurable: true }); } } } function LAZY_VALUE(initFunction) { return new UILazyPropertyValue(initFunction); } const _UIObject = class { constructor() { } get class() { return Object.getPrototypeOf(this).constructor; } get superclass() { return Object.getPrototypeOf(Object.getPrototypeOf(this)).constructor; } isKindOfClass(classObject) { if (this.isMemberOfClass(classObject)) { return YES; } for (let superclassObject = this.superclass; IS(superclassObject); superclassObject = superclassObject.superclass) { if (superclassObject == classObject) { return YES; } } return NO; } isMemberOfClass(classObject) { return this.class == classObject; } static recordAnnotation(annotation, target) { if (!_UIObject.annotationsMap.has(target)) { _UIObject.annotationsMap.set(target, []); } _UIObject.annotationsMap.get(target).push(annotation); } static classHasAnnotation(classObject, annotation) { var _a; return (_a = _UIObject.annotationsMap.get(classObject)) == null ? void 0 : _a.contains(annotation); } static annotationsOnClass(classObject) { var _a; return (_a = _UIObject.annotationsMap.get(classObject)) != null ? _a : []; } static wrapObject(object) { if (IS_NOT(object)) { return nil; } if (object instanceof _UIObject) { return object; } return Object.assign(new _UIObject(), object); } valueForKey(key) { return this[key]; } valueForKeyPath(keyPath, defaultValue) { return _UIObject.valueForKeyPath(keyPath, this, defaultValue); } static valueForKeyPath(keyPath, object, defaultValue) { if (IS_NOT(keyPath)) { return object; } const keys = keyPath.split("."); let currentObject = object; for (let i = 0; i < keys.length; i++) { const key = keys[i]; if (key.substring(0, 2) == "[]") { currentObject = currentObject[key.substring(2)]; const remainingKeyPath = keys.slice(i + 1).join("."); const currentArray = currentObject; currentObject = currentArray.map((subObject) => _UIObject.valueForKeyPath(remainingKeyPath, subObject)); break; } currentObject = currentObject == null ? void 0 : currentObject[key]; if (IS_LIKE_NULL(currentObject)) { currentObject = defaultValue; } } return currentObject; } setValueForKeyPath(keyPath, value, createPath = YES) { return _UIObject.setValueForKeyPath(keyPath, value, this, createPath); } static setValueForKeyPath(keyPath, value, currentObject, createPath) { const keys = keyPath.split("."); let didSetValue = NO; keys.forEach((key, index, array) => { if (index == array.length - 1 && IS_NOT_LIKE_NULL(currentObject)) { currentObject[key] = value; didSetValue = YES; return; } else if (IS_NOT(currentObject)) { return; } const currentObjectValue = currentObject[key]; if (IS_LIKE_NULL(currentObjectValue) && createPath) { currentObject[key] = {}; } currentObject = currentObject[key]; }); return didSetValue; } configureWithObject(object) { return _UIObject.configureWithObject(this, object); } configuredWithObject(object) { this.configureWithObject(object); return this; } static configureWithObject(configurationTarget, object) { const isAnObject = (item) => item && typeof item === "object" && !Array.isArray(item) && !(item instanceof import_UICoreExtensionValueObject.UICoreExtensionValueObject); const isAPureObject = (item) => isAnObject(item) && Object.getPrototypeOf(item) === Object.getPrototypeOf({}); function isAClass(funcOrClass) { if (IS_NOT(funcOrClass)) { return NO; } const isFunction = (functionToCheck) => functionToCheck && {}.toString.call(functionToCheck) === "[object Function]"; const propertyNames = Object.getOwnPropertyNames(funcOrClass); return isFunction(funcOrClass) && !propertyNames.includes("arguments") && propertyNames.includes("prototype"); } const result = {}; let keyPathsAndValues = []; function prepareKeyPathsAndValues(target, source, keyPath = "") { if ((isAnObject(target) || isAClass(target)) && isAnObject(source)) { source.forEach((sourceValue, key) => { const valueKeyPath = keyPath + "." + key; function addValueAndKeyPath(sourceValue2) { keyPathsAndValues.push({ value: sourceValue2, keyPath: valueKeyPath.replace(".", "") }); } if (isAPureObject(sourceValue) || isAClass(sourceValue)) { if (!(key in target) || target[key] instanceof Function) { addValueAndKeyPath(sourceValue); } else { prepareKeyPathsAndValues(target[key], sourceValue, valueKeyPath); } } else if (sourceValue instanceof import_UICoreExtensionValueObject.UICoreExtensionValueObject) { addValueAndKeyPath(sourceValue.value); } else { addValueAndKeyPath(sourceValue); } }); } } prepareKeyPathsAndValues(configurationTarget, object); keyPathsAndValues = keyPathsAndValues.sort((a, b) => { const firstKeyPath = a.keyPath.split(".").length; const secondKeyPath = b.keyPath.split(".").length; if (firstKeyPath < secondKeyPath) { return -1; } if (firstKeyPath > secondKeyPath) { return 1; } return 0; }); keyPathsAndValues.forEach((valueAndKeyPath) => { const keyPath = valueAndKeyPath.keyPath; let value = valueAndKeyPath.value; const getTargetFunction = (bindThis = NO) => { let result2 = _UIObject.valueForKeyPath(keyPath, configurationTarget); if (bindThis) { const indexOfDot = keyPath.lastIndexOf("."); const thisObject = _UIObject.valueForKeyPath(keyPath.substring(0, indexOfDot), configurationTarget); result2 = result2.bind(thisObject); } return result2; }; if (value instanceof UILazyPropertyValue) { const indexOfDot = keyPath.lastIndexOf("."); const thisObject = _UIObject.valueForKeyPath(keyPath.substring(0, indexOfDot), configurationTarget); const key = keyPath.substring(indexOfDot + 1); value.setLazyPropertyValue(key, thisObject); return; } if (value instanceof UIFunctionCall) { value.callFunction(getTargetFunction(YES)); return; } if (value instanceof UIFunctionExtender) { value = value.extendedFunction(getTargetFunction()); } _UIObject.setValueForKeyPath(keyPath, _UIObject.valueForKeyPath(keyPath, configurationTarget), result, YES); _UIObject.setValueForKeyPath(keyPath, value, configurationTarget, YES); }); return result; } static configuredWithObject(configurationTarget, object) { this.configureWithObject(configurationTarget, object); return configurationTarget; } get methods() { const thisObject = this; const result = {}; thisObject.forEach((value, key) => { if (value instanceof Function && key != "methods") { result[key] = value.bind(thisObject); } }); return result; } performFunctionWithSelf(functionToPerform) { return functionToPerform(this); } performingFunctionWithSelf(functionToPerform) { functionToPerform(this); return this; } performFunctionWithDelay(delay, functionToCall) { new import_UITimer.UITimer(delay, NO, functionToCall); } }; let UIObject = _UIObject; UIObject.annotationsMap = /* @__PURE__ */ new WeakMap(); // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { CALL, EXTEND, FIRST, FIRST_OR_NIL, IF, IS, IS_AN_EMAIL_ADDRESS, IS_DEFINED, IS_LIKE_NULL, IS_NIL, IS_NOT, IS_NOT_LIKE_NULL, IS_NOT_NIL, IS_UNDEFINED, LAZY_VALUE, MAKE_ID, NO, RETURNER, UIFunctionCall, UIFunctionExtender, UILazyPropertyValue, UIObject, YES, nil, wrapInNil }); //# sourceMappingURL=UIObject.js.map