@cntwg/html-helper 0.0.25 → 0.0.27

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.
@@ -1,4 +1,4 @@
1
- // [v0.1.069-20250416]
1
+ // [v0.1.072-20250827]
2
2
 
3
3
  // === module init block ===
4
4
 
@@ -35,17 +35,16 @@ const {
35
35
  //CSS_CLASS_ACTIVE,
36
36
  } = CSS_CLASS_STRING;
37
37
 
38
- // === module extra block (helper functions) ===
38
+ // === module inner block ===
39
39
 
40
40
  /**
41
+ * Searches an element in a list by a given attributes of that element.
41
42
  * @function srchListElementByAttr
42
43
  * @param {TItemsListEx} list
43
- * @param {string} name
44
- * @param {string} [value=""]
44
+ * @param {string} name - target attribute name
45
+ * @param {string} [value=""] - target attribute value
45
46
  * @returns {object}
46
47
  * @inner
47
- * @description Searches an element in a list by a given attributes
48
- * of that element.
49
48
  */
50
49
  function srchListElementByAttr(list, name, value = '') {
51
50
  const _value = readAsAttrValue(value);
@@ -90,11 +89,11 @@ const ILC_SMODE_CTRL = 2;
90
89
  */
91
90
 
92
91
  /**
92
+ * An options set for `THtmlItemsListContainer`-class
93
93
  * @typedef {Object} OPT_hlconsett
94
- * @property {boolean} [autoHideNewItems=false]
95
- * @property {boolean} [markCurrentItem=false]
94
+ * @property {boolean} [autoHideNewItems=false] - indicates whether to hide a new element
95
+ * @property {boolean} [markCurrentItem=false] - indicates whether to mark a current element
96
96
  * @property {(string|string[])} [itemBaseClassID]
97
- * @description An options set for `THtmlItemsListContainer`-class
98
97
  */
99
98
 
100
99
  /**
@@ -108,19 +107,19 @@ class THtmlItemsListContainer {
108
107
  /** @type {OPT_hlconsett} */
109
108
  #_options;// = null;
110
109
  /**
110
+ * A container status
111
111
  * @typedef {Object} statILCont
112
112
  * @property {number} curIndex
113
113
  * @property {?HTMLElement} curItem
114
114
  * @inner
115
- * @description A container status
116
115
  */
117
116
  /** @type {statILCont} */
118
117
  #_status;
119
118
 
120
119
  /**
121
- * @param {HTMLElement} host
120
+ * Creates an instance of a list container
121
+ * @param {HTMLElement} host - host element
122
122
  * @param {OPT_hlconsett} [opt] - options
123
- * @description Creates an instance of a list container
124
123
  */
125
124
  constructor(host, opt) {
126
125
  // check host
@@ -220,8 +219,8 @@ class THtmlItemsListContainer {
220
219
  }
221
220
 
222
221
  /**
222
+ * Clears an instance content.
223
223
  * @returns {void}
224
- * @description Clears an instance content.
225
224
  */
226
225
  clear() {
227
226
  this.#_items.clear();
@@ -233,58 +232,58 @@ class THtmlItemsListContainer {
233
232
  }
234
233
 
235
234
  /**
235
+ * Checks if an instance contains no items.
236
236
  * @returns {boolean}
237
- * @description Checks if an instance contains no items.
238
237
  */
239
238
  isEmpty() {
240
239
  return this.#_items.isEmpty();
241
240
  }
242
241
 
243
242
  /**
243
+ * Checks if an instance contains any items.
244
244
  * @returns {boolean}
245
- * @description Checks if an instance contains any items.
246
245
  */
247
246
  isNotEmpty() {
248
247
  return this.#_items.isNotEmpty();
249
248
  }
250
249
 
251
250
  /**
251
+ * Checks if a given value is a valid index and
252
+ * it fits an index range within an instance.
252
253
  * @param {any} value - index value
253
254
  * @returns {boolean}
254
- * @description Checks if a given value is a valid index and
255
- * it fits an index range within an instance.
256
255
  */
257
256
  chkIndex(value) {
258
257
  return this.#_items.chkIndex(value);
259
258
  }
260
259
 
261
260
  /**
261
+ * Checks if a given value is a valid index and
262
+ * it fits an index range within an instance.
262
263
  * @param {any} value - value to check
263
264
  * @param {boolean} [opt=false] - defines a type of result
264
265
  * @returns {(boolean|number)}
265
266
  * @see TItemsListEx.chkIndexEx
266
- * @description Checks if a given value is a valid index and
267
- * it fits an index range within an instance.
268
267
  */
269
268
  chkIndexEx(...args) {
270
269
  return this.#_items.chkIndexEx(...args);
271
270
  }
272
271
 
273
272
  /**
273
+ * Returns an index of a given element.
274
274
  * @param {HTMLElement} item - element to search
275
275
  * @returns {number}
276
- * @description Returns an index of a given element.
277
276
  */
278
277
  srchIndex(item) {
279
278
  return isHTMLElement(item) ? this.#_items.srchIndex(item) : -1;
280
279
  }
281
280
 
282
281
  /**
282
+ * Returns an index of an element wich has an attribute
283
+ * with a given name and value.
283
284
  * @param {string} name - attribute name
284
285
  * @param {any} [value=""] - attribute value
285
286
  * @returns {number}
286
- * @description Returns an index of an element wich has an attribute
287
- * with a given name and value.
288
287
  */
289
288
  srchIndexByAttr(name, value = '') {
290
289
  const _name = typeof name === 'string' ? name.trim() : '';
@@ -293,9 +292,9 @@ class THtmlItemsListContainer {
293
292
  };
294
293
 
295
294
  /**
295
+ * Returns an index of an element wich has a given ID.
296
296
  * @param {string} value - element ID
297
297
  * @returns {number}
298
- * @description Returns an index of an element wich has a given ID.
299
298
  */
300
299
  srchIndexByID(value) {
301
300
  const { index } = srchListElementByAttr(this.#_items, 'id', value);
@@ -303,9 +302,9 @@ class THtmlItemsListContainer {
303
302
  };
304
303
 
305
304
  /**
305
+ * Sets a current index.
306
306
  * @param {(number|string)} index - element index
307
307
  * @returns {boolean}
308
- * @description Sets a current index.
309
308
  */
310
309
  setCurIndex(index) {
311
310
  const isSUCCEED = this.#_items.setIndex(index);
@@ -324,8 +323,8 @@ class THtmlItemsListContainer {
324
323
  }
325
324
 
326
325
  /**
326
+ * Resets a current index.
327
327
  * @returns {void}
328
- * @description Resets a current index.
329
328
  */
330
329
  rstCurIndex() {
331
330
  const _status = this.#_status;
@@ -338,9 +337,9 @@ class THtmlItemsListContainer {
338
337
  }
339
338
 
340
339
  /**
340
+ * Returns an item addressed by a given index.
341
341
  * @param {(number|string)} index - element index
342
342
  * @returns {?HTMLElement}
343
- * @description Returns an item addressed by a given index.
344
343
  */
345
344
  getItem(index) {
346
345
  const item = this.#_items.getItem(index);
@@ -348,11 +347,10 @@ class THtmlItemsListContainer {
348
347
  }
349
348
 
350
349
  /**
350
+ * Returns an item wich has an attribute with a given name and value.
351
351
  * @param {string} name - attribute name
352
352
  * @param {any} [value=""] - attribute value
353
353
  * @returns {?HTMLElement}
354
- * @description Returns an item wich has an attribute
355
- * with a given name and value.
356
354
  */
357
355
  getItemByAttr(name, value = '') {
358
356
  const _name = typeof name === 'string' ? name.trim() : '';
@@ -361,9 +359,9 @@ class THtmlItemsListContainer {
361
359
  }
362
360
 
363
361
  /**
362
+ * Returns an item wich has a given ID.
364
363
  * @param {string} value - element ID
365
364
  * @returns {?HTMLElement}
366
- * @description Returns an item wich has a given ID.
367
365
  */
368
366
  getItemByID(value) {
369
367
  const { item } = srchListElementByAttr(this.#_items, 'id', value);
@@ -371,10 +369,10 @@ class THtmlItemsListContainer {
371
369
  }
372
370
 
373
371
  /**
372
+ * Adds an item to an instance.
374
373
  * @param {HTMLElement} item
375
374
  * @param {boolean} [opt=false]
376
375
  * @returns {number}
377
- * @description Adds an item to an instance.
378
376
  */
379
377
  addItem(item, opt) {
380
378
  const forceCI = typeof opt === 'boolean' ? opt : false;
@@ -396,11 +394,11 @@ class THtmlItemsListContainer {
396
394
  }
397
395
 
398
396
  /**
397
+ * Deletes an item from an instance.
399
398
  * @param {(number|string)} index - element index
400
399
  * @param {any} [opt]
401
400
  * @param {boolean} [optEx=true]
402
401
  * @returns {boolean}
403
- * @description Deletes an item from an instance.
404
402
  */
405
403
  delItem(index, opt, optEx) {
406
404
  const _items = this.#_items;
@@ -422,10 +420,10 @@ class THtmlItemsListContainer {
422
420
  }
423
421
 
424
422
  /**
423
+ * Selects an item addressed by a given index.
425
424
  * @param {(number|string)} index - element index
426
425
  * @param {boolean} [opt=false]
427
426
  * @returns {boolean}
428
- * @description Selects an item addressed by a given index.
429
427
  */
430
428
  selectItem(index, opt) {
431
429
  const forceCI = typeof opt === 'boolean' ? opt : false;
@@ -435,45 +433,45 @@ class THtmlItemsListContainer {
435
433
  }
436
434
 
437
435
  /**
436
+ * Unselects an item addressed by a given index.
438
437
  * @param {(number|string)} index - element index
439
438
  * @returns {boolean}
440
- * @description Unselects an item addressed by a given index.
441
439
  */
442
440
  unselectItem(index) {
443
441
  return unselectHTMLElement(this.#_items.getItem(index));
444
442
  }
445
443
 
446
444
  /**
445
+ * Hides an item addressed by a given index.
447
446
  * @param {(number|string)} index - element index
448
447
  * @returns {boolean}
449
- * @description Hides an item addressed by a given index.
450
448
  */
451
449
  hideItem(index) {
452
450
  return hideHTMLElement(this.#_items.getItem(index));
453
451
  }
454
452
 
455
453
  /**
454
+ * Shows an item addressed by a given index.
456
455
  * @param {(number|string)} index - element index
457
456
  * @returns {boolean}
458
- * @description Shows an item addressed by a given index.
459
457
  */
460
458
  showItem(index) {
461
459
  return showHTMLElement(this.#_items.getItem(index));
462
460
  }
463
461
 
464
462
  /**
463
+ * Checks whether an item is selected.
465
464
  * @param {(number|string)} index - element index
466
465
  * @returns {boolean}
467
- * @description Checks whether an item is selected.
468
466
  */
469
467
  isSelectedItem(index) {
470
468
  return isSelectedHTMLElement(this.#_items.getItem(index));
471
469
  }
472
470
 
473
471
  /**
472
+ * Checks whether an item is hidden.
474
473
  * @param {(number|string)} index - element index
475
474
  * @returns {boolean}
476
- * @description Checks whether an item is hidden.
477
475
  */
478
476
  isHiddenItem(index) {
479
477
  return isHiddenHTMLElement(this.#_items.getItem(index));
@@ -483,13 +481,15 @@ class THtmlItemsListContainer {
483
481
 
484
482
  /**
485
483
  * @typedef {Object} OPT_hlctlsett
486
- * @property {boolean} [autoHideNewItems=false]
487
- * @property {boolean} [markCurrentItem=false]
484
+ * @property {boolean} [autoHideNewItems=false] - indicates whether to hide a new element
485
+ * @property {boolean} [markCurrentItem=false] - indicates whether to mark a current element
488
486
  * @property {(string|string[])} [itemBaseClassID]
489
- * @property {boolean} [showStubsIfEmpty=false]
490
- * @property {boolean} [allowGroupSelection=false]
491
- * @property {boolean} [allowSelectionLocks=false]
492
- * @property {object} [stubs]
487
+ * @property {boolean} [showStubsIfEmpty=false] - indicates whether to show stubs if empty
488
+ * @property {boolean} [allowGroupSelection=false] - indicates whether
489
+ * a selection of the elements group is allowed
490
+ * @property {boolean} [allowSelectionLocks=false] - indicates whether locking
491
+ * of an element selection is allowed
492
+ * @property {OPT_stubconsett} [stubs]
493
493
  * @description An options set for `THtmlItemsListController`-class
494
494
  */
495
495
 
@@ -508,6 +508,7 @@ class THtmlItemsListController extends THtmlItemsListContainer {
508
508
  /** @property {OPT_hlctlsett} */
509
509
  #_options;// = null;
510
510
  /**
511
+ * A controler status
511
512
  * @typedef {Object} statILCtrl
512
513
  * @property {boolean} isSelectionLocked
513
514
  * @property {boolean} isStubItemShown
@@ -517,7 +518,6 @@ class THtmlItemsListController extends THtmlItemsListContainer {
517
518
  * @property {number} execDelItemDI
518
519
  * @property {number} execDelItemCI
519
520
  * @inner
520
- * @description A controler status
521
521
  */
522
522
  /** @property {statILCtrl} */
523
523
  #_status;
@@ -525,15 +525,16 @@ class THtmlItemsListController extends THtmlItemsListContainer {
525
525
  #_events;
526
526
 
527
527
  /**
528
- * @param {?HTMLElement} host
529
- * @param {OPT_hlctlsett} [opt]
530
- * @description Creates a new instance of the class.
528
+ * Creates a new instance of the class.
529
+ * @param {?HTMLElement} host - host element
530
+ * @param {OPT_hlctlsett} [opt] - options
531
531
  */
532
532
  constructor(host, opt) {
533
533
  // check host element
534
534
  const isHostEnabled = isHTMLElement(host);
535
535
  const _host = isHostEnabled ? host : null;
536
536
  // check options
537
+ /** @type {OPT_hlctlsett} */
537
538
  const _options = isPlainObject(opt) ? opt : {};
538
539
  // call parent constructor
539
540
  super(_host, _options);
@@ -588,7 +589,7 @@ class THtmlItemsListController extends THtmlItemsListContainer {
588
589
  }
589
590
 
590
591
  /**
591
- * @param {object} e
592
+ * @param {object} e - event
592
593
  * @returns {void}
593
594
  * @private
594
595
  */
@@ -605,8 +606,8 @@ class THtmlItemsListController extends THtmlItemsListContainer {
605
606
  //console.log('CHECK: e => ditail:['+e.detail+']');
606
607
  //console.log('CHECK: e => phase:['+eventPhase+']');
607
608
  const onClickNum = readAsNumber(e.detail, 0);
608
- const _status = this.#_status;
609
- const { isSelectionLocked, catchEventOnHost } = _status;
609
+ const host = this.#_host;
610
+ const { isSelectionLocked, catchEventOnHost } = this.#_status;
610
611
  let curItem = null;
611
612
  switch (eventPhase) {
612
613
  //* // NOTE: currently on eventPhase = 2 and 3
@@ -614,7 +615,7 @@ class THtmlItemsListController extends THtmlItemsListContainer {
614
615
  /**/// capturing stage
615
616
  case 2: {
616
617
  /**/// target stage
617
- if (target !== this.#_host) curItem = target;
618
+ if (target !== host) curItem = target;
618
619
  break;
619
620
  }
620
621
  case 3: {
@@ -622,9 +623,7 @@ class THtmlItemsListController extends THtmlItemsListContainer {
622
623
  curItem = catchEventOnHost ? target : currentTarget;
623
624
  break;
624
625
  }
625
- default: {
626
- break;
627
- }
626
+ default: {}
628
627
  };
629
628
  if (
630
629
  !isSelectionLocked
@@ -632,7 +631,19 @@ class THtmlItemsListController extends THtmlItemsListContainer {
632
631
  && (onClickNum === 0 || onClickNum === 1)
633
632
  && !curItem.classList.contains(CSS_CLASS_DISABLED)
634
633
  ) {
635
- //console.log('CHECK: e => tag:['+curItem.tagName+']');
634
+ //console.log(`CHECK: e => tag:[${curItem.tagName}]`);
635
+ if (host) {
636
+ // find an actual list element in case when some inner element was clicking
637
+ let tmpItem = curItem;
638
+ while (tmpItem) {
639
+ //console.log(`CHECK: e => target.tag:[${tmpItem.tagName}]`);
640
+ tmpItem = tmpItem.parentElement;
641
+ //console.log(`CHECK: e => next.tag:[${tmpItem.tagName}]`);
642
+ if (tmpItem === host) break;
643
+ if (tmpItem) curItem = tmpItem;
644
+ };
645
+ };
646
+ //console.log(`CHECK: e => tag:[${curItem.tagName}]`);
636
647
  this.#_selectItemEx(curItem, {
637
648
  ctrlKey: ctrlKey,
638
649
  shiftKey: shiftKey,
@@ -642,7 +653,7 @@ class THtmlItemsListController extends THtmlItemsListContainer {
642
653
  };
643
654
 
644
655
  /**
645
- * @param {string} name
656
+ * @param {string} name - event name
646
657
  * @param {...any} args
647
658
  * @returns {void}
648
659
  * @private
@@ -686,9 +697,9 @@ class THtmlItemsListController extends THtmlItemsListContainer {
686
697
  }
687
698
 
688
699
  /**
700
+ * Clears an instance content.
689
701
  * @returns {void}
690
702
  * @fires THtmlItemsListController#list-clear
691
- * @description Clears an instance content.
692
703
  */
693
704
  clear() {
694
705
  // // TODO: clear event handler on elements if set
@@ -706,8 +717,8 @@ class THtmlItemsListController extends THtmlItemsListContainer {
706
717
  }
707
718
 
708
719
  /**
720
+ * Locks an element selection.
709
721
  * @returns {void}
710
- * @description Locks an element selection.
711
722
  */
712
723
  lockItemsSelection() {
713
724
  if (this.#_options.allowSelectionLocks) {
@@ -716,19 +727,19 @@ class THtmlItemsListController extends THtmlItemsListContainer {
716
727
  }
717
728
 
718
729
  /**
730
+ * Unlocks an element selection.
719
731
  * @returns {void}
720
- * @description Unlocks an element selection.
721
732
  */
722
733
  unlockItemsSelection() {
723
734
  this.#_status.isSelectionLocked = false;
724
735
  }
725
736
 
726
737
  /**
738
+ * Sets a current index.
727
739
  * @param {(number|string)} index - element index
728
740
  * @returns {boolean}
729
741
  * @fires THtmlItemsListController#current-item-chosen
730
742
  * @private
731
- * @description Sets a current index.
732
743
  */
733
744
  #_setCurIndex(index) {
734
745
  const isSUCCEED = super.setCurIndex(index);
@@ -746,17 +757,17 @@ class THtmlItemsListController extends THtmlItemsListContainer {
746
757
  };
747
758
 
748
759
  /**
760
+ * Sets a current index.
749
761
  * @param {(number|string)} index - element index
750
762
  * @returns {boolean}
751
- * @description Sets a current index.
752
763
  */
753
764
  setCurIndex(index) {
754
765
  return this.selectItem(index, true);
755
766
  }
756
767
 
757
768
  /**
769
+ * Resets a current index.
758
770
  * @returns {void}
759
- * @description Resets a current index.
760
771
  */
761
772
  rstCurIndex() {
762
773
  const {
@@ -775,12 +786,12 @@ class THtmlItemsListController extends THtmlItemsListContainer {
775
786
  }
776
787
 
777
788
  /**
778
- * @param {HTMLElement} item
789
+ * Adds an element to an instance members.
790
+ * @param {HTMLElement} item - some element
779
791
  * @param {boolean} [opt=false]
780
792
  * @returns {number}
781
793
  * @fires THtmlItemsListController#first-item-added
782
794
  * @fires THtmlItemsListController#item-added
783
- * @description Adds an element to an instance members.
784
795
  */
785
796
  addItem(item, opt) {
786
797
  const index = super.addItem(item, false);
@@ -838,12 +849,12 @@ class THtmlItemsListController extends THtmlItemsListContainer {
838
849
  }
839
850
 
840
851
  /**
852
+ * Deletes an element from an instance members.
841
853
  * @param {(number|string)} index - element index
842
854
  * @param {any} [opt]
843
855
  * @returns {boolean}
844
856
  * @fires THtmlItemsListController#list-clear
845
857
  * @fires THtmlItemsListController#item-removed
846
- * @description Deletes an element from an instance members.
847
858
  */
848
859
  delItem(index, opt) {
849
860
  const item = super.getItem(index);
@@ -898,11 +909,11 @@ class THtmlItemsListController extends THtmlItemsListContainer {
898
909
  }
899
910
 
900
911
  /**
912
+ * Selects an element.
901
913
  * @param {(number|string)} index - element index
902
914
  * @returns {boolean}
903
915
  * @private
904
916
  * @fires THtmlItemsListController#item-selected
905
- * @description Selects an element.
906
917
  */
907
918
  #_selectItem(index) {
908
919
  const item = super.getItem(index);
@@ -924,6 +935,7 @@ class THtmlItemsListController extends THtmlItemsListContainer {
924
935
  }
925
936
 
926
937
  /**
938
+ * Selects an element.
927
939
  * @param {?HTMLElement} item - element
928
940
  * @param {object} [opt]
929
941
  * @param {boolean} [opt.ctrlKey=false]
@@ -931,7 +943,6 @@ class THtmlItemsListController extends THtmlItemsListContainer {
931
943
  * @param {boolean} [opt.forceCI=false]
932
944
  * @returns {void}
933
945
  * @private
934
- * @description Selects an element.
935
946
  */
936
947
  #_selectItemEx(item, opt){
937
948
  const _selects = this.#_selects;
@@ -986,10 +997,10 @@ class THtmlItemsListController extends THtmlItemsListContainer {
986
997
  }
987
998
 
988
999
  /**
1000
+ * Selects an element.
989
1001
  * @param {(number|string)} index - element index
990
1002
  * @param {boolean} [opt=false]
991
1003
  * @returns {boolean}
992
- * @description Selects an element.
993
1004
  */
994
1005
  selectItem(index, opt) {
995
1006
  const item = super.getItem(index);
@@ -1006,10 +1017,10 @@ class THtmlItemsListController extends THtmlItemsListContainer {
1006
1017
  }
1007
1018
 
1008
1019
  /**
1020
+ * Unselects an element.
1009
1021
  * @param {(number|string)} index - element index
1010
1022
  * @returns {boolean}
1011
1023
  * @fires THtmlItemsListController#item-unselected
1012
- * @description Unselects an element.
1013
1024
  */
1014
1025
  unselectItem(index) {
1015
1026
  const item = super.getItem(index);
@@ -1031,10 +1042,10 @@ class THtmlItemsListController extends THtmlItemsListContainer {
1031
1042
  }
1032
1043
 
1033
1044
  /**
1045
+ * Hides an element.
1034
1046
  * @param {(number|string)} index - element index
1035
1047
  * @returns {boolean}
1036
1048
  * @fires THtmlItemsListController#item-hidden
1037
- * @description Hides an element.
1038
1049
  */
1039
1050
  hideItem(index) {
1040
1051
  const item = super.getItem(index);
@@ -1055,10 +1066,10 @@ class THtmlItemsListController extends THtmlItemsListContainer {
1055
1066
  }
1056
1067
 
1057
1068
  /**
1069
+ * Shows an element.
1058
1070
  * @param {(number|string)} index - element index
1059
1071
  * @returns {boolean}
1060
1072
  * @fires THtmlItemsListController#item-shown
1061
- * @description Shows an element.
1062
1073
  */
1063
1074
  showItem(index) {
1064
1075
  const item = super.getItem(index);
@@ -1079,10 +1090,10 @@ class THtmlItemsListController extends THtmlItemsListContainer {
1079
1090
  }
1080
1091
 
1081
1092
  /**
1093
+ * Sets a callback function to handle event.
1082
1094
  * @param {string} name - event name
1083
1095
  * @param {func} evnt
1084
1096
  * @returns {void}
1085
- * @description Sets a callback function to handle event.
1086
1097
  */
1087
1098
  on(name, evnt) {
1088
1099
  pushEventHandler(this.#_events, name, evnt);