eleva
Version:
A minimalist and lightweight, pure vanilla JavaScript frontend runtime framework.
4 lines (3 loc) • 4.42 kB
JavaScript
/*! Eleva Props Plugin v1.0.0-rc.2 | MIT License | https://elevajs.com */
var t,e;t=this,e=function(t){"use strict";function e(){return e=Object.assign?Object.assign.bind():function(t){for(var e=1;e<arguments.length;e++){var n=arguments[e];for(var a in n)({}).hasOwnProperty.call(n,a)&&(t[a]=n[a])}return t},e.apply(null,arguments)}class n{static parse(t,e){return"string"!=typeof t?t:t.replace(this.expressionPattern,(t,n)=>this.evaluate(n,e))}static evaluate(t,e){if("string"!=typeof t)return t;try{return new Function("data",`with(data) { return ${t}; }`)(e)}catch(t){return""}}}n.expressionPattern=/\{\{\s*(.*?)\s*\}\}/g,t.PropsPlugin={name:"props",version:"1.0.0-rc.2",description:"Advanced props data handling for complex data structures with automatic type detection and reactivity",install(t,a={}){const{enableAutoParsing:o=!0,enableReactivity:r=!0,onError:i=null}=a,s=t=>{try{if("string"!=typeof t)return t;if("true"===t)return!0;if("false"===t)return!1;if("null"===t)return null;if("undefined"===t)return;if(t.startsWith("{")||t.startsWith("["))try{return JSON.parse(t)}catch(e){throw new Error(`Invalid JSON: ${t}`)}if("1"===t)return!0;if("0"===t)return!1;if(""===t)return!0;if(!isNaN(t)&&""!==t&&!isNaN(parseFloat(t)))return Number(t);if(t.match(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/)){const e=new Date(t);if(!isNaN(e.getTime()))return e}return t}catch(e){return i&&i(e,t),t}},c=e=>{const n={};return Object.entries(e).forEach(([e,a])=>{n[e]=a&&"object"==typeof a&&"value"in a&&"watch"in a?a:new t.signal(a)}),n};t._extractProps=t=>{const e={},n=t.attributes;for(let a=n.length-1;a>=0;a--){const r=n[a];if(r.name.startsWith(":")){const n=r.name.slice(1),a=o?s(r.value):r.value;e[n]=a,t.removeAttribute(r.name)}}return e};const l=t.mount;t.mount=async(e,n,a={})=>{const o=r?c(a):a;return await l.call(t,e,n,o)};const f=t._mountComponents,p=new WeakMap,u=new Set;t._mountComponents=async(a,o,i)=>{for(const[n,s]of Object.entries(o))if(n)for(const o of a.querySelectorAll(n)){if(!(o instanceof HTMLElement))continue;const n=t._extractProps(o);let l=n,f=p.get(a);if(!f){let t=a;for(;t&&!f;){if(t._eleva_instance&&t._eleva_instance.data){f=t._eleva_instance.data,p.set(a,f);break}t=t.parentElement}}if(r&&f){const a={};Object.keys(n).forEach(e=>{f[e]&&f[e]instanceof t.signal&&(a[e]=f[e])}),l=e({},n,a)}let d=l;if(r){const t={};Object.entries(l).forEach(([e,n])=>{n&&"object"==typeof n&&"value"in n&&"watch"in n||(t[e]=n)}),d=e({},c(t),l)}const m=await t.mount(o,s,d);m&&!i.includes(m)&&(i.push(m),r&&Object.keys(n).length>0&&!f&&u.add({instance:m,extractedProps:n,container:a,component:s}))}if(r&&u.size>0)for(const e of u){const{instance:a,extractedProps:o,container:r,component:i}=e;let s=p.get(r);if(!s){let t=r;for(;t&&!s;){if(t._eleva_instance&&t._eleva_instance.data){s=t._eleva_instance.data,p.set(r,s);break}t=t.parentElement}}if(s){const r={};if(Object.keys(o).forEach(e=>{s[e]&&s[e]instanceof t.signal&&(r[e]=s[e])}),Object.keys(r).length>0){Object.assign(a.data,r),Object.keys(r).forEach(e=>{const o=r[e];o&&"function"==typeof o.watch&&o.watch(()=>{const e=t._components.get(i)||i;if(e&&e.template){const o="function"==typeof e.template?e.template(a.data):e.template,r=n.parse(o,a.data);t.renderer.patchDOM(a.container,r)}})});const e=t._components.get(i)||i;if(e&&e.template){const o="function"==typeof e.template?e.template(a.data):e.template,r=n.parse(o,a.data);t.renderer.patchDOM(a.container,r)}}u.delete(e)}}},t.props={parse:t=>o?s(t):t,detectType:t=>null===t?"null":void 0===t?"undefined":"boolean"==typeof t?"boolean":"number"==typeof t?"number":"string"==typeof t?"string":"function"==typeof t?"function":t instanceof Date?"date":t instanceof Map?"map":t instanceof Set?"set":Array.isArray(t)?"array":"object"==typeof t?"object":"unknown"},t._originalExtractProps=t._extractProps,t._originalMount=l,t._originalMountComponents=f},uninstall(t){t._originalExtractProps&&(t._extractProps=t._originalExtractProps,delete t._originalExtractProps),t._originalMount&&(t.mount=t._originalMount,delete t._originalMount),t._originalMountComponents&&(t._mountComponents=t._originalMountComponents,delete t._originalMountComponents),t.props&&delete t.props}}},"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).ElevaPropsPlugin={});
//# sourceMappingURL=props.umd.min.js.map