@twobirds/microcomponents 0.1.9 → 0.1.11

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.
@@ -6,222 +6,214 @@ import { observe } from './observables.js';
6
6
 
7
7
  type inputTypes = HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement;
8
8
 
9
- const getInputName = (
10
- e: HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement
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
- constructor(e: inputTypes) {}
14
+ constructor(e: inputTypes) {}
17
15
 
18
- abstract get val(): any;
16
+ abstract get val(): any;
19
17
 
20
- abstract set val(value: any);
18
+ abstract set val(value: any);
21
19
  }
22
20
 
23
21
  class RadioHandler extends InputBase {
24
- #e: HTMLInputElement;
25
-
26
- constructor(e: HTMLInputElement) {
27
- super(e);
28
- this.#e = e;
29
- }
30
-
31
- get val(): string {
32
- let ret = new FormData(this.#e.form as HTMLFormElement).get(this.#e.name);
33
-
34
- //console.log('radio[', this.#e.name, '] = "' + ret + '"');
35
- return ret as string;
36
- }
37
-
38
- set val(value: string) {
39
- let arr = [
40
- ...(this.#e.form as HTMLFormElement).querySelectorAll(
41
- `input[type="radio"][name="${this.#e.name}"]`
42
- ),
43
- ] as HTMLInputElement[];
44
-
45
- (
46
- arr
47
- .filter((e) => e.getAttribute('value') === value)
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
- #e: HTMLInputElement;
49
+ #e: HTMLInputElement;
55
50
 
56
- constructor(e: HTMLInputElement) {
57
- super(e);
58
- this.#e = e;
59
- }
51
+ constructor(e: HTMLInputElement) {
52
+ super(e);
53
+ this.#e = e;
54
+ }
60
55
 
61
- get val(): boolean {
62
- return this.#e.checked;
63
- }
56
+ get val(): boolean {
57
+ return this.#e.checked;
58
+ }
64
59
 
65
- set val(value: boolean) {
66
- this.#e.checked = value;
67
- }
60
+ set val(value: boolean) {
61
+ this.#e.checked = value;
62
+ }
68
63
  }
69
64
 
70
65
  class InputHandler extends InputBase {
71
- #e: HTMLInputElement;
66
+ #e: HTMLInputElement;
72
67
 
73
- constructor(e: HTMLInputElement) {
74
- super(e);
75
- this.#e = e;
76
- }
68
+ constructor(e: HTMLInputElement) {
69
+ super(e);
70
+ this.#e = e;
71
+ }
77
72
 
78
- get val(): string {
79
- return this.#e.value;
80
- }
73
+ get val(): string {
74
+ return this.#e.value;
75
+ }
81
76
 
82
- set val(value: string) {
83
- this.#e.value = value;
84
- }
77
+ set val(value: string) {
78
+ this.#e.value = value;
79
+ }
85
80
  }
86
81
 
87
82
  class SelectHandler extends InputBase {
88
- #e: HTMLSelectElement;
89
- #multi: boolean = false;
90
-
91
- constructor(e: HTMLSelectElement) {
92
- super(e);
93
- this.#e = e;
94
- this.#multi = e.multiple;
95
- }
96
-
97
- get val(): string[] | string {
98
- const that = this,
99
- ret: string[] = [];
100
-
101
- // get all options
102
- [...that.#e.querySelectorAll('option')]
103
- .filter((o) => o.selected)
104
- .forEach((o) => {
105
- ret.push(o.value);
106
- });
107
-
108
- if (this.#multi) return ret;
109
- return ret.length ? ret[0] : '';
110
- }
111
-
112
- set val(value: string[] | string) {
113
- const that = this;
114
-
115
- value = typeof value === 'string' ? [value] : value;
116
-
117
- // set all options
118
- [...that.#e.querySelectorAll('option')].forEach((o) => {
119
- if (value.indexOf(o.value) > -1) {
120
- o.selected = true;
121
- } else {
122
- o.selected = false;
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
- #e: HTMLTextAreaElement;
130
- constructor(e: HTMLTextAreaElement) {
131
- super(e);
132
- this.#e = e;
133
- }
134
-
135
- get val(): string {
136
- return this.#e.value || '';
137
- }
138
-
139
- set val(value: string) {
140
- this.#e.value = value;
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
- let inputList = [
146
- ...(e || document).querySelectorAll('input,select,textarea'),
147
- ],
148
- handlers: LooseObject = {};
149
-
150
- inputList.forEach((input) => {
151
- const name: string | null = (input as inputTypes).getAttribute('name');
152
- if (name) {
153
- let handler: any = false;
154
- switch (getInputName(input as inputTypes)) {
155
- case 'input':
156
- switch ((input as HTMLInputElement).type) {
157
- case 'radio':
158
- handler = new RadioHandler(input as HTMLInputElement);
159
- break;
160
- case 'checkbox':
161
- handler = new CheckboxHandler(input as HTMLInputElement);
162
- break;
163
- default:
164
- handler = new InputHandler(input as HTMLInputElement);
165
- }
166
- break;
167
- case 'select':
168
- handler = new SelectHandler(input as HTMLSelectElement);
169
- break;
170
- case 'textarea':
171
- handler = new TextAreaHandler(input as HTMLTextAreaElement);
172
- break;
173
- default:
174
- console.warn(
175
- 'handler missing:',
176
- name,
177
- 'for',
178
- input.tagName.toLowerCase(),
179
- input
180
- );
181
- break;
182
- }
183
-
184
- if (handler) {
185
- DC.add(input as HTMLElement, name, handler);
186
- Object.defineProperty(handlers, name, {
187
- enumerable: true,
188
- configurable: true,
189
- get: (): any => {
190
- return handler.val;
191
- },
192
- set: (value: any) => {
193
- if (handler.val !== value) {
194
- handler.val = value;
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
- targetObject: LooseObject,
207
- propertyName: string,
208
- dom: HTMLElement | ShadowRoot
199
+ targetObject: LooseObject,
200
+ propertyName: string,
201
+ dom: HTMLElement | ShadowRoot
209
202
  ) {
210
- let handlers = getInputHandlers(dom);
203
+ let handlers = getInputHandlers(dom);
211
204
 
212
- targetObject[propertyName] = Object.assign({}, handlers);
205
+ targetObject[propertyName] = Object.assign({}, handlers);
213
206
 
214
- observe(targetObject, propertyName);
207
+ observe(targetObject, propertyName);
215
208
 
216
- copyGettersSetters(handlers, targetObject[propertyName]);
209
+ copyGettersSetters(handlers, targetObject[propertyName]);
217
210
 
218
- Object.seal(targetObject[propertyName]); // object structure may not be changed from now on
211
+ Object.seal(targetObject[propertyName]); // object structure may not be changed from now on
219
212
 
220
- [...dom.querySelectorAll('input, textarea, select')].forEach((input) => {
221
- input.addEventListener('input', (ev) => {
222
- //console.log('input event', input, ev);
223
- targetObject[propertyName].notify();
224
- });
225
- });
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
+ });
226
219
  }
227
-
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 | Object | any[], parseThis: Object): any;
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[];
@@ -1 +1 @@
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,CAC5B,CAAC,EAAE,WAAW,EACd,MAAM,GAAE,OAAc,GACpB,WAAW,CAmBb;AAED,wBAAgB,SAAS,CAAC,EAAE,EAAE,WAAW,EAAE,EAAE,EAAE,WAAW,GAAG,OAAO,CAInE;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,OAyCrE;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"}
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 (JSON.stringify(flattenObject(o1)) === JSON.stringify(flattenObject(o2)));
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;