jqx-es 1.2.4 → 1.2.6

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/src/_JQxX.js DELETED
@@ -1,500 +0,0 @@
1
- import {
2
- addHandlerId, ExamineElementFeatureFactory,
3
- insertPositions,
4
- IS, isCommentOrTextNode,
5
- isNode,
6
- isNonEmptyString, randomString,
7
- systemLog,
8
- toDashedNotation,
9
- truncateHtmlStr
10
- } from "./Utilities.js";
11
- import {ATTRS, createElementFromHtmlString, inject2DOMTree} from "./DOM.js";
12
-
13
- const isIt = ExamineElementFeatureFactory();
14
-
15
- /* region functions */
16
- function emptyElement(el) {
17
- return el && (el.textContent = "");
18
- }
19
-
20
- function loop(instance, callback) {
21
- const cleanCollection = instance.collection.filter(el => !isCommentOrTextNode(el));
22
- for (let i = 0; i < cleanCollection.length; i += 1) {
23
- callback(cleanCollection[i], i);
24
- }
25
-
26
- return instance;
27
- }
28
-
29
- function compareCI(key, compareTo) {
30
- return key.toLowerCase().trim() === compareTo.trim().toLowerCase();
31
- }
32
-
33
- function cloneAndDestroy(elem) {
34
- const cloned = elem.cloneNode(true)
35
- cloned.removeAttribute && cloned.removeAttribute(`id`);
36
- elem.isConnected ? elem.remove() : elem = null;
37
- return cloned;
38
- }
39
-
40
- function setData(el, keyValuePairs) {
41
- if (el && IS(keyValuePairs, Object)) {
42
- for (const [key, value] of Object.entries(keyValuePairs)) {
43
- el.setAttribute(`data-${toDashedNotation(key)}`, value);
44
- }
45
- }
46
- }
47
-
48
- function before (instance, elem2AddBefore) {
49
- return instance.andThen(elem2AddBefore, true);
50
- }
51
-
52
- function after(instance, elem2AddAfter) {
53
- return instance.andThen(elem2AddAfter);
54
- }
55
-
56
- function checkProp(prop) {
57
- return prop.startsWith(`data`) || ATTRS.html.find(attr => prop.toLowerCase() === attr);
58
- }
59
-
60
- function css(el, keyOrKvPairs, value, jqx) {
61
- if (value && IS(keyOrKvPairs, String)) {
62
- keyOrKvPairs = {[keyOrKvPairs]: value === "-" ? "" : value};
63
- }
64
-
65
- let nwClass = undefined;
66
-
67
- if (keyOrKvPairs.className) {
68
- nwClass = keyOrKvPairs.className;
69
- delete keyOrKvPairs.className;
70
- }
71
-
72
- const classExists = ([...el.classList].find(c => c.startsWith(`JQxClass-`) || nwClass && c === nwClass));
73
- nwClass = classExists || nwClass || `JQxClass-${randomString().slice(1)}`;
74
- jqx.editCssRule(`.${nwClass}`, keyOrKvPairs);
75
- el.classList.add(nwClass);
76
- }
77
-
78
- function assignAttrValues(/*NODOC*/el, keyValuePairs) {
79
- if (el) {
80
- for (let [key, value] of Object.entries(keyValuePairs)) {
81
- key = toDashedNotation(key);
82
- if (key.startsWith(`data`)) {
83
- return setData(el, value);
84
- }
85
-
86
- if (IS(value, String) && checkProp(key)) {
87
- el.setAttribute(key, value.split(/[, ]/)?.join(` `));
88
- }
89
- }
90
- }
91
- }
92
-
93
- function applyStyle(el, rules) {
94
- if (IS(rules, Object)) {
95
- for (let [key, value] of Object.entries(rules)) {
96
- let priority;
97
- if (/!important/i.test(value)) {
98
- value = value.slice(0, value.indexOf(`!`)).trim();
99
- priority = 'important';
100
- }
101
-
102
- el.style.setProperty(toDashedNotation(key), value, priority)
103
- }
104
- }
105
- }
106
-
107
- function logDebug(...args) {
108
- debugLog.log(`❗` + args.map(v => String(v)).join(`, `) ) ;
109
- }
110
-
111
- function findParentScrollDistance(node, distance = 0, top = true) {
112
- node = node?.parentElement;
113
- const what = top ? `scrollTop` : `scrollLeft`;
114
- distance += node ? node[what] : 0;
115
- return !node ? distance : findParentScrollDistance(node, distance, top);
116
- }
117
- /* endregion functions */
118
-
119
- function instanceExtensionsFactory(instance) {
120
- const allMethods = {
121
- addClass: (...classNames) => loop(instance, el => el && classNames.forEach(cn => el.classList.add(cn))),
122
- after,
123
- afterMe: after,
124
- andThen: (elem2Add, before = false) => {
125
- if (!elem2Add || !IS(elem2Add, String, Node, Proxy)) {
126
- logDebug(`[JQx instance].[beforeMe | afterMe | andThen]: insufficient input [${elem2Add}]`, );
127
- return instance;
128
- }
129
-
130
- elem2Add = elem2Add?.isJQx
131
- ? elem2Add.collection
132
- : IS(elem2Add, Node) ? jqx.virtual(elem2Add).collection
133
- : jqx.virtual(createElementFromHtmlString(elem2Add)).collection;
134
-
135
- const [index, method, reCollected] = before
136
- ? [0, `before`, elem2Add.concat(instance.collection)]
137
- : [instance.collection.length - 1, `after`, instance.collection.concat(elem2Add)];
138
-
139
- instance[index][method](...elem2Add);
140
- instance.collection = reCollected;
141
- return instance;
142
- },
143
- append: (...elems2Append) => {
144
- if (!instance.is.empty && elems2Append.length) {
145
- const shouldMove = instance.length === 1;
146
-
147
- for (let elem2Append of elems2Append) {
148
- if (!elem2Append.isJQx && isNonEmptyString(elem2Append)) {
149
- const elem2Append4Test = elem2Append.trim();
150
- const isPlainString = !/^<(.+)[^>]+>$/m.test(elem2Append4Test);
151
- let toAppend = isPlainString ? jqx.text(elem2Append) : createElementFromHtmlString(elem2Append);
152
- loop(instance, el => el.append(shouldMove ? toAppend : cloneAndDestroy(toAppend)));
153
- }
154
-
155
- if (isNode(elem2Append)) {
156
- loop(instance, el => el.append(shouldMove ? elem2Append : cloneAndDestroy(elem2Append)));
157
- }
158
-
159
- if (elem2Append.isJQx && !elem2Append.is.empty) {
160
- loop(instance, el =>
161
- elem2Append.collection.forEach(elem =>
162
- el.append(shouldMove ? elem : cloneAndDestroy(elem)))
163
- );
164
- }
165
- }
166
- }
167
- return instance;
168
- },
169
- appendTo: (appendTo) => {
170
- if (!appendTo.isJQx) {
171
- appendTo = jqx(appendTo);
172
- }
173
- appendTo.append(instance);
174
- return instance;
175
- },
176
- attr: (keyOrObj, value) => {
177
- const firstElem = instance[0];
178
-
179
- if (!firstElem) { return instance }
180
-
181
- if (!value && isNonEmptyString(keyOrObj)) {
182
- keyOrObj = toDashedNotation(keyOrObj);
183
-
184
- if (keyOrObj === `class`) {
185
- return [...firstElem?.classList]?.join(` `);
186
- }
187
-
188
- return firstElem?.getAttribute(keyOrObj);
189
- }
190
-
191
- if (isNonEmptyString(keyOrObj) && value) {
192
- keyOrObj = toDashedNotation(keyOrObj);
193
-
194
- switch(true) {
195
- case keyOrObj.startsWith(`data-`):
196
- keyOrObj = { data: {[keyOrObj.replace(`data-`, ``)]: value } };
197
- break;
198
- default: keyOrObj = { [keyOrObj]: value };
199
- }
200
- }
201
-
202
- if (IS(keyOrObj, Object) && !instance.is.empty) {
203
- assignAttrValues(firstElem, keyOrObj);
204
- }
205
-
206
- return instance;
207
- },
208
- before,
209
- beforeMe: before,
210
- clear: instance => loop(instance, emptyElement),
211
- closest: (selector) => {
212
- const theClosest = isNonEmptyString(selector) ? instance[0].closest(selector) : null;
213
- return theClosest ? jqx(theClosest) : instance
214
- },
215
- computedStyle: (property) => instance.first() && getComputedStyle(instance.first())[property],
216
- css: (keyOrKvPairs, value) => loop(instance, el => css(el, keyOrKvPairs, value, jqx)),
217
- duplicate: (toDOM = false, root = document.body) => {
218
- const nodes = instance.toNodeList().map(el => el.removeAttribute && el.removeAttribute(`id`) || el);
219
- const nwJQx = jqx.virtual(nodes);
220
- return toDOM ? nwJQx.toDOM(root) : nwJQx;
221
- },
222
- each: (cb) => loop(instance, cb),
223
- empty: instance => loop(instance, emptyElement),
224
- find: (selector) =>
225
- instance.collection.length > 0 ? [...instance.first()?.querySelectorAll(selector)] : [],
226
- find$: (selector) => { return instance.collection.length > 0 ? jqx(selector, instance) : instance; },
227
- first: (asJQxInstance = false) => {
228
- if (instance.collection.length > 0) {
229
- return asJQxInstance
230
- ? instance.single()
231
- : instance.collection[0];
232
- }
233
- return undefined;
234
- },
235
- first$: (indexOrSelector) => instance.single(indexOrSelector),
236
- getData: (dataAttribute, valueWhenFalsy) =>
237
- instance.first() && instance.first().dataset?.[dataAttribute] || valueWhenFalsy,
238
- hasClass: (...classNames) => {
239
- const firstElem = instance[0];
240
- return !firstElem || !firstElem.classList.length
241
- ? false : classNames.find(cn => firstElem.classList.contains(cn)) && true || false;
242
- },
243
- hide: instance => loop(instance, el => applyStyle(el, {display: `none !important`})),
244
- html: (htmlValue, append) => {
245
- if (htmlValue === undefined) {
246
- const node = instance.node;
247
- return node?.getHTML && node.getHTML() || ``;
248
- }
249
-
250
- if (!instance.isEmpty()) {
251
- const nwElement = createElementFromHtmlString(
252
- `<div>${htmlValue.isJQx ? htmlValue.HTML.get(true) : htmlValue}</div>`
253
- );
254
-
255
- if (!IS(nwElement, Comment)) {
256
- const cb = el => {
257
- if (!append) { el.textContent = ``; }
258
-
259
- return el.insertAdjacentHTML(jqx.at.end, nwElement.getHTML());
260
- }
261
- return loop(instance, cb);
262
- }
263
- }
264
-
265
- return instance;
266
- },
267
- htmlFor: (forQuery, htmlString = "", append = false) => {
268
- if (forQuery && instance.collection.length) {
269
- if (!forQuery || !isNonEmptyString(htmlString)) { return instance; }
270
-
271
- const el2Change = instance.find$(forQuery);
272
-
273
- if (el2Change.length < 1) { return instance; }
274
-
275
- const nwElement = createElementFromHtmlString(`<span>${htmlString}</span>`);
276
-
277
- el2Change.each(el => {
278
- if (!append) { el.textContent = ``; }
279
- el.insertAdjacentHTML(jqx.at.end, nwElement?.getHTML());
280
- });
281
-
282
- }
283
-
284
- return instance;
285
- },
286
- isEmpty: instance => instance.collection.length < 1,
287
- nth$: (indexOrSelector) => instance.single(indexOrSelector),
288
- on: (type, ...callback) => {
289
- if (instance.collection.length && IS(type, String, Array)) {
290
- if (type?.length < 1 || callback.length < 1) { return instance; }
291
- const cssSelector4Handler = addHandlerId(instance);
292
- jqx.handle({type, selector: cssSelector4Handler, handlers: callback});
293
- }
294
-
295
- return instance;
296
- },
297
- prepend: (...elems2Prepend) => {
298
- if (!instance.is.empty && elems2Prepend) {
299
- const shouldMove = instance.length === 1;
300
-
301
- for (let elem2Prepend of elems2Prepend) {
302
- if (isNonEmptyString(elem2Prepend)) {
303
- elem2Prepend = elem2Prepend.trim();
304
- const isPlainString = !/^<(.+)[^>]+>$/m.test(elem2Prepend);
305
- let toPrepend = isPlainString ? jqx.text(elem2Prepend) : createElementFromHtmlString(elem2Prepend);
306
- toPrepend = shouldMove ? toPrepend : cloneAndDestroy(toPrepend);
307
- loop(instance, el => el.prepend(toPrepend.cloneNode(true)));
308
- }
309
-
310
- if (isNode(elem2Prepend)) {
311
- loop(instance, el => el.prepend(shouldMove ? elem2Prepend : cloneAndDestroy(elem2Prepend)));
312
- }
313
-
314
- if (elem2Prepend.isJQx && !elem2Prepend.is.empty) {
315
- elem2Prepend.collection.length > 1 && elem2Prepend.collection.reverse();
316
- loop(instance, el => loop( elem2Prepend, elem => el.prepend(shouldMove ? elem : cloneAndDestroy(elem)) ) );
317
- elem2Prepend.collection.reverse();
318
- }
319
- }
320
- }
321
-
322
- return instance;
323
- },
324
- prependTo: (prependTo) => {
325
- if (!prependTo.isJQx) {
326
- prependTo = jqx.virtual(prependTo);
327
- }
328
-
329
- prependTo.prepend(instance);
330
- return instance;
331
- },
332
- prop: (nameOrProperties, value) => {
333
- if (IS(nameOrProperties, String) && !value) {
334
- return nameOrProperties.startsWith(`data`)
335
- ? instance[0]?.dataset[nameOrProperties.slice(nameOrProperties.indexOf(`-`)+1)]
336
- : instance[0]?.[nameOrProperties];
337
- }
338
-
339
- const props = !IS(nameOrProperties, Object) ? { [nameOrProperties]: value } : nameOrProperties;
340
- for (let [propName, propValue] of Object.entries(props)) {
341
- propName = propName.trim();
342
-
343
- if (propValue && !checkProp(propName) || !propValue) {
344
- return false;
345
- }
346
-
347
- const isId = propName.toLowerCase() === `id`;
348
-
349
- if (isId) { return instance[0].id = propValue; }
350
-
351
- loop(instance, el => {
352
- if (propName.startsWith(`data`)) {
353
- return el.dataset[propName.slice(propName.indexOf(`-`)+1)] = propValue;
354
- }
355
-
356
- el[propName] = propValue;
357
- });
358
- }
359
-
360
- return instance;
361
- },
362
- remove: (selector) => {
363
- systemLog.log(`remove ${truncateHtmlStr(instance.HTML.get(1), 40)}${selector ? ` /w selector ${selector}` : ``}`);
364
- const remover = el => el.remove();
365
- const removeFromCollection = () =>
366
- instance.collection = instance.collection.filter(el => document.documentElement.contains(el));
367
-
368
- if (selector) {
369
- const selectedElements = instance.find$(selector);
370
- if (!selectedElements.is.empty) {
371
- loop(selectedElements, remover);
372
- removeFromCollection();
373
- }
374
- return instance;
375
- }
376
- loop(instance, remover);
377
- removeFromCollection();
378
- return instance;
379
- },
380
- rmAttr: (...attrNames) => {
381
- for (const attr of attrNames) { instance.node.removeAttribute(attr); }
382
- return instance;
383
- },
384
- removeClass: (...classNames) =>
385
- loop(instance, el => { if (el) { for (const cn of classNames) { el.classList.remove(cn); } } }),
386
- renderTo: (root = document.body, at = jqx.at.end) => {
387
- instance.first$().toDOM(root, at);
388
- return instance;
389
- },
390
- replace: (oldChild, newChild) => {
391
- const firstElem = instance[0];
392
-
393
- if (!oldChild || (!newChild || !IS(newChild, HTMLElement) && !newChild.isJQx)) {
394
- console.error(`JQx replace: invalid replacement value`);
395
- return instance;
396
- }
397
-
398
- if (newChild.isJQx) {
399
- newChild = newChild[0];
400
- }
401
-
402
- if (IS(newChild, NodeList)) {
403
- newChild = newChild[0];
404
- }
405
-
406
- if (firstElem && oldChild) {
407
- oldChild = IS(oldChild, String)
408
- ? firstElem.querySelectorAll(oldChild)
409
- : oldChild.isJQx
410
- ? oldChild.collection
411
- : oldChild;
412
-
413
- if (IS(oldChild, HTMLElement, NodeList, Array) && IS(newChild, HTMLElement)) {
414
- (IS(oldChild, HTMLElement) ? [oldChild] : [...oldChild])
415
- .forEach(chld => chld.replaceWith(newChild.cloneNode(true)));
416
- }
417
- }
418
-
419
- return instance;
420
- },
421
- replaceClass: (className, ...nwClassNames) => loop( instance, el => {
422
- el.classList.remove(className);
423
- for (const name of nwClassNames) { el.classList.add(name); }
424
- } ),
425
- replaceMe: (newChild) => /*NODOC*/ instance.replaceWith(newChild),
426
- replaceWith: (newChild) => {
427
- newChild = IS(newChild, Element) ? newChild : newChild.isJQx ? newChild[0] : undefined;
428
-
429
- if (newChild) {
430
- instance[0].replaceWith(newChild);
431
- instance = jqx.virtual(newChild);
432
- }
433
-
434
- return instance;
435
- },
436
- setData: (keyValuePairs) => loop(instance, el => setData(el, keyValuePairs)),
437
- show: instance => loop(instance, el => applyStyle(el, {display: `revert-layer !important`})),
438
- single: (indexOrSelector) => {
439
- if (instance.collection.length > 0) {
440
- if (IS(indexOrSelector, String)) {
441
- return instance.find$(indexOrSelector);
442
- }
443
-
444
- if (IS(indexOrSelector, Number)) {
445
- return jqx(instance.collection[indexOrSelector]);
446
- }
447
-
448
- return jqx(instance.collection[0]);
449
- }
450
-
451
- return instance;
452
- },
453
- style: (keyOrKvPairs, value) => {
454
- const loopCollectionLambda = el => {
455
- if (value && IS(keyOrKvPairs, String)) {
456
- keyOrKvPairs = { [keyOrKvPairs]: value || `none` };
457
- }
458
-
459
- applyStyle(el, keyOrKvPairs);
460
- };
461
- return loop(instance, loopCollectionLambda);
462
- },
463
- text: (textValue, append = false) => {
464
- if (instance.isEmpty()) { return instance; }
465
- if (!IS(textValue, String)) { return instance.first().textContent; }
466
- const loopCollectionLambda = el => el.textContent = append ? el.textContent + textValue : textValue;
467
- return loop(instance, loopCollectionLambda);
468
- },
469
- toDOM: (root = document.body, position = insertPositions.BeforeEnd) => {
470
- if (instance.isVirtual) { instance.isVirtual = false; }
471
- instance.collection = inject2DOMTree(instance.collection, root, position);
472
-
473
- return instance;
474
- },
475
- toggleClass: (className) => loop(instance, el => el.classList.toggle(className)),
476
- toNodeList: instance => [...instance.collection].map(el => document.importNode(el, true)),
477
- trigger: (evtType, SpecifiedEvent = Event, options = {}) => {
478
- if (instance.collection.length) {
479
- const evObj = new SpecifiedEvent( evtType, { ...options, bubbles: options.bubbles??true} );
480
- for( let elem of instance.collection ) { elem.dispatchEvent(evObj); }
481
- }
482
- return instance;
483
- },
484
- val: (newValue) => {
485
- const firstElem = instance[0];
486
-
487
- if (!firstElem || !IS(firstElem, HTMLInputElement, HTMLSelectElement, HTMLTextAreaElement)) {
488
- return instance;
489
- }
490
-
491
- if (newValue === undefined) {
492
- return firstElem.value;
493
- }
494
-
495
- firstElem.value = !IS(newValue, String) ? "" : newValue;
496
-
497
- return instance;
498
- },
499
- };
500
- }