@twobirds/microcomponents 0.1.16 → 0.1.19

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.min.js DELETED
@@ -1,345 +0,0 @@
1
- /******/ // The require scope
2
- /******/ var __webpack_require__ = {};
3
- /******/
4
- /************************************************************************/
5
- /******/ /* webpack/runtime/define property getters */
6
- /******/ (() => {
7
- /******/ // define getter functions for harmony exports
8
- /******/ __webpack_require__.d = (exports, definition) => {
9
- /******/ for(var key in definition) {
10
- /******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
11
- /******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
12
- /******/ }
13
- /******/ }
14
- /******/ };
15
- /******/ })();
16
- /******/
17
- /******/ /* webpack/runtime/hasOwnProperty shorthand */
18
- /******/ (() => {
19
- /******/ __webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
20
- /******/ })();
21
- /******/
22
- /************************************************************************/
23
- var __webpack_exports__ = {};
24
- /* harmony export */ __webpack_require__.d(__webpack_exports__, {
25
- /* harmony export */ AU: () => (/* binding */ off),
26
- /* harmony export */ DC: () => (/* binding */ DC),
27
- /* harmony export */ E_: () => (/* binding */ autoload),
28
- /* harmony export */ MC: () => (/* binding */ MC),
29
- /* harmony export */ Oc: () => (/* binding */ McBase),
30
- /* harmony export */ hZ: () => (/* binding */ trigger),
31
- /* harmony export */ on: () => (/* binding */ on),
32
- /* harmony export */ xH: () => (/* binding */ one)
33
- /* harmony export */ });
34
-
35
- const isBrowser = new Function('try {return this===window;}catch(e){ return false;}');
36
- function isDOM(element) {
37
- return element && element instanceof HTMLElement;
38
- }
39
- function add(target, key, element = {}) {
40
- let node = target;
41
- if (!node._mc) {
42
- node._mc = new DC(target);
43
- }
44
- const _mc = node._mc;
45
- if (key.length && !_mc[key]) {
46
- _mc[key] = element;
47
- if (target instanceof HTMLElement && !target.getAttribute('data-mc')) {
48
- target.setAttribute('data-mc', '');
49
- }
50
- }
51
- return node;
52
- }
53
- function remove(target, key) {
54
- if (!key)
55
- return;
56
- const _mc = target?._mc || {};
57
- if (_mc && _mc[key]) {
58
- delete _mc[key];
59
- }
60
- if (!Object.keys(_mc).length &&
61
- target instanceof HTMLElement &&
62
- target.hasAttribute('data-mc')) {
63
- target.removeAttribute('data-mc');
64
- }
65
- return;
66
- }
67
- function init(element) {
68
- setTimeout(() => {
69
- if (element.constructor === DC)
70
- return;
71
- element.trigger('Init');
72
- }, 0);
73
- }
74
- function getMcEventName(eventName) {
75
- return '_mc' + eventName[0].toUpperCase() + eventName.substring(1);
76
- }
77
- function off(domNode, eventName, handler) {
78
- domNode.removeEventListener(getMcEventName(eventName), handler);
79
- }
80
- function on(domNode, eventName, handler, once = false) {
81
- domNode.addEventListener(getMcEventName(eventName), handler, { capture: false, once });
82
- return handler;
83
- }
84
- function one(domNode, eventName, handler) {
85
- on(domNode, eventName, handler, true);
86
- return handler;
87
- }
88
- class BubbleController {
89
- stopped = false;
90
- constructor() { }
91
- stop() {
92
- this.stopped = true;
93
- }
94
- }
95
- function trigger(domNode, event, data = {}, bubble = 'l', controller) {
96
- const eventName = /^_mc/.test(event) ? event : getMcEventName(event), bubbleController = controller ? controller : new BubbleController(), ev = new CustomEvent(eventName, {
97
- detail: { data, control: bubbleController },
98
- bubbles: false,
99
- });
100
- if (bubbleController.stopped)
101
- return;
102
- if (/l/.test(bubble)) {
103
- domNode.dispatchEvent(ev);
104
- ev.stopImmediatePropagation();
105
- if (bubbleController.stopped)
106
- return;
107
- }
108
- if (/[ud]/.test(bubble)) {
109
- if (bubble.indexOf('l') === -1) {
110
- bubble += 'l';
111
- }
112
- setTimeout(() => {
113
- if (bubble.indexOf('u') > -1) {
114
- let parentMc = domNode.parentElement?.closest('[data-mc]');
115
- if (parentMc) {
116
- parentMc?._mc.trigger(eventName, data, bubble, bubbleController);
117
- }
118
- }
119
- if (bubble.indexOf('d') > -1) {
120
- let childNodes = domNode._mc.children();
121
- if (childNodes) {
122
- childNodes.forEach((_mc) => _mc.trigger(eventName, data, bubble));
123
- }
124
- }
125
- }, 0);
126
- }
127
- }
128
- function autoAttachListeners(element) {
129
- if (!isBrowser() || element.constructor === DC)
130
- return;
131
- const target = element.target;
132
- if (target instanceof HTMLElement) {
133
- let that = element;
134
- Object.getOwnPropertyNames(that.constructor.prototype)
135
- .filter((key) => /^(on[A-Z]|one[A-Z])/.test(key) &&
136
- typeof that.constructor.prototype[key] === 'function')
137
- .forEach((key) => {
138
- let once = /^one/.test(key), withoutOnOne = key.replace(/^on[e]{0,1}/, ''), nativeEventName = withoutOnOne.toLowerCase();
139
- if ('on' + nativeEventName in target) {
140
- let listener = (ev) => {
141
- that[key](ev);
142
- };
143
- target.addEventListener(nativeEventName, listener);
144
- }
145
- else {
146
- let listener = (ev) => {
147
- that[key](ev);
148
- };
149
- on(target, withoutOnOne, listener, once);
150
- }
151
- });
152
- }
153
- }
154
- function getMcChildNodes(domNode) {
155
- const id = Math.random().toString().replace('.', '');
156
- const selector = '[data-mc]:not([temp_id="' + id + '"] [data-mc] [data-mc])';
157
- domNode.setAttribute('temp_id', id);
158
- const mcArray = [...domNode.querySelectorAll(selector)]
159
- .filter((e) => !!e._mc)
160
- .map((e) => e._mc);
161
- domNode.removeAttribute('temp_id');
162
- return mcArray.length ? mcArray : null;
163
- }
164
- class McBase {
165
- _target;
166
- constructor(target) {
167
- let element = this;
168
- this._target = new WeakRef(target || {});
169
- autoAttachListeners(element);
170
- init(element);
171
- }
172
- get target() {
173
- return this._target.deref();
174
- }
175
- }
176
- class MC extends McBase {
177
- constructor(target) {
178
- super(target);
179
- }
180
- }
181
- class DC extends McBase {
182
- constructor(target) {
183
- super(target);
184
- }
185
- static add(target, key, element = {}) {
186
- return add(target, key, element);
187
- }
188
- static remove(target, key) {
189
- remove(target, key);
190
- }
191
- off(eventName, handler) {
192
- off(this.target, eventName, handler);
193
- }
194
- on(eventName, handler) {
195
- return on(this.target, eventName, handler);
196
- }
197
- one(eventName, handler) {
198
- return one(this.target, eventName, handler);
199
- }
200
- trigger(eventName, data = {}, bubble = 'l', controller) {
201
- trigger(this.target, eventName, data, bubble, controller);
202
- }
203
- ancestors() {
204
- let currentNode = this?.target, result = [];
205
- if (!isDOM(currentNode))
206
- return [];
207
- while ((currentNode = currentNode.closest('[data-mc]'))) {
208
- if (currentNode?._mc) {
209
- result.push(currentNode?._mc);
210
- }
211
- }
212
- return result.length ? result : null;
213
- }
214
- closest() {
215
- let target = this.target;
216
- if (!isDOM(target))
217
- return null;
218
- return target.closest('[data-mc]')?._mc || null;
219
- }
220
- children() {
221
- let target = this?.target;
222
- if (!isDOM(target))
223
- return null;
224
- return getMcChildNodes(target);
225
- }
226
- descendants() {
227
- const target = this?.target;
228
- if (!isDOM(target))
229
- return [];
230
- const mcArray = [...target.querySelectorAll('[data-mc]')]
231
- .filter((e) => !!e._mc)
232
- .map((e) => e._mc);
233
- return mcArray.length ? mcArray : null;
234
- }
235
- }
236
- const loadingDCs = new Set();
237
- function attachDC(element) {
238
- return () => {
239
- const elements = Array.from(document.querySelectorAll(`[data-mc*="${element}"]`)).filter((element) => element instanceof HTMLElement);
240
- if (elements.length === 0)
241
- return;
242
- const Klass = customElements.get(element);
243
- elements.forEach((node) => {
244
- try {
245
- DC.add(node, Klass.name, new Klass(node));
246
- }
247
- catch (error) {
248
- if (Klass['create']) {
249
- let element = Klass.create(node);
250
- element.setAttribute('data-mc', node.getAttribute('data-mc'));
251
- node.replaceWith(element);
252
- node = element;
253
- }
254
- else {
255
- throw `Class ${Klass.name} doesn't have a callable constructor AND class doesn't have a static .create() method.`;
256
- }
257
- }
258
- const new_mc = node
259
- .getAttribute('data-mc')
260
- .split(' ')
261
- .filter((name) => name !== element && name !== '')
262
- .join(' ')
263
- .trim();
264
- node.setAttribute('data-mc', new_mc);
265
- });
266
- };
267
- }
268
- function makeLoadScript(element) {
269
- if (loadingDCs.has(element))
270
- return;
271
- loadingDCs.add(element);
272
- const fileName = './' + element.split('-').join('/') + '.js';
273
- const se = document.createElement('script');
274
- se.setAttribute('src', fileName);
275
- se.setAttribute('blocking', 'render');
276
- se.async = true;
277
- se.setAttribute('type', 'module');
278
- se.setAttribute('data-name', element);
279
- se.setAttribute('loading', '');
280
- const loadedPromise = customElements.whenDefined(element);
281
- loadedPromise.then(attachDC(element));
282
- se.addEventListener('load', (ev) => {
283
- loadingDCs.delete(element);
284
- document.head.querySelector(`script[data-name="${element}"]`)?.remove();
285
- });
286
- se.addEventListener('error', (ev) => {
287
- console.error('could not load DC code from', fileName);
288
- });
289
- document.head.append(se);
290
- }
291
- function loadUndefinedMCs() {
292
- [...document.querySelectorAll(':not(:defined)')]
293
- .filter((element) => !!element &&
294
- !customElements.get(element.tagName.toLowerCase()) &&
295
- !loadingDCs.has(element.tagName.toLowerCase()))
296
- .forEach((el) => {
297
- makeLoadScript(el.tagName.toLowerCase());
298
- });
299
- [...document.querySelectorAll('[data-mc]:not([data-mc=""]')]
300
- .filter((element) => !!element)
301
- .forEach((el) => {
302
- const _mc = el
303
- .getAttribute('data-mc')
304
- ?.split(' ')
305
- .filter((name) => name !== '');
306
- _mc?.forEach((element) => {
307
- const isDefined = customElements.get(element);
308
- if (!loadingDCs.has(element) && !isDefined) {
309
- makeLoadScript(element);
310
- }
311
- else if (isDefined) {
312
- attachDC(element)();
313
- }
314
- });
315
- });
316
- }
317
- let autoloadEnabled = false;
318
- let observer = null;
319
- function autoload(enable = true) {
320
- if (!enable) {
321
- observer?.disconnect();
322
- observer = null;
323
- return autoloadEnabled;
324
- }
325
- autoloadEnabled = enable;
326
- observer = new MutationObserver(loadUndefinedMCs);
327
- setTimeout(() => {
328
- observer.observe(document.body, { childList: true, subtree: true });
329
- loadUndefinedMCs();
330
- }, 5);
331
- window.autoload = window.autoload || autoload;
332
- return autoloadEnabled;
333
- }
334
- autoload.state = loadingDCs;
335
-
336
-
337
- var __webpack_exports__DC = __webpack_exports__.DC;
338
- var __webpack_exports__MC = __webpack_exports__.MC;
339
- var __webpack_exports__McBase = __webpack_exports__.Oc;
340
- var __webpack_exports__autoload = __webpack_exports__.E_;
341
- var __webpack_exports__off = __webpack_exports__.AU;
342
- var __webpack_exports__on = __webpack_exports__.on;
343
- var __webpack_exports__one = __webpack_exports__.xH;
344
- var __webpack_exports__trigger = __webpack_exports__.hZ;
345
- export { __webpack_exports__DC as DC, __webpack_exports__MC as MC, __webpack_exports__McBase as McBase, __webpack_exports__autoload as autoload, __webpack_exports__off as off, __webpack_exports__on as on, __webpack_exports__one as one, __webpack_exports__trigger as trigger };
@@ -1,5 +0,0 @@
1
- export declare function defineCE(tagName: string, DefinitionClass: {
2
- new (...args: any[]): any;
3
- } | Function, observedAttributes?: string[]): void;
4
- export declare function getCE(tagName: string): CustomElementConstructor | undefined;
5
- //# sourceMappingURL=elements.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"elements.d.ts","sourceRoot":"","sources":["../src/elements.ts"],"names":[],"mappings":"AA6CA,wBAAgB,QAAQ,CACpB,OAAO,EAAE,MAAM,EACf,eAAe,EAAE;IAAE,KAAK,GAAG,IAAI,EAAE,GAAG,EAAE,GAAG,GAAG,CAAA;CAAE,GAAG,QAAQ,EACzD,kBAAkB,GAAE,MAAM,EAAO,QAyDpC;AAED,wBAAgB,KAAK,CAAC,OAAO,EAAE,MAAM,GAAG,wBAAwB,GAAG,SAAS,CAE3E"}
package/dist/elements.js DELETED
@@ -1,67 +0,0 @@
1
- 'use strict';
2
- import { DC, McBase } from './MC.js';
3
- import { kebabToPascal } from './helpers.js';
4
- class CeBaseClass extends HTMLElement {
5
- constructor() {
6
- super();
7
- }
8
- connectedCallback() {
9
- this.dispatchEvent(new CustomEvent('_mcConnected'));
10
- }
11
- connectedMoveCallback() {
12
- this.dispatchEvent(new CustomEvent('_mcConnectedMove'));
13
- }
14
- disconnectedCallback() {
15
- this.dispatchEvent(new CustomEvent('_mcDisconnected'));
16
- }
17
- adoptedCallback() {
18
- this.dispatchEvent(new CustomEvent('_mcAdopted'));
19
- }
20
- attributeChangedCallback(name, oldValue, newValue) {
21
- this.dispatchEvent(new CustomEvent('_mcAttributeChanged', { detail: { name, oldValue, newValue } }));
22
- }
23
- formResetCallback() {
24
- this.dispatchEvent(new CustomEvent('_mcFormReset'));
25
- }
26
- }
27
- function createNamedClass(name, ClassBody) {
28
- return Object.defineProperty(ClassBody, 'name', { value: name });
29
- }
30
- function isClass(func) {
31
- return typeof func === 'function' && /^class\s/.test(Function.prototype.toString.call(func));
32
- }
33
- export function defineCE(tagName, DefinitionClass, observedAttributes = []) {
34
- if (customElements.get(tagName)) {
35
- console.warn(`IGNORED: Custom element "${tagName}" is already defined!`);
36
- return;
37
- }
38
- if (!tagName.includes('-')) {
39
- throw new Error('createCustomElement: tagName must contain at least one hyphen ("-").');
40
- }
41
- let name = kebabToPascal(tagName);
42
- if (!isClass(DefinitionClass.prototype.constructor)) {
43
- customElements.define(tagName, DefinitionClass);
44
- return;
45
- }
46
- if (!(DefinitionClass.prototype instanceof McBase)) {
47
- customElements.define(tagName, DefinitionClass);
48
- return;
49
- }
50
- customElements.define(tagName, createNamedClass(name, class extends CeBaseClass {
51
- instance;
52
- constructor(data) {
53
- super();
54
- const instance = isClass(DefinitionClass)
55
- ?
56
- new DefinitionClass(this, data)
57
- : DefinitionClass;
58
- DC.add(this, name, instance);
59
- }
60
- static get observedAttributes() {
61
- return observedAttributes;
62
- }
63
- }));
64
- }
65
- export function getCE(tagName) {
66
- return customElements.get(tagName);
67
- }
@@ -1 +0,0 @@
1
- var t={d:(e,n)=>{for(var r in n)t.o(n,r)&&!t.o(e,r)&&Object.defineProperty(e,r,{enumerable:!0,get:n[r]})},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e)},e={};t.d(e,{c:()=>d,t:()=>m});const n=new Function("try {return this===window;}catch(e){ return false;}");function r(t){return t&&t instanceof HTMLElement}function c(t){return"_mc"+t[0].toUpperCase()+t.substring(1)}function o(t,e,n,r=!1){return t.addEventListener(c(e),n,{capture:!1,once:r}),n}class s{stopped=!1;constructor(){}stop(){this.stopped=!0}}class a{_target;constructor(t){var e;this._target=new WeakRef(t||{}),function(t){if(!n()||t.constructor===i)return;const e=t.target;if(e instanceof HTMLElement){let n=t;Object.getOwnPropertyNames(n.constructor.prototype).filter((t=>/^(on[A-Z]|one[A-Z])/.test(t)&&"function"==typeof n.constructor.prototype[t])).forEach((t=>{let r=/^one/.test(t),c=t.replace(/^on[e]{0,1}/,""),s=c.toLowerCase();if("on"+s in e){let r=e=>{n[t](e)};e.addEventListener(s,r)}else o(e,c,(e=>{n[t](e)}),r)}))}}(this),e=this,setTimeout((()=>{e.constructor!==i&&e.trigger("Init")}),0)}get target(){return this._target.deref()}}class i extends a{constructor(t){super(t)}static add(t,e,n={}){return function(t,e,n={}){let r=t;r._mc||(r._mc=new i(t));const c=r._mc;return e.length&&!c[e]&&(c[e]=n,t instanceof HTMLElement&&!t.getAttribute("data-mc")&&t.setAttribute("data-mc","")),r}(t,e,n)}static remove(t,e){!function(t,e){if(!e)return;const n=t?._mc||{};n&&n[e]&&delete n[e],!Object.keys(n).length&&t instanceof HTMLElement&&t.hasAttribute("data-mc")&&t.removeAttribute("data-mc")}(t,e)}off(t,e){!function(t,e,n){t.removeEventListener(c(e),n)}(this.target,t,e)}on(t,e){return o(this.target,t,e)}one(t,e){return function(t,e,n){return o(t,e,n,!0),n}(this.target,t,e)}trigger(t,e={},n="l",r){!function(t,e,n={},r="l",o){const a=/^_mc/.test(e)?e:c(e),i=o||new s,l=new CustomEvent(a,{detail:{data:n,control:i},bubbles:!1});i.stopped||/l/.test(r)&&(t.dispatchEvent(l),l.stopImmediatePropagation(),i.stopped)||/[ud]/.test(r)&&(-1===r.indexOf("l")&&(r+="l"),setTimeout((()=>{if(r.indexOf("u")>-1){let e=t.parentElement?.closest("[data-mc]");e&&e?._mc.trigger(a,n,r,i)}if(r.indexOf("d")>-1){let e=t._mc.children();e&&e.forEach((t=>t.trigger(a,n,r)))}}),0))}(this.target,t,e,n,r)}ancestors(){let t=this?.target,e=[];if(!r(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 r(t)&&t.closest("[data-mc]")?._mc||null}children(){let t=this?.target;return r(t)?function(t){const e=Math.random().toString().replace(".",""),n='[data-mc]:not([temp_id="'+e+'"] [data-mc] [data-mc])';t.setAttribute("temp_id",e);const r=[...t.querySelectorAll(n)].filter((t=>!!t._mc)).map((t=>t._mc));return t.removeAttribute("temp_id"),r.length?r:null}(t):null}descendants(){const t=this?.target;if(!r(t))return[];const e=[...t.querySelectorAll("[data-mc]")].filter((t=>!!t._mc)).map((t=>t._mc));return e.length?e:null}}new Set;class l extends HTMLElement{constructor(){super()}connectedCallback(){this.dispatchEvent(new CustomEvent("_mcConnected"))}connectedMoveCallback(){this.dispatchEvent(new CustomEvent("_mcConnectedMove"))}disconnectedCallback(){this.dispatchEvent(new CustomEvent("_mcDisconnected"))}adoptedCallback(){this.dispatchEvent(new CustomEvent("_mcAdopted"))}attributeChangedCallback(t,e,n){this.dispatchEvent(new CustomEvent("_mcAttributeChanged",{detail:{name:t,oldValue:e,newValue:n}}))}formResetCallback(){this.dispatchEvent(new CustomEvent("_mcFormReset"))}}function u(t){return"function"==typeof t&&/^class\s/.test(Function.prototype.toString.call(t))}function d(t,e,n=[]){if(customElements.get(t))return void console.warn(`IGNORED: Custom element "${t}" is already defined!`);if(!t.includes("-"))throw new Error('createCustomElement: tagName must contain at least one hyphen ("-").');let r=t.split("-").map((t=>t.charAt(0).toUpperCase()+t.slice(1))).join("");u(e.prototype.constructor)&&e.prototype instanceof a?customElements.define(t,function(t,e){return Object.defineProperty(e,"name",{value:t})}(r,class extends l{instance;constructor(t){super();const n=u(e)?new e(this,t):e;i.add(this,r,n)}static get observedAttributes(){return n}})):customElements.define(t,e)}function m(t){return customElements.get(t)}var p=e.c,f=e.t;export{p as defineCE,f as getCE};
@@ -1,3 +0,0 @@
1
- import { LooseObject } from './helpers.js';
2
- export declare function formValues(targetObject: LooseObject, propertyName: string, dom: HTMLElement | ShadowRoot): void;
3
- //# sourceMappingURL=formValues.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"formValues.d.ts","sourceRoot":"","sources":["../src/formValues.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,WAAW,EAAsB,MAAM,cAAc,CAAC;AAmM/D,wBAAgB,UAAU,CACtB,YAAY,EAAE,WAAW,EACzB,YAAY,EAAE,MAAM,EACpB,GAAG,EAAE,WAAW,GAAG,UAAU,QAkBhC"}
@@ -1,159 +0,0 @@
1
- 'use strict';
2
- import { copyGettersSetters } from './helpers.js';
3
- import { DC } from './MC.js';
4
- import { observe } from './observables.js';
5
- const getInputName = (e) => {
6
- return e.tagName.toLowerCase();
7
- };
8
- class InputBase {
9
- constructor(e) { }
10
- }
11
- class RadioHandler extends InputBase {
12
- #e;
13
- constructor(e) {
14
- super(e);
15
- this.#e = e;
16
- }
17
- get val() {
18
- let ret = new FormData(this.#e.form).get(this.#e.name);
19
- return ret;
20
- }
21
- set val(value) {
22
- let arr = [
23
- ...this.#e.form.querySelectorAll(`input[type="radio"][name="${this.#e.name}"]`),
24
- ];
25
- arr.filter((e) => e.getAttribute('value') === value).shift().checked =
26
- true;
27
- }
28
- }
29
- class CheckboxHandler extends InputBase {
30
- #e;
31
- constructor(e) {
32
- super(e);
33
- this.#e = e;
34
- }
35
- get val() {
36
- return this.#e.checked;
37
- }
38
- set val(value) {
39
- this.#e.checked = value;
40
- }
41
- }
42
- class InputHandler extends InputBase {
43
- #e;
44
- constructor(e) {
45
- super(e);
46
- this.#e = e;
47
- }
48
- get val() {
49
- return this.#e.value;
50
- }
51
- set val(value) {
52
- this.#e.value = value;
53
- }
54
- }
55
- class SelectHandler extends InputBase {
56
- #e;
57
- #multi = false;
58
- constructor(e) {
59
- super(e);
60
- this.#e = e;
61
- this.#multi = e.multiple;
62
- }
63
- get val() {
64
- const that = this, ret = [];
65
- [...that.#e.querySelectorAll('option')]
66
- .filter((o) => o.selected)
67
- .forEach((o) => {
68
- ret.push(o.value);
69
- });
70
- if (this.#multi)
71
- return ret;
72
- return ret.length ? ret[0] : '';
73
- }
74
- set val(value) {
75
- const that = this;
76
- value = typeof value === 'string' ? [value] : value;
77
- [...that.#e.querySelectorAll('option')].forEach((o) => {
78
- if (value.indexOf(o.value) > -1) {
79
- o.selected = true;
80
- }
81
- else {
82
- o.selected = false;
83
- }
84
- });
85
- }
86
- }
87
- class TextAreaHandler extends InputBase {
88
- #e;
89
- constructor(e) {
90
- super(e);
91
- this.#e = e;
92
- }
93
- get val() {
94
- return this.#e.value || '';
95
- }
96
- set val(value) {
97
- this.#e.value = value;
98
- }
99
- }
100
- const getInputHandlers = (e) => {
101
- let inputList = [...(e || document).querySelectorAll('input,select,textarea')], handlers = {};
102
- inputList.forEach((input) => {
103
- const name = input.getAttribute('name');
104
- if (name) {
105
- let handler = false;
106
- switch (getInputName(input)) {
107
- case 'input':
108
- switch (input.type) {
109
- case 'radio':
110
- handler = new RadioHandler(input);
111
- break;
112
- case 'checkbox':
113
- handler = new CheckboxHandler(input);
114
- break;
115
- default:
116
- handler = new InputHandler(input);
117
- }
118
- break;
119
- case 'select':
120
- handler = new SelectHandler(input);
121
- break;
122
- case 'textarea':
123
- handler = new TextAreaHandler(input);
124
- break;
125
- default:
126
- console.warn('handler missing:', name, 'for', input.tagName.toLowerCase(), input);
127
- break;
128
- }
129
- if (handler) {
130
- DC.add(input, name, handler);
131
- Object.defineProperty(handlers, name, {
132
- enumerable: true,
133
- configurable: true,
134
- get: () => {
135
- return handler.val;
136
- },
137
- set: (value) => {
138
- if (handler.val !== value) {
139
- handler.val = value;
140
- }
141
- },
142
- });
143
- }
144
- }
145
- });
146
- return handlers;
147
- };
148
- export function formValues(targetObject, propertyName, dom) {
149
- let handlers = getInputHandlers(dom);
150
- targetObject[propertyName] = Object.assign({}, handlers);
151
- observe(targetObject, propertyName);
152
- copyGettersSetters(handlers, targetObject[propertyName]);
153
- Object.seal(targetObject[propertyName]);
154
- [...dom.querySelectorAll('input, textarea, select')].forEach((input) => {
155
- input.addEventListener('input', (ev) => {
156
- targetObject[propertyName].notify();
157
- });
158
- });
159
- }
@@ -1 +0,0 @@
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){return JSON.stringify(n(t))===JSON.stringify(n(e))}function a(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:()=>N});const c=new Function("try {return this===window;}catch(e){ return false;}");function o(t){return t&&t instanceof HTMLElement}function l(t){return"_mc"+t[0].toUpperCase()+t.substring(1)}function i(t,e,r,n=!1){return t.addEventListener(l(e),r,{capture:!1,once:n}),r}class u{stopped=!1;constructor(){}stop(){this.stopped=!0}}function f(t,e,r={},n="l",s){const a=/^_mc/.test(e)?e:l(e),c=s||new u,o=new CustomEvent(a,{detail:{data:r,control:c},bubbles:!1});c.stopped||/l/.test(n)&&(t.dispatchEvent(o),o.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 h{_target;constructor(t){var e;this._target=new WeakRef(t||{}),function(t){if(!c()||t.constructor===d)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 i(e,s,(e=>{r[t](e)}),n)}))}}(this),e=this,setTimeout((()=>{e.constructor!==d&&e.trigger("Init")}),0)}get target(){return this._target.deref()}}class d extends h{constructor(t){super(t)}static add(t,e,r={}){return function(t,e,r={}){let n=t;n._mc||(n._mc=new d(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(l(e),r)}(this.target,t,e)}on(t,e){return i(this.target,t,e)}one(t,e){return function(t,e,r){return i(t,e,r,!0),r}(this.target,t,e)}trigger(t,e={},r="l",n){f(this.target,t,e,r,n)}ancestors(){let t=this?.target,e=[];if(!o(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 o(t)&&t.closest("[data-mc]")?._mc||null}children(){let t=this?.target;return o(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(!o(t))return[];const e=[...t.querySelectorAll("[data-mc]")].filter((t=>!!t._mc)).map((t=>t._mc));return e.length?e:null}}function p(t){return null!=t&&"object"==typeof t}function m(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 g=[HTMLInputElement,HTMLSelectElement,HTMLTextAreaElement];class b extends d{data={};oldDisplay="";constructor(t){super(t),m(t).forEach((t=>this.data[t]="")),O(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&&!g.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 y={};function v(t){let e=t.constructor.name;return y[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=[],p(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(p(this.valueOf())){!function(t){var e;(e=t,[e].concat([...e.querySelectorAll("*")]).filter((t=>!!m(t).length)).filter((t=>"STYLE"!==t?.tagName)).reverse()).forEach((t=>{t?._mc?._placeholders||d.add(t,"_placeholders",new b(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&&f(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=p(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 y[r]=s,s}(t)}const O=(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],n=new(v(r))(r),c=function(){let t;return()=>{clearTimeout(t),t=setTimeout((function(){!function(){n.notify()}(...arguments)}),0)}}();Object.defineProperty(t,e,{enumerable:!0,get(){if(p(n.valueOf())){let t=structuredClone(Object.assign({},n));setTimeout((function(){let e=structuredClone(Object.assign({},n));console.log("compare",t,"to",e,"with",n.callbacks,"isChanged:",!s(e,t)),s(e,t)||c()}),0)}return n},set(t){if(typeof t==typeof n.valueOf())if(p(n.valueOf())&&n.valueOf()instanceof Array==0)void 0===n?.constructor?.prototype?.observe&&(n=new(v(t))(structuredClone(Object.assign({},t))),a(t,n)),Object.assign(n,t),n.notify();else{let e=n.valueOf(),r=n.callbacks;t!==e&&(n=new(v(t))(t),n.callbacks=r,c())}else console.warn("IGNORED - cannot change observable types, IS:",typeof n.valueOf(),n,"- CANNOT BECOME:",typeof t)}}),t[e]=r}})),t);class E{constructor(t){}}class w extends E{#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 A extends E{#n;constructor(t){super(t),this.#n=t}get val(){return this.#n.checked}set val(t){this.#n.checked=t}}class k extends E{#n;constructor(t){super(t),this.#n=t}get val(){return this.#n.value}set val(t){this.#n.value=t}}class _ extends E{#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 j extends E{#n;constructor(t){super(t),this.#n=t}get val(){return this.#n.value||""}set val(t){this.#n.value=t}}const S=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 w(t);break;case"checkbox":n=new A(t);break;default:n=new k(t)}break;case"select":n=new _(t);break;case"textarea":n=new j(t);break;default:console.warn("handler missing:",e,"for",t.tagName.toLowerCase(),t)}n&&(d.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 N(t,e,r){let n=S(r);t[e]=Object.assign({},n),O(t,e),a(n,t[e]),Object.seal(t[e]),[...r.querySelectorAll("input, textarea, select")].forEach((r=>{r.addEventListener("input",(r=>{t[e].notify()}))}))}var x=e.R;export{x as formValues};
package/dist/helpers.d.ts DELETED
@@ -1,14 +0,0 @@
1
- export interface LooseObject {
2
- [key: string | symbol]: any;
3
- }
4
- export declare function isNativeObject(value: any): boolean;
5
- export declare function sortObject(o: LooseObject): LooseObject;
6
- export declare function flattenObject(o: LooseObject, doSort?: boolean): LooseObject;
7
- export declare function deepEqual(o1: LooseObject, o2: LooseObject): boolean;
8
- export declare function nameSpace(ns: string | string[], obj?: LooseObject): any;
9
- export declare function parse(what: string | object | any[], parseThis: object): any;
10
- export declare function kebabToPascal(str: string): string;
11
- export declare function debounce(func: Function, milliseconds: number): Function;
12
- export declare function htmlToElements(html: string): HTMLElement[];
13
- export declare function copyGettersSetters(source: LooseObject, target: LooseObject): void;
14
- //# sourceMappingURL=helpers.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../src/helpers.ts"],"names":[],"mappings":"AAEA,MAAM,WAAW,WAAW;IACxB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,GAAG,CAAC;CAC/B;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"}