@cntwg/html-helper 0.0.19 → 0.0.21

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.062-20240705]
2
2
 
3
3
  // === module init block ===
4
4
 
@@ -61,16 +61,36 @@ function srchListElementByAttr(list, name, value = '') {
61
61
  return isACCEPTED ? { index, item } : { index, item: null };
62
62
  };
63
63
 
64
- // === module main block (function definitions) ===
64
+ // === module main block ===
65
65
 
66
- // === module main block (class definitions) ===
66
+ /***
67
+ * (* constant definitions *)
68
+ */
67
69
 
70
+ /***
71
+ * (* function definitions *)
72
+ */
73
+
74
+ /***
75
+ * (* class definitions *)
76
+ */
77
+
78
+ /**
79
+ * @description This class implements an instance of a list container
80
+ */
68
81
  class THtmlItemsListContainer {
69
82
  #_host = null;
70
83
  #_items = null;
71
84
  #_options = null;
72
85
  #_status = null;
73
86
 
87
+ /**
88
+ * @param {HTMLElement}
89
+ * @param {object} [opt]
90
+ * @param {bool} [opt.autoHideNewItems=false]
91
+ * @param {bool} [opt.markCurrentItem=false]
92
+ * @param {(string|array)} [opt.itemBaseClassID]
93
+ */
74
94
  constructor(host, opt) {
75
95
  // check host
76
96
  this.#_host = isHTMLElement(host) ? host : null;
@@ -102,35 +122,68 @@ class THtmlItemsListContainer {
102
122
  this.#_options = _options;
103
123
  }
104
124
 
125
+ /**
126
+ * @property {number}
127
+ * @readonly
128
+ */
105
129
  get count(){
106
130
  return this.#_items.count;
107
131
  }
108
132
 
133
+ /**
134
+ * @property {index}
135
+ * @readonly
136
+ */
109
137
  get curIndex(){
110
138
  return this.#_items.curIndex;
111
139
  }
112
140
 
141
+ /**
142
+ * @property {index}
143
+ * @readonly
144
+ */
113
145
  get minIndex(){
114
146
  return this.#_items.minIndex;
115
147
  }
116
148
 
149
+ /**
150
+ * @property {index}
151
+ * @readonly
152
+ */
117
153
  get maxIndex(){
118
154
  return this.#_items.maxIndex;
119
155
  }
120
156
 
157
+ /**
158
+ * @property {index}
159
+ * @readonly
160
+ */
121
161
  get prevIndex(){
122
162
  return this.#_items.prevIndex;
123
163
  }
124
164
 
165
+ /**
166
+ * @property {index}
167
+ * @readonly
168
+ */
125
169
  get nextIndex(){
126
170
  return this.#_items.nextIndex;
127
171
  }
128
172
 
173
+ /**
174
+ * @property {?HTMLElement}
175
+ * @readonly
176
+ */
129
177
  get curItem(){
130
178
  const item = this.#_items.curItem;
131
179
  return isHTMLElement(item) ? item : null;
132
180
  }
133
181
 
182
+ /**
183
+ * @param {none}
184
+ * @returns {none}
185
+ * @description Clears an instance content.
186
+ */
134
187
  clear(){
135
188
  this.#_items.clear();
136
189
  const _status = this.#_status;
@@ -140,37 +193,80 @@ class THtmlItemsListContainer {
140
193
  if (_host) _host.innerHTML = '';
141
194
  }
142
195
 
196
+ /**
197
+ * @param {none}
198
+ * @returns {bool}
199
+ * @description Checks if an instance contains no items.
200
+ */
143
201
  isEmpty(){
144
202
  return this.#_items.isEmpty();
145
203
  }
146
204
 
205
+ /**
206
+ * @param {none}
207
+ * @returns {bool}
208
+ * @description Checks if an instance contains any items.
209
+ */
147
210
  isNotEmpty(){
148
211
  return this.#_items.isNotEmpty();
149
212
  }
150
213
 
214
+ /**
215
+ * @param {any}
216
+ * @returns {bool}
217
+ * @description Checks if a given value is a valid index and
218
+ * it fits an index range within an instance.
219
+ */
151
220
  chkIndex(value){
152
221
  return this.#_items.chkIndex(value);
153
222
  }
154
223
 
224
+ /**
225
+ * @see TItemsListEx.chkIndexEx
226
+ * @description Checks if a given value is a valid index and
227
+ * it fits an index range within an instance.
228
+ */
155
229
  chkIndexEx(...args){
156
230
  return this.#_items.chkIndexEx(...args);
157
231
  }
158
232
 
233
+ /**
234
+ * @param {HTMLElement}
235
+ * @returns {index}
236
+ * @description Returns an index of a given element.
237
+ */
159
238
  srchIndex(item){
160
239
  return isHTMLElement(item) ? this.#_items.srchIndex(item) : -1;
161
240
  }
162
241
 
242
+ /**
243
+ * @param {ID_STRING}
244
+ * @param {any}
245
+ * @returns {index}
246
+ * @description Returns an index of an element wich has an attribute
247
+ * with a given name and value.
248
+ */
163
249
  srchIndexByAttr(name, value = ''){
164
250
  const _name = typeof name === 'string' ? name.trim() : '';
165
251
  const { index } = srchListElementByAttr(this.#_items, _name, value);
166
252
  return index;
167
253
  };
168
254
 
255
+ /**
256
+ * @param {ID_STRING}
257
+ * @returns {index}
258
+ * @description Returns an index of an element wich has a given ID.
259
+ */
169
260
  srchIndexByID(value){
170
261
  const { index } = srchListElementByAttr(this.#_items, 'id', value);
171
262
  return index;
172
263
  };
173
264
 
265
+ /**
266
+ * @param {index}
267
+ * @returns {bool}
268
+ * @description Sets a current index.
269
+ */
174
270
  setCurIndex(index){
175
271
  const isSUCCEED = this.#_items.setIndex(index);
176
272
  if (isSUCCEED) {
@@ -187,6 +283,11 @@ class THtmlItemsListContainer {
187
283
  return isSUCCEED;
188
284
  }
189
285
 
286
+ /**
287
+ * @param {none}
288
+ * @returns {none}
289
+ * @description Resets a current index.
290
+ */
190
291
  rstCurIndex(){
191
292
  const _status = this.#_status;
192
293
  if (this.#_options.markCurrentItem && _status.curIndex !== -1) {
@@ -197,22 +298,46 @@ class THtmlItemsListContainer {
197
298
  _status.curItem = this.curItem;
198
299
  }
199
300
 
301
+ /**
302
+ * @param {index}
303
+ * @returns {?HTMLElement}
304
+ * @description Returns an item addressed by a given index.
305
+ */
200
306
  getItem(index){
201
307
  const item = this.#_items.getItem(index);
202
308
  return isHTMLElement(item) ? item : null;
203
309
  }
204
310
 
311
+ /**
312
+ * @param {ID_STRING}
313
+ * @param {any}
314
+ * @returns {?HTMLElement}
315
+ * @description Returns an item wich has an attribute
316
+ * with a given name and value.
317
+ */
205
318
  getItemByAttr(name, value = ''){
206
319
  const _name = typeof name === 'string' ? name.trim() : '';
207
320
  const { item } = srchListElementByAttr(this.#_items, _name, value);
208
321
  return item;
209
322
  }
210
323
 
324
+ /**
325
+ * @param {ID_STRING}
326
+ * @param {any}
327
+ * @returns {?HTMLElement}
328
+ * @description Returns an item wich has a given ID.
329
+ */
211
330
  getItemByID(value){
212
331
  const { item } = srchListElementByAttr(this.#_items, 'id', value);
213
332
  return item;
214
333
  }
215
334
 
335
+ /**
336
+ * @param {HTMLElement}
337
+ * @param {bool} [opt=false]
338
+ * @returns {index}
339
+ * @description Adds an item to an instance.
340
+ */
216
341
  addItem(item, opt){
217
342
  const forceCI = typeof opt === 'boolean' ? opt : false;
218
343
  const index = (
@@ -232,6 +357,13 @@ class THtmlItemsListContainer {
232
357
  return isSUCCEED ? index : -1;
233
358
  }
234
359
 
360
+ /**
361
+ * @param {index}
362
+ * @param {any} [opt]
363
+ * @param {bool} [optEx=true]
364
+ * @returns {bool}
365
+ * @description Deletes an item from an instance.
366
+ */
235
367
  delItem(index, opt, optEx){
236
368
  const _items = this.#_items;
237
369
  const item = _items.delItemEx(index, opt);
@@ -251,6 +383,12 @@ class THtmlItemsListContainer {
251
383
  return isSUCCEED;
252
384
  }
253
385
 
386
+ /**
387
+ * @param {index}
388
+ * @param {bool} [opt=false]
389
+ * @returns {bool}
390
+ * @description Selects an item addressed by a given index.
391
+ */
254
392
  selectItem(index, opt){
255
393
  const forceCI = typeof opt === 'boolean' ? opt : false;
256
394
  const isSUCCEED = selectHtmlElement(this.#_items.getItem(index));
@@ -258,28 +396,58 @@ class THtmlItemsListContainer {
258
396
  return isSUCCEED;
259
397
  }
260
398
 
399
+ /**
400
+ * @param {index}
401
+ * @returns {bool}
402
+ * @description Unselects an item addressed by a given index.
403
+ */
261
404
  unselectItem(index){
262
405
  return unselectHtmlElement(this.#_items.getItem(index));
263
406
  }
264
407
 
408
+ /**
409
+ * @param {index}
410
+ * @returns {bool}
411
+ * @description Hides an item addressed by a given index.
412
+ */
265
413
  hideItem(index){
266
414
  return hideHtmlElement(this.#_items.getItem(index));
267
415
  }
268
416
 
417
+ /**
418
+ * @param {index}
419
+ * @returns {bool}
420
+ * @description Shows an item addressed by a given index.
421
+ */
269
422
  showItem(index){
270
423
  return showHtmlElement(this.#_items.getItem(index));
271
424
  }
272
425
 
426
+ /**
427
+ * @param {index}
428
+ * @returns {bool}
429
+ * @description Checks whether an item is selected.
430
+ */
273
431
  isSelectedItem(index) {
274
432
  return isSelectedHTMLElement(this.#_items.getItem(index));
275
433
  }
276
434
 
435
+ /**
436
+ * @param {index}
437
+ * @returns {bool}
438
+ * @description Checks whether an item is hidden.
439
+ */
277
440
  isHiddenItem(index) {
278
441
  return isHiddenHTMLElement(this.#_items.getItem(index));
279
442
  }
280
443
 
281
444
  };
282
445
 
446
+ /**
447
+ * @augments THtmlItemsListContainer
448
+ * @description This class enhanced a capabilities implemented
449
+ * in the `THtmlItemsListContainer` class
450
+ */
283
451
  class THtmlItemsListController extends THtmlItemsListContainer {
284
452
  #_host = null;
285
453
  #_stubs = null;
@@ -288,6 +456,17 @@ class THtmlItemsListController extends THtmlItemsListContainer {
288
456
  #_status = null;
289
457
  #_events = null;
290
458
 
459
+ /**
460
+ * @param {HTMLElement}
461
+ * @param {object} [opt]
462
+ * @param {bool} [opt.autoHideNewItems=false]
463
+ * @param {bool} [opt.markCurrentItem=false]
464
+ * @param {(string|array)} [opt.itemBaseClassID]
465
+ * @param {bool} [opt.showStubsIfEmpty=false]
466
+ * @param {bool} [opt.allowGroupSelection=false]
467
+ * @param {bool} [opt.allowSelectionLocks=false]
468
+ * @param {object} [opt.stubs]
469
+ */
291
470
  constructor(host, opt) {
292
471
  // check host element
293
472
  const isHostEnabled = isHTMLElement(host);
@@ -345,6 +524,11 @@ class THtmlItemsListController extends THtmlItemsListContainer {
345
524
  this.#_status = _status;
346
525
  }
347
526
 
527
+ /**
528
+ * @param {object}
529
+ * @returns {none}
530
+ * @private
531
+ */
348
532
  #_on_will_select_item = (e) => {
349
533
  //console.log('THtmlItemsListController._on_will_select_item() ==> was called...');
350
534
  //e.preventDefault(); /* need to reconsider reason for use */
@@ -394,10 +578,21 @@ class THtmlItemsListController extends THtmlItemsListContainer {
394
578
  };
395
579
  };
396
580
 
581
+ /**
582
+ * @param {ID_STRING}
583
+ * @param {...any}
584
+ * @returns {none}
585
+ * @private
586
+ * @see triggerEventHandler
587
+ */
397
588
  #_triggerEvent = (name, ...args) => {
398
589
  triggerEventHandler(this.#_events, name, ...args);
399
590
  };
400
591
 
592
+ /**
593
+ * @property {array}
594
+ * @readonly
595
+ */
401
596
  get SelectedItems(){
402
597
  const _selects = this.#_selects;
403
598
  // // TODO: consider to use: `[...<value>]`
@@ -408,14 +603,28 @@ class THtmlItemsListController extends THtmlItemsListContainer {
408
603
  return result;
409
604
  }
410
605
 
606
+ /**
607
+ * @property {THtmlStubItemsSet}
608
+ * @readonly
609
+ */
411
610
  get StubItems(){
412
611
  return this.#_stubs;
413
612
  }
414
613
 
614
+ /**
615
+ * @property {bool}
616
+ * @readonly
617
+ */
415
618
  get isSelectionLocked(){
416
619
  return this.#_status.isSelectionLocked;
417
620
  }
418
621
 
622
+ /**
623
+ * @param {none}
624
+ * @returns {none}
625
+ * @fires THtmlItemsListController#list-clear
626
+ * @description Clears an instance content.
627
+ */
419
628
  clear(){
420
629
  // // TODO: clear event handler on elements if set
421
630
  super.clear();
@@ -424,21 +633,48 @@ class THtmlItemsListController extends THtmlItemsListContainer {
424
633
  // show default stub-item
425
634
  this.#_status.isStubItemShown = this.#_stubs.showDefItem();
426
635
  };
636
+ /**
637
+ * @event THtmlItemsListController#list-clear
638
+ * @type {none}
639
+ */
427
640
  this.#_triggerEvent('list-clear');
428
641
  }
429
642
 
643
+ /**
644
+ * @param {none}
645
+ * @returns {none}
646
+ * @description Locks an element selection.
647
+ */
430
648
  lockItemsSelection(){
431
649
  if (this.#_options.allowSelectionLocks) {
432
650
  this.#_status.isSelectionLocked = true;
433
651
  };
434
652
  }
435
653
 
654
+ /**
655
+ * @param {none}
656
+ * @returns {none}
657
+ * @description Unlocks an element selection.
658
+ */
436
659
  unlockItemsSelection(){
437
660
  this.#_status.isSelectionLocked = false;
438
661
  }
439
662
 
663
+ /**
664
+ * @param {index}
665
+ * @returns {bool}
666
+ * @fires THtmlItemsListController#current-item-chosen
667
+ * @private
668
+ * @description Sets a current index.
669
+ */
440
670
  #_setCurIndex(index){
441
671
  const isSUCCEED = super.setCurIndex(index);
672
+ /**
673
+ * @event THtmlItemsListController#current-item-chosen
674
+ * @type {object}
675
+ * @property {index} index
676
+ * @property {null} item
677
+ */
442
678
  if (isSUCCEED) this.#_triggerEvent('current-item-chosen', {
443
679
  index: Number(index),
444
680
  item: null,
@@ -446,10 +682,20 @@ class THtmlItemsListController extends THtmlItemsListContainer {
446
682
  return isSUCCEED;
447
683
  };
448
684
 
685
+ /**
686
+ * @param {index}
687
+ * @returns {bool}
688
+ * @description Sets a current index.
689
+ */
449
690
  setCurIndex(index){
450
691
  return this.selectItem(index, true);
451
692
  }
452
693
 
694
+ /**
695
+ * @param {none}
696
+ * @returns {none}
697
+ * @description Resets a current index.
698
+ */
453
699
  rstCurIndex(){
454
700
  const {
455
701
  execDelItem,
@@ -466,6 +712,13 @@ class THtmlItemsListController extends THtmlItemsListContainer {
466
712
  super.rstCurIndex();
467
713
  }
468
714
 
715
+ /**
716
+ * @param {HTMLElement}
717
+ * @returns {index}
718
+ * @fires THtmlItemsListController#first-item-added
719
+ * @fires THtmlItemsListController#item-added
720
+ * @description Adds an element to an instance members.
721
+ */
469
722
  addItem(item, opt){
470
723
  const index = super.addItem(item, false);
471
724
  if (index !== -1) {
@@ -488,11 +741,23 @@ class THtmlItemsListController extends THtmlItemsListContainer {
488
741
  // hide default stub-item
489
742
  _status.isStubItemShown = false;
490
743
  };
744
+ /**
745
+ * @event THtmlItemsListController#first-item-added
746
+ * @type {object}
747
+ * @property {index} index
748
+ * @property {?HTMLElement} item
749
+ */
491
750
  this.#_triggerEvent('first-item-added', {
492
751
  index: index,
493
752
  item: item,
494
753
  });
495
754
  };
755
+ /**
756
+ * @event THtmlItemsListController#item-added
757
+ * @type {object}
758
+ * @property {index} index
759
+ * @property {?HTMLElement} item
760
+ */
496
761
  this.#_triggerEvent('item-added', {
497
762
  index: index,
498
763
  item: item,
@@ -509,6 +774,14 @@ class THtmlItemsListController extends THtmlItemsListContainer {
509
774
  return index;
510
775
  }
511
776
 
777
+ /**
778
+ * @param {index}
779
+ * @param {any} [opt]
780
+ * @returns {bool}
781
+ * @fires THtmlItemsListController#list-clear
782
+ * @fires THtmlItemsListController#item-removed
783
+ * @description Deletes an element from an instance members.
784
+ */
512
785
  delItem(index, opt){
513
786
  const item = super.getItem(index);
514
787
  let isSUCCEED = item !== undefined;
@@ -530,9 +803,18 @@ class THtmlItemsListController extends THtmlItemsListContainer {
530
803
  // show default stub-item
531
804
  _status.isStubItemShown = this.#_stubs.showDefItem();
532
805
  };
806
+ /**
807
+ * @see THtmlItemsListController#list-clear
808
+ */
533
809
  this.#_triggerEvent('list-clear');
534
810
  } else {
535
811
  this.#_selects.delete(item);
812
+ /**
813
+ * @event THtmlItemsListController#item-removed
814
+ * @type {object}
815
+ * @property {index} index
816
+ * @property {?HTMLElement} item
817
+ */
536
818
  this.#_triggerEvent('item-removed', {
537
819
  index: Number(index),
538
820
  item: item,
@@ -552,11 +834,24 @@ class THtmlItemsListController extends THtmlItemsListContainer {
552
834
  return isSUCCEED;
553
835
  }
554
836
 
837
+ /**
838
+ * @param {index}
839
+ * @returns {bool}
840
+ * @private
841
+ * @fires THtmlItemsListController#item-selected
842
+ * @description Selects an element.
843
+ */
555
844
  #_selectItem(index){
556
845
  const item = super.getItem(index);
557
846
  const isSUCCEED = selectHtmlElement(item);
558
847
  if (isSUCCEED) {
559
848
  this.#_selects.add(item);
849
+ /**
850
+ * @event THtmlItemsListController#item-selected
851
+ * @type {object}
852
+ * @property {index} index
853
+ * @property {?HTMLElement} item
854
+ */
560
855
  this.#_triggerEvent('item-selected', {
561
856
  index: Number(index),
562
857
  item: item,
@@ -565,6 +860,16 @@ class THtmlItemsListController extends THtmlItemsListContainer {
565
860
  return isSUCCEED;
566
861
  }
567
862
 
863
+ /**
864
+ * @param {index}
865
+ * @param {object} [opt]
866
+ * @param {bool} [opt.ctrlKey=false]
867
+ * @param {bool} [opt.shiftKey=false]
868
+ * @param {bool} [opt.forceCI=false]
869
+ * @returns {none}
870
+ * @private
871
+ * @description Selects an element.
872
+ */
568
873
  #_selectItemEx(item, opt){
569
874
  const _selects = this.#_selects;
570
875
  let {
@@ -617,6 +922,12 @@ class THtmlItemsListController extends THtmlItemsListContainer {
617
922
  };
618
923
  }
619
924
 
925
+ /**
926
+ * @param {index}
927
+ * @param {bool} [opt=false]
928
+ * @returns {bool}
929
+ * @description Selects an element.
930
+ */
620
931
  selectItem(index, opt){
621
932
  const item = super.getItem(index);
622
933
  let isSUCCEED = isHTMLElement(item);
@@ -631,11 +942,23 @@ class THtmlItemsListController extends THtmlItemsListContainer {
631
942
  return isSUCCEED;
632
943
  }
633
944
 
945
+ /**
946
+ * @param {index}
947
+ * @returns {bool}
948
+ * @fires THtmlItemsListController#item-unselected
949
+ * @description Unselects an element.
950
+ */
634
951
  unselectItem(index){
635
952
  const item = super.getItem(index);
636
953
  let isSUCCEED = unselectHtmlElement(item);
637
954
  if (isSUCCEED) {
638
955
  this.#_selects.delete(item);
956
+ /**
957
+ * @event THtmlItemsListController#item-unselected
958
+ * @type {object}
959
+ * @property {index} index
960
+ * @property {?HTMLElement} item
961
+ */
639
962
  this.#_triggerEvent('item-unselected', {
640
963
  index: Number(index),
641
964
  item: item,
@@ -644,10 +967,22 @@ class THtmlItemsListController extends THtmlItemsListContainer {
644
967
  return isSUCCEED;
645
968
  }
646
969
 
970
+ /**
971
+ * @param {index}
972
+ * @returns {bool}
973
+ * @fires THtmlItemsListController#item-hidden
974
+ * @description Hides an element.
975
+ */
647
976
  hideItem(index){
648
977
  const item = super.getItem(index);
649
978
  let isSUCCEED = hideHtmlElement(item);
650
979
  if (isSUCCEED) {
980
+ /**
981
+ * @event THtmlItemsListController#item-hidden
982
+ * @type {object}
983
+ * @property {index} index
984
+ * @property {?HTMLElement} item
985
+ */
651
986
  this.#_triggerEvent('item-hidden', {
652
987
  index: Number(index),
653
988
  item: item,
@@ -656,10 +991,22 @@ class THtmlItemsListController extends THtmlItemsListContainer {
656
991
  return isSUCCEED;
657
992
  }
658
993
 
994
+ /**
995
+ * @param {index}
996
+ * @returns {bool}
997
+ * @fires THtmlItemsListController#item-shown
998
+ * @description Shows an element.
999
+ */
659
1000
  showItem(index){
660
1001
  const item = super.getItem(index);
661
1002
  let isSUCCEED = showHtmlElement(item);
662
1003
  if (isSUCCEED) {
1004
+ /**
1005
+ * @event THtmlItemsListController#item-shown
1006
+ * @type {object}
1007
+ * @property {index} index
1008
+ * @property {?HTMLElement} item
1009
+ */
663
1010
  this.#_triggerEvent('item-shown', {
664
1011
  index: Number(index),
665
1012
  item: item,
@@ -668,6 +1015,12 @@ class THtmlItemsListController extends THtmlItemsListContainer {
668
1015
  return isSUCCEED;
669
1016
  }
670
1017
 
1018
+ /**
1019
+ * @param {ID_STRING}
1020
+ * @param {func}
1021
+ * @returns {none}
1022
+ * @description Sets a callback function to handle event.
1023
+ */
671
1024
  on(name, evnt){
672
1025
  pushEventHandler(this.#_events, name, evnt);
673
1026
  }