@twobirds/microcomponents 0.1.6 → 0.1.10
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/README.md +4 -0
- package/dist/MC.d.ts.map +1 -1
- package/dist/MC.js +22 -21
- package/dist/MC.min.js +22 -21
- package/dist/MC.ts +373 -359
- package/dist/elements.d.ts.map +1 -1
- package/dist/elements.js +3 -3
- package/dist/elements.ts +91 -100
- package/dist/formValues.d.ts.map +1 -1
- package/dist/formValues.js +4 -6
- package/dist/formValues.min.js +1 -1
- package/dist/formValues.ts +173 -179
- package/dist/helpers.d.ts +1 -1
- package/dist/helpers.d.ts.map +1 -1
- package/dist/helpers.js +2 -4
- package/dist/helpers.ts +101 -113
- package/dist/microcomponents.d.ts.map +1 -1
- package/dist/microcomponents.js +1 -1
- package/dist/microcomponents.min.js +1 -1
- package/dist/microcomponents.ts +1 -3
- package/dist/observables.d.ts.map +1 -1
- package/dist/observables.js +17 -23
- package/dist/observables.min.js +1 -1
- package/dist/observables.ts +344 -378
- package/package.json +15 -4
package/dist/formValues.ts
CHANGED
|
@@ -6,220 +6,214 @@ import { observe } from './observables.js';
|
|
|
6
6
|
|
|
7
7
|
type inputTypes = HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement;
|
|
8
8
|
|
|
9
|
-
const getInputName = (
|
|
10
|
-
|
|
11
|
-
): string => {
|
|
12
|
-
return e.tagName.toLowerCase();
|
|
9
|
+
const getInputName = (e: HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement): string => {
|
|
10
|
+
return e.tagName.toLowerCase();
|
|
13
11
|
};
|
|
14
12
|
|
|
15
13
|
abstract class InputBase {
|
|
16
|
-
|
|
14
|
+
constructor(e: inputTypes) {}
|
|
17
15
|
|
|
18
|
-
|
|
16
|
+
abstract get val(): any;
|
|
19
17
|
|
|
20
|
-
|
|
18
|
+
abstract set val(value: any);
|
|
21
19
|
}
|
|
22
20
|
|
|
23
21
|
class RadioHandler extends InputBase {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
.shift() as HTMLInputElement
|
|
49
|
-
).checked = true;
|
|
50
|
-
}
|
|
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
|
+
}
|
|
51
46
|
}
|
|
52
47
|
|
|
53
48
|
class CheckboxHandler extends InputBase {
|
|
54
|
-
|
|
49
|
+
#e: HTMLInputElement;
|
|
55
50
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
51
|
+
constructor(e: HTMLInputElement) {
|
|
52
|
+
super(e);
|
|
53
|
+
this.#e = e;
|
|
54
|
+
}
|
|
60
55
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
56
|
+
get val(): boolean {
|
|
57
|
+
return this.#e.checked;
|
|
58
|
+
}
|
|
64
59
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
60
|
+
set val(value: boolean) {
|
|
61
|
+
this.#e.checked = value;
|
|
62
|
+
}
|
|
68
63
|
}
|
|
69
64
|
|
|
70
65
|
class InputHandler extends InputBase {
|
|
71
|
-
|
|
66
|
+
#e: HTMLInputElement;
|
|
72
67
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
68
|
+
constructor(e: HTMLInputElement) {
|
|
69
|
+
super(e);
|
|
70
|
+
this.#e = e;
|
|
71
|
+
}
|
|
77
72
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
73
|
+
get val(): string {
|
|
74
|
+
return this.#e.value;
|
|
75
|
+
}
|
|
81
76
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
77
|
+
set val(value: string) {
|
|
78
|
+
this.#e.value = value;
|
|
79
|
+
}
|
|
85
80
|
}
|
|
86
81
|
|
|
87
82
|
class SelectHandler extends InputBase {
|
|
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
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
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
|
+
}
|
|
126
121
|
}
|
|
127
122
|
|
|
128
123
|
class TextAreaHandler extends InputBase {
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
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
|
+
}
|
|
142
137
|
}
|
|
143
138
|
|
|
144
139
|
const getInputHandlers = (e: HTMLElement | ShadowRoot): LooseObject => {
|
|
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
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
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(
|
|
168
|
+
'handler missing:',
|
|
169
|
+
name,
|
|
170
|
+
'for',
|
|
171
|
+
input.tagName.toLowerCase(),
|
|
172
|
+
input
|
|
173
|
+
);
|
|
174
|
+
break;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
if (handler) {
|
|
178
|
+
DC.add(input as HTMLElement, name, handler);
|
|
179
|
+
Object.defineProperty(handlers, name, {
|
|
180
|
+
enumerable: true,
|
|
181
|
+
configurable: true,
|
|
182
|
+
get: (): any => {
|
|
183
|
+
return handler.val;
|
|
184
|
+
},
|
|
185
|
+
set: (value: any) => {
|
|
186
|
+
if (handler.val !== value) {
|
|
187
|
+
handler.val = value;
|
|
188
|
+
}
|
|
189
|
+
},
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
return handlers;
|
|
203
196
|
};
|
|
204
197
|
|
|
205
198
|
export function formValues(
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
199
|
+
targetObject: LooseObject,
|
|
200
|
+
propertyName: string,
|
|
201
|
+
dom: HTMLElement | ShadowRoot
|
|
209
202
|
) {
|
|
210
|
-
|
|
203
|
+
let handlers = getInputHandlers(dom);
|
|
211
204
|
|
|
212
|
-
|
|
205
|
+
targetObject[propertyName] = Object.assign({}, handlers);
|
|
213
206
|
|
|
214
|
-
|
|
207
|
+
observe(targetObject, propertyName);
|
|
215
208
|
|
|
216
|
-
|
|
209
|
+
copyGettersSetters(handlers, targetObject[propertyName]);
|
|
217
210
|
|
|
218
|
-
|
|
219
|
-
input.addEventListener('input', (ev) => {
|
|
220
|
-
//console.log('input event', input, ev);
|
|
221
|
-
targetObject[propertyName].notify();
|
|
222
|
-
});
|
|
223
|
-
});
|
|
224
|
-
}
|
|
211
|
+
Object.seal(targetObject[propertyName]); // object structure may not be changed from now on
|
|
225
212
|
|
|
213
|
+
[...dom.querySelectorAll('input, textarea, select')].forEach((input) => {
|
|
214
|
+
input.addEventListener('input', (ev) => {
|
|
215
|
+
//console.log('input event', input, ev);
|
|
216
|
+
targetObject[propertyName].notify();
|
|
217
|
+
});
|
|
218
|
+
});
|
|
219
|
+
}
|
package/dist/helpers.d.ts
CHANGED
|
@@ -6,7 +6,7 @@ export declare function sortObject(o: LooseObject): LooseObject;
|
|
|
6
6
|
export declare function flattenObject(o: LooseObject, doSort?: boolean): LooseObject;
|
|
7
7
|
export declare function deepEqual(o1: LooseObject, o2: LooseObject): boolean;
|
|
8
8
|
export declare function nameSpace(ns: string | string[], obj?: LooseObject): any;
|
|
9
|
-
export declare function parse(what: string |
|
|
9
|
+
export declare function parse(what: string | object | any[], parseThis: object): any;
|
|
10
10
|
export declare function kebabToPascal(str: string): string;
|
|
11
11
|
export declare function debounce(func: Function, milliseconds: number): Function;
|
|
12
12
|
export declare function htmlToElements(html: string): HTMLElement[];
|
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;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,CAejF;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"}
|
package/dist/helpers.js
CHANGED
|
@@ -17,9 +17,7 @@ export function sortObject(o) {
|
|
|
17
17
|
export function flattenObject(o, doSort = true) {
|
|
18
18
|
let result = {};
|
|
19
19
|
[...Object.getOwnPropertyNames(o)].forEach((propName) => {
|
|
20
|
-
if (o[propName] &&
|
|
21
|
-
o[propName]['constructor'] &&
|
|
22
|
-
o[propName].constructor === Object) {
|
|
20
|
+
if (o[propName] && o[propName]['constructor'] && o[propName].constructor === Object) {
|
|
23
21
|
const temp = flattenObject(o[propName], false);
|
|
24
22
|
for (const j in temp) {
|
|
25
23
|
result[propName + '.' + j] = temp[j];
|
|
@@ -32,7 +30,7 @@ export function flattenObject(o, doSort = true) {
|
|
|
32
30
|
return doSort ? sortObject(result) : result;
|
|
33
31
|
}
|
|
34
32
|
export function deepEqual(o1, o2) {
|
|
35
|
-
return
|
|
33
|
+
return JSON.stringify(flattenObject(o1)) === JSON.stringify(flattenObject(o2));
|
|
36
34
|
}
|
|
37
35
|
export function nameSpace(ns, obj = {}) {
|
|
38
36
|
let nsa = ns.constructor === String ? ns.split('.') : ns;
|