@cntwg/html-helper 0.0.20 → 0.0.22

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.060-20230408]
1
+ // [v0.1.063-20240714]
2
2
 
3
3
  // === module init block ===
4
4
 
@@ -37,6 +37,16 @@ const ILC_SMODE_CTRL = 2;
37
37
 
38
38
  // === module extra block (helper functions) ===
39
39
 
40
+ /**
41
+ * @function srchListElementByAttr
42
+ * @param {TItemsListEx}
43
+ * @param {string}
44
+ * @param {string}
45
+ * @returns {object}
46
+ * @inner
47
+ * @description Searches an element in a list by a given attributes
48
+ * of that element.
49
+ */
40
50
  function srchListElementByAttr(list, name, value = '') {
41
51
  const _value = readAsAttrValue(value);
42
52
  let item = null;
@@ -61,16 +71,36 @@ function srchListElementByAttr(list, name, value = '') {
61
71
  return isACCEPTED ? { index, item } : { index, item: null };
62
72
  };
63
73
 
64
- // === module main block (function definitions) ===
74
+ // === module main block ===
65
75
 
66
- // === module main block (class definitions) ===
76
+ /***
77
+ * (* constant definitions *)
78
+ */
67
79
 
80
+ /***
81
+ * (* function definitions *)
82
+ */
83
+
84
+ /***
85
+ * (* class definitions *)
86
+ */
87
+
88
+ /**
89
+ * @description This class implements an instance of a list container
90
+ */
68
91
  class THtmlItemsListContainer {
69
92
  #_host = null;
70
93
  #_items = null;
71
94
  #_options = null;
72
95
  #_status = null;
73
96
 
97
+ /**
98
+ * @param {HTMLElement}
99
+ * @param {object} [opt]
100
+ * @param {bool} [opt.autoHideNewItems=false]
101
+ * @param {bool} [opt.markCurrentItem=false]
102
+ * @param {(string|array)} [opt.itemBaseClassID]
103
+ */
74
104
  constructor(host, opt) {
75
105
  // check host
76
106
  this.#_host = isHTMLElement(host) ? host : null;
@@ -102,35 +132,68 @@ class THtmlItemsListContainer {
102
132
  this.#_options = _options;
103
133
  }
104
134
 
135
+ /**
136
+ * @property {number}
137
+ * @readonly
138
+ */
105
139
  get count(){
106
140
  return this.#_items.count;
107
141
  }
108
142
 
143
+ /**
144
+ * @property {index}
145
+ * @readonly
146
+ */
109
147
  get curIndex(){
110
148
  return this.#_items.curIndex;
111
149
  }
112
150
 
151
+ /**
152
+ * @property {index}
153
+ * @readonly
154
+ */
113
155
  get minIndex(){
114
156
  return this.#_items.minIndex;
115
157
  }
116
158
 
159
+ /**
160
+ * @property {index}
161
+ * @readonly
162
+ */
117
163
  get maxIndex(){
118
164
  return this.#_items.maxIndex;
119
165
  }
120
166
 
167
+ /**
168
+ * @property {index}
169
+ * @readonly
170
+ */
121
171
  get prevIndex(){
122
172
  return this.#_items.prevIndex;
123
173
  }
124
174
 
175
+ /**
176
+ * @property {index}
177
+ * @readonly
178
+ */
125
179
  get nextIndex(){
126
180
  return this.#_items.nextIndex;
127
181
  }
128
182
 
183
+ /**
184
+ * @property {?HTMLElement}
185
+ * @readonly
186
+ */
129
187
  get curItem(){
130
188
  const item = this.#_items.curItem;
131
189
  return isHTMLElement(item) ? item : null;
132
190
  }
133
191
 
192
+ /**
193
+ * @param {none}
194
+ * @returns {none}
195
+ * @description Clears an instance content.
196
+ */
134
197
  clear(){
135
198
  this.#_items.clear();
136
199
  const _status = this.#_status;
@@ -140,37 +203,80 @@ class THtmlItemsListContainer {
140
203
  if (_host) _host.innerHTML = '';
141
204
  }
142
205
 
206
+ /**
207
+ * @param {none}
208
+ * @returns {bool}
209
+ * @description Checks if an instance contains no items.
210
+ */
143
211
  isEmpty(){
144
212
  return this.#_items.isEmpty();
145
213
  }
146
214
 
215
+ /**
216
+ * @param {none}
217
+ * @returns {bool}
218
+ * @description Checks if an instance contains any items.
219
+ */
147
220
  isNotEmpty(){
148
221
  return this.#_items.isNotEmpty();
149
222
  }
150
223
 
224
+ /**
225
+ * @param {any}
226
+ * @returns {bool}
227
+ * @description Checks if a given value is a valid index and
228
+ * it fits an index range within an instance.
229
+ */
151
230
  chkIndex(value){
152
231
  return this.#_items.chkIndex(value);
153
232
  }
154
233
 
234
+ /**
235
+ * @see TItemsListEx.chkIndexEx
236
+ * @description Checks if a given value is a valid index and
237
+ * it fits an index range within an instance.
238
+ */
155
239
  chkIndexEx(...args){
156
240
  return this.#_items.chkIndexEx(...args);
157
241
  }
158
242
 
243
+ /**
244
+ * @param {HTMLElement}
245
+ * @returns {index}
246
+ * @description Returns an index of a given element.
247
+ */
159
248
  srchIndex(item){
160
249
  return isHTMLElement(item) ? this.#_items.srchIndex(item) : -1;
161
250
  }
162
251
 
252
+ /**
253
+ * @param {ID_STRING}
254
+ * @param {any}
255
+ * @returns {index}
256
+ * @description Returns an index of an element wich has an attribute
257
+ * with a given name and value.
258
+ */
163
259
  srchIndexByAttr(name, value = ''){
164
260
  const _name = typeof name === 'string' ? name.trim() : '';
165
261
  const { index } = srchListElementByAttr(this.#_items, _name, value);
166
262
  return index;
167
263
  };
168
264
 
265
+ /**
266
+ * @param {ID_STRING}
267
+ * @returns {index}
268
+ * @description Returns an index of an element wich has a given ID.
269
+ */
169
270
  srchIndexByID(value){
170
271
  const { index } = srchListElementByAttr(this.#_items, 'id', value);
171
272
  return index;
172
273
  };
173
274
 
275
+ /**
276
+ * @param {index}
277
+ * @returns {bool}
278
+ * @description Sets a current index.
279
+ */
174
280
  setCurIndex(index){
175
281
  const isSUCCEED = this.#_items.setIndex(index);
176
282
  if (isSUCCEED) {
@@ -187,6 +293,11 @@ class THtmlItemsListContainer {
187
293
  return isSUCCEED;
188
294
  }
189
295
 
296
+ /**
297
+ * @param {none}
298
+ * @returns {none}
299
+ * @description Resets a current index.
300
+ */
190
301
  rstCurIndex(){
191
302
  const _status = this.#_status;
192
303
  if (this.#_options.markCurrentItem && _status.curIndex !== -1) {
@@ -197,22 +308,46 @@ class THtmlItemsListContainer {
197
308
  _status.curItem = this.curItem;
198
309
  }
199
310
 
311
+ /**
312
+ * @param {index}
313
+ * @returns {?HTMLElement}
314
+ * @description Returns an item addressed by a given index.
315
+ */
200
316
  getItem(index){
201
317
  const item = this.#_items.getItem(index);
202
318
  return isHTMLElement(item) ? item : null;
203
319
  }
204
320
 
321
+ /**
322
+ * @param {ID_STRING}
323
+ * @param {any}
324
+ * @returns {?HTMLElement}
325
+ * @description Returns an item wich has an attribute
326
+ * with a given name and value.
327
+ */
205
328
  getItemByAttr(name, value = ''){
206
329
  const _name = typeof name === 'string' ? name.trim() : '';
207
330
  const { item } = srchListElementByAttr(this.#_items, _name, value);
208
331
  return item;
209
332
  }
210
333
 
334
+ /**
335
+ * @param {ID_STRING}
336
+ * @param {any}
337
+ * @returns {?HTMLElement}
338
+ * @description Returns an item wich has a given ID.
339
+ */
211
340
  getItemByID(value){
212
341
  const { item } = srchListElementByAttr(this.#_items, 'id', value);
213
342
  return item;
214
343
  }
215
344
 
345
+ /**
346
+ * @param {HTMLElement}
347
+ * @param {bool} [opt=false]
348
+ * @returns {index}
349
+ * @description Adds an item to an instance.
350
+ */
216
351
  addItem(item, opt){
217
352
  const forceCI = typeof opt === 'boolean' ? opt : false;
218
353
  const index = (
@@ -232,6 +367,13 @@ class THtmlItemsListContainer {
232
367
  return isSUCCEED ? index : -1;
233
368
  }
234
369
 
370
+ /**
371
+ * @param {index}
372
+ * @param {any} [opt]
373
+ * @param {bool} [optEx=true]
374
+ * @returns {bool}
375
+ * @description Deletes an item from an instance.
376
+ */
235
377
  delItem(index, opt, optEx){
236
378
  const _items = this.#_items;
237
379
  const item = _items.delItemEx(index, opt);
@@ -251,6 +393,12 @@ class THtmlItemsListContainer {
251
393
  return isSUCCEED;
252
394
  }
253
395
 
396
+ /**
397
+ * @param {index}
398
+ * @param {bool} [opt=false]
399
+ * @returns {bool}
400
+ * @description Selects an item addressed by a given index.
401
+ */
254
402
  selectItem(index, opt){
255
403
  const forceCI = typeof opt === 'boolean' ? opt : false;
256
404
  const isSUCCEED = selectHtmlElement(this.#_items.getItem(index));
@@ -258,28 +406,58 @@ class THtmlItemsListContainer {
258
406
  return isSUCCEED;
259
407
  }
260
408
 
409
+ /**
410
+ * @param {index}
411
+ * @returns {bool}
412
+ * @description Unselects an item addressed by a given index.
413
+ */
261
414
  unselectItem(index){
262
415
  return unselectHtmlElement(this.#_items.getItem(index));
263
416
  }
264
417
 
418
+ /**
419
+ * @param {index}
420
+ * @returns {bool}
421
+ * @description Hides an item addressed by a given index.
422
+ */
265
423
  hideItem(index){
266
424
  return hideHtmlElement(this.#_items.getItem(index));
267
425
  }
268
426
 
427
+ /**
428
+ * @param {index}
429
+ * @returns {bool}
430
+ * @description Shows an item addressed by a given index.
431
+ */
269
432
  showItem(index){
270
433
  return showHtmlElement(this.#_items.getItem(index));
271
434
  }
272
435
 
436
+ /**
437
+ * @param {index}
438
+ * @returns {bool}
439
+ * @description Checks whether an item is selected.
440
+ */
273
441
  isSelectedItem(index) {
274
442
  return isSelectedHTMLElement(this.#_items.getItem(index));
275
443
  }
276
444
 
445
+ /**
446
+ * @param {index}
447
+ * @returns {bool}
448
+ * @description Checks whether an item is hidden.
449
+ */
277
450
  isHiddenItem(index) {
278
451
  return isHiddenHTMLElement(this.#_items.getItem(index));
279
452
  }
280
453
 
281
454
  };
282
455
 
456
+ /**
457
+ * @augments THtmlItemsListContainer
458
+ * @description This class enhanced a capabilities implemented
459
+ * in the `THtmlItemsListContainer` class
460
+ */
283
461
  class THtmlItemsListController extends THtmlItemsListContainer {
284
462
  #_host = null;
285
463
  #_stubs = null;
@@ -288,6 +466,17 @@ class THtmlItemsListController extends THtmlItemsListContainer {
288
466
  #_status = null;
289
467
  #_events = null;
290
468
 
469
+ /**
470
+ * @param {HTMLElement}
471
+ * @param {object} [opt]
472
+ * @param {bool} [opt.autoHideNewItems=false]
473
+ * @param {bool} [opt.markCurrentItem=false]
474
+ * @param {(string|array)} [opt.itemBaseClassID]
475
+ * @param {bool} [opt.showStubsIfEmpty=false]
476
+ * @param {bool} [opt.allowGroupSelection=false]
477
+ * @param {bool} [opt.allowSelectionLocks=false]
478
+ * @param {object} [opt.stubs]
479
+ */
291
480
  constructor(host, opt) {
292
481
  // check host element
293
482
  const isHostEnabled = isHTMLElement(host);
@@ -345,6 +534,11 @@ class THtmlItemsListController extends THtmlItemsListContainer {
345
534
  this.#_status = _status;
346
535
  }
347
536
 
537
+ /**
538
+ * @param {object}
539
+ * @returns {none}
540
+ * @private
541
+ */
348
542
  #_on_will_select_item = (e) => {
349
543
  //console.log('THtmlItemsListController._on_will_select_item() ==> was called...');
350
544
  //e.preventDefault(); /* need to reconsider reason for use */
@@ -394,10 +588,21 @@ class THtmlItemsListController extends THtmlItemsListContainer {
394
588
  };
395
589
  };
396
590
 
591
+ /**
592
+ * @param {ID_STRING}
593
+ * @param {...any}
594
+ * @returns {none}
595
+ * @private
596
+ * @see triggerEventHandler
597
+ */
397
598
  #_triggerEvent = (name, ...args) => {
398
599
  triggerEventHandler(this.#_events, name, ...args);
399
600
  };
400
601
 
602
+ /**
603
+ * @property {array}
604
+ * @readonly
605
+ */
401
606
  get SelectedItems(){
402
607
  const _selects = this.#_selects;
403
608
  // // TODO: consider to use: `[...<value>]`
@@ -408,14 +613,28 @@ class THtmlItemsListController extends THtmlItemsListContainer {
408
613
  return result;
409
614
  }
410
615
 
616
+ /**
617
+ * @property {THtmlStubItemsSet}
618
+ * @readonly
619
+ */
411
620
  get StubItems(){
412
621
  return this.#_stubs;
413
622
  }
414
623
 
624
+ /**
625
+ * @property {bool}
626
+ * @readonly
627
+ */
415
628
  get isSelectionLocked(){
416
629
  return this.#_status.isSelectionLocked;
417
630
  }
418
631
 
632
+ /**
633
+ * @param {none}
634
+ * @returns {none}
635
+ * @fires THtmlItemsListController#list-clear
636
+ * @description Clears an instance content.
637
+ */
419
638
  clear(){
420
639
  // // TODO: clear event handler on elements if set
421
640
  super.clear();
@@ -424,21 +643,48 @@ class THtmlItemsListController extends THtmlItemsListContainer {
424
643
  // show default stub-item
425
644
  this.#_status.isStubItemShown = this.#_stubs.showDefItem();
426
645
  };
646
+ /**
647
+ * @event THtmlItemsListController#list-clear
648
+ * @type {none}
649
+ */
427
650
  this.#_triggerEvent('list-clear');
428
651
  }
429
652
 
653
+ /**
654
+ * @param {none}
655
+ * @returns {none}
656
+ * @description Locks an element selection.
657
+ */
430
658
  lockItemsSelection(){
431
659
  if (this.#_options.allowSelectionLocks) {
432
660
  this.#_status.isSelectionLocked = true;
433
661
  };
434
662
  }
435
663
 
664
+ /**
665
+ * @param {none}
666
+ * @returns {none}
667
+ * @description Unlocks an element selection.
668
+ */
436
669
  unlockItemsSelection(){
437
670
  this.#_status.isSelectionLocked = false;
438
671
  }
439
672
 
673
+ /**
674
+ * @param {index}
675
+ * @returns {bool}
676
+ * @fires THtmlItemsListController#current-item-chosen
677
+ * @private
678
+ * @description Sets a current index.
679
+ */
440
680
  #_setCurIndex(index){
441
681
  const isSUCCEED = super.setCurIndex(index);
682
+ /**
683
+ * @event THtmlItemsListController#current-item-chosen
684
+ * @type {object}
685
+ * @property {index} index
686
+ * @property {null} item
687
+ */
442
688
  if (isSUCCEED) this.#_triggerEvent('current-item-chosen', {
443
689
  index: Number(index),
444
690
  item: null,
@@ -446,10 +692,20 @@ class THtmlItemsListController extends THtmlItemsListContainer {
446
692
  return isSUCCEED;
447
693
  };
448
694
 
695
+ /**
696
+ * @param {index}
697
+ * @returns {bool}
698
+ * @description Sets a current index.
699
+ */
449
700
  setCurIndex(index){
450
701
  return this.selectItem(index, true);
451
702
  }
452
703
 
704
+ /**
705
+ * @param {none}
706
+ * @returns {none}
707
+ * @description Resets a current index.
708
+ */
453
709
  rstCurIndex(){
454
710
  const {
455
711
  execDelItem,
@@ -466,6 +722,13 @@ class THtmlItemsListController extends THtmlItemsListContainer {
466
722
  super.rstCurIndex();
467
723
  }
468
724
 
725
+ /**
726
+ * @param {HTMLElement}
727
+ * @returns {index}
728
+ * @fires THtmlItemsListController#first-item-added
729
+ * @fires THtmlItemsListController#item-added
730
+ * @description Adds an element to an instance members.
731
+ */
469
732
  addItem(item, opt){
470
733
  const index = super.addItem(item, false);
471
734
  if (index !== -1) {
@@ -488,11 +751,23 @@ class THtmlItemsListController extends THtmlItemsListContainer {
488
751
  // hide default stub-item
489
752
  _status.isStubItemShown = false;
490
753
  };
754
+ /**
755
+ * @event THtmlItemsListController#first-item-added
756
+ * @type {object}
757
+ * @property {index} index
758
+ * @property {?HTMLElement} item
759
+ */
491
760
  this.#_triggerEvent('first-item-added', {
492
761
  index: index,
493
762
  item: item,
494
763
  });
495
764
  };
765
+ /**
766
+ * @event THtmlItemsListController#item-added
767
+ * @type {object}
768
+ * @property {index} index
769
+ * @property {?HTMLElement} item
770
+ */
496
771
  this.#_triggerEvent('item-added', {
497
772
  index: index,
498
773
  item: item,
@@ -509,6 +784,14 @@ class THtmlItemsListController extends THtmlItemsListContainer {
509
784
  return index;
510
785
  }
511
786
 
787
+ /**
788
+ * @param {index}
789
+ * @param {any} [opt]
790
+ * @returns {bool}
791
+ * @fires THtmlItemsListController#list-clear
792
+ * @fires THtmlItemsListController#item-removed
793
+ * @description Deletes an element from an instance members.
794
+ */
512
795
  delItem(index, opt){
513
796
  const item = super.getItem(index);
514
797
  let isSUCCEED = item !== undefined;
@@ -530,9 +813,18 @@ class THtmlItemsListController extends THtmlItemsListContainer {
530
813
  // show default stub-item
531
814
  _status.isStubItemShown = this.#_stubs.showDefItem();
532
815
  };
816
+ /**
817
+ * @see THtmlItemsListController#list-clear
818
+ */
533
819
  this.#_triggerEvent('list-clear');
534
820
  } else {
535
821
  this.#_selects.delete(item);
822
+ /**
823
+ * @event THtmlItemsListController#item-removed
824
+ * @type {object}
825
+ * @property {index} index
826
+ * @property {?HTMLElement} item
827
+ */
536
828
  this.#_triggerEvent('item-removed', {
537
829
  index: Number(index),
538
830
  item: item,
@@ -552,11 +844,24 @@ class THtmlItemsListController extends THtmlItemsListContainer {
552
844
  return isSUCCEED;
553
845
  }
554
846
 
847
+ /**
848
+ * @param {index}
849
+ * @returns {bool}
850
+ * @private
851
+ * @fires THtmlItemsListController#item-selected
852
+ * @description Selects an element.
853
+ */
555
854
  #_selectItem(index){
556
855
  const item = super.getItem(index);
557
856
  const isSUCCEED = selectHtmlElement(item);
558
857
  if (isSUCCEED) {
559
858
  this.#_selects.add(item);
859
+ /**
860
+ * @event THtmlItemsListController#item-selected
861
+ * @type {object}
862
+ * @property {index} index
863
+ * @property {?HTMLElement} item
864
+ */
560
865
  this.#_triggerEvent('item-selected', {
561
866
  index: Number(index),
562
867
  item: item,
@@ -565,6 +870,16 @@ class THtmlItemsListController extends THtmlItemsListContainer {
565
870
  return isSUCCEED;
566
871
  }
567
872
 
873
+ /**
874
+ * @param {index}
875
+ * @param {object} [opt]
876
+ * @param {bool} [opt.ctrlKey=false]
877
+ * @param {bool} [opt.shiftKey=false]
878
+ * @param {bool} [opt.forceCI=false]
879
+ * @returns {none}
880
+ * @private
881
+ * @description Selects an element.
882
+ */
568
883
  #_selectItemEx(item, opt){
569
884
  const _selects = this.#_selects;
570
885
  let {
@@ -617,6 +932,12 @@ class THtmlItemsListController extends THtmlItemsListContainer {
617
932
  };
618
933
  }
619
934
 
935
+ /**
936
+ * @param {index}
937
+ * @param {bool} [opt=false]
938
+ * @returns {bool}
939
+ * @description Selects an element.
940
+ */
620
941
  selectItem(index, opt){
621
942
  const item = super.getItem(index);
622
943
  let isSUCCEED = isHTMLElement(item);
@@ -631,11 +952,23 @@ class THtmlItemsListController extends THtmlItemsListContainer {
631
952
  return isSUCCEED;
632
953
  }
633
954
 
955
+ /**
956
+ * @param {index}
957
+ * @returns {bool}
958
+ * @fires THtmlItemsListController#item-unselected
959
+ * @description Unselects an element.
960
+ */
634
961
  unselectItem(index){
635
962
  const item = super.getItem(index);
636
963
  let isSUCCEED = unselectHtmlElement(item);
637
964
  if (isSUCCEED) {
638
965
  this.#_selects.delete(item);
966
+ /**
967
+ * @event THtmlItemsListController#item-unselected
968
+ * @type {object}
969
+ * @property {index} index
970
+ * @property {?HTMLElement} item
971
+ */
639
972
  this.#_triggerEvent('item-unselected', {
640
973
  index: Number(index),
641
974
  item: item,
@@ -644,10 +977,22 @@ class THtmlItemsListController extends THtmlItemsListContainer {
644
977
  return isSUCCEED;
645
978
  }
646
979
 
980
+ /**
981
+ * @param {index}
982
+ * @returns {bool}
983
+ * @fires THtmlItemsListController#item-hidden
984
+ * @description Hides an element.
985
+ */
647
986
  hideItem(index){
648
987
  const item = super.getItem(index);
649
988
  let isSUCCEED = hideHtmlElement(item);
650
989
  if (isSUCCEED) {
990
+ /**
991
+ * @event THtmlItemsListController#item-hidden
992
+ * @type {object}
993
+ * @property {index} index
994
+ * @property {?HTMLElement} item
995
+ */
651
996
  this.#_triggerEvent('item-hidden', {
652
997
  index: Number(index),
653
998
  item: item,
@@ -656,10 +1001,22 @@ class THtmlItemsListController extends THtmlItemsListContainer {
656
1001
  return isSUCCEED;
657
1002
  }
658
1003
 
1004
+ /**
1005
+ * @param {index}
1006
+ * @returns {bool}
1007
+ * @fires THtmlItemsListController#item-shown
1008
+ * @description Shows an element.
1009
+ */
659
1010
  showItem(index){
660
1011
  const item = super.getItem(index);
661
1012
  let isSUCCEED = showHtmlElement(item);
662
1013
  if (isSUCCEED) {
1014
+ /**
1015
+ * @event THtmlItemsListController#item-shown
1016
+ * @type {object}
1017
+ * @property {index} index
1018
+ * @property {?HTMLElement} item
1019
+ */
663
1020
  this.#_triggerEvent('item-shown', {
664
1021
  index: Number(index),
665
1022
  item: item,
@@ -668,6 +1025,12 @@ class THtmlItemsListController extends THtmlItemsListContainer {
668
1025
  return isSUCCEED;
669
1026
  }
670
1027
 
1028
+ /**
1029
+ * @param {ID_STRING}
1030
+ * @param {func}
1031
+ * @returns {none}
1032
+ * @description Sets a callback function to handle event.
1033
+ */
671
1034
  on(name, evnt){
672
1035
  pushEventHandler(this.#_events, name, evnt);
673
1036
  }