@twobirds/microcomponents 0.1.16 → 0.1.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/MC.d.ts.map +1 -1
- package/dist/MC.js +2 -5
- package/dist/MC.min.js +2 -5
- package/dist/MC.ts +336 -346
- package/dist/elements.d.ts.map +1 -1
- package/dist/elements.ts +90 -90
- package/dist/formValues.d.ts.map +1 -1
- package/dist/formValues.js +0 -1
- package/dist/formValues.min.js +1 -1
- package/dist/formValues.ts +165 -171
- package/dist/helpers.d.ts.map +1 -1
- package/dist/helpers.ts +100 -100
- package/dist/microcomponents.min.js +1 -1
- package/dist/observables.d.ts.map +1 -1
- package/dist/observables.js +3 -9
- package/dist/observables.min.js +1 -1
- package/dist/observables.ts +287 -325
- package/package.json +1 -1
package/dist/elements.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"elements.d.ts","sourceRoot":"","sources":["../src/elements.ts"],"names":[],"mappings":"AA6CA,wBAAgB,QAAQ,
|
|
1
|
+
{"version":3,"file":"elements.d.ts","sourceRoot":"","sources":["../src/elements.ts"],"names":[],"mappings":"AA6CA,wBAAgB,QAAQ,CACvB,OAAO,EAAE,MAAM,EACf,eAAe,EAAE;IAAE,KAAK,GAAG,IAAI,EAAE,GAAG,EAAE,GAAG,GAAG,CAAA;CAAE,GAAG,QAAQ,EACzD,kBAAkB,GAAE,MAAM,EAAO,QAyDjC;AAED,wBAAgB,KAAK,CAAC,OAAO,EAAE,MAAM,GAAG,wBAAwB,GAAG,SAAS,CAE3E"}
|
package/dist/elements.ts
CHANGED
|
@@ -4,107 +4,107 @@ import { DC, McBase } from './MC.js';
|
|
|
4
4
|
import { kebabToPascal } from './helpers.js';
|
|
5
5
|
|
|
6
6
|
class CeBaseClass extends HTMLElement {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
7
|
+
constructor() {
|
|
8
|
+
super();
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
connectedCallback() {
|
|
12
|
+
this.dispatchEvent(new CustomEvent('_mcConnected'));
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
connectedMoveCallback() {
|
|
16
|
+
this.dispatchEvent(new CustomEvent('_mcConnectedMove'));
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
disconnectedCallback() {
|
|
20
|
+
this.dispatchEvent(new CustomEvent('_mcDisconnected'));
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
adoptedCallback() {
|
|
24
|
+
this.dispatchEvent(new CustomEvent('_mcAdopted'));
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
attributeChangedCallback(name: string, oldValue: any, newValue: any) {
|
|
28
|
+
this.dispatchEvent(
|
|
29
|
+
new CustomEvent('_mcAttributeChanged', { detail: { name, oldValue, newValue } })
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
formResetCallback() {
|
|
34
|
+
this.dispatchEvent(new CustomEvent('_mcFormReset'));
|
|
35
|
+
}
|
|
36
36
|
}
|
|
37
37
|
|
|
38
38
|
function createNamedClass(name: string, ClassBody: { new (...args: any[]): any }) {
|
|
39
|
-
|
|
39
|
+
return Object.defineProperty(ClassBody, 'name', { value: name });
|
|
40
40
|
}
|
|
41
41
|
|
|
42
42
|
function isClass(func: any) {
|
|
43
|
-
|
|
43
|
+
return typeof func === 'function' && /^class\s/.test(Function.prototype.toString.call(func));
|
|
44
44
|
}
|
|
45
45
|
|
|
46
46
|
export function defineCE(
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
47
|
+
tagName: string,
|
|
48
|
+
DefinitionClass: { new (...args: any[]): any } | Function,
|
|
49
|
+
observedAttributes: string[] = []
|
|
50
50
|
) {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
51
|
+
if (customElements.get(tagName)) {
|
|
52
|
+
console.warn(`IGNORED: Custom element "${tagName}" is already defined!`);
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
if (!tagName.includes('-')) {
|
|
57
|
+
throw new Error('createCustomElement: tagName must contain at least one hyphen ("-").');
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
let name = kebabToPascal(tagName);
|
|
61
|
+
|
|
62
|
+
// not a class, but a pure function
|
|
63
|
+
if (!isClass(DefinitionClass.prototype.constructor)) {
|
|
64
|
+
customElements.define(
|
|
65
|
+
tagName,
|
|
66
|
+
// @ts-ignore
|
|
67
|
+
DefinitionClass
|
|
68
|
+
);
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// a class, but not a MC
|
|
73
|
+
if (!(DefinitionClass.prototype instanceof McBase)) {
|
|
74
|
+
customElements.define(
|
|
75
|
+
tagName,
|
|
76
|
+
// @ts-ignore
|
|
77
|
+
DefinitionClass
|
|
78
|
+
);
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// a MC
|
|
83
|
+
customElements.define(
|
|
84
|
+
tagName,
|
|
85
|
+
createNamedClass(
|
|
86
|
+
name,
|
|
87
|
+
class extends CeBaseClass {
|
|
88
|
+
instance: any;
|
|
89
|
+
|
|
90
|
+
constructor(data?: any) {
|
|
91
|
+
super();
|
|
92
|
+
// @ts-ignore
|
|
93
|
+
const instance = isClass(DefinitionClass)
|
|
94
|
+
? // @ts-ignore
|
|
95
|
+
new DefinitionClass(this, data)
|
|
96
|
+
: DefinitionClass;
|
|
97
|
+
DC.add(this, name, instance);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
static get observedAttributes() {
|
|
101
|
+
return observedAttributes;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
)
|
|
105
|
+
);
|
|
106
106
|
}
|
|
107
107
|
|
|
108
108
|
export function getCE(tagName: string): CustomElementConstructor | undefined {
|
|
109
|
-
|
|
109
|
+
return customElements.get(tagName);
|
|
110
110
|
}
|
package/dist/formValues.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"formValues.d.ts","sourceRoot":"","sources":["../src/formValues.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,WAAW,EAAsB,MAAM,cAAc,CAAC;
|
|
1
|
+
{"version":3,"file":"formValues.d.ts","sourceRoot":"","sources":["../src/formValues.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,WAAW,EAAsB,MAAM,cAAc,CAAC;AA6L/D,wBAAgB,UAAU,CACzB,YAAY,EAAE,WAAW,EACzB,YAAY,EAAE,MAAM,EACpB,GAAG,EAAE,WAAW,GAAG,UAAU,QAkB7B"}
|
package/dist/formValues.js
CHANGED
|
@@ -150,7 +150,6 @@ export function formValues(targetObject, propertyName, dom) {
|
|
|
150
150
|
targetObject[propertyName] = Object.assign({}, handlers);
|
|
151
151
|
observe(targetObject, propertyName);
|
|
152
152
|
copyGettersSetters(handlers, targetObject[propertyName]);
|
|
153
|
-
Object.seal(targetObject[propertyName]);
|
|
154
153
|
[...dom.querySelectorAll('input, textarea, select')].forEach((input) => {
|
|
155
154
|
input.addEventListener('input', (ev) => {
|
|
156
155
|
targetObject[propertyName].notify();
|
package/dist/formValues.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var t={d:(e,r)=>{for(var n in r)t.o(r,n)&&!t.o(e,n)&&Object.defineProperty(e,n,{enumerable:!0,get:r[n]})},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e)},e={};function r(t){let e={};return[...Object.keys(t)].sort().forEach((n=>{var s;(s=t[n])&&s.constructor&&s.constructor===Object?e[n]=r(t[n]):e[n]=t[n]})),e}function n(t,e=!0){let s={};return[...Object.getOwnPropertyNames(t)].forEach((e=>{if(t[e]instanceof Object){const r=n(t[e],!1);s[e]={};for(const t in r)s[e+"."+t]=r[t]}else s[e]=t[e]})),e?r(s):s}function s(t,e){
|
|
1
|
+
var t={d:(e,r)=>{for(var n in r)t.o(r,n)&&!t.o(e,n)&&Object.defineProperty(e,n,{enumerable:!0,get:r[n]})},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e)},e={};function r(t){let e={};return[...Object.keys(t)].sort().forEach((n=>{var s;(s=t[n])&&s.constructor&&s.constructor===Object?e[n]=r(t[n]):e[n]=t[n]})),e}function n(t,e=!0){let s={};return[...Object.getOwnPropertyNames(t)].forEach((e=>{if(t[e]instanceof Object){const r=n(t[e],!1);s[e]={};for(const t in r)s[e+"."+t]=r[t]}else s[e]=t[e]})),e?r(s):s}function s(t,e){const r=Object.getOwnPropertyDescriptors(t);for(const[t,n]of Object.entries(r))("get"in n||"set"in n)&&Object.defineProperty(e,t,n)}t.d(e,{R:()=>S});const a=new Function("try {return this===window;}catch(e){ return false;}");function c(t){return t&&t instanceof HTMLElement}function o(t){return"_mc"+t[0].toUpperCase()+t.substring(1)}function l(t,e,r,n=!1){return t.addEventListener(o(e),r,{capture:!1,once:n}),r}class i{stopped=!1;constructor(){}stop(){this.stopped=!0}}function u(t,e,r={},n="l",s){const a=/^_mc/.test(e)?e:o(e),c=s||new i,l=new CustomEvent(a,{detail:{data:r,control:c},bubbles:!1});c.stopped||/l/.test(n)&&(t.dispatchEvent(l),l.stopImmediatePropagation(),c.stopped)||/[ud]/.test(n)&&(-1===n.indexOf("l")&&(n+="l"),setTimeout((()=>{if(n.indexOf("u")>-1){let e=t.parentElement?.closest("[data-mc]");e&&e?._mc.trigger(a,r,n,c)}if(n.indexOf("d")>-1){let e=t._mc.children();e&&e.forEach((t=>t.trigger(a,r,n)))}}),0))}class f{_target;constructor(t){var e;this._target=new WeakRef(t||{}),function(t){if(!a()||t.constructor===h)return;const e=t.target;if(e instanceof HTMLElement){let r=t;Object.getOwnPropertyNames(r.constructor.prototype).filter((t=>/^(on[A-Z]|one[A-Z])/.test(t)&&"function"==typeof r.constructor.prototype[t])).forEach((t=>{let n=/^one/.test(t),s=t.replace(/^on[e]{0,1}/,""),a=s.toLowerCase();if("on"+a in e){let n=e=>{r[t](e)};e.addEventListener(a,n)}else l(e,s,(e=>{r[t](e)}),n)}))}}(this),e=this,setTimeout((()=>{e.constructor!==h&&e.trigger("Init")}),0)}get target(){return this._target.deref()}}class h extends f{constructor(t){super(t)}static add(t,e,r={}){return function(t,e,r={}){let n=t;n._mc||(n._mc=new h(t));const s=n._mc;return e.length&&!s[e]&&(s[e]=r,t instanceof HTMLElement&&!t.getAttribute("data-mc")&&t.setAttribute("data-mc","")),n}(t,e,r)}static remove(t,e){!function(t,e){if(!e)return;const r=t?._mc||{};r&&r[e]&&delete r[e],!Object.keys(r).length&&t instanceof HTMLElement&&t.hasAttribute("data-mc")&&t.removeAttribute("data-mc")}(t,e)}off(t,e){!function(t,e,r){t.removeEventListener(o(e),r)}(this.target,t,e)}on(t,e){return l(this.target,t,e)}one(t,e){return function(t,e,r){return l(t,e,r,!0),r}(this.target,t,e)}trigger(t,e={},r="l",n){u(this.target,t,e,r,n)}ancestors(){let t=this?.target,e=[];if(!c(t))return[];for(;t=t.closest("[data-mc]");)t?._mc&&e.push(t?._mc);return e.length?e:null}closest(){let t=this.target;return c(t)&&t.closest("[data-mc]")?._mc||null}children(){let t=this?.target;return c(t)?function(t){const e=Math.random().toString().replace(".",""),r='[data-mc]:not([temp_id="'+e+'"] [data-mc] [data-mc])';t.setAttribute("temp_id",e);const n=[...t.querySelectorAll(r)].filter((t=>!!t._mc)).map((t=>t._mc));return t.removeAttribute("temp_id"),n.length?n:null}(t):null}descendants(){const t=this?.target;if(!c(t))return[];const e=[...t.querySelectorAll("[data-mc]")].filter((t=>!!t._mc)).map((t=>t._mc));return e.length?e:null}}function d(t){return null!=t&&"object"==typeof t}function p(t){let e=new Set,r=/\{[^\{\}]*\}/g;return(t.getAttributeNames?t.getAttributeNames():[]).filter((e=>t.getAttribute(e)?.match(r))).forEach((e=>{let n=new Set(t.getAttribute(e)?.match(r));n.forEach((t=>n.add(t.replace(/[\{\}]/g,""))))})),[...t.childNodes].filter((t=>3===t.nodeType&&(t.nodeValue||"").match(r))).forEach((t=>{(t.nodeValue||"").match(r).forEach((t=>{e.add(t.replace(/[\{\}]/g,""))}))})),[...e].map((t=>t.replace(/[\{\}]/g,"")))}new Set;const m=[HTMLInputElement,HTMLSelectElement,HTMLTextAreaElement];class g extends h{data={};oldDisplay="";constructor(t){super(t),p(t).forEach((t=>this.data[t]="")),v(this),this.#t()}#t(){let t=this.target,e=!1;(t.getAttributeNames?t.getAttributeNames():[]).filter((e=>t.getAttribute(e)?.match(/\{[^\{\}]*\}/g))).forEach((e=>{let r=new Set(t.getAttribute(e)?.match(/\{[^\{\}]*\}/g));var n;this.data.observe((n=t.getAttribute(e),s=>{let a=n;r.forEach((t=>{a=a.replace(t,s[t.replace(/[\{\}]/g,"")])})),t.setAttribute(e,a)}))})),[...t.childNodes].filter((t=>3===t.nodeType&&(t.nodeValue||"").match(/\{[^\{\}]*\}/g))).forEach((r=>{e=!0;let n=new Set(r.nodeValue.match(/\{[^\{\}]*\}/g));this.data.observe((e=>s=>{let a=e;n.forEach((t=>{a=a.replace(t,s[t.replace(/[\{\}]/g,"")])})),this.oldDisplay?t.style=this.oldDisplay:t.removeAttribute("style"),delete this.oldDisplay,r.nodeValue=a})(r.nodeValue)),this.oldDisplay=t.getAttribute("style")||"",e&&!m.includes(t.constructor)&&"FIELDSET"!==t.tagName&&(t.style.display="none")}))}onData(t){let e={};Object.keys(t.detail.data).forEach((r=>{this.data.hasOwnProperty(r)&&(e[r]=t.detail.data[r])})),JSON.stringify(e)!==JSON.stringify(this.data)&&Object.assign(this.data,e)}}const b={};function y(t){let e=t.constructor.name;return b[e]||function(t){const e=t.constructor,r=e.name;let s=class extends e{#e=!0;#r=[];constructor(t){super(t),this.constructor.callbacks=[],d(t)&&Object.getOwnPropertyNames(t).forEach((e=>{this[e]=t[e]}))}get callbacks(){return this.#r}set callbacks(t){this.#r=t}observe(t){return this.#r.push(t),this}bind(t){if(d(this.valueOf())){!function(t){var e;(e=t,[e].concat([...e.querySelectorAll("*")]).filter((t=>!!p(t).length)).filter((t=>"STYLE"!==t?.tagName)).reverse()).forEach((t=>{t?._mc?._placeholders||h.add(t,"_placeholders",new g(t))}))}(t);const e=[t,...t.querySelectorAll("[data-mc]")].filter((t=>!!t?._mc?._placeholders)),r=function(t){let r=n(t);e.forEach((t=>{t&&u(t,"data",r)}))};this.#r.push(r),this.notify()}else console.warn("IGNORED - cannot bind a non-Object to the dom, IS:",typeof this.valueOf(),this);return this}notify(t){let e=d(this.valueOf())&&this.valueOf()instanceof Array==0?structuredClone(Object.assign({},this)):this.valueOf();return void 0!==t&&(this.#e=t),this.#e&&this.#r.forEach((t=>{t(e)})),this}};return b[r]=s,s}(t)}const v=(t,e)=>((e?[e]:[...Object.getOwnPropertyNames(t)]).forEach((e=>{if(t.hasOwnProperty(e)&&"function"!=typeof t[e]&&!t?.[e]?.constructor?.prototype?.observe){if("_"===e[0]||void 0===t?.[e])return t;let r=t[e],a=new(y(r))(r),c=function(){let t;return()=>{clearTimeout(t),t=setTimeout((function(){!function(){a.notify()}(...arguments)}),0)}}();Object.defineProperty(t,e,{enumerable:!0,get(){if(d(a.valueOf())){let t=structuredClone(Object.assign({},a));setTimeout((function(){var e,r;e=structuredClone(Object.assign({},a)),r=t,JSON.stringify(n(e))!==JSON.stringify(n(r))&&c()}),0)}return a},set(t){if(typeof t==typeof a.valueOf())if(d(a.valueOf())&&a.valueOf()instanceof Array==0)void 0===a?.constructor?.prototype?.observe&&(a=new(y(t))(structuredClone(Object.assign({},t))),s(t,a)),Object.assign(a,t),a.notify();else{let e=a.valueOf(),r=a.callbacks;t!==e&&(a=new(y(t))(t),a.callbacks=r,c())}else console.warn("IGNORED - cannot change observable types, IS:",typeof a.valueOf(),a,"- CANNOT BECOME:",typeof t)}}),t[e]=r}})),t);class O{constructor(t){}}class E extends O{#n;constructor(t){super(t),this.#n=t}get val(){return new FormData(this.#n.form).get(this.#n.name)}set val(t){[...this.#n.form.querySelectorAll(`input[type="radio"][name="${this.#n.name}"]`)].filter((e=>e.getAttribute("value")===t)).shift().checked=!0}}class w extends O{#n;constructor(t){super(t),this.#n=t}get val(){return this.#n.checked}set val(t){this.#n.checked=t}}class A extends O{#n;constructor(t){super(t),this.#n=t}get val(){return this.#n.value}set val(t){this.#n.value=t}}class _ extends O{#n;#s=!1;constructor(t){super(t),this.#n=t,this.#s=t.multiple}get val(){const t=[];return[...this.#n.querySelectorAll("option")].filter((t=>t.selected)).forEach((e=>{t.push(e.value)})),this.#s?t:t.length?t[0]:""}set val(t){t="string"==typeof t?[t]:t,[...this.#n.querySelectorAll("option")].forEach((e=>{t.indexOf(e.value)>-1?e.selected=!0:e.selected=!1}))}}class k extends O{#n;constructor(t){super(t),this.#n=t}get val(){return this.#n.value||""}set val(t){this.#n.value=t}}const j=t=>{let e=[...(t||document).querySelectorAll("input,select,textarea")],r={};return e.forEach((t=>{const e=t.getAttribute("name");if(e){let n=!1;switch((t=>t.tagName.toLowerCase())(t)){case"input":switch(t.type){case"radio":n=new E(t);break;case"checkbox":n=new w(t);break;default:n=new A(t)}break;case"select":n=new _(t);break;case"textarea":n=new k(t);break;default:console.warn("handler missing:",e,"for",t.tagName.toLowerCase(),t)}n&&(h.add(t,e,n),Object.defineProperty(r,e,{enumerable:!0,configurable:!0,get:()=>n.val,set:t=>{n.val!==t&&(n.val=t)}}))}})),r};function S(t,e,r){let n=j(r);t[e]=Object.assign({},n),v(t,e),s(n,t[e]),[...r.querySelectorAll("input, textarea, select")].forEach((r=>{r.addEventListener("input",(r=>{t[e].notify()}))}))}var N=e.R;export{N as formValues};
|
package/dist/formValues.ts
CHANGED
|
@@ -7,213 +7,207 @@ import { observe } from './observables.js';
|
|
|
7
7
|
type inputTypes = HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement;
|
|
8
8
|
|
|
9
9
|
const getInputName = (e: HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement): string => {
|
|
10
|
-
|
|
10
|
+
return e.tagName.toLowerCase();
|
|
11
11
|
};
|
|
12
12
|
|
|
13
13
|
abstract class InputBase {
|
|
14
|
-
|
|
14
|
+
constructor(e: inputTypes) {}
|
|
15
15
|
|
|
16
|
-
|
|
16
|
+
abstract get val(): any;
|
|
17
17
|
|
|
18
|
-
|
|
18
|
+
abstract set val(value: any);
|
|
19
19
|
}
|
|
20
20
|
|
|
21
21
|
class RadioHandler extends InputBase {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
22
|
+
#e: HTMLInputElement;
|
|
23
|
+
|
|
24
|
+
constructor(e: HTMLInputElement) {
|
|
25
|
+
super(e);
|
|
26
|
+
this.#e = e;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
get val(): string {
|
|
30
|
+
let ret = new FormData(this.#e.form as HTMLFormElement).get(this.#e.name);
|
|
31
|
+
|
|
32
|
+
//console.log('radio[', this.#e.name, '] = "' + ret + '"');
|
|
33
|
+
return ret as string;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
set val(value: string) {
|
|
37
|
+
let arr = [
|
|
38
|
+
...(this.#e.form as HTMLFormElement).querySelectorAll(
|
|
39
|
+
`input[type="radio"][name="${this.#e.name}"]`
|
|
40
|
+
),
|
|
41
|
+
] as HTMLInputElement[];
|
|
42
|
+
|
|
43
|
+
(arr.filter((e) => e.getAttribute('value') === value).shift() as HTMLInputElement).checked =
|
|
44
|
+
true;
|
|
45
|
+
}
|
|
46
46
|
}
|
|
47
47
|
|
|
48
48
|
class CheckboxHandler extends InputBase {
|
|
49
|
-
|
|
49
|
+
#e: HTMLInputElement;
|
|
50
50
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
51
|
+
constructor(e: HTMLInputElement) {
|
|
52
|
+
super(e);
|
|
53
|
+
this.#e = e;
|
|
54
|
+
}
|
|
55
55
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
56
|
+
get val(): boolean {
|
|
57
|
+
return this.#e.checked;
|
|
58
|
+
}
|
|
59
59
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
60
|
+
set val(value: boolean) {
|
|
61
|
+
this.#e.checked = value;
|
|
62
|
+
}
|
|
63
63
|
}
|
|
64
64
|
|
|
65
65
|
class InputHandler extends InputBase {
|
|
66
|
-
|
|
66
|
+
#e: HTMLInputElement;
|
|
67
67
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
68
|
+
constructor(e: HTMLInputElement) {
|
|
69
|
+
super(e);
|
|
70
|
+
this.#e = e;
|
|
71
|
+
}
|
|
72
72
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
73
|
+
get val(): string {
|
|
74
|
+
return this.#e.value;
|
|
75
|
+
}
|
|
76
76
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
77
|
+
set val(value: string) {
|
|
78
|
+
this.#e.value = value;
|
|
79
|
+
}
|
|
80
80
|
}
|
|
81
81
|
|
|
82
82
|
class SelectHandler extends InputBase {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
83
|
+
#e: HTMLSelectElement;
|
|
84
|
+
#multi: boolean = false;
|
|
85
|
+
|
|
86
|
+
constructor(e: HTMLSelectElement) {
|
|
87
|
+
super(e);
|
|
88
|
+
this.#e = e;
|
|
89
|
+
this.#multi = e.multiple;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
get val(): string[] | string {
|
|
93
|
+
const that = this,
|
|
94
|
+
ret: string[] = [];
|
|
95
|
+
|
|
96
|
+
// get all options
|
|
97
|
+
[...that.#e.querySelectorAll('option')]
|
|
98
|
+
.filter((o) => o.selected)
|
|
99
|
+
.forEach((o) => {
|
|
100
|
+
ret.push(o.value);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
if (this.#multi) return ret;
|
|
104
|
+
return ret.length ? ret[0] : '';
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
set val(value: string[] | string) {
|
|
108
|
+
const that = this;
|
|
109
|
+
|
|
110
|
+
value = typeof value === 'string' ? [value] : value;
|
|
111
|
+
|
|
112
|
+
// set all options
|
|
113
|
+
[...that.#e.querySelectorAll('option')].forEach((o) => {
|
|
114
|
+
if (value.indexOf(o.value) > -1) {
|
|
115
|
+
o.selected = true;
|
|
116
|
+
} else {
|
|
117
|
+
o.selected = false;
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
121
|
}
|
|
122
122
|
|
|
123
123
|
class TextAreaHandler extends InputBase {
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
124
|
+
#e: HTMLTextAreaElement;
|
|
125
|
+
constructor(e: HTMLTextAreaElement) {
|
|
126
|
+
super(e);
|
|
127
|
+
this.#e = e;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
get val(): string {
|
|
131
|
+
return this.#e.value || '';
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
set val(value: string) {
|
|
135
|
+
this.#e.value = value;
|
|
136
|
+
}
|
|
137
137
|
}
|
|
138
138
|
|
|
139
139
|
const getInputHandlers = (e: HTMLElement | ShadowRoot): LooseObject => {
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
});
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
});
|
|
194
|
-
|
|
195
|
-
return handlers;
|
|
140
|
+
let inputList = [...(e || document).querySelectorAll('input,select,textarea')],
|
|
141
|
+
handlers: LooseObject = {};
|
|
142
|
+
|
|
143
|
+
inputList.forEach((input) => {
|
|
144
|
+
const name: string | null = (input as inputTypes).getAttribute('name');
|
|
145
|
+
if (name) {
|
|
146
|
+
let handler: any = false;
|
|
147
|
+
switch (getInputName(input as inputTypes)) {
|
|
148
|
+
case 'input':
|
|
149
|
+
switch ((input as HTMLInputElement).type) {
|
|
150
|
+
case 'radio':
|
|
151
|
+
handler = new RadioHandler(input as HTMLInputElement);
|
|
152
|
+
break;
|
|
153
|
+
case 'checkbox':
|
|
154
|
+
handler = new CheckboxHandler(input as HTMLInputElement);
|
|
155
|
+
break;
|
|
156
|
+
default:
|
|
157
|
+
handler = new InputHandler(input as HTMLInputElement);
|
|
158
|
+
}
|
|
159
|
+
break;
|
|
160
|
+
case 'select':
|
|
161
|
+
handler = new SelectHandler(input as HTMLSelectElement);
|
|
162
|
+
break;
|
|
163
|
+
case 'textarea':
|
|
164
|
+
handler = new TextAreaHandler(input as HTMLTextAreaElement);
|
|
165
|
+
break;
|
|
166
|
+
default:
|
|
167
|
+
console.warn('handler missing:', name, 'for', input.tagName.toLowerCase(), input);
|
|
168
|
+
break;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
if (handler) {
|
|
172
|
+
DC.add(input as HTMLElement, name, handler);
|
|
173
|
+
Object.defineProperty(handlers, name, {
|
|
174
|
+
enumerable: true,
|
|
175
|
+
configurable: true,
|
|
176
|
+
get: (): any => {
|
|
177
|
+
return handler.val;
|
|
178
|
+
},
|
|
179
|
+
set: (value: any) => {
|
|
180
|
+
if (handler.val !== value) {
|
|
181
|
+
handler.val = value;
|
|
182
|
+
}
|
|
183
|
+
},
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
return handlers;
|
|
196
190
|
};
|
|
197
191
|
|
|
198
192
|
export function formValues(
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
193
|
+
targetObject: LooseObject,
|
|
194
|
+
propertyName: string,
|
|
195
|
+
dom: HTMLElement | ShadowRoot
|
|
202
196
|
) {
|
|
203
|
-
|
|
197
|
+
let handlers = getInputHandlers(dom);
|
|
204
198
|
|
|
205
|
-
|
|
199
|
+
targetObject[propertyName] = Object.assign({}, handlers);
|
|
206
200
|
|
|
207
|
-
|
|
201
|
+
observe(targetObject, propertyName);
|
|
208
202
|
|
|
209
|
-
|
|
203
|
+
copyGettersSetters(handlers, targetObject[propertyName]);
|
|
210
204
|
|
|
211
|
-
|
|
205
|
+
//Object.seal(targetObject[propertyName]); // object structure may not be changed from now on
|
|
212
206
|
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
207
|
+
[...dom.querySelectorAll('input, textarea, select')].forEach((input) => {
|
|
208
|
+
input.addEventListener('input', (ev) => {
|
|
209
|
+
//console.log('input event', input, ev);
|
|
210
|
+
targetObject[propertyName].notify();
|
|
211
|
+
});
|
|
212
|
+
});
|
|
219
213
|
}
|
package/dist/helpers.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../src/helpers.ts"],"names":[],"mappings":"AAEA,MAAM,WAAW,WAAW;
|
|
1
|
+
{"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../src/helpers.ts"],"names":[],"mappings":"AAEA,MAAM,WAAW,WAAW;IAC3B,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,GAAG,CAAC;CAC5B;AAED,wBAAgB,cAAc,CAAC,KAAK,EAAE,GAAG,GAAG,OAAO,CAElD;AAED,wBAAgB,UAAU,CAAC,CAAC,EAAE,WAAW,GAAG,WAAW,CAatD;AAED,wBAAgB,aAAa,CAAC,CAAC,EAAE,WAAW,EAAE,MAAM,GAAE,OAAc,GAAG,WAAW,CAgBjF;AAED,wBAAgB,SAAS,CAAC,EAAE,EAAE,WAAW,EAAE,EAAE,EAAE,WAAW,GAAG,OAAO,CAEnE;AAED,wBAAgB,SAAS,CAAC,EAAE,EAAE,MAAM,GAAG,MAAM,EAAE,EAAE,GAAG,GAAE,WAAgB,GAAG,GAAG,CAW3E;AAED,wBAAgB,KAAK,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,GAAG,EAAE,EAAE,SAAS,EAAE,MAAM,OAsCrE;AAED,wBAAgB,aAAa,CAAC,GAAG,EAAE,MAAM,UAKxC;AAED,wBAAgB,QAAQ,CAAC,IAAI,EAAE,QAAQ,EAAE,YAAY,EAAE,MAAM,GAAG,QAAQ,CASvE;AAED,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,GAAG,WAAW,EAAE,CAU1D;AAED,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,WAAW,EAAE,MAAM,EAAE,WAAW,QAO1E"}
|