Skip to content
Snippets Groups Projects
Commit ee06f143 authored by Volker Schukai's avatar Volker Schukai :alien:
Browse files

Merge branch 'MONSTER/-/d249dd34-0c43-40b7-96fb-cb7a8624ad11' into 'master'

Release 1.20.0

See merge request oss/libraries/javascript/monster!27
parents ffae3931 745ae8dd
Branches
Tags
No related merge requests found
Showing
with 60 additions and 34 deletions
/** Monster 1.19.0, © 2021 schukai GmbH, Released under the AGPL 3.0 License. */
"use strict";import{assignToNamespace,Monster}from"../namespace.js";import{Base}from"./base.js";import{isArray,isObject,isPrimitive}from"./is.js";import{Observer}from"./observer.js";import{ObserverList}from"./observerlist.js";import{validateObject}from"./validate.js";class ProxyObserver extends Base{constructor(e){super(),this.realSubject=validateObject(e),this.subject=new Proxy(e,getHandler.call(this)),this.objectMap=new WeakMap,this.objectMap.set(this.realSubject,this.subject),this.proxyMap=new WeakMap,this.proxyMap.set(this.subject,this.realSubject),this.observers=new ObserverList}getSubject(){return this.subject}getRealSubject(){return this.realSubject}attachObserver(e){return this.observers.attach(e),this}detachObserver(e){return this.observers.detach(e),this}notifyObservers(){return this.observers.notify(this)}containsObserver(e){return this.observers.contains(e)}}function getHandler(){const a=this,s={get:function(e,t,r){r=Reflect.get(e,t,r);if("symbol"==typeof t)return r;if(isPrimitive(r))return r;if(isArray(r)||isObject(r)){if(a.objectMap.has(r))return a.objectMap.get(r);if(a.proxyMap.has(r))return r;t=new Proxy(r,s);return a.objectMap.set(r,t),a.proxyMap.set(t,r),t}return r},set:function(e,t,r,s){a.proxyMap.has(r)&&(r=a.proxyMap.get(r)),a.proxyMap.has(e)&&(e=a.proxyMap.get(e));let o=Reflect.get(e,t,s);if(a.proxyMap.has(o)&&(o=a.proxyMap.get(o)),o===r)return!0;let i=Reflect.getOwnPropertyDescriptor(e,t);return void 0===i&&(i={writable:!0,enumerable:!0,configurable:!0}),i.value=r,e=Reflect.defineProperty(e,t,i),"symbol"!=typeof t&&a.observers.notify(a),e},deleteProperty:function(e,t){return t in e&&(delete e[t],"symbol"!=typeof t&&a.observers.notify(a),!0)},defineProperty:function(e,t,r){r=Reflect.defineProperty(e,t,r);return"symbol"!=typeof t&&a.observers.notify(a),r},setPrototypeOf:function(e,t){var r=Reflect.setPrototypeOf(object1,t);return"symbol"!=typeof t&&a.observers.notify(a),r}};return s}assignToNamespace("Monster.Types",ProxyObserver);export{Monster,ProxyObserver};
\ No newline at end of file
'use strict';import{assignToNamespace,Monster}from"../namespace.js";import{Base}from"./base.js";import{isArray,isObject,isPrimitive}from"./is.js";import{Observer}from"./observer.js";import{ObserverList}from"./observerlist.js";import{validateObject}from"./validate.js";class ProxyObserver extends Base{constructor(object){super();this.realSubject=validateObject(object);this.subject=new Proxy(object,getHandler.call(this));this.objectMap=new WeakMap;this.objectMap.set(this.realSubject,this.subject);this.proxyMap=new WeakMap;this.proxyMap.set(this.subject,this.realSubject);this.observers=new ObserverList}getSubject(){return this.subject}getRealSubject(){return this.realSubject}attachObserver(observer){this.observers.attach(observer);return this}detachObserver(observer){this.observers.detach(observer);return this}notifyObservers(){return this.observers.notify(this)}containsObserver(observer){return this.observers.contains(observer)}}assignToNamespace("Monster.Types",ProxyObserver);export{Monster,ProxyObserver};function getHandler(){const proxy=this;const handler={get:function(target,key,receiver){const value=Reflect.get(target,key,receiver);if(typeof key==="symbol"){return value}if(isPrimitive(value)){return value}if(isArray(value)||isObject(value)){if(proxy.objectMap.has(value)){return proxy.objectMap.get(value)}else if(proxy.proxyMap.has(value)){return value}else{let p=new Proxy(value,handler);proxy.objectMap.set(value,p);proxy.proxyMap.set(p,value);return p}}return value},set:function(target,key,value,receiver){if(proxy.proxyMap.has(value)){value=proxy.proxyMap.get(value)}if(proxy.proxyMap.has(target)){target=proxy.proxyMap.get(target)}let current=Reflect.get(target,key,receiver);if(proxy.proxyMap.has(current)){current=proxy.proxyMap.get(current)}if(current===value){return true}let result;let descriptor=Reflect.getOwnPropertyDescriptor(target,key);if(descriptor===undefined){descriptor={writable:true,enumerable:true,configurable:true}}descriptor["value"]=value;result=Reflect.defineProperty(target,key,descriptor);if(typeof key!=="symbol"){proxy.observers.notify(proxy)}return result},deleteProperty:function(target,key){if(key in target){delete target[key];if(typeof key!=="symbol"){proxy.observers.notify(proxy)}return true}return false},defineProperty:function(target,key,descriptor){let result=Reflect.defineProperty(target,key,descriptor);if(typeof key!=="symbol"){proxy.observers.notify(proxy)}return result},setPrototypeOf:function(target,key){let result=Reflect.setPrototypeOf(object1,key);if(typeof key!=="symbol"){proxy.observers.notify(proxy)}return result}};return handler}
/** Monster 1.19.0, © 2021 schukai GmbH, Released under the AGPL 3.0 License. */
"use strict";import{assignToNamespace,Monster}from"../namespace.js";import{Base}from"./base.js";class Queue extends Base{constructor(){super(),this.data=[]}isEmpty(){return 0===this.data.length}peek(){if(!this.isEmpty())return this.data[0]}add(s){return this.data.push(s),this}clear(){return this.data=[],this}poll(){if(!this.isEmpty())return this.data.shift()}}assignToNamespace("Monster.Types",Queue);export{Monster,Queue};
\ No newline at end of file
'use strict';import{assignToNamespace,Monster}from"../namespace.js";import{Base}from"./base.js";class Queue extends Base{constructor(){super();this.data=[]}isEmpty(){return this.data.length===0}peek(){if(this.isEmpty()){return undefined}return this.data[0]}add(value){this.data.push(value);return this}clear(){this.data=[];return this}poll(){if(this.isEmpty()){return undefined}return this.data.shift()}}assignToNamespace("Monster.Types",Queue);export{Monster,Queue};
/** Monster 1.19.0, © 2021 schukai GmbH, Released under the AGPL 3.0 License. */
"use strict";import{random}from"../math/random.js";import{assignToNamespace,Monster}from"../namespace.js";import{getGlobal}from"./global.js";import{ID}from"./id.js";let internalCounter=0;class RandomID extends ID{constructor(){super(),internalCounter+=1,this.id=getGlobal().btoa(random(1,1e4)).replace(/=/g,"").replace(/^[0-9]+/,"X")+internalCounter}}assignToNamespace("Monster.Types",RandomID);export{Monster,RandomID};
\ No newline at end of file
'use strict';import{random}from"../math/random.js";import{assignToNamespace,Monster}from"../namespace.js";import{getGlobal}from"./global.js";import{ID}from"./id.js";let internalCounter=0;class RandomID extends ID{constructor(){super();internalCounter+=1;this.id=getGlobal().btoa(random(1,10000)).replace(/=/g,"").replace(/^[0-9]+/,"X")+internalCounter}}assignToNamespace("Monster.Types",RandomID);export{Monster,RandomID};
/** Monster 1.19.0, © 2021 schukai GmbH, Released under the AGPL 3.0 License. */
"use strict";import{assignToNamespace,Monster}from"../namespace.js";import{Base}from"./base.js";class Stack extends Base{constructor(){super(),this.data=[]}isEmpty(){return 0===this.data.length}peek(){if(!this.isEmpty())return this.data?.[this.data.length-1]}push(t){return this.data.push(t),this}clear(){return this.data=[],this}pop(){if(!this.isEmpty())return this.data.pop()}}assignToNamespace("Monster.Types",Stack);export{Monster,Stack};
\ No newline at end of file
'use strict';import{assignToNamespace,Monster}from"../namespace.js";import{Base}from"./base.js";class Stack extends Base{constructor(){super();this.data=[]}isEmpty(){return this.data.length===0}peek(){if(this.isEmpty()){return undefined}return this.data?.[this.data.length-1]}push(value){this.data.push(value);return this}clear(){this.data=[];return this}pop(){if(this.isEmpty()){return undefined}return this.data.pop()}}assignToNamespace("Monster.Types",Stack);export{Monster,Stack};
/** Monster 1.19.0, © 2021 schukai GmbH, Released under the AGPL 3.0 License. */
"use strict";import{assignToNamespace,Monster}from"../namespace.js";import{isIterable,isString}from"../types/is.js";import{validateFunction,validateString}from"../types/validate.js";import{Base}from"./base.js";class TokenList extends Base{constructor(t){super(),this.tokens=new Set,void 0!==t&&this.add(t)}getIterator(){return this[Symbol.iterator]()}[Symbol.iterator](){let t=0,e=this.entries();return{next:()=>t<e.length?{value:e?.[t++],done:!1}:{done:!0}}}contains(r){if(isString(r)){r=r.trim();let e=0;return r.split(" ").forEach(t=>!1!==this.tokens.has(t.trim())&&void e++),0<e}if(isIterable(r)){let t=0;for(var e of r){if(validateString(e),!1===this.tokens.has(e.trim()))return!1;t++}return 0<t}return!1}add(t){if(isString(t))t.split(" ").forEach(t=>{this.tokens.add(t.trim())});else if(isIterable(t))for(var e of t)validateString(e),this.tokens.add(e.trim());else if(void 0!==t)throw new TypeError("unsupported value");return this}clear(){return this.tokens.clear(),this}remove(t){if(isString(t))t.split(" ").forEach(t=>{this.tokens.delete(t.trim())});else if(isIterable(t))for(var e of t)validateString(e),this.tokens.delete(e.trim());else if(void 0!==t)throw new TypeError("unsupported value");return this}replace(t,e){if(validateString(t),validateString(e),!this.contains(t))return this;let r=Array.from(this.tokens);t=r.indexOf(t);return-1===t||(r.splice(t,1,e),this.tokens=new Set,this.add(r)),this}toggle(t){if(isString(t))t.split(" ").forEach(t=>{toggleValue.call(this,t)});else if(isIterable(t))for(var e of t)toggleValue.call(this,e);else if(void 0!==t)throw new TypeError("unsupported value");return this}entries(){return Array.from(this.tokens)}forEach(t){return validateFunction(t),this.tokens.forEach(t),this}toString(){return this.entries().join(" ")}}function toggleValue(t){if(!(this instanceof TokenList))throw Error("must be called with TokenList.call");return validateString(t),t=t.trim(),this.contains(t)?this.remove(t):this.add(t),this}assignToNamespace("Monster.Types",TokenList);export{Monster,TokenList};
\ No newline at end of file
'use strict';import{assignToNamespace,Monster}from"../namespace.js";import{isIterable,isString}from"../types/is.js";import{validateFunction,validateString}from"../types/validate.js";import{Base}from"./base.js";class TokenList extends Base{constructor(init){super();this.tokens=new Set;if(typeof init!=="undefined"){this.add(init)}}getIterator(){return this[Symbol.iterator]()}[Symbol.iterator](){let index=0;let entries=this.entries();return{next:()=>{if(index<entries.length){return{value:entries?.[index++],done:false}}else{return{done:true}}}}}contains(value){if(isString(value)){value=value.trim();let counter=0;value.split(" ").forEach(token=>{if(this.tokens.has(token.trim())===false)return false;counter++});return counter>0?true:false}if(isIterable(value)){let counter=0;for(let token of value){validateString(token);if(this.tokens.has(token.trim())===false)return false;counter++}return counter>0?true:false}return false}add(value){if(isString(value)){value.split(" ").forEach(token=>{this.tokens.add(token.trim())})}else if(isIterable(value)){for(let token of value){validateString(token);this.tokens.add(token.trim())}}else if(typeof value!=="undefined"){throw new TypeError("unsupported value")}return this}clear(){this.tokens.clear();return this}remove(value){if(isString(value)){value.split(" ").forEach(token=>{this.tokens.delete(token.trim())})}else if(isIterable(value)){for(let token of value){validateString(token);this.tokens.delete(token.trim())}}else if(typeof value!=="undefined"){throw new TypeError("unsupported value")}return this}replace(token,newToken){validateString(token);validateString(newToken);if(!this.contains(token)){return this}let a=Array.from(this.tokens);let i=a.indexOf(token);if(i===-1)return this;a.splice(i,1,newToken);this.tokens=new Set;this.add(a);return this}toggle(value){if(isString(value)){value.split(" ").forEach(token=>{toggleValue.call(this,token)})}else if(isIterable(value)){for(let token of value){toggleValue.call(this,token)}}else if(typeof value!=="undefined"){throw new TypeError("unsupported value")}return this}entries(){return Array.from(this.tokens)}forEach(callback){validateFunction(callback);this.tokens.forEach(callback);return this}toString(){return this.entries().join(" ")}}function toggleValue(token){if(!(this instanceof TokenList))throw Error("must be called with TokenList.call");validateString(token);token=token.trim();if(this.contains(token)){this.remove(token);return this}this.add(token);return this}assignToNamespace("Monster.Types",TokenList);export{Monster,TokenList};
/** Monster 1.19.0, © 2021 schukai GmbH, Released under the AGPL 3.0 License. */
"use strict";import{assignToNamespace,Monster}from"../namespace.js";function typeOf(t){let e={}.toString.call(t).match(/\s([a-zA-Z]+)/)[1];return"Object"===e&&(t=/^(class|function)\s+(\w+)/.exec(t.constructor.toString()),e=t&&2<t.length?t[2]:""),e.toLowerCase()}assignToNamespace("Monster.Types",typeOf);export{Monster,typeOf};
\ No newline at end of file
'use strict';import{assignToNamespace,Monster}from"../namespace.js";function typeOf(value){let type={}.toString.call(value).match(/\s([a-zA-Z]+)/)[1];if("Object"===type){const results=/^(class|function)\s+(\w+)/.exec(value.constructor.toString());type=results&&results.length>2?results[2]:""}return type.toLowerCase()}assignToNamespace("Monster.Types",typeOf);export{Monster,typeOf};
/** Monster 1.19.0, © 2021 schukai GmbH, Released under the AGPL 3.0 License. */
"use strict";import{assignToNamespace,Monster}from"../namespace.js";import{Queue}from"./queue.js";import{validateObject}from"./validate.js";class UniqueQueue extends Queue{constructor(){super(),this.unique=new WeakSet}add(e){return validateObject(e),this.unique.has(e)||(this.unique.add(e),super.add(e)),this}clear(){return super.clear(),this.unique=new WeakSet,this}poll(){if(!this.isEmpty()){var e=this.data.shift();return this.unique.delete(e),e}}}assignToNamespace("Monster.Types",UniqueQueue);export{Monster,UniqueQueue};
\ No newline at end of file
'use strict';import{assignToNamespace,Monster}from"../namespace.js";import{Queue}from"./queue.js";import{validateObject}from"./validate.js";class UniqueQueue extends Queue{constructor(){super();this.unique=new WeakSet}add(value){validateObject(value);if(!this.unique.has(value)){this.unique.add(value);super.add(value)}return this}clear(){super.clear();this.unique=new WeakSet;return this}poll(){if(this.isEmpty()){return undefined}let value=this.data.shift();this.unique.delete(value);return value}}assignToNamespace("Monster.Types",UniqueQueue);export{Monster,UniqueQueue};
/** Monster 1.19.0, © 2021 schukai GmbH, Released under the AGPL 3.0 License. */
"use strict";import{assignToNamespace,Monster}from"../namespace.js";import{isArray,isBoolean,isFunction,isInstance,isInteger,isIterable,isObject,isPrimitive,isString,isSymbol}from"./is.js";function validateIterable(e){if(!isIterable(e))throw new TypeError("value is not iterable");return e}function validatePrimitive(e){if(!isPrimitive(e))throw new TypeError("value is not a primitive");return e}function validateBoolean(e){if(!isBoolean(e))throw new TypeError("value is not a boolean");return e}function validateString(e){if(!isString(e))throw new TypeError("value is not a string");return e}function validateObject(e){if(!isObject(e))throw new TypeError("value is not a object");return e}function validateInstance(e,i){if(isInstance(e,i))return e;{let e="";throw(isObject(i)||isFunction(i))&&(e=i?.name),e=e&&" "+e,new TypeError("value is not an instance of"+e)}}function validateArray(e){if(!isArray(e))throw new TypeError("value is not an array");return e}function validateSymbol(e){if(!isSymbol(e))throw new TypeError("value is not an symbol");return e}function validateFunction(e){if(!isFunction(e))throw new TypeError("value is not a function");return e}function validateInteger(e){if(!isInteger(e))throw new TypeError("value is not an integer");return e}assignToNamespace("Monster.Types",validatePrimitive,validateBoolean,validateString,validateObject,validateArray,validateFunction,validateIterable,validateInteger);export{Monster,validatePrimitive,validateBoolean,validateString,validateObject,validateInstance,validateArray,validateFunction,validateIterable,validateInteger,validateSymbol};
\ No newline at end of file
'use strict';import{assignToNamespace,Monster}from"../namespace.js";import{isArray,isBoolean,isFunction,isInstance,isInteger,isIterable,isObject,isPrimitive,isString,isSymbol}from"./is.js";function validateIterable(value){if(!isIterable(value)){throw new TypeError("value is not iterable")}return value}function validatePrimitive(value){if(!isPrimitive(value)){throw new TypeError("value is not a primitive")}return value}function validateBoolean(value){if(!isBoolean(value)){throw new TypeError("value is not a boolean")}return value}function validateString(value){if(!isString(value)){throw new TypeError("value is not a string")}return value}function validateObject(value){if(!isObject(value)){throw new TypeError("value is not a object")}return value}function validateInstance(value,instance){if(!isInstance(value,instance)){let n="";if(isObject(instance)||isFunction(instance)){n=instance?.["name"]}if(n){n=" "+n}throw new TypeError("value is not an instance of"+n)}return value}function validateArray(value){if(!isArray(value)){throw new TypeError("value is not an array")}return value}function validateSymbol(value){if(!isSymbol(value)){throw new TypeError("value is not an symbol")}return value}function validateFunction(value){if(!isFunction(value)){throw new TypeError("value is not a function")}return value}function validateInteger(value){if(!isInteger(value)){throw new TypeError("value is not an integer")}return value}assignToNamespace("Monster.Types",validatePrimitive,validateBoolean,validateString,validateObject,validateArray,validateFunction,validateIterable,validateInteger);export{Monster,validatePrimitive,validateBoolean,validateString,validateObject,validateInstance,validateArray,validateFunction,validateIterable,validateInteger,validateSymbol};
/** Monster 1.19.0, © 2021 schukai GmbH, Released under the AGPL 3.0 License. */
"use strict";import{assignToNamespace,Monster}from"../namespace.js";import{Base}from"./base.js";class Version extends Base{constructor(r,t,e){var s;if(super(),"string"==typeof r&&void 0===t&&void 0===e&&(s=r.toString().split("."),r=parseInt(s[0]||0),t=parseInt(s[1]||0),e=parseInt(s[2]||0)),void 0===r)throw new Error("major version is undefined");if(void 0===t&&(t=0),void 0===e&&(e=0),this.major=parseInt(r),this.minor=parseInt(t),this.patch=parseInt(e),isNaN(this.major))throw new Error("major is not a number");if(isNaN(this.minor))throw new Error("minor is not a number");if(isNaN(this.patch))throw new Error("patch is not a number")}toString(){return this.major+"."+this.minor+"."+this.patch}compareTo(r){if("string"!=typeof(r=r instanceof Version?r.toString():r))throw new Error("type exception");if(r===this.toString())return 0;var t=[this.major,this.minor,this.patch],e=r.split("."),s=Math.max(t.length,e.length);for(let r=0;r<s;r+=1){if(t[r]&&!e[r]&&0<parseInt(t[r])||parseInt(t[r])>parseInt(e[r]))return 1;if(e[r]&&!t[r]&&0<parseInt(e[r])||parseInt(t[r])<parseInt(e[r]))return-1}return 0}}assignToNamespace("Monster.Types",Version);let monsterVersion;function getVersion(){return monsterVersion instanceof Version||(monsterVersion=new Version("1.19.0")),monsterVersion}assignToNamespace("Monster",getVersion);export{Monster,Version,getVersion};
\ No newline at end of file
'use strict';import{assignToNamespace,Monster}from"../namespace.js";import{Base}from"./base.js";class Version extends Base{constructor(major,minor,patch){super();if(typeof major==="string"&&minor===undefined&&patch===undefined){let parts=major.toString().split(".");major=parseInt(parts[0]||0);minor=parseInt(parts[1]||0);patch=parseInt(parts[2]||0)}if(major===undefined){throw new Error("major version is undefined")}if(minor===undefined){minor=0}if(patch===undefined){patch=0}this.major=parseInt(major);this.minor=parseInt(minor);this.patch=parseInt(patch);if(isNaN(this.major)){throw new Error("major is not a number")}if(isNaN(this.minor)){throw new Error("minor is not a number")}if(isNaN(this.patch)){throw new Error("patch is not a number")}}toString(){return this.major+"."+this.minor+"."+this.patch}compareTo(version){if(version instanceof Version){version=version.toString()}if(typeof version!=="string"){throw new Error("type exception")}if(version===this.toString()){return 0}let a=[this.major,this.minor,this.patch];let b=version.split(".");let len=Math.max(a.length,b.length);for(let i=0;i<len;i+=1){if(a[i]&&!b[i]&&parseInt(a[i])>0||parseInt(a[i])>parseInt(b[i])){return 1}else if(b[i]&&!a[i]&&parseInt(b[i])>0||parseInt(a[i])<parseInt(b[i])){return-1}}return 0}}assignToNamespace("Monster.Types",Version);let monsterVersion;function getVersion(){if(monsterVersion instanceof Version){return monsterVersion}monsterVersion=new Version("1.19.0");return monsterVersion}assignToNamespace("Monster",getVersion);export{Monster,Version,getVersion};
/** Monster 1.19.0, © 2021 schukai GmbH, Released under the AGPL 3.0 License. */
"use strict";import{assignToNamespace,Monster}from"../namespace.js";import{getGlobal}from"../types/global.js";import{isArray,isFunction,isObject,isPrimitive}from"../types/is.js";function clone(n){if(null===n)return n;if(isPrimitive(n))return n;if(isFunction(n))return n;if(isArray(n)){let e=[];for(var t=0,r=n.length;t<r;t++)e[t]=clone(n[t]);return e}if(isObject(n)){if(n instanceof Date){let e=new Date;return e.setTime(n.getTime()),e}if("undefined"!=typeof Element&&n instanceof Element)return n;if("undefined"!=typeof HTMLDocument&&n instanceof HTMLDocument)return n;if("undefined"!=typeof DocumentFragment&&n instanceof DocumentFragment)return n;if(n===getGlobal())return n;if("undefined"!=typeof globalContext&&n===globalContext)return n;if("undefined"!=typeof window&&n===window)return n;if("undefined"!=typeof document&&n===document)return n;if("undefined"!=typeof navigator&&n===navigator)return n;if("undefined"!=typeof JSON&&n===JSON)return n;try{if(n instanceof Proxy)return n}catch(e){}return cloneObject(n)}throw new Error("unable to clone obj! its type isn't supported.")}function cloneObject(e){var n,t;if(e.hasOwnProperty("getClone"),"function"===e.getClone)return e.getClone();for(t in n={},"function"==typeof e.constructor&&"function"==typeof e.constructor.call&&(n=new e.constructor),e)e.hasOwnProperty(t)&&(Monster.Types.isPrimitive(e[t])?n[t]=e[t]:n[t]=clone(e[t]));return n}assignToNamespace("Monster.Util",clone);export{Monster,clone};
\ No newline at end of file
'use strict';import{assignToNamespace,Monster}from"../namespace.js";import{getGlobal}from"../types/global.js";import{isArray,isFunction,isObject,isPrimitive}from"../types/is.js";function clone(obj){if(null===obj){return obj}if(isPrimitive(obj)){return obj}if(isFunction(obj)){return obj}if(isArray(obj)){let copy=[];for(var i=0,len=obj.length;i<len;i++){copy[i]=clone(obj[i])}return copy}if(isObject(obj)){if(obj instanceof Date){let copy=new Date;copy.setTime(obj.getTime());return copy}if(typeof Element!=="undefined"&&obj instanceof Element)return obj;if(typeof HTMLDocument!=="undefined"&&obj instanceof HTMLDocument)return obj;if(typeof DocumentFragment!=="undefined"&&obj instanceof DocumentFragment)return obj;if(obj===getGlobal())return obj;if(typeof globalContext!=="undefined"&&obj===globalContext)return obj;if(typeof window!=="undefined"&&obj===window)return obj;if(typeof document!=="undefined"&&obj===document)return obj;if(typeof navigator!=="undefined"&&obj===navigator)return obj;if(typeof JSON!=="undefined"&&obj===JSON)return obj;try{if(obj instanceof Proxy){return obj}}catch(e){}return cloneObject(obj)}throw new Error("unable to clone obj! its type isn't supported.")}function cloneObject(obj){var copy;if(typeof obj.hasOwnProperty("getClone")&&obj.getClone==="function"){return obj.getClone()}copy={};if(typeof obj.constructor==="function"&&typeof obj.constructor.call==="function"){copy=new obj.constructor}for(let key in obj){if(!obj.hasOwnProperty(key)){continue}if(Monster.Types.isPrimitive(obj[key])){copy[key]=obj[key];continue}copy[key]=clone(obj[key])}return copy}assignToNamespace("Monster.Util",clone);export{Monster,clone};
/** Monster 1.19.0, © 2021 schukai GmbH, Released under the AGPL 3.0 License. */
"use strict";import{assignToNamespace,Monster}from"../namespace.js";import{Base}from"../types/base.js";import{isFunction}from"../types/is.js";class Comparator extends Base{constructor(r){if(super(),isFunction(r))this.compare=r;else{if(void 0!==r)throw new TypeError("unsupported type");this.compare=function(r,e){if(typeof r!=typeof e)throw new TypeError("impractical comparison");return r===e?0:r<e?-1:1}}}reverse(){const s=this.compare;return this.compare=(r,e)=>s(e,r),this}equal(r,e){return 0===this.compare(r,e)}greaterThan(r,e){return 0<this.compare(r,e)}greaterThanOrEqual(r,e){return this.greaterThan(r,e)||this.equal(r,e)}lessThanOrEqual(r,e){return this.lessThan(r,e)||this.equal(r,e)}lessThan(r,e){return this.compare(r,e)<0}}assignToNamespace("Monster.Util",Comparator);export{Monster,Comparator};
\ No newline at end of file
'use strict';import{assignToNamespace,Monster}from"../namespace.js";import{Base}from"../types/base.js";import{isFunction}from"../types/is.js";class Comparator extends Base{constructor(callback){super();if(isFunction(callback)){this.compare=callback}else if(callback!==undefined){throw new TypeError("unsupported type")}else{this.compare=function(a,b){if(typeof a!==typeof b){throw new TypeError("impractical comparison")}if(a===b){return 0}return a<b?-1:1}}}reverse(){const original=this.compare;this.compare=(a,b)=>original(b,a);return this}equal(a,b){return this.compare(a,b)===0}greaterThan(a,b){return this.compare(a,b)>0}greaterThanOrEqual(a,b){return this.greaterThan(a,b)||this.equal(a,b)}lessThanOrEqual(a,b){return this.lessThan(a,b)||this.equal(a,b)}lessThan(a,b){return this.compare(a,b)<0}}assignToNamespace("Monster.Util",Comparator);export{Monster,Comparator};
/** Monster 1.19.0, © 2021 schukai GmbH, Released under the AGPL 3.0 License. */
"use strict";import{assignToNamespace,Monster}from"../namespace.js";import{validateObject}from"../types/validate.js";function deepFreeze(e){var t;validateObject(e);for(t of Object.getOwnPropertyNames(e)){var r=e[t];e[t]=r&&"object"==typeof r?deepFreeze(r):r}return Object.freeze(e)}assignToNamespace("Monster.Util",deepFreeze);export{Monster,deepFreeze};
\ No newline at end of file
'use strict';import{assignToNamespace,Monster}from"../namespace.js";import{validateObject}from"../types/validate.js";function deepFreeze(object){validateObject(object);var propNames=Object.getOwnPropertyNames(object);for(let name of propNames){let value=object[name];object[name]=value&&typeof value==="object"?deepFreeze(value):value}return Object.freeze(object)}assignToNamespace("Monster.Util",deepFreeze);export{Monster,deepFreeze};
/** Monster 1.19.0, © 2021 schukai GmbH, Released under the AGPL 3.0 License. */
"use strict";const namespace="Monster.Util";export{namespace};
\ No newline at end of file
'use strict';export const namespace="Monster.Util";
......@@ -10462,8 +10462,8 @@ __webpack_require__.r(__webpack_exports__);
/* harmony export */ "fireEvent": function() { return /* binding */ fireEvent; }
/* harmony export */ });
/* harmony import */ var _namespace_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(2);
/* harmony import */ var _types_validate_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(14);
/* harmony import */ var _attributes_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(32);
/* harmony import */ var _types_is_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(9);
/* harmony import */ var _types_validate_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(14);
/* harmony import */ var _util_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(41);
 
/**
......@@ -10518,7 +10518,7 @@ function fireEvent(element, type) {
return;
}
 
var event = new Event((0,_types_validate_js__WEBPACK_IMPORTED_MODULE_1__.validateString)(type), {
var event = new Event((0,_types_validate_js__WEBPACK_IMPORTED_MODULE_2__.validateString)(type), {
bubbles: true,
cancelable: true
});
......@@ -10543,7 +10543,8 @@ function fireEvent(element, type) {
}
/**
* This function gets the path `Event.composedPath()` from an event and tries to find the next element
* up the tree `element.closest()` with the attribute and value. If no value is specified, only the attribute is searched.
* up the tree `element.closest()` with the attribute and value. If no value, or a value that is undefined or null,
* is specified, only the attribute is searched.
*
* You can call the function via the monster namespace `new Monster.DOM.findTargetElementFromEvent()`.
*
......@@ -10566,7 +10567,7 @@ function fireEvent(element, type) {
* @since 1.14.0
* @param {Event} event
* @param {string} attributeName
* @param {string|null} attributeValue
* @param {string|null|undefined} attributeValue
* @throws {Error} unsupported event
* @memberOf Monster.DOM
* @throws {TypeError} value is not a string
......@@ -10576,17 +10577,23 @@ function fireEvent(element, type) {
 
 
function findTargetElementFromEvent(event, attributeName, attributeValue) {
(0,_types_validate_js__WEBPACK_IMPORTED_MODULE_1__.validateInstance)(event, Event);
(0,_types_validate_js__WEBPACK_IMPORTED_MODULE_2__.validateInstance)(event, Event);
 
if (typeof event.composedPath !== 'function') {
throw new Error('unsupported event');
}
 
var path = event.composedPath();
var element = path === null || path === void 0 ? void 0 : path[0];
var element = path === null || path === void 0 ? void 0 : path[0]; // closest cannot be used here, because closest is not correct for slotted elements
 
if (element instanceof HTMLElement) {
return (0,_attributes_js__WEBPACK_IMPORTED_MODULE_2__.findClosestByAttribute)(element, attributeName, attributeValue);
if ((0,_types_is_js__WEBPACK_IMPORTED_MODULE_1__.isArray)(path)) {
for (var i = 0; i < path.length; i++) {
var o = path[i];
if (o instanceof HTMLElement && o.hasAttribute(attributeName) && (attributeValue === undefined || o.getAttribute(attributeName) === attributeValue)) {
return o;
}
}
}
 
return undefined;
......
This diff is collapsed.
This diff is collapsed.
{
"name": "@schukai/monster",
"version": "1.19.0",
"types": "./source/types/package.d.ts",
"description": "Monster is a simple library for creating fast, robust and lightweight websites.",
"keywords": [
"framework",
......@@ -19,6 +20,10 @@
"monster"
],
"type": "module",
"exports": {
"import": "./dist/monster.js",
"default": "./dist/monster.js"
},
"homepage": "https://monsterjs.org",
"repository": {
"type": "git",
......@@ -33,6 +38,7 @@
"> 1%",
"IE 10"
],
"browserify": {
"transform": [
[
......
......@@ -4,8 +4,8 @@
* @author schukai GmbH
*/
import {assignToNamespace, Monster} from '../namespace.js';
import {isArray} from "../types/is.js";
import {validateInstance, validateString} from "../types/validate.js";
import {findClosestByAttribute} from "./attributes.js";
import {getDocument} from "./util.js";
/**
......@@ -66,7 +66,8 @@ function fireEvent(element, type) {
/**
* This function gets the path `Event.composedPath()` from an event and tries to find the next element
* up the tree `element.closest()` with the attribute and value. If no value is specified, only the attribute is searched.
* up the tree `element.closest()` with the attribute and value. If no value, or a value that is undefined or null,
* is specified, only the attribute is searched.
*
* You can call the function via the monster namespace `new Monster.DOM.findTargetElementFromEvent()`.
*
......@@ -89,7 +90,7 @@ function fireEvent(element, type) {
* @since 1.14.0
* @param {Event} event
* @param {string} attributeName
* @param {string|null} attributeValue
* @param {string|null|undefined} attributeValue
* @throws {Error} unsupported event
* @memberOf Monster.DOM
* @throws {TypeError} value is not a string
......@@ -106,8 +107,17 @@ function findTargetElementFromEvent(event, attributeName, attributeValue) {
const path = event.composedPath();
const element = path?.[0];
if (element instanceof HTMLElement) {
return findClosestByAttribute(element, attributeName, attributeValue);
// closest cannot be used here, because closest is not correct for slotted elements
if (isArray(path)) {
for (let i = 0; i < path.length; i++) {
const o = path[i];
if (o instanceof HTMLElement &&
o.hasAttribute(attributeName)
&& (attributeValue===undefined || o.getAttribute(attributeName) === attributeValue)) {
return o;
}
}
}
return undefined;
......
export declare let Monster: Object;
\ No newline at end of file
......@@ -19,12 +19,14 @@ describe('Events', function () {
let div = document.createElement('div');
container.appendChild(div);
div.addEventListener('click', (event) => {
let e = findTargetElementFromEvent(event, 'data-monster');
expect(e.getAttribute('data-monster')).to.be.equal('hello')
done();
})
setTimeout(()=>{
fireEvent(div, 'click');
},0)
});
});
......@@ -74,7 +76,7 @@ describe('Events', function () {
it('should throw error', function () {
expect(() => fireEvent({}, 'touch')).to.throw(Error);
;
});
});
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment