@twobirds/microcomponents 0.0.67 → 0.0.68
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/.vscode/settings.json +1 -1
- package/README.md +3 -3
- package/assets/documentation/formvalues.md +26 -0
- package/assets/test/MicroComponents.svg +15 -0
- package/assets/test/test.html +5 -5
- package/dist/microcomponents.d.ts +0 -1
- package/dist/microcomponents.d.ts.map +1 -1
- package/dist/microcomponents.min.js +1 -0
- package/dist/microcomponents.ts +1 -1
- package/package.json +6 -5
- package/public/MC.d.ts +36 -0
- package/public/MC.d.ts.map +1 -0
- package/public/MC.ts +605 -0
- package/public/McEvent.d.ts +15 -0
- package/public/McEvent.d.ts.map +1 -0
- package/public/McEvent.ts +96 -0
- package/public/documentation/formvalues.md +26 -0
- package/public/elements.d.ts +5 -0
- package/public/elements.d.ts.map +1 -0
- package/public/elements.ts +76 -0
- package/public/formValues.d.ts +3 -0
- package/public/formValues.d.ts.map +1 -0
- package/public/formValues.ts +233 -0
- package/public/helpers.d.ts +14 -0
- package/public/helpers.d.ts.map +1 -0
- package/public/helpers.ts +149 -0
- package/public/microcomponents.d.ts +7 -0
- package/public/microcomponents.d.ts.map +1 -0
- package/public/microcomponents.es3.d.ts +2 -0
- package/public/microcomponents.es3.d.ts.map +1 -0
- package/public/microcomponents.js +0 -1
- package/public/microcomponents.js.map +1 -1
- package/public/microcomponents.min.js +1 -0
- package/public/microcomponents.ts +8 -0
- package/public/observables.d.ts +10 -0
- package/public/observables.d.ts.map +1 -0
- package/public/observables.ts +401 -0
- package/public/selectors.d.ts +108 -0
- package/public/selectors.d.ts.map +1 -0
- package/public/selectors.ts +1634 -0
- package/public/src/microcomponents.ts +1 -1
- package/public/test/MC.d.ts +36 -0
- package/public/test/MC.d.ts.map +1 -0
- package/public/test/MC.js +378 -0
- package/public/test/MC.js.map +1 -0
- package/public/test/MC.ts +605 -0
- package/public/test/McEvent.d.ts +15 -0
- package/public/test/McEvent.d.ts.map +1 -0
- package/public/test/McEvent.js +71 -0
- package/public/test/McEvent.js.map +1 -0
- package/public/test/McEvent.ts +96 -0
- package/public/test/MicroComponents.svg +15 -0
- package/public/test/elements.d.ts +5 -0
- package/public/test/elements.d.ts.map +1 -0
- package/public/test/elements.js +48 -0
- package/public/test/elements.js.map +1 -0
- package/public/test/elements.ts +76 -0
- package/public/test/formValues.d.ts +3 -0
- package/public/test/formValues.d.ts.map +1 -0
- package/public/test/formValues.js +168 -0
- package/public/test/formValues.js.map +1 -0
- package/public/test/formValues.ts +233 -0
- package/public/test/helpers.d.ts +14 -0
- package/public/test/helpers.d.ts.map +1 -0
- package/public/test/helpers.js +113 -0
- package/public/test/helpers.js.map +1 -0
- package/public/test/helpers.ts +149 -0
- package/public/test/microcomponents.d.ts +7 -0
- package/public/test/microcomponents.d.ts.map +1 -0
- package/public/test/microcomponents.es3.d.ts +2 -0
- package/public/test/microcomponents.es3.d.ts.map +1 -0
- package/public/test/microcomponents.es3.js +303 -0
- package/public/test/microcomponents.es3.js.map +1 -0
- package/public/test/microcomponents.js +8 -0
- package/public/test/microcomponents.js.map +1 -0
- package/public/test/microcomponents.min.js +1 -0
- package/public/test/microcomponents.ts +8 -0
- package/public/test/observables.d.ts +10 -0
- package/public/test/observables.d.ts.map +1 -0
- package/public/test/observables.js +250 -0
- package/public/test/observables.js.map +1 -0
- package/public/test/observables.ts +401 -0
- package/public/test/selectors.d.ts +108 -0
- package/public/test/selectors.d.ts.map +1 -0
- package/public/test/selectors.js +971 -0
- package/public/test/selectors.js.map +1 -0
- package/public/test/selectors.ts +1634 -0
- package/public/test/test/child.d.ts +2 -0
- package/public/test/test/child.d.ts.map +1 -0
- package/public/test/{child.js → test/child.js} +1 -1
- package/public/test/test/child.js.map +1 -0
- package/public/test/test/grandchild.d.ts +2 -0
- package/public/test/test/grandchild.d.ts.map +1 -0
- package/public/test/{grandchild.js → test/grandchild.js} +1 -1
- package/public/test/test/grandchild.js.map +1 -0
- package/public/test/test/grandparent.d.ts +2 -0
- package/public/test/test/grandparent.d.ts.map +1 -0
- package/public/test/test/grandparent.js.map +1 -0
- package/public/test/test/greatgrandchild.d.ts +2 -0
- package/public/test/test/greatgrandchild.d.ts.map +1 -0
- package/public/test/{greatgrandchild.js → test/greatgrandchild.js} +1 -1
- package/public/test/test/greatgrandchild.js.map +1 -0
- package/public/test/test/parent.d.ts +2 -0
- package/public/test/test/parent.d.ts.map +1 -0
- package/public/test/{parent.js → test/parent.js} +1 -1
- package/public/test/test/parent.js.map +1 -0
- package/public/test/test/testform.d.ts +2 -0
- package/public/test/test/testform.d.ts.map +1 -0
- package/public/test/test/testform.js.map +1 -0
- package/public/test/test/testmc.d.ts +6 -0
- package/public/test/test/testmc.d.ts.map +1 -0
- package/public/test/{testmc.js → test/testmc.js} +1 -1
- package/public/test/test/testmc.js.map +1 -0
- package/public/test/test.d.ts +2 -0
- package/public/test/test.d.ts.map +1 -0
- package/public/test/test.html +5 -5
- package/public/test/test.ts +193 -0
- package/public/typedoc/classes/MC.DC.html +2 -2
- package/public/typedoc/classes/MC.MC.html +2 -2
- package/public/typedoc/classes/MC.McBase.html +2 -2
- package/public/typedoc/classes/MC.McEvent.html +2 -2
- package/public/typedoc/functions/MC.addListener.html +1 -1
- package/public/typedoc/functions/MC.autoload.html +1 -1
- package/public/typedoc/functions/MC.removeListener.html +1 -1
- package/public/typedoc/functions/elements.defineCE.html +1 -1
- package/public/typedoc/functions/elements.getCE.html +1 -1
- package/public/typedoc/functions/formValues.formValues.html +1 -1
- package/public/typedoc/functions/observables.observe.html +1 -1
- package/public/typedoc/functions/selectors._d.html +1 -1
- package/public/typedoc/functions/selectors._t.html +1 -1
- package/public/typedoc/index.html +3 -3
- package/public/typedoc/interfaces/MC.HTMLMcElement.html +2 -2
- package/public/typedoc/interfaces/selectors.DSelector.html +2 -2
- package/public/typedoc/interfaces/selectors.TSelector.html +2 -2
- package/public/typedoc/modules/MC.html +1 -1
- package/public/typedoc/modules/elements.html +1 -1
- package/public/typedoc/modules/formValues.html +1 -1
- package/public/typedoc/modules/observables.html +1 -1
- package/public/typedoc/modules/selectors.html +1 -1
- package/public/typedoc/types/observables.Observable.html +1 -1
- package/public/typedoc/types/selectors.selection.html +1 -1
- package/public/typedoc/types/selectors.singleSelector.html +1 -1
- package/src/microcomponents.ts +1 -1
- package/src/test/{child.ts → test/child.ts} +1 -1
- package/src/test/{grandchild.ts → test/grandchild.ts} +1 -1
- package/src/test/{grandparent.ts → test/grandparent.ts} +1 -1
- package/src/test/{greatgrandchild.ts → test/greatgrandchild.ts} +1 -1
- package/src/test/{parent.ts → test/parent.ts} +1 -1
- package/src/test/{testform.ts → test/testform.ts} +1 -1
- package/src/test/{testmc.ts → test/testmc.ts} +1 -1
- package/tsconfig.json +2 -2
- package/webpack.config.js +5 -5
- package/dist/microcomponents.js +0 -1
- package/public/test/child.js.map +0 -1
- package/public/test/grandchild.js.map +0 -1
- package/public/test/grandparent.js.map +0 -1
- package/public/test/greatgrandchild.js.map +0 -1
- package/public/test/parent.js.map +0 -1
- package/public/test/testform.js.map +0 -1
- package/public/test/testmc.js.map +0 -1
- /package/public/test/{grandparent.js → test/grandparent.js} +0 -0
- /package/public/test/{testform.js → test/testform.js} +0 -0
- /package/public/{test.js → test/test.js} +0 -0
- /package/public/{test.js.map → test/test.js.map} +0 -0
|
@@ -0,0 +1,1634 @@
|
|
|
1
|
+
import { MC, DC, HTMLMcElement, addListener, removeListener } from './MC.js';
|
|
2
|
+
import { LooseObject } from './helpers.js';
|
|
3
|
+
|
|
4
|
+
/*
|
|
5
|
+
helpers
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
const isObject = ( value: any ): boolean => {
|
|
9
|
+
return value != null && typeof value === 'object';
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const words = ( classes: Array<string> ): Array<string> => {
|
|
13
|
+
|
|
14
|
+
let classSet: Set<string> = new Set();
|
|
15
|
+
|
|
16
|
+
// read classes into set
|
|
17
|
+
classes.forEach( c => {
|
|
18
|
+
c = c.trim();
|
|
19
|
+
c.split(' ').forEach( e => {
|
|
20
|
+
if ( e !== '' ){
|
|
21
|
+
classSet.add(e);
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
// now we have a sanitized array
|
|
27
|
+
classes = Array.from( classSet );
|
|
28
|
+
|
|
29
|
+
return classes;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
type inputTypes = HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement;
|
|
33
|
+
|
|
34
|
+
const getInputName = ( e: HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement ): string => {
|
|
35
|
+
|
|
36
|
+
let ret = e instanceof HTMLInputElement
|
|
37
|
+
? 'input'
|
|
38
|
+
: e instanceof HTMLSelectElement
|
|
39
|
+
? 'select'
|
|
40
|
+
: e instanceof HTMLTextAreaElement
|
|
41
|
+
? 'textarea'
|
|
42
|
+
: ''
|
|
43
|
+
;
|
|
44
|
+
|
|
45
|
+
return ret;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
const getInputHandlers = ( e: HTMLElement | ShadowRoot ): LooseObject => {
|
|
50
|
+
|
|
51
|
+
let
|
|
52
|
+
//inputList = ( Array.from( e.querySelectorAll('*') ) as HTMLElement[] ).filter( i => isInputElement(i) ),
|
|
53
|
+
inputList = $d('input,select,textarea', e).array,
|
|
54
|
+
handlers: LooseObject = {};
|
|
55
|
+
|
|
56
|
+
inputList.forEach( (i) => {
|
|
57
|
+
const
|
|
58
|
+
name: string = ( i as inputTypes ).getAttribute('name') || 'undefined';
|
|
59
|
+
|
|
60
|
+
if ( name ){
|
|
61
|
+
let h: InputBase;
|
|
62
|
+
switch ( getInputName( i as inputTypes ) ){
|
|
63
|
+
case 'input':
|
|
64
|
+
//console.log( i, 'is HTMLInputElement');
|
|
65
|
+
h = new InputHandler( i as HTMLInputElement, e ); // e = element to search radios in = radioBase
|
|
66
|
+
Object.defineProperty( handlers, name, {
|
|
67
|
+
enumerable: true,
|
|
68
|
+
configurable: true,
|
|
69
|
+
get: (): any => { return h.val },
|
|
70
|
+
set: ( value: any ) => { h.val = value }
|
|
71
|
+
});
|
|
72
|
+
break;
|
|
73
|
+
case 'select':
|
|
74
|
+
//console.log( i, 'is HTMLSelectElement');
|
|
75
|
+
h = new SelectHandler( i as HTMLSelectElement );
|
|
76
|
+
Object.defineProperty( handlers, name, {
|
|
77
|
+
enumerable: true,
|
|
78
|
+
configurable: true,
|
|
79
|
+
get: (): any => { return h.val },
|
|
80
|
+
set: ( value: any ) => { h.val = value }
|
|
81
|
+
});
|
|
82
|
+
break;
|
|
83
|
+
case 'textarea':
|
|
84
|
+
//console.log( i, 'is HTMLTextAreaElement');
|
|
85
|
+
h = new TextAreaHandler( i as HTMLTextAreaElement );
|
|
86
|
+
Object.defineProperty( handlers, name, {
|
|
87
|
+
enumerable: true,
|
|
88
|
+
configurable: true,
|
|
89
|
+
get: (): any => { return h.val },
|
|
90
|
+
set: ( value: any ) => { h.val = value }
|
|
91
|
+
});
|
|
92
|
+
break;
|
|
93
|
+
default:
|
|
94
|
+
console.warn('handler missing:', name, 'for', i);
|
|
95
|
+
break;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
return handlers;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
abstract class InputBase{
|
|
104
|
+
constructor( e: any ){
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
abstract get val(): any;
|
|
108
|
+
|
|
109
|
+
abstract set val( value: any );
|
|
110
|
+
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
class InputHandler extends InputBase {
|
|
114
|
+
|
|
115
|
+
#e: HTMLInputElement;
|
|
116
|
+
#type: string;
|
|
117
|
+
#radioBase: any;
|
|
118
|
+
|
|
119
|
+
constructor( e: HTMLInputElement, radioBase?: HTMLElement | ShadowRoot ){
|
|
120
|
+
super( e );
|
|
121
|
+
this.#e = e;
|
|
122
|
+
this.#type = e.type;
|
|
123
|
+
this.#radioBase = radioBase;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
get val(): any{
|
|
127
|
+
let that = this,
|
|
128
|
+
type = that.#type,
|
|
129
|
+
ret: any;
|
|
130
|
+
|
|
131
|
+
switch (type){
|
|
132
|
+
case 'radio':
|
|
133
|
+
const check = $d( `input[type="radio"][name="${ this.#e.getAttribute('name') }"]`, this.#radioBase )
|
|
134
|
+
.filter( e => e.checked );
|
|
135
|
+
if ( !check.length ) return '';
|
|
136
|
+
ret = check.array[0];
|
|
137
|
+
return ret.value;
|
|
138
|
+
case 'checkbox':
|
|
139
|
+
return that.#e.checked;
|
|
140
|
+
default:
|
|
141
|
+
return that.#e.value;
|
|
142
|
+
break;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
return '';
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
set val( value: any ){
|
|
149
|
+
let that = this,
|
|
150
|
+
type = that.#type,
|
|
151
|
+
ret: any;
|
|
152
|
+
|
|
153
|
+
switch (type){
|
|
154
|
+
case 'radio':
|
|
155
|
+
const
|
|
156
|
+
selection = `input[type="radio"][name="${ this.#e.getAttribute('name') }"]`,
|
|
157
|
+
radios = $d( selection, this.#radioBase );
|
|
158
|
+
|
|
159
|
+
// console.log('set radios', selection, this.#radioBase, radios.array );
|
|
160
|
+
|
|
161
|
+
if ( !radios.length ) return;
|
|
162
|
+
|
|
163
|
+
if ( value === '' ){ // uncheck all
|
|
164
|
+
radios.forEach( r => { if (r.checked) r.checked = false } );
|
|
165
|
+
//console.log('unset all radios', radios.array );
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
const setRadioElement = radios.filter( r => r.getAttribute('value') === value );
|
|
170
|
+
|
|
171
|
+
if ( !setRadioElement.length ) return;
|
|
172
|
+
|
|
173
|
+
// console.log('set this radio', setRadioElement.array );
|
|
174
|
+
|
|
175
|
+
(setRadioElement.array[0] as HTMLInputElement).checked = true;
|
|
176
|
+
|
|
177
|
+
// console.log('done', setRadioElement.array );
|
|
178
|
+
return;
|
|
179
|
+
break;
|
|
180
|
+
case 'checkbox':
|
|
181
|
+
that.#e.checked = value;
|
|
182
|
+
break;
|
|
183
|
+
default:
|
|
184
|
+
that.#e.value = value;
|
|
185
|
+
break;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
class SelectHandler extends InputBase {
|
|
194
|
+
|
|
195
|
+
#e: HTMLSelectElement;
|
|
196
|
+
#multi: boolean = false;
|
|
197
|
+
|
|
198
|
+
constructor( e: HTMLSelectElement ){
|
|
199
|
+
super( e );
|
|
200
|
+
this.#e = e;
|
|
201
|
+
this.#multi = e.type === 'select-multiple';
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
get val(): string[] | string{
|
|
205
|
+
const
|
|
206
|
+
that = this,
|
|
207
|
+
ret: string[] = [];
|
|
208
|
+
|
|
209
|
+
// get all options
|
|
210
|
+
const options = $d( that.#e.querySelectorAll('option') )
|
|
211
|
+
.filter( o => o.selected )
|
|
212
|
+
.forEach( o => { if ( o.selected ) ret.push( o.value ) } );
|
|
213
|
+
|
|
214
|
+
if ( this.#multi ) return ret;
|
|
215
|
+
return ret.length ? ret[0] : '';
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
set val( value: string[] | string ){
|
|
219
|
+
const
|
|
220
|
+
that = this;
|
|
221
|
+
|
|
222
|
+
value = typeof value === 'string' ? [ value ] : value;
|
|
223
|
+
|
|
224
|
+
//console.log('set values', value );
|
|
225
|
+
|
|
226
|
+
// set all options
|
|
227
|
+
const options = $d( that.#e.querySelectorAll('option') )
|
|
228
|
+
.forEach( o => {
|
|
229
|
+
if ( value.indexOf( o.value ) > -1 ) {
|
|
230
|
+
o.setAttribute( 'selected', true )
|
|
231
|
+
o.selected = true;
|
|
232
|
+
} else {
|
|
233
|
+
o.removeAttribute( 'selected' )
|
|
234
|
+
o.selected = false;
|
|
235
|
+
}
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
/*
|
|
239
|
+
if (pOption.selected) {
|
|
240
|
+
if (
|
|
241
|
+
!pOption.disabled &&
|
|
242
|
+
(!pOption.parentNode.disabled ||
|
|
243
|
+
pOption.parentNode.nodeName !==
|
|
244
|
+
'optgroup')
|
|
245
|
+
) {
|
|
246
|
+
var value = pOption.value;
|
|
247
|
+
|
|
248
|
+
ret.push(value);
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
*/
|
|
252
|
+
return;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
class TextAreaHandler extends InputBase {
|
|
258
|
+
#e: HTMLTextAreaElement
|
|
259
|
+
constructor( e: HTMLTextAreaElement ){
|
|
260
|
+
super(e);
|
|
261
|
+
this.#e = e;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
get val(): string{
|
|
265
|
+
return this.#e.value;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
set val( value: string ){
|
|
269
|
+
this.#e.value = value;
|
|
270
|
+
return;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/*
|
|
276
|
+
selector classes
|
|
277
|
+
*/
|
|
278
|
+
|
|
279
|
+
class DomSelector{
|
|
280
|
+
|
|
281
|
+
#set: Set<HTMLElement> = new Set();
|
|
282
|
+
|
|
283
|
+
public get set(): Set<HTMLElement> {
|
|
284
|
+
return this.#set;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
public get array(): HTMLElement[] {
|
|
288
|
+
return Array.from( this.#set );
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
public set set( set: Set<HTMLElement> ) {
|
|
292
|
+
this.#set = set;
|
|
293
|
+
return;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
public get length(): number {
|
|
297
|
+
return this.#set.size;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
constructor( selection?: selection, element: any = document.body ){
|
|
301
|
+
|
|
302
|
+
let
|
|
303
|
+
that = this,
|
|
304
|
+
undefinedTags: Set<any> = new Set();
|
|
305
|
+
|
|
306
|
+
// // console.log('DomSelector constructor', ...arguments);
|
|
307
|
+
|
|
308
|
+
if ( !selection ) {
|
|
309
|
+
return that;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/* autoloading disabled !
|
|
313
|
+
if ( selection instanceof Array ){
|
|
314
|
+
selection.forEach( (select) => {
|
|
315
|
+
_getElementList( select, element ).forEach( (e) => {
|
|
316
|
+
if ( e.localName?.indexOf('-') !== -1 || e.getAttribute?.('is') ) _loadRequirements( e );
|
|
317
|
+
that.#set.add( e );
|
|
318
|
+
});
|
|
319
|
+
});
|
|
320
|
+
} else {
|
|
321
|
+
_getElementList( selection, element ).forEach( (e) => {
|
|
322
|
+
if ( e.localName?.indexOf('-') !== -1 || e.getAttribute?.('is') ) _loadRequirements( e );
|
|
323
|
+
that.#set.add( e );
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
*/
|
|
327
|
+
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
$t( nameSpace?: string ): TSelector {
|
|
331
|
+
let
|
|
332
|
+
result = new TbSelector()
|
|
333
|
+
;
|
|
334
|
+
|
|
335
|
+
this.#set.forEach( (e: HTMLElement) => {
|
|
336
|
+
if ( (e as HTMLMcElement)._mc ){
|
|
337
|
+
Object.values( (e as HTMLMcElement)._mc ).forEach( ( mc: MC | DC ) => {
|
|
338
|
+
result.set.add( mc );
|
|
339
|
+
});
|
|
340
|
+
}
|
|
341
|
+
});
|
|
342
|
+
|
|
343
|
+
if ( nameSpace ) {
|
|
344
|
+
result.filter( mc => (mc as LooseObject).nameSpace === nameSpace );
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
return result;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
add( selection: selection, target?: HTMLElement ): DSelector {
|
|
351
|
+
|
|
352
|
+
let
|
|
353
|
+
that = this
|
|
354
|
+
;
|
|
355
|
+
|
|
356
|
+
$d( selection, target ).forEach( element => {
|
|
357
|
+
that.#set.add( element );
|
|
358
|
+
});
|
|
359
|
+
|
|
360
|
+
return that;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
addClass( ...classes: string[] ): DSelector {
|
|
364
|
+
|
|
365
|
+
// sanitize classes
|
|
366
|
+
classes = words( classes );
|
|
367
|
+
|
|
368
|
+
// now set those classes
|
|
369
|
+
this.forEach( element => {
|
|
370
|
+
element.classList.add( ...classes );
|
|
371
|
+
});
|
|
372
|
+
|
|
373
|
+
return this;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
after( selection: selection, target?: HTMLElement ): DSelector {
|
|
377
|
+
|
|
378
|
+
let
|
|
379
|
+
that = this,
|
|
380
|
+
after = $d( selection, target );
|
|
381
|
+
;
|
|
382
|
+
|
|
383
|
+
if ( !after.length ) return that;
|
|
384
|
+
|
|
385
|
+
after
|
|
386
|
+
.first()
|
|
387
|
+
.forEach( element => {
|
|
388
|
+
that.array.reverse().forEach( (e: HTMLElement) => {
|
|
389
|
+
e.after( element );
|
|
390
|
+
});
|
|
391
|
+
});
|
|
392
|
+
|
|
393
|
+
return that;
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
append( selection: selection, target?: HTMLElement ): DSelector {
|
|
397
|
+
|
|
398
|
+
let
|
|
399
|
+
that = this,
|
|
400
|
+
addTo = that.array['0'],
|
|
401
|
+
append = $d( selection, target )
|
|
402
|
+
;
|
|
403
|
+
|
|
404
|
+
if ( addTo ){
|
|
405
|
+
append.forEach( (e: HTMLElement) => {
|
|
406
|
+
addTo.append( e );
|
|
407
|
+
});
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
return that;
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
appendTo( selection: selection, target?: HTMLElement ): DSelector {
|
|
414
|
+
|
|
415
|
+
let
|
|
416
|
+
that = this,
|
|
417
|
+
appendTo = $d( selection, target ).array['0']
|
|
418
|
+
;
|
|
419
|
+
|
|
420
|
+
if ( appendTo ){
|
|
421
|
+
that.forEach( (e: HTMLElement) => {
|
|
422
|
+
appendTo.append( e );
|
|
423
|
+
});
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
return that;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
attr(): LooseObject;
|
|
430
|
+
attr( obj: LooseObject ): DSelector;
|
|
431
|
+
attr( name: string ): string | null;
|
|
432
|
+
attr( first?: string | LooseObject, second?: string | object | null ): DSelector;
|
|
433
|
+
attr( first?: unknown, second?: unknown ): DSelector | LooseObject | string | null {
|
|
434
|
+
let
|
|
435
|
+
that = this,
|
|
436
|
+
attributes: LooseObject = {}
|
|
437
|
+
;
|
|
438
|
+
|
|
439
|
+
// if no elements in set -> skip
|
|
440
|
+
if ( !that.length ) return that;
|
|
441
|
+
|
|
442
|
+
// if no arguments, return attributes of first in list as hash object
|
|
443
|
+
if (!arguments.length) {
|
|
444
|
+
Array.from( [ ...that.set ][0].attributes ).forEach( (attribute: LooseObject) => {
|
|
445
|
+
attributes[attribute.name] = attribute.value;
|
|
446
|
+
});
|
|
447
|
+
return attributes;
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
// if first is a string
|
|
451
|
+
// if no value (second) is given -> return attribute value of first element in node selection
|
|
452
|
+
// if value (second) is null -> remove attribute and return this
|
|
453
|
+
// else set attribute values and return this
|
|
454
|
+
if ( typeof first === 'string' ) {
|
|
455
|
+
|
|
456
|
+
// return attributes
|
|
457
|
+
if ( second === undefined ){
|
|
458
|
+
return [ ...that.set ][0].getAttribute( first );
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
// remove attribute
|
|
462
|
+
if ( second === null ){
|
|
463
|
+
[ ...that.set ].forEach( (e: HTMLElement) =>{
|
|
464
|
+
e.removeAttribute( first );
|
|
465
|
+
});
|
|
466
|
+
return that;
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
// if attribute value is an object -> convert to json string
|
|
470
|
+
if ( typeof second === 'object' ){
|
|
471
|
+
second = JSON.stringify( second );
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
// set attribute
|
|
475
|
+
[ ...that.set ].forEach( (e: HTMLElement) =>{
|
|
476
|
+
e.setAttribute( first, second as string );
|
|
477
|
+
});
|
|
478
|
+
|
|
479
|
+
return that;
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
// if first is an object set attributes to all nodes
|
|
483
|
+
if ( typeof first === 'object' ) {
|
|
484
|
+
[ ...that.set ].forEach( (e: HTMLElement) =>{
|
|
485
|
+
Object.keys( first as LooseObject ).forEach( (key: string) => {
|
|
486
|
+
$d(e).attr( key, ( first as LooseObject ).key );
|
|
487
|
+
});
|
|
488
|
+
});
|
|
489
|
+
return that;
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
return that;
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
before( selection: selection, target?: HTMLElement ): DSelector {
|
|
496
|
+
|
|
497
|
+
let
|
|
498
|
+
that = this,
|
|
499
|
+
before = $d( selection, target );
|
|
500
|
+
;
|
|
501
|
+
|
|
502
|
+
if ( !before.length ) return that;
|
|
503
|
+
|
|
504
|
+
before
|
|
505
|
+
.first()
|
|
506
|
+
.forEach( element => {
|
|
507
|
+
that.forEach( (e: HTMLElement) => {
|
|
508
|
+
e.before( element );
|
|
509
|
+
});
|
|
510
|
+
});
|
|
511
|
+
|
|
512
|
+
return that;
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
children( selection?: selection, rootNode?: HTMLElement ): DSelector{
|
|
516
|
+
|
|
517
|
+
let
|
|
518
|
+
that: DomSelector = this,
|
|
519
|
+
set: Set<HTMLElement> = new Set()
|
|
520
|
+
;
|
|
521
|
+
|
|
522
|
+
that.#set.forEach( (e: HTMLElement) => {
|
|
523
|
+
Array.from( e.children ).forEach( ( c ) => {
|
|
524
|
+
set.add( c as HTMLElement );
|
|
525
|
+
});
|
|
526
|
+
});
|
|
527
|
+
|
|
528
|
+
that = $d( [ ...set] );
|
|
529
|
+
|
|
530
|
+
if ( selection ){
|
|
531
|
+
let compare = $d( selection, rootNode );
|
|
532
|
+
that.filter( ( e: HTMLElement ) => compare.has( e ) );
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
return that;
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
descendants( selection?: selection, rootNode?: HTMLElement ): DSelector{
|
|
539
|
+
let
|
|
540
|
+
that: DomSelector = this,
|
|
541
|
+
set: Set<HTMLElement> = new Set()
|
|
542
|
+
;
|
|
543
|
+
|
|
544
|
+
that.#set.forEach( (e: HTMLElement) => {
|
|
545
|
+
e.querySelectorAll('*').forEach( ( h ) => {
|
|
546
|
+
set.add( h as HTMLElement );
|
|
547
|
+
});
|
|
548
|
+
});
|
|
549
|
+
|
|
550
|
+
that = $d( [ ...set] );
|
|
551
|
+
|
|
552
|
+
if ( selection ){
|
|
553
|
+
let compare = $d( selection, rootNode );
|
|
554
|
+
that.filter( ( e: HTMLElement ) => compare.has( e ) );
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
return that;
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
empty(): DSelector {
|
|
561
|
+
return this.filter( (e: any)=>false );
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
first( selection?: selection, rootNode?: HTMLElement ): DSelector{
|
|
565
|
+
let
|
|
566
|
+
that: DomSelector = this,
|
|
567
|
+
set: Set<HTMLElement> = new Set(),
|
|
568
|
+
compare: any = false;
|
|
569
|
+
;
|
|
570
|
+
|
|
571
|
+
// get compare selection if specified
|
|
572
|
+
if ( selection ){
|
|
573
|
+
compare = $d( selection, rootNode );
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
// get compare selection if specified
|
|
577
|
+
that.#set.forEach( (e: HTMLElement) => {
|
|
578
|
+
if ( e.parentNode ){
|
|
579
|
+
set.add( e.parentNode.children[0] as HTMLElement );
|
|
580
|
+
}
|
|
581
|
+
});
|
|
582
|
+
|
|
583
|
+
// make result
|
|
584
|
+
that = $d( [ ...set ] );
|
|
585
|
+
|
|
586
|
+
// compare if necessay
|
|
587
|
+
if ( selection ){
|
|
588
|
+
that.filter( ( e: HTMLElement ) => compare.has( e ) );
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
return that;
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
has( element: HTMLElement ): boolean{
|
|
595
|
+
return this.#set.has( element );
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
hasClass( ...classes: string[] ): boolean {
|
|
599
|
+
|
|
600
|
+
// sanitize classes
|
|
601
|
+
classes = words( classes );
|
|
602
|
+
|
|
603
|
+
// now test those classes
|
|
604
|
+
return this.every( e => {
|
|
605
|
+
return Array.from( e.classList ).every( c => e.contains( c ) );
|
|
606
|
+
});
|
|
607
|
+
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
|
|
611
|
+
html( html?: string ): string | DSelector{
|
|
612
|
+
let that = this;
|
|
613
|
+
|
|
614
|
+
if ( html ){
|
|
615
|
+
that.forEach( (e: HTMLElement) => {
|
|
616
|
+
!e.parentNode
|
|
617
|
+
? (()=>{
|
|
618
|
+
console.warn('Cannot access the .innerHTML of a HTMLElement that hasnt been inserted yet', e );
|
|
619
|
+
console.trace( e, this );
|
|
620
|
+
console.info( 'If you called this from inside a Web Component constructor, consider using the %c[connected] event callback!', 'color: blue;' )
|
|
621
|
+
})()
|
|
622
|
+
: e.innerHTML = html;
|
|
623
|
+
});
|
|
624
|
+
return that;
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
return that.array[0].innerHTML;
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
last( selection?: selection, rootNode?: HTMLElement ): DSelector{
|
|
631
|
+
let
|
|
632
|
+
that: DomSelector = this,
|
|
633
|
+
set: Set<HTMLElement> = new Set()
|
|
634
|
+
;
|
|
635
|
+
|
|
636
|
+
that.#set.forEach( (e: HTMLElement) => {
|
|
637
|
+
let
|
|
638
|
+
siblings = e.parentNode?.children as HTMLCollection
|
|
639
|
+
;
|
|
640
|
+
if ( siblings ) {
|
|
641
|
+
set.add( Array.from(siblings).at(-1) as HTMLElement );
|
|
642
|
+
}
|
|
643
|
+
});
|
|
644
|
+
|
|
645
|
+
that = $d( [ ...set] );
|
|
646
|
+
|
|
647
|
+
if ( selection ){
|
|
648
|
+
let compare = $d( selection, rootNode );
|
|
649
|
+
that.filter( ( e: HTMLElement ) => compare.has( e ) );
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
return that;
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
/*
|
|
656
|
+
load(): DSelector{
|
|
657
|
+
let that = this;
|
|
658
|
+
|
|
659
|
+
if( !that.length ){
|
|
660
|
+
that.add( document.body );
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
that.forEach( e => _loadRequirements( e ) );
|
|
664
|
+
|
|
665
|
+
return that;
|
|
666
|
+
}
|
|
667
|
+
*/
|
|
668
|
+
|
|
669
|
+
next( selection?: selection, rootNode?: HTMLElement ): DSelector{
|
|
670
|
+
let
|
|
671
|
+
that: DomSelector = this,
|
|
672
|
+
set: Set<HTMLElement> = new Set()
|
|
673
|
+
;
|
|
674
|
+
|
|
675
|
+
that.#set.forEach( (e: HTMLElement) => {
|
|
676
|
+
let
|
|
677
|
+
siblings = e.parentNode?.children as HTMLCollection
|
|
678
|
+
;
|
|
679
|
+
if ( siblings ) {
|
|
680
|
+
let
|
|
681
|
+
arr = Array.from(siblings),
|
|
682
|
+
pos = arr.indexOf(e) + 1
|
|
683
|
+
;
|
|
684
|
+
if ( pos < arr.length ){
|
|
685
|
+
set.add( arr.at(pos) as HTMLElement );
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
});
|
|
689
|
+
|
|
690
|
+
that = $d( [ ...set] );
|
|
691
|
+
|
|
692
|
+
if ( selection ){
|
|
693
|
+
let compare = $d( selection, rootNode );
|
|
694
|
+
that.filter( ( e: HTMLElement ) => compare.has( e ) );
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
return that;
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
normalize(): DSelector {
|
|
701
|
+
let that = this;
|
|
702
|
+
that.#set.forEach( (e: HTMLElement) => { e.normalize() });
|
|
703
|
+
return that;
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
off( eventName: string, cb: Function ): DSelector{
|
|
707
|
+
let
|
|
708
|
+
that = this,
|
|
709
|
+
eventNames =
|
|
710
|
+
eventName.indexOf(' ') > -1
|
|
711
|
+
? eventName.split(' ')
|
|
712
|
+
: [ eventName ]
|
|
713
|
+
;
|
|
714
|
+
|
|
715
|
+
// remove handler
|
|
716
|
+
that.array.forEach( (e: HTMLElement) => {
|
|
717
|
+
eventNames.forEach(function ( n: string ) {
|
|
718
|
+
removeListener(
|
|
719
|
+
e,
|
|
720
|
+
n,
|
|
721
|
+
cb as EventListenerOrEventListenerObject
|
|
722
|
+
);
|
|
723
|
+
});
|
|
724
|
+
});
|
|
725
|
+
|
|
726
|
+
return that;
|
|
727
|
+
}
|
|
728
|
+
|
|
729
|
+
on( eventName: string, cb: Function, capture: boolean = false, once: boolean = false ): Function{
|
|
730
|
+
|
|
731
|
+
let
|
|
732
|
+
that = this,
|
|
733
|
+
eventNames =
|
|
734
|
+
eventName.indexOf(' ') > -1
|
|
735
|
+
? eventName.split(' ').filter( (s) => s !== '' )
|
|
736
|
+
: [ eventName ]
|
|
737
|
+
;
|
|
738
|
+
|
|
739
|
+
function wrapper(){ wrapper.cb.apply(that, Array.from(arguments) ); };
|
|
740
|
+
wrapper.cb = cb;
|
|
741
|
+
|
|
742
|
+
// attach handler
|
|
743
|
+
that.forEach( (e: HTMLElement) => {
|
|
744
|
+
eventNames.forEach(function ( n: string ) {
|
|
745
|
+
addListener(
|
|
746
|
+
e,
|
|
747
|
+
n,
|
|
748
|
+
(wrapper as EventListenerOrEventListenerObject),
|
|
749
|
+
capture,
|
|
750
|
+
once
|
|
751
|
+
);
|
|
752
|
+
});
|
|
753
|
+
});
|
|
754
|
+
|
|
755
|
+
return wrapper;
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
one( eventName: string, cb: Function, capture: boolean = false ): Function{
|
|
759
|
+
let
|
|
760
|
+
that = this,
|
|
761
|
+
eventNames =
|
|
762
|
+
eventName.indexOf(' ') > -1
|
|
763
|
+
? eventName.split(' ').filter( (s) => s !== '' )
|
|
764
|
+
: [ eventName ]
|
|
765
|
+
;
|
|
766
|
+
|
|
767
|
+
function wrapper(){ wrapper.cb.apply(that, Array.from(arguments) ); };
|
|
768
|
+
wrapper.cb = cb;
|
|
769
|
+
|
|
770
|
+
// attach handler
|
|
771
|
+
that.forEach( (e: HTMLElement) => {
|
|
772
|
+
eventNames.forEach(function ( n: string ) {
|
|
773
|
+
addListener(
|
|
774
|
+
e,
|
|
775
|
+
n,
|
|
776
|
+
(wrapper as EventListenerOrEventListenerObject),
|
|
777
|
+
capture,
|
|
778
|
+
true
|
|
779
|
+
);
|
|
780
|
+
});
|
|
781
|
+
});
|
|
782
|
+
|
|
783
|
+
return wrapper;
|
|
784
|
+
}
|
|
785
|
+
|
|
786
|
+
parent( selection?: selection, rootNode?: HTMLElement ): DSelector {
|
|
787
|
+
let
|
|
788
|
+
that: DomSelector = this,
|
|
789
|
+
set: Set<HTMLElement> = new Set()
|
|
790
|
+
;
|
|
791
|
+
|
|
792
|
+
that.#set.forEach( (e: HTMLElement) => {
|
|
793
|
+
if ( e.parentNode && (e.parentNode as HTMLElement).tagName !== 'HTML') {
|
|
794
|
+
set.add( e.parentNode as HTMLElement );
|
|
795
|
+
}
|
|
796
|
+
});
|
|
797
|
+
|
|
798
|
+
that = $d( [ ...set] );
|
|
799
|
+
|
|
800
|
+
if ( selection ){
|
|
801
|
+
let compare = $d( selection, rootNode );
|
|
802
|
+
that.filter( ( e: HTMLElement ) => compare.has( e ) );
|
|
803
|
+
}
|
|
804
|
+
|
|
805
|
+
return that;
|
|
806
|
+
}
|
|
807
|
+
|
|
808
|
+
parents( selection?: selection, rootNode?: HTMLElement ): DSelector {
|
|
809
|
+
let
|
|
810
|
+
that: DomSelector = this,
|
|
811
|
+
set: Set<HTMLElement> = new Set()
|
|
812
|
+
;
|
|
813
|
+
|
|
814
|
+
that.#set.forEach( (e: HTMLElement) => {
|
|
815
|
+
let
|
|
816
|
+
current = e
|
|
817
|
+
;
|
|
818
|
+
while ( current.parentNode && (current.parentNode as HTMLElement).tagName !== 'HTML' ){
|
|
819
|
+
current = current.parentNode as HTMLElement;
|
|
820
|
+
set.add( current );
|
|
821
|
+
}
|
|
822
|
+
});
|
|
823
|
+
|
|
824
|
+
that = $d( [ ...set] );
|
|
825
|
+
|
|
826
|
+
if ( selection ){
|
|
827
|
+
let compare = $d( selection, rootNode );
|
|
828
|
+
that.filter( ( e: HTMLElement ) => compare.has( e ) );
|
|
829
|
+
}
|
|
830
|
+
|
|
831
|
+
return that;
|
|
832
|
+
}
|
|
833
|
+
|
|
834
|
+
prepend( selection: selection, target?: HTMLElement ): DSelector {
|
|
835
|
+
|
|
836
|
+
let
|
|
837
|
+
that = this,
|
|
838
|
+
addTo = that.array['0'],
|
|
839
|
+
prepend = $d( selection, target )
|
|
840
|
+
;
|
|
841
|
+
|
|
842
|
+
if ( addTo ){
|
|
843
|
+
prepend.forEach( (e: HTMLElement) => {
|
|
844
|
+
addTo.prepend( e );
|
|
845
|
+
});
|
|
846
|
+
}
|
|
847
|
+
|
|
848
|
+
return that;
|
|
849
|
+
}
|
|
850
|
+
|
|
851
|
+
|
|
852
|
+
prev( selection?: selection, rootNode?: HTMLElement ): DSelector{
|
|
853
|
+
let
|
|
854
|
+
that: DomSelector = this,
|
|
855
|
+
set: Set<HTMLElement> = new Set()
|
|
856
|
+
;
|
|
857
|
+
|
|
858
|
+
that.#set.forEach( (e: HTMLElement) => {
|
|
859
|
+
let
|
|
860
|
+
siblings = e.parentNode?.children as HTMLCollection
|
|
861
|
+
;
|
|
862
|
+
if ( siblings ) {
|
|
863
|
+
let
|
|
864
|
+
arr = Array.from(siblings),
|
|
865
|
+
pos = arr.indexOf(e) - 1
|
|
866
|
+
;
|
|
867
|
+
if ( pos > -1 ){
|
|
868
|
+
set.add( arr.at(pos) as HTMLElement );
|
|
869
|
+
}
|
|
870
|
+
}
|
|
871
|
+
});
|
|
872
|
+
|
|
873
|
+
that = $d( [ ...set] );
|
|
874
|
+
|
|
875
|
+
if ( selection ){
|
|
876
|
+
let compare = $d( selection, rootNode );
|
|
877
|
+
that.filter( ( e: HTMLElement ) => compare.has( e ) );
|
|
878
|
+
}
|
|
879
|
+
|
|
880
|
+
return that;
|
|
881
|
+
}
|
|
882
|
+
|
|
883
|
+
removeClass( ...classes: string[] ): DSelector {
|
|
884
|
+
|
|
885
|
+
// sanitize classes
|
|
886
|
+
classes = words( classes );
|
|
887
|
+
|
|
888
|
+
// now set those classes
|
|
889
|
+
this.forEach( element => {
|
|
890
|
+
element.classList.remove( ...classes );
|
|
891
|
+
});
|
|
892
|
+
|
|
893
|
+
return this;
|
|
894
|
+
}
|
|
895
|
+
|
|
896
|
+
text( text?: string ): string | DSelector{
|
|
897
|
+
let that = this;
|
|
898
|
+
if ( text ){
|
|
899
|
+
that.forEach( (e: HTMLElement) => {
|
|
900
|
+
!e.parentNode
|
|
901
|
+
? (()=>{
|
|
902
|
+
console.warn('Cannot access the .textContent of a HTMLElement that hasnt been inserted yet', e );
|
|
903
|
+
console.trace( e, this );
|
|
904
|
+
console.info( 'If you called this from inside a Web Component constructor, consider using the %c[connected] event callback!', 'color: blue;' )
|
|
905
|
+
})()
|
|
906
|
+
: e.innerText = text;
|
|
907
|
+
});
|
|
908
|
+
return that as DomSelector;
|
|
909
|
+
}
|
|
910
|
+
|
|
911
|
+
let t = that.array[0].innerText;
|
|
912
|
+
return t ?? '' as string;
|
|
913
|
+
}
|
|
914
|
+
|
|
915
|
+
toggleClass( ...classes: string[] ): DSelector {
|
|
916
|
+
|
|
917
|
+
// sanitize classes
|
|
918
|
+
classes = words( classes );
|
|
919
|
+
|
|
920
|
+
// now set those classes
|
|
921
|
+
this.forEach( element => {
|
|
922
|
+
element.classList.toggle( ...classes );
|
|
923
|
+
});
|
|
924
|
+
|
|
925
|
+
return this;
|
|
926
|
+
}
|
|
927
|
+
|
|
928
|
+
trigger( eventName: string, data: any = {}, bubbles: boolean = false, cancelable: boolean = false, composed: boolean = false ): DSelector{
|
|
929
|
+
let
|
|
930
|
+
that = this,
|
|
931
|
+
ev: Event,
|
|
932
|
+
options: LooseObject = { bubbles, cancelable, composed },
|
|
933
|
+
eventNames =
|
|
934
|
+
eventName.indexOf(' ') > -1
|
|
935
|
+
? eventName.split(' ')
|
|
936
|
+
: [ eventName ]
|
|
937
|
+
;
|
|
938
|
+
|
|
939
|
+
that.#set.forEach( (e: HTMLElement) => {
|
|
940
|
+
eventNames.forEach(function ( n: string ) {
|
|
941
|
+
// console.log()
|
|
942
|
+
ev = new Event( n, options );
|
|
943
|
+
(ev as LooseObject ).data = data;
|
|
944
|
+
e.dispatchEvent( ev );
|
|
945
|
+
});
|
|
946
|
+
});
|
|
947
|
+
|
|
948
|
+
return that;
|
|
949
|
+
}
|
|
950
|
+
|
|
951
|
+
// INFO:
|
|
952
|
+
// val(): LooseObject; // no param: get all as LooseObject
|
|
953
|
+
// val( p1: string ): Array<string> | number | string; // get from input name
|
|
954
|
+
// val( p1: object ): DomSelector; // set from object
|
|
955
|
+
// val( p1: string, p2: any ): DomSelector; // set from name and value
|
|
956
|
+
val( p1?: any, p2?: any ): any {
|
|
957
|
+
|
|
958
|
+
if ( !this.length ) return this;
|
|
959
|
+
|
|
960
|
+
const
|
|
961
|
+
that = this
|
|
962
|
+
;
|
|
963
|
+
|
|
964
|
+
let result: any,
|
|
965
|
+
handlers: LooseObject = getInputHandlers( that.array[0] );
|
|
966
|
+
|
|
967
|
+
// work with it
|
|
968
|
+
if ( !p1 ){ // get all inputs as LooseObject
|
|
969
|
+
result = Object.assign( {}, handlers );
|
|
970
|
+
// Object.keys( handlers ).forEach( ( key ) => result[key] = handlers.hasOwnProperty(key) ? handlers[key].val : undefined );
|
|
971
|
+
} else {
|
|
972
|
+
if ( isObject(p1) ){ // set inputs from Object
|
|
973
|
+
Object.assign( handlers, p1 );
|
|
974
|
+
// Object.keys( p1 ).forEach( ( key ) => !!handlers[key] ? handlers[key].val = p1[key] : false );
|
|
975
|
+
result = that;
|
|
976
|
+
} else if ( typeof p1 === 'string' ){
|
|
977
|
+
if ( p2 === undefined ){ // get a single input
|
|
978
|
+
result = handlers[p1];
|
|
979
|
+
} else { // set a single input
|
|
980
|
+
handlers[p1] = p2;
|
|
981
|
+
result = that;
|
|
982
|
+
}
|
|
983
|
+
}
|
|
984
|
+
}
|
|
985
|
+
|
|
986
|
+
return result;
|
|
987
|
+
}
|
|
988
|
+
|
|
989
|
+
values( values?: LooseObject ): LooseObject {
|
|
990
|
+
const
|
|
991
|
+
that = this,
|
|
992
|
+
holder: LooseObject = {};
|
|
993
|
+
|
|
994
|
+
let
|
|
995
|
+
data: LooseObject = {};
|
|
996
|
+
|
|
997
|
+
//console.log( '$d().values()', values );
|
|
998
|
+
|
|
999
|
+
if ( !that.length ) {
|
|
1000
|
+
console.warn('no dom selection to search inputs in');
|
|
1001
|
+
return {};
|
|
1002
|
+
}
|
|
1003
|
+
|
|
1004
|
+
let handlers = getInputHandlers( that.array[0] );
|
|
1005
|
+
|
|
1006
|
+
//console.log( 'handlers', Object.keys(handlers), handlers['cb1'] );
|
|
1007
|
+
|
|
1008
|
+
// let values = that.val();
|
|
1009
|
+
|
|
1010
|
+
// console.log( 'values', values );
|
|
1011
|
+
|
|
1012
|
+
Object.keys( handlers ).forEach( (key: string) => {
|
|
1013
|
+
//console.log(key);
|
|
1014
|
+
data[key] = handlers[key];
|
|
1015
|
+
});
|
|
1016
|
+
|
|
1017
|
+
// data = Object.assign( {}, that.val() )
|
|
1018
|
+
//console.log( 'data', that.array[0], data, Object.keys( data ) );
|
|
1019
|
+
|
|
1020
|
+
if ( Object.keys( data ).length === 0 ) {
|
|
1021
|
+
console.warn('no inputs in this dom selection', that.array[0], data );
|
|
1022
|
+
}
|
|
1023
|
+
|
|
1024
|
+
//console.log( '$d val()', data );
|
|
1025
|
+
if (values) {
|
|
1026
|
+
Object
|
|
1027
|
+
.keys( values )
|
|
1028
|
+
.forEach( key => {
|
|
1029
|
+
if ( handlers.hasOwnProperty(key) ) {
|
|
1030
|
+
console.log( 'key found', key );
|
|
1031
|
+
handlers[key] = values[key];
|
|
1032
|
+
}
|
|
1033
|
+
});
|
|
1034
|
+
// .forEach( key => handlers[key] = values[key] )
|
|
1035
|
+
}
|
|
1036
|
+
|
|
1037
|
+
return structuredClone( Object.assign( handlers ) );
|
|
1038
|
+
}
|
|
1039
|
+
|
|
1040
|
+
/*
|
|
1041
|
+
array method mapping
|
|
1042
|
+
*/
|
|
1043
|
+
|
|
1044
|
+
at( index: number ): DSelector {
|
|
1045
|
+
return $d( this.array.at( index ) as HTMLElement ) as DomSelector;
|
|
1046
|
+
}
|
|
1047
|
+
|
|
1048
|
+
concat( items: DSelector | any[] ): DSelector{
|
|
1049
|
+
let
|
|
1050
|
+
that = this,
|
|
1051
|
+
arr = items instanceof DomSelector
|
|
1052
|
+
? items.array
|
|
1053
|
+
: items as HTMLElement[]
|
|
1054
|
+
;
|
|
1055
|
+
|
|
1056
|
+
arr.forEach( (e: HTMLElement) => {
|
|
1057
|
+
that.#set.add( e );
|
|
1058
|
+
});
|
|
1059
|
+
|
|
1060
|
+
return that;
|
|
1061
|
+
}
|
|
1062
|
+
|
|
1063
|
+
entries(): IterableIterator<[number, any]> {
|
|
1064
|
+
return this.array.entries();
|
|
1065
|
+
}
|
|
1066
|
+
|
|
1067
|
+
every( predicate: (value: any, index: number, array: any[]) => unknown, thisArg?: any ): boolean {
|
|
1068
|
+
return this.array.every( predicate, thisArg );
|
|
1069
|
+
}
|
|
1070
|
+
|
|
1071
|
+
filter( predicate: (value: any, index: number, array: any[]) => unknown, thisArg?: any ): DSelector {
|
|
1072
|
+
this.#set = new Set( this.array.filter( predicate, thisArg ) );
|
|
1073
|
+
return this;
|
|
1074
|
+
}
|
|
1075
|
+
|
|
1076
|
+
forEach( predicate: (value: any, index: number, array: any[]) => unknown, thisArg?: any ): DSelector {
|
|
1077
|
+
this.array.forEach( predicate, thisArg );
|
|
1078
|
+
return this;
|
|
1079
|
+
}
|
|
1080
|
+
|
|
1081
|
+
includes( item: HTMLElement ): boolean{
|
|
1082
|
+
return this.array.indexOf( item, 0 ) !== -1;
|
|
1083
|
+
}
|
|
1084
|
+
|
|
1085
|
+
indexOf( item: HTMLElement, start?: number ): number{
|
|
1086
|
+
return this.array.indexOf( item, start );
|
|
1087
|
+
}
|
|
1088
|
+
|
|
1089
|
+
map( predicate: (value: any, index: number, array: any[]) => unknown, thisArg?: any ): Array<any>{
|
|
1090
|
+
return this.array.map( predicate, thisArg );
|
|
1091
|
+
}
|
|
1092
|
+
|
|
1093
|
+
pop(): HTMLElement {
|
|
1094
|
+
return this.array.pop() as HTMLElement;
|
|
1095
|
+
}
|
|
1096
|
+
|
|
1097
|
+
push( ...items: HTMLElement[] ): DSelector {
|
|
1098
|
+
items.forEach( (item: HTMLElement) => {
|
|
1099
|
+
this.#set.add( item );
|
|
1100
|
+
});
|
|
1101
|
+
return this;
|
|
1102
|
+
}
|
|
1103
|
+
|
|
1104
|
+
shift(): HTMLElement | undefined {
|
|
1105
|
+
let
|
|
1106
|
+
that = this,
|
|
1107
|
+
element = that.#set.size ? that.array[0] : undefined
|
|
1108
|
+
;
|
|
1109
|
+
|
|
1110
|
+
if ( element ){
|
|
1111
|
+
that.#set.delete( element );
|
|
1112
|
+
}
|
|
1113
|
+
|
|
1114
|
+
return element;
|
|
1115
|
+
}
|
|
1116
|
+
|
|
1117
|
+
slice( start: number, end?: number ): DSelector {
|
|
1118
|
+
return $d( this.array.slice( start, end ) );
|
|
1119
|
+
}
|
|
1120
|
+
|
|
1121
|
+
some( predicate: (value: any, index: number, array: any[]) => unknown, thisArg?: any ): boolean{
|
|
1122
|
+
return this.array.some( predicate, thisArg );
|
|
1123
|
+
}
|
|
1124
|
+
|
|
1125
|
+
splice( start: number, deleteCount?: number | undefined ): DSelector;
|
|
1126
|
+
splice( start: number, deleteCount: number, ...items: HTMLElement[] ): DSelector {
|
|
1127
|
+
let
|
|
1128
|
+
that = this,
|
|
1129
|
+
arr = that.array
|
|
1130
|
+
;
|
|
1131
|
+
|
|
1132
|
+
arr.splice( start, deleteCount, ...items );
|
|
1133
|
+
return $d( arr );
|
|
1134
|
+
}
|
|
1135
|
+
|
|
1136
|
+
}
|
|
1137
|
+
|
|
1138
|
+
export interface DSelector extends DomSelector{};
|
|
1139
|
+
|
|
1140
|
+
class TbSelector{
|
|
1141
|
+
|
|
1142
|
+
#set: Set<any> = new Set();
|
|
1143
|
+
|
|
1144
|
+
public get set(): Set<any> {
|
|
1145
|
+
return this.#set;
|
|
1146
|
+
}
|
|
1147
|
+
|
|
1148
|
+
public get array(): any[] {
|
|
1149
|
+
return Array.from( this.#set );
|
|
1150
|
+
}
|
|
1151
|
+
|
|
1152
|
+
public set set( set: Set<any> ) {
|
|
1153
|
+
this.#set = set;
|
|
1154
|
+
return;
|
|
1155
|
+
}
|
|
1156
|
+
|
|
1157
|
+
public get length(): number {
|
|
1158
|
+
return this.#set.size;
|
|
1159
|
+
}
|
|
1160
|
+
|
|
1161
|
+
public set length( value: number ){
|
|
1162
|
+
//ignore
|
|
1163
|
+
}
|
|
1164
|
+
|
|
1165
|
+
constructor( selection?: selection, element: HTMLElement = document.body ){
|
|
1166
|
+
|
|
1167
|
+
if ( !selection ) {
|
|
1168
|
+
return this;
|
|
1169
|
+
}
|
|
1170
|
+
|
|
1171
|
+
$d( selection, element ).forEach( (e: HTMLMcElement) => {
|
|
1172
|
+
$t( e ).forEach( (t: any ) => {
|
|
1173
|
+
this.#set.add(t);
|
|
1174
|
+
});
|
|
1175
|
+
});
|
|
1176
|
+
|
|
1177
|
+
}
|
|
1178
|
+
|
|
1179
|
+
$d( selection?: selection, element: HTMLElement = document.body ): DSelector {
|
|
1180
|
+
let
|
|
1181
|
+
result = new DomSelector(),
|
|
1182
|
+
set: Set<HTMLElement> = new Set(),
|
|
1183
|
+
compare: boolean = !!selection || false,
|
|
1184
|
+
compareSelection: Set<HTMLElement> = new Set()
|
|
1185
|
+
;
|
|
1186
|
+
|
|
1187
|
+
if ( compare ){
|
|
1188
|
+
compareSelection = $d( selection, element ).set;
|
|
1189
|
+
}
|
|
1190
|
+
|
|
1191
|
+
this.#set.forEach( (e: any) => {
|
|
1192
|
+
if ( !compare || compareSelection.has( (e as any)._tg ) ) {
|
|
1193
|
+
set.add( (e as any)._tg );
|
|
1194
|
+
}
|
|
1195
|
+
});
|
|
1196
|
+
|
|
1197
|
+
result.set = set;
|
|
1198
|
+
|
|
1199
|
+
return result;
|
|
1200
|
+
}
|
|
1201
|
+
|
|
1202
|
+
children( nameSpace?: string ): TSelector{
|
|
1203
|
+
|
|
1204
|
+
let
|
|
1205
|
+
that: TbSelector = this,
|
|
1206
|
+
set: Set<any> = new Set()
|
|
1207
|
+
;
|
|
1208
|
+
|
|
1209
|
+
that.#set.forEach( (e: any) => {
|
|
1210
|
+
e.children( nameSpace ).forEach( ( tb: any ) => {
|
|
1211
|
+
set.add( tb );
|
|
1212
|
+
});
|
|
1213
|
+
});
|
|
1214
|
+
|
|
1215
|
+
that.#set = set;
|
|
1216
|
+
|
|
1217
|
+
return that;
|
|
1218
|
+
}
|
|
1219
|
+
|
|
1220
|
+
descendants( nameSpace?: string ): TSelector{
|
|
1221
|
+
|
|
1222
|
+
let
|
|
1223
|
+
that: TbSelector = this,
|
|
1224
|
+
set: Set<any> = new Set()
|
|
1225
|
+
;
|
|
1226
|
+
|
|
1227
|
+
that.#set.forEach( (e: any) => {
|
|
1228
|
+
e.descendants( nameSpace ).forEach( ( tb: any ) => {
|
|
1229
|
+
set.add( tb );
|
|
1230
|
+
});
|
|
1231
|
+
});
|
|
1232
|
+
|
|
1233
|
+
that.#set = set;
|
|
1234
|
+
|
|
1235
|
+
return that;
|
|
1236
|
+
}
|
|
1237
|
+
|
|
1238
|
+
first( nameSpace?: string ): TSelector{
|
|
1239
|
+
|
|
1240
|
+
let
|
|
1241
|
+
that: TbSelector = this,
|
|
1242
|
+
set: Set<any> = new Set()
|
|
1243
|
+
;
|
|
1244
|
+
|
|
1245
|
+
that.#set.forEach( (e: any) => {
|
|
1246
|
+
e
|
|
1247
|
+
.parent()
|
|
1248
|
+
.children()
|
|
1249
|
+
.$d()
|
|
1250
|
+
.first()
|
|
1251
|
+
.$t()
|
|
1252
|
+
.forEach( (tb: any) => {
|
|
1253
|
+
set.add( tb );
|
|
1254
|
+
});
|
|
1255
|
+
});
|
|
1256
|
+
|
|
1257
|
+
that.#set = set;
|
|
1258
|
+
|
|
1259
|
+
if ( nameSpace ){
|
|
1260
|
+
that.filter( ( tb: any ) => (tb as any).nameSpace = nameSpace );
|
|
1261
|
+
}
|
|
1262
|
+
|
|
1263
|
+
return that;
|
|
1264
|
+
}
|
|
1265
|
+
|
|
1266
|
+
has( element: any ): boolean{
|
|
1267
|
+
return this.#set.has( element );
|
|
1268
|
+
}
|
|
1269
|
+
|
|
1270
|
+
last( nameSpace?: string ): TSelector{
|
|
1271
|
+
let
|
|
1272
|
+
that: TbSelector = this,
|
|
1273
|
+
set: Set<any> = new Set()
|
|
1274
|
+
;
|
|
1275
|
+
|
|
1276
|
+
that.#set.forEach( (e: any) => {
|
|
1277
|
+
e
|
|
1278
|
+
.parent()
|
|
1279
|
+
.children()
|
|
1280
|
+
.$d()
|
|
1281
|
+
.last()
|
|
1282
|
+
.$t()
|
|
1283
|
+
.forEach( (tb: any) => {
|
|
1284
|
+
set.add( tb );
|
|
1285
|
+
});
|
|
1286
|
+
});
|
|
1287
|
+
|
|
1288
|
+
that.#set = set;
|
|
1289
|
+
|
|
1290
|
+
if ( nameSpace ){
|
|
1291
|
+
that.filter( ( e: any ) => ( e as LooseObject).nameSpace = nameSpace );
|
|
1292
|
+
}
|
|
1293
|
+
|
|
1294
|
+
return that;
|
|
1295
|
+
}
|
|
1296
|
+
|
|
1297
|
+
next( nameSpace?: string ): TSelector{
|
|
1298
|
+
|
|
1299
|
+
let
|
|
1300
|
+
that: TbSelector = this,
|
|
1301
|
+
set: Set<any> = new Set()
|
|
1302
|
+
;
|
|
1303
|
+
|
|
1304
|
+
that.#set.forEach( (e: any) => {
|
|
1305
|
+
let
|
|
1306
|
+
elements: Set<HTMLElement> = new Set(),
|
|
1307
|
+
element: HTMLElement
|
|
1308
|
+
;
|
|
1309
|
+
|
|
1310
|
+
element = (e as any)._tg;
|
|
1311
|
+
while ( element.nextElementSibling !== null ){
|
|
1312
|
+
element = element.nextElementSibling as HTMLElement;
|
|
1313
|
+
elements.add( element );
|
|
1314
|
+
}
|
|
1315
|
+
|
|
1316
|
+
$t([...elements])
|
|
1317
|
+
.$d()
|
|
1318
|
+
.first()
|
|
1319
|
+
.$t()
|
|
1320
|
+
.forEach( (tb: any) => {
|
|
1321
|
+
set.add( tb );
|
|
1322
|
+
});
|
|
1323
|
+
});
|
|
1324
|
+
|
|
1325
|
+
that.#set = set;
|
|
1326
|
+
|
|
1327
|
+
if ( nameSpace ){
|
|
1328
|
+
that.filter( ( e: any ) => ( e as LooseObject).nameSpace = nameSpace );
|
|
1329
|
+
}
|
|
1330
|
+
|
|
1331
|
+
return that;
|
|
1332
|
+
}
|
|
1333
|
+
|
|
1334
|
+
ns( nameSpace: string ): TSelector{
|
|
1335
|
+
return this.filter( (tb: any) => (tb as any).nameSpace === nameSpace );
|
|
1336
|
+
}
|
|
1337
|
+
|
|
1338
|
+
off( name: string, cb: Function ): TSelector{
|
|
1339
|
+
let
|
|
1340
|
+
that = this,
|
|
1341
|
+
eventNames =
|
|
1342
|
+
name.indexOf(' ') > -1
|
|
1343
|
+
? name.split(' ').filter( (s) => s !== '' )
|
|
1344
|
+
: [ name ]
|
|
1345
|
+
;
|
|
1346
|
+
|
|
1347
|
+
// remove handlers
|
|
1348
|
+
that.$d().array.forEach( (e: HTMLElement) => {
|
|
1349
|
+
eventNames.forEach(function ( n: string ) {
|
|
1350
|
+
// console.log('Tb Selector remove event', `[TB]${n}`, e, cb );
|
|
1351
|
+
removeListener(
|
|
1352
|
+
e,
|
|
1353
|
+
`[TB]${n}`,
|
|
1354
|
+
cb as EventListenerOrEventListenerObject
|
|
1355
|
+
);
|
|
1356
|
+
});
|
|
1357
|
+
});
|
|
1358
|
+
|
|
1359
|
+
return that;
|
|
1360
|
+
}
|
|
1361
|
+
|
|
1362
|
+
on( name: string, cb: Function, once: boolean = false ): Function{
|
|
1363
|
+
|
|
1364
|
+
let
|
|
1365
|
+
that = this,
|
|
1366
|
+
eventNames =
|
|
1367
|
+
name.indexOf(' ') > -1
|
|
1368
|
+
? name.split(' ').filter( (s) => s !== '' )
|
|
1369
|
+
: [ name ]
|
|
1370
|
+
;
|
|
1371
|
+
|
|
1372
|
+
function wrapper(){ wrapper.cb.apply(that, Array.from(arguments) ); };
|
|
1373
|
+
wrapper.cb = cb;
|
|
1374
|
+
|
|
1375
|
+
// attach handler
|
|
1376
|
+
that.forEach( ( tb: any) => {
|
|
1377
|
+
eventNames.forEach(function ( n: string ) {
|
|
1378
|
+
addListener(
|
|
1379
|
+
(tb as any)._tg,
|
|
1380
|
+
`[TB]${n}`,
|
|
1381
|
+
(wrapper as EventListenerOrEventListenerObject),
|
|
1382
|
+
false,
|
|
1383
|
+
once
|
|
1384
|
+
);
|
|
1385
|
+
});
|
|
1386
|
+
});
|
|
1387
|
+
|
|
1388
|
+
return wrapper;
|
|
1389
|
+
}
|
|
1390
|
+
|
|
1391
|
+
one( name: string, cb: Function ): Function{
|
|
1392
|
+
let
|
|
1393
|
+
that = this,
|
|
1394
|
+
eventNames =
|
|
1395
|
+
name.indexOf(' ') > -1
|
|
1396
|
+
? name.split(' ').filter( (s) => s !== '' )
|
|
1397
|
+
: [ name ]
|
|
1398
|
+
;
|
|
1399
|
+
|
|
1400
|
+
function wrapper(){ wrapper.cb.apply(that, Array.from(arguments) ); };
|
|
1401
|
+
wrapper.cb = cb;
|
|
1402
|
+
|
|
1403
|
+
// attach handler
|
|
1404
|
+
that.forEach( ( tb: any) => {
|
|
1405
|
+
eventNames.forEach(function ( n: string ) {
|
|
1406
|
+
addListener(
|
|
1407
|
+
(tb as any)._tg,
|
|
1408
|
+
`[TB]${n}`,
|
|
1409
|
+
(wrapper as EventListenerOrEventListenerObject),
|
|
1410
|
+
false,
|
|
1411
|
+
true
|
|
1412
|
+
);
|
|
1413
|
+
});
|
|
1414
|
+
});
|
|
1415
|
+
|
|
1416
|
+
return wrapper;
|
|
1417
|
+
}
|
|
1418
|
+
|
|
1419
|
+
parent( nameSpace?: string ): TSelector {
|
|
1420
|
+
|
|
1421
|
+
let
|
|
1422
|
+
that: TbSelector = this,
|
|
1423
|
+
set: Set<any> = new Set()
|
|
1424
|
+
;
|
|
1425
|
+
|
|
1426
|
+
that.#set.forEach( (e: any) => {
|
|
1427
|
+
e.parent( nameSpace ).forEach( ( tb: any ) => {
|
|
1428
|
+
set.add( tb );
|
|
1429
|
+
});
|
|
1430
|
+
});
|
|
1431
|
+
|
|
1432
|
+
that.#set = set;
|
|
1433
|
+
|
|
1434
|
+
return that;
|
|
1435
|
+
}
|
|
1436
|
+
|
|
1437
|
+
parents( nameSpace?: string ): TSelector {
|
|
1438
|
+
|
|
1439
|
+
let
|
|
1440
|
+
that: TbSelector = this,
|
|
1441
|
+
set: Set<any> = new Set()
|
|
1442
|
+
;
|
|
1443
|
+
|
|
1444
|
+
that.#set.forEach( (e: any) => {
|
|
1445
|
+
e.parents( nameSpace ).forEach( ( tb: any ) => {
|
|
1446
|
+
set.add( tb );
|
|
1447
|
+
});
|
|
1448
|
+
});
|
|
1449
|
+
|
|
1450
|
+
that.#set = set;
|
|
1451
|
+
|
|
1452
|
+
return that;
|
|
1453
|
+
}
|
|
1454
|
+
|
|
1455
|
+
prev( nameSpace?: string ): TSelector{
|
|
1456
|
+
|
|
1457
|
+
let
|
|
1458
|
+
that: TbSelector = this,
|
|
1459
|
+
set: Set<any> = new Set()
|
|
1460
|
+
;
|
|
1461
|
+
|
|
1462
|
+
that.#set.forEach( (e: any) => {
|
|
1463
|
+
let
|
|
1464
|
+
elements: Set<HTMLElement> = new Set(),
|
|
1465
|
+
element: HTMLElement
|
|
1466
|
+
;
|
|
1467
|
+
|
|
1468
|
+
element = (e as any)._tg;
|
|
1469
|
+
while ( element.previousElementSibling !== null ){
|
|
1470
|
+
element = element.previousElementSibling as HTMLElement;
|
|
1471
|
+
elements.add( element );
|
|
1472
|
+
}
|
|
1473
|
+
|
|
1474
|
+
$t([...elements])
|
|
1475
|
+
.$d()
|
|
1476
|
+
.last()
|
|
1477
|
+
.$t()
|
|
1478
|
+
.forEach( (tb: any) => {
|
|
1479
|
+
set.add( tb );
|
|
1480
|
+
});
|
|
1481
|
+
});
|
|
1482
|
+
|
|
1483
|
+
that.#set = set;
|
|
1484
|
+
|
|
1485
|
+
if ( nameSpace ){
|
|
1486
|
+
that.filter( ( e: any ) => ( e as LooseObject).nameSpace = nameSpace );
|
|
1487
|
+
}
|
|
1488
|
+
|
|
1489
|
+
return that;
|
|
1490
|
+
}
|
|
1491
|
+
|
|
1492
|
+
trigger( ev: string, data: any = {}, bubble: string = 'l' ): TSelector{
|
|
1493
|
+
let
|
|
1494
|
+
that = this
|
|
1495
|
+
;
|
|
1496
|
+
|
|
1497
|
+
that.forEach( ( tb: any ) => {
|
|
1498
|
+
tb.trigger( ev, data, bubble );
|
|
1499
|
+
});
|
|
1500
|
+
|
|
1501
|
+
return that;
|
|
1502
|
+
}
|
|
1503
|
+
|
|
1504
|
+
/*
|
|
1505
|
+
array method mapping
|
|
1506
|
+
*/
|
|
1507
|
+
|
|
1508
|
+
at( index: number ): TSelector {
|
|
1509
|
+
return $t( this.array.at( index ) ) as TbSelector;
|
|
1510
|
+
}
|
|
1511
|
+
|
|
1512
|
+
concat( items: TSelector ): TSelector {
|
|
1513
|
+
|
|
1514
|
+
let
|
|
1515
|
+
that = this
|
|
1516
|
+
;
|
|
1517
|
+
|
|
1518
|
+
items.forEach( ( tb: any) => {
|
|
1519
|
+
that.#set.add( tb );
|
|
1520
|
+
});
|
|
1521
|
+
|
|
1522
|
+
return that;
|
|
1523
|
+
}
|
|
1524
|
+
|
|
1525
|
+
entries(): IterableIterator<[number, any]> {
|
|
1526
|
+
return this.array.entries();
|
|
1527
|
+
}
|
|
1528
|
+
|
|
1529
|
+
every( predicate: (value: any, index: number, array: any[]) => unknown, thisArg?: any ): boolean {
|
|
1530
|
+
return this.array.every( predicate, thisArg );
|
|
1531
|
+
}
|
|
1532
|
+
|
|
1533
|
+
filter( predicate: (value: any, index: number, array: any[]) => unknown, thisArg?: any ): TSelector {
|
|
1534
|
+
return $t( this.array.filter( predicate, thisArg ) );
|
|
1535
|
+
}
|
|
1536
|
+
|
|
1537
|
+
forEach( predicate: (value: any, index: number, array: any[]) => unknown, thisArg?: any ): TSelector {
|
|
1538
|
+
let
|
|
1539
|
+
that = this
|
|
1540
|
+
;
|
|
1541
|
+
that.array.forEach( predicate, thisArg );
|
|
1542
|
+
return that;
|
|
1543
|
+
}
|
|
1544
|
+
|
|
1545
|
+
includes( item: any ): boolean{
|
|
1546
|
+
return this.array.indexOf( item ) !== -1;
|
|
1547
|
+
}
|
|
1548
|
+
|
|
1549
|
+
indexOf( item: any, start?: number ): number{
|
|
1550
|
+
return this.array.indexOf( item, start );
|
|
1551
|
+
}
|
|
1552
|
+
|
|
1553
|
+
map( predicate: (value: any, index: number, array: any[]) => unknown, thisArg?: any ): Array<any>{
|
|
1554
|
+
return this.array.map( predicate, thisArg );
|
|
1555
|
+
}
|
|
1556
|
+
|
|
1557
|
+
pop(): any {
|
|
1558
|
+
|
|
1559
|
+
let
|
|
1560
|
+
that = this,
|
|
1561
|
+
arr = that.array,
|
|
1562
|
+
result = arr.pop()
|
|
1563
|
+
;
|
|
1564
|
+
|
|
1565
|
+
that.#set = new Set( arr );
|
|
1566
|
+
|
|
1567
|
+
return result as any;
|
|
1568
|
+
}
|
|
1569
|
+
|
|
1570
|
+
push( item: any ): TSelector {
|
|
1571
|
+
let
|
|
1572
|
+
that = this
|
|
1573
|
+
;
|
|
1574
|
+
that.#set.add( item );
|
|
1575
|
+
return that;
|
|
1576
|
+
}
|
|
1577
|
+
|
|
1578
|
+
shift(): any | undefined {
|
|
1579
|
+
let
|
|
1580
|
+
that = this,
|
|
1581
|
+
first = this.array.shift() as any
|
|
1582
|
+
;
|
|
1583
|
+
that.#set.delete( first );
|
|
1584
|
+
return first;
|
|
1585
|
+
}
|
|
1586
|
+
|
|
1587
|
+
slice( start: number, end?: number ): TSelector {
|
|
1588
|
+
return $t( this.array.slice( start, end ) );
|
|
1589
|
+
}
|
|
1590
|
+
|
|
1591
|
+
some( predicate: (value: any, index: number, array: any[]) => unknown, thisArg?: any ): boolean{
|
|
1592
|
+
return this.array.some( predicate, thisArg );
|
|
1593
|
+
}
|
|
1594
|
+
|
|
1595
|
+
splice( start: number, deleteCount?: number | undefined ): TSelector;
|
|
1596
|
+
splice( start: number, deleteCount: number, ...items: never[] ): TSelector {
|
|
1597
|
+
let
|
|
1598
|
+
that = this,
|
|
1599
|
+
arr = that.array,
|
|
1600
|
+
result = arr.splice( start, deleteCount, ...items )
|
|
1601
|
+
;
|
|
1602
|
+
|
|
1603
|
+
return $t( result );
|
|
1604
|
+
}
|
|
1605
|
+
|
|
1606
|
+
}
|
|
1607
|
+
|
|
1608
|
+
export interface TSelector extends TbSelector{};
|
|
1609
|
+
|
|
1610
|
+
export const $d = ( selection: selection, element: any = document ): DSelector => {
|
|
1611
|
+
|
|
1612
|
+
let
|
|
1613
|
+
set = new Set<HTMLElement>()
|
|
1614
|
+
;
|
|
1615
|
+
|
|
1616
|
+
if ( selection instanceof Array ){
|
|
1617
|
+
selection.forEach( value => {
|
|
1618
|
+
$d( value, element ).forEach( n => {
|
|
1619
|
+
set.add( n );
|
|
1620
|
+
});
|
|
1621
|
+
});
|
|
1622
|
+
return new DomSelector( [...set] );
|
|
1623
|
+
} else {
|
|
1624
|
+
return new DomSelector( selection, element );
|
|
1625
|
+
}
|
|
1626
|
+
}
|
|
1627
|
+
|
|
1628
|
+
export const $t = ( selection?: selection, element: any = document ): TSelector => {
|
|
1629
|
+
return $d( selection, element ).$t();
|
|
1630
|
+
}
|
|
1631
|
+
|
|
1632
|
+
export type singleSelector = string | ShadowRoot | HTMLElement | HTMLElement[] | HTMLCollection | NodeList | DSelector | MC | DC | TSelector | undefined;
|
|
1633
|
+
|
|
1634
|
+
export type selection = singleSelector | singleSelector[];
|