@cntwg/html-helper 0.0.14 → 0.0.15

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,6 +1,6 @@
1
- >|***rev.*:**|0.1.9|
1
+ >|***rev.*:**|0.1.11|
2
2
  >|:---|---:|
3
- >|date:|2022-09-12|
3
+ >|date:|2022-09-25|
4
4
 
5
5
  ## Introduction
6
6
 
@@ -26,6 +26,7 @@ The `options` structure has listed below:
26
26
  |option name|value type|default value|description|
27
27
  |:---|---|---:|:---|
28
28
  |`autoHideNewItems`|`boolean`|`false`|alters behavior of the `addItem` class method. If set to `true`, the item which was added to a container members will be automatically hidden|
29
+ |`markCurrentItem`|`boolean`|`false`|alters behavior of the `setCurIndex` class method. If set to `true`, the item which was added to a container members will be automatically marked as current|
29
30
 
30
31
  #### class properties
31
32
 
@@ -75,11 +76,15 @@ This method tries to find a given item in the list and if it is found returns it
75
76
 
76
77
  ##### **srchIndexByAttr(name, value)**
77
78
 
78
- This method tries to find an item by its given attribute in the list and if it is found returns its index.
79
+ This method tries to find an item that has an attribute with a given `name` and `value` in the list and if it is found its index is returned.
80
+
81
+ The `value` can be a `boolean`, `number` or a `string`. If it is not given the empty string value is used. In any other cases the method will failed.
82
+
83
+ > Note: In case when a `value` is an empty string the method will accept any item with an attribute which name is equal to a given `name` regardless of a value such attribute has.
79
84
 
80
85
  ##### **srchIndexByID(value)**
81
86
 
82
- This method tries to find an item by its `ID`-attribute in the list and if it is found returns its index.
87
+ This method tries to find an item by its `ID`-attribute in the list and if it is found returns its index (*see description of `srchIndexByAttr` method for details how a `value` parameter is used*).
83
88
 
84
89
  ##### **setCurIndex(value)**
85
90
 
@@ -95,11 +100,11 @@ This method returns an item addressed by a given `index`.
95
100
 
96
101
  ##### **getItemByAttr(name, value)**
97
102
 
98
- This method tries to find an item by its given attribute in the list and if found returns it.
103
+ This method tries to find an item by its given attribute in the list and if found returns it (*see description of `srchIndexByAttr` method for details how a `value` parameter is used*).
99
104
 
100
105
  ##### **getItemByID(value)**
101
106
 
102
- This method tries to find an item by its `ID`-attribute in the list and if found returns it.
107
+ This method tries to find an item by its `ID`-attribute in the list and if found returns it (*see description of `srchIndexByAttr` method for details how a `value` parameter is used*).
103
108
 
104
109
  ##### **addItem(item\[, options])**
105
110
 
@@ -141,7 +146,7 @@ This method checks whether or not an item addressed by an `index` is hidden and
141
146
 
142
147
  ### **THtmlItemsListController**
143
148
 
144
- This class implements a controller that provide mechanism for menaging a list container and its elements placed on an HTML-form.
149
+ This class implements a controller that provide mechanism for managing a list container and its elements placed on an HTML-form.
145
150
 
146
151
  #### class constructor
147
152
 
@@ -157,6 +162,7 @@ The `options` structure has listed below:
157
162
  |option name|value type|default value|description|
158
163
  |:---|---|---:|:---|
159
164
  |`autoHideNewItems`|`boolean`|`false`|alters behavior of the `addItem` class method. If set to `true`, the item which was added to a container members will be automatically hidden|
165
+ |`markCurrentItem`|`boolean`|`false`|alters behavior of the `setCurIndex` class method. If set to `true`, the item which was added to a container members will be automatically marked as current|
160
166
  |`showStubsIfEmpty`|`boolean`|`false`|if set, in case of the empty list, the default stub-item will be displayed|
161
167
  |`allowGroupSelection`|`boolean`|`false`|if set the more than one element can be selected in the list|
162
168
  |`allowSelectionLocks`|`boolean`|`false`|<*reserved, experimental*>|
@@ -277,7 +283,7 @@ This method checks whether or not an item addressed by an `index` is hidden and
277
283
 
278
284
  This method sets event handler for the list event addressed by `name` parameter.
279
285
 
280
- > Note: for current implementation you can't reset or set new event handler (especailly another one) if a hanlder with the same name exists already.
286
+ > Note: for current implementation you can't reset or set new event handler (especially another one) if a handler with the same name exists already.
281
287
 
282
288
  #### class events
283
289
 
@@ -290,8 +296,8 @@ The class generates the events listed in the table bellow:
290
296
  |`item-added`|fired when an item was added to the list|
291
297
  |`item-removed`|fired when an item was deleted from the list. It will not occur in case the item is the last.|
292
298
  |`current-item-chosen`|fired when an item was choosen as current|
293
- |`item-selected`|fired when an item was marked as selested in the list|
294
- |`item-unselected`|fired when an item was marked as unselested in the list|
299
+ |`item-selected`|fired when an item was marked as selected in the list|
300
+ |`item-unselected`|fired when an item was unmarked as selected in the list|
295
301
  |`item-hidden`|fired when an item was marked as hidden in the list|
296
302
  |`item-shown`|fired when an item was marked as non-hidden in the list|
297
303
 
@@ -339,7 +345,7 @@ This class method tries to include a given `item` to a set members with a label
339
345
 
340
346
  The `actForce` parameter if given alters the method behaviour:
341
347
 
342
- - If set to `true` it force replacements of an existing item which label is qual to `name`;
348
+ - If set to `true` it force replacements of an existing item which label is equal to `name`;
343
349
  - If set to `false` and item with the same label already exists the method will failed;
344
350
  - If parameter is not given or not of a `boolean` type its default value meant to be `false`.
345
351
 
@@ -400,6 +406,6 @@ The `options` parameter is an `object` that contains the following set of parame
400
406
  |:---|---|---:|:---|
401
407
  |`useClear`|`boolean`|`true`|if set to `true`, before the load of the items, the list is cleared|
402
408
  |`load_as_new`|`boolean`|`true`|*will deprecate (use `useClear` instead)*|
403
- |`force`|`boolean`|`false`|if set to `true`, the item which is allready present will be replaced with a new one|
409
+ |`force`|`boolean`|`false`|if set to `true`, the item which is already present will be replaced with a new one|
404
410
 
405
411
  If the `options` parameter is a `boolean` value it treats as a `options.useClear` option.
@@ -1,6 +1,6 @@
1
- >|***rev.*:**|0.1.3|
1
+ >|***rev.*:**|0.1.4|
2
2
  >|:---|---:|
3
- >|date:|2022-09-03|
3
+ >|date:|2022-09-22|
4
4
 
5
5
  ## Introduction
6
6
 
@@ -116,3 +116,7 @@ The `options` structure has listed below:
116
116
  If succeed, the function returned `HTMLElement` and `null` if failed.
117
117
 
118
118
  This function make all the jobs under the hood. So there is no need to call DOM-object directly in many situations.
119
+
120
+ #### **valueToIDString(value)**
121
+
122
+ This function tries to convert a given `value` to `ID_STRING`. If failed an empty string is returned.
package/index.js CHANGED
@@ -1,4 +1,4 @@
1
- // [v0.1.009-20220912]
1
+ // [v0.1.010-20220922]
2
2
 
3
3
  // === module init block ===
4
4
 
@@ -43,6 +43,7 @@ exports.BTS_DEF_GROUP_NAME = html_bts.BTS_DEF_GROUP_NAME;
43
43
  exports.THtmlListButtonsController = html_list_btn.THtmlListButtonsController;
44
44
 
45
45
  // experimental
46
+ exports.valueToIDString = html_helper.valueToIDString;
46
47
  exports.createNewHtmlElement = html_helper.createNewHtmlElement;
47
48
 
48
49
  exports.classes = {
@@ -51,4 +52,5 @@ exports.classes = {
51
52
  THtmlItemsListController: exports.THtmlItemsListController,
52
53
  THtmlButtonsSet: exports.THtmlButtonsSet,
53
54
  THtmlListButtonsController: exports.THtmlListButtonsController,
55
+ THtmlButtonsControllerARCSet: exports.THtmlButtonsControllerARCSet,
54
56
  };
@@ -1,4 +1,4 @@
1
- // [v0.1.042-20220912]
1
+ // [v0.1.043-20220922]
2
2
 
3
3
  // === module init block ===
4
4
 
@@ -10,6 +10,7 @@ const {
10
10
 
11
11
  const {
12
12
  isHTMLElement, CSS_CLASS_STRING,
13
+ valueToIDString,
13
14
  } = require('./html-helper-lib.js');
14
15
 
15
16
  const {
@@ -53,7 +54,8 @@ class THtmlButtonsSet {
53
54
  constructor(){
54
55
  // define default functions for manipulators
55
56
  const __user_index_getter = (value) => {
56
- return readAsString(value, '', { useTrim: true, numberToString: true });
57
+ //return readAsString(value, '', { useTrim: true, numberToString: true });
58
+ return valueToIDString(value);
57
59
  };
58
60
  const __user_item_getter = (value) => {
59
61
  return THtmlButtonsSet.isHTMLButton(value) ? value : null;
@@ -1,11 +1,10 @@
1
- // [v0.1.036-20220902]
1
+ // [v0.1.044-20220925]
2
2
 
3
3
  // === module init block ===
4
4
 
5
5
  const {
6
- readAsBool, readAsNumber, readAsString,
7
- isEmptyString,
8
- isNotEmptyString,
6
+ readAsBool, readAsNumber,
7
+ valueToIndex,
9
8
  isArray, isObject,
10
9
  isPlainObject,
11
10
  TItemsListEx,
@@ -15,6 +14,7 @@ const {
15
14
  isHTMLElement, isSelectedHTMLElement, isHiddenHTMLElement,
16
15
  showHtmlElement, hideHtmlElement,
17
16
  selectHtmlElement, unselectHtmlElement,
17
+ markHtmlElementAsCurrent, unmarkCurrentHtmlElement,
18
18
  CSS_CLASS_STRING,
19
19
  } = require('./html-helper-lib.js');
20
20
 
@@ -24,10 +24,31 @@ const {
24
24
  CSS_CLASS_SELECTED,
25
25
  CSS_CLASS_DISABLED,
26
26
  CSS_CLASS_HIDDEN,
27
+ CSS_CLASS_CURRENT,
28
+ CSS_CLASS_ACTIVE,
27
29
  } = CSS_CLASS_STRING;
28
30
 
29
31
  // === module extra block (helper functions) ===
30
32
 
33
+ function readAsAttrValue(value){
34
+ let result = null;
35
+ switch (typeof value) {
36
+ case 'boolean':
37
+ result = value.toString();
38
+ break;
39
+ case 'number':
40
+ if (Number.isNaN(value)) break;
41
+ result = value.toString();
42
+ break;
43
+ case 'string':
44
+ result = value.trim();
45
+ break;
46
+ default:
47
+ break;
48
+ };
49
+ return result;
50
+ };
51
+
31
52
  // === module main block (function definitions) ===
32
53
 
33
54
  // === module main block (class definitions) ===
@@ -36,16 +57,33 @@ class THtmlItemsListContainer {
36
57
  #_host = null;
37
58
  #_items = null;
38
59
  #_options = null;
60
+ #_status = null;
39
61
 
40
62
  constructor(host, opt) {
41
- this.#_items = new TItemsListEx();
63
+ // check host
42
64
  this.#_host = isHTMLElement(host) ? host : null;
65
+ // init items container
66
+ const _items = new TItemsListEx();
67
+ this.#_items = _items;
43
68
  // load options
44
69
  const _options = isPlainObject(opt) ? opt : {};
45
- let { autoHideNewItems, } = _options;
46
- if (typeof autoHideNewItems !== 'boolean') autoHideNewItems = false;
70
+ let {
71
+ autoHideNewItems,
72
+ markCurrentItem,
73
+ } = _options;
74
+ if (typeof autoHideNewItems !== 'boolean') {
75
+ _options.autoHideNewItems = autoHideNewItems = false;
76
+ };
77
+ if (typeof markCurrentItem !== 'boolean') {
78
+ _options.markCurrentItem = markCurrentItem = false;
79
+ };
80
+ // init status
81
+ const _status = {
82
+ curIndex: _items.curIndex,
83
+ curItem: _items.curItem,
84
+ };
85
+ this.#_status = _status;
47
86
  // save options
48
- _options.autoHideNewItems = autoHideNewItems;
49
87
  this.#_options = _options;
50
88
  }
51
89
 
@@ -80,7 +118,11 @@ class THtmlItemsListContainer {
80
118
 
81
119
  clear(){
82
120
  this.#_items.clear();
83
- if (this.#_host) this.#_host.innerHTML = '';
121
+ const _status = this.#_status;
122
+ _status.curIndex = this.curIndex;
123
+ _status.curItem = this.curItem;
124
+ const _host = this.#_host;
125
+ if (_host) _host.innerHTML = '';
84
126
  }
85
127
 
86
128
  isEmpty(){
@@ -103,22 +145,20 @@ class THtmlItemsListContainer {
103
145
  return isHTMLElement(item) ? this.#_items.srchIndex(item) : -1;
104
146
  }
105
147
 
106
- srchIndexByAttr(name, value){
107
- let index = -1;
148
+ srchIndexByAttr(name, value = ''){
108
149
  const _name = typeof name === 'string' ? name.trim() : '';
109
- const _value = readAsString(value, '', {
110
- useTrim: true,
111
- numberToString: true,
112
- boolToString: true,
113
- });
114
- if (_name !== '' && _value !== '') {
150
+ const _value = readAsAttrValue(value);
151
+ let index = -1;
152
+ if (_name !== '' && _value !== null) {
153
+ const acceptAnyVal = _value === '';
154
+ const _items = this.#_items;
115
155
  let item = null;
116
- for (let i = 0; i < this.count; i++) {
117
- item = this.getItem(i);
156
+ for (let i = 0; i < _items.count; i++) {
157
+ item = _items.getItem(i);
118
158
  if (
119
159
  isHTMLElement(item)
120
160
  && item.hasAttribute(_name)
121
- && item.getAttribute(_name) === _value
161
+ && (acceptAnyVal || item.getAttribute(_name) === _value)
122
162
  ) {
123
163
  index = i;
124
164
  break;
@@ -133,11 +173,29 @@ class THtmlItemsListContainer {
133
173
  };
134
174
 
135
175
  setCurIndex(index){
136
- return this.#_items.setIndex(index);
176
+ const isSUCCEED = this.#_items.setIndex(index);
177
+ if (isSUCCEED) {
178
+ const markCurrentItem = this.#_options.markCurrentItem;
179
+ const _status = this.#_status;
180
+ if (markCurrentItem && _status.curIndex !== -1) {
181
+ unmarkCurrentHtmlElement(_status.curItem);
182
+ };
183
+ const item = this.getItem(index);
184
+ _status.curIndex = Number(index);
185
+ _status.curItem = item;
186
+ if (markCurrentItem) markHtmlElementAsCurrent(item);
187
+ };
188
+ return isSUCCEED;
137
189
  }
138
190
 
139
191
  rstCurIndex(){
192
+ const _status = this.#_status;
193
+ if (this.#_options.markCurrentItem && _status.curIndex !== -1) {
194
+ unmarkCurrentHtmlElement(_status.curItem);
195
+ };
140
196
  this.#_items.rstIndex();
197
+ _status.curIndex = this.#_items.curIndex;
198
+ _status.curItem = this.curItem;
141
199
  }
142
200
 
143
201
  getItem(index){
@@ -145,59 +203,49 @@ class THtmlItemsListContainer {
145
203
  return isHTMLElement(item) ? item : null;
146
204
  }
147
205
 
148
- getItemByAttr(name, value = ''){
149
- let _name = '';
150
- let item = null;
151
- let index = -1;
152
- value = readAsString(value, '', {
153
- useTrim: true,
154
- numberToString: true,
155
- boolToString: true,
156
- });
157
- if (typeof name === 'string' && ((_name = name.trim()) !== '')){
158
- const useAnyName = isEmptyString(name);
159
- const useAnyVal = isEmptyString(value);
160
- for (let i = 0; i < this.count; i++) {
161
- item = this.getItem(i);
162
- if (
163
- isHTMLElement(item)
164
- && item.hasAttribute(_name)
165
- && (useAnyVal || item.getAttribute(_name) === value)
166
- ) {
167
- index = i;
168
- break;
169
- };
170
- };
171
- };
172
- return index !== -1 ? item : null;
206
+ getItemByAttr(...args){
207
+ return this.getItem(this.srchIndexByAttr(...args));
173
208
  }
174
209
 
175
210
  getItemByID(value){
176
- return this.getItemByAttr('id', value);
211
+ return this.getItem(this.srchIndexByID(value));
177
212
  }
178
213
 
179
214
  addItem(item, opt){
180
- let index = isHTMLElement(item) ? this.#_items.addItemEx(item, opt) : -1;
181
- let isSUCCEED = index !== -1;
215
+ const forceCI = typeof opt === 'boolean' ? opt : false;
216
+ const index = (
217
+ isHTMLElement(item)
218
+ ? this.#_items.addItemEx(item, false)
219
+ : -1
220
+ );
221
+ const isSUCCEED = index !== -1;
182
222
  if (isSUCCEED) {
183
223
  if (this.#_options.autoHideNewItems) hideHtmlElement(item);
184
- if (this.#_host) this.#_host.appendChild(item);
224
+ if (forceCI) this.setCurIndex(index);
225
+ if (this.#_host) this.#_host.append(item);
185
226
  };
186
227
  return isSUCCEED ? index : -1;
187
228
  }
188
229
 
189
230
  delItem(index, opt){
190
- let item = this.#_items.delItemEx(index, opt);
191
- let isSUCCEED = isHTMLElement(item);
192
- if (isSUCCEED && this.#_host && this.#_host.contains(item)) {
193
- this.#_host.removeChild(item);
231
+ const forceCI = typeof opt === 'boolean' ? opt : true;
232
+ const item = this.#_items.delItemEx(index, forceCI);
233
+ const isSUCCEED = isHTMLElement(item);
234
+ if (isSUCCEED) {
235
+ const _status = this.#_status;
236
+ const curItem = this.curItem;
237
+ _status.curIndex = this.#_items.curIndex;
238
+ _status.curItem = curItem;
239
+ if (this.#_options.markCurrentItem) markHtmlElementAsCurrent(curItem);
240
+ if (this.#_host) item.remove();
194
241
  };
195
242
  return isSUCCEED;
196
243
  }
197
244
 
198
245
  selectItem(index, opt){
199
- let isSUCCEED = selectHtmlElement(this.#_items.getItem(index));
200
- if (isSUCCEED && readAsBool(opt, false)) this.#_items.setIndex(index);
246
+ const forceCI = typeof opt === 'boolean' ? opt : false;
247
+ const isSUCCEED = selectHtmlElement(this.#_items.getItem(index));
248
+ if (isSUCCEED && forceCI) this.setCurIndex(index);
201
249
  return isSUCCEED;
202
250
  }
203
251
 
@@ -227,7 +275,6 @@ class THtmlItemsListController extends THtmlItemsListContainer {
227
275
  #_host = null;
228
276
  #_stubs = null;
229
277
  #_selects = null;
230
- #_visibles = null;
231
278
  #_options = null;
232
279
  #_status = null;
233
280
  #_events = null;
@@ -242,21 +289,23 @@ class THtmlItemsListController extends THtmlItemsListContainer {
242
289
  super(_host, _options);
243
290
  // load options
244
291
  let {
245
- autoHideNewItems,
292
+ //autoHideNewItems, // [*] processed by parent
293
+ //markCurrentItem, // [*] processed by parent
246
294
  showStubsIfEmpty,
247
295
  allowGroupSelection,
248
296
  allowSelectionLocks,
249
297
  stubs: stubsList,
250
298
  } = _options;
251
- if (typeof autoHideNewItems !== 'boolean') autoHideNewItems = false;
252
- if (typeof showStubsIfEmpty !== 'boolean') showStubsIfEmpty = false;
253
- if (typeof allowGroupSelection !== 'boolean') allowGroupSelection = false;
254
- if (typeof allowSelectionLocks !== 'boolean') allowSelectionLocks = false;
299
+ if (typeof showStubsIfEmpty !== 'boolean') {
300
+ _options.showStubsIfEmpty = showStubsIfEmpty = false;
301
+ };
302
+ if (typeof allowGroupSelection !== 'boolean') {
303
+ _options.allowGroupSelection = allowGroupSelection = false;
304
+ };
305
+ if (typeof allowSelectionLocks !== 'boolean') {
306
+ _options.allowSelectionLocks = allowSelectionLocks = false;
307
+ };
255
308
  // save options
256
- _options.autoHideNewItems = autoHideNewItems;
257
- _options.showStubsIfEmpty = showStubsIfEmpty;
258
- _options.allowGroupSelection = allowGroupSelection;
259
- _options.allowSelectionLocks = allowSelectionLocks;
260
309
  this.#_options = _options;
261
310
  // init status flags set
262
311
  const _status = {
@@ -271,8 +320,6 @@ class THtmlItemsListController extends THtmlItemsListContainer {
271
320
  this.#_stubs = new THtmlStubItemsSet(_host, stubsList);
272
321
  // init index of selected items
273
322
  this.#_selects = new Set();
274
- // init index of visible items
275
- this.#_visibles = new Set();
276
323
  // init events controller
277
324
  this.#_events = new Map();
278
325
  if (isHostEnabled) {
@@ -378,8 +425,10 @@ class THtmlItemsListController extends THtmlItemsListContainer {
378
425
  };
379
426
 
380
427
  get SelectedItems(){
428
+ const _selects = this.#_selects;
429
+ // // TODO: consider to use: `[...<value>]`
381
430
  let result = [];
382
- for (let item of this.#_selects) {
431
+ for (let item of _selects) {
383
432
  if (item) result.push(item);
384
433
  };
385
434
  return result;
@@ -397,7 +446,6 @@ class THtmlItemsListController extends THtmlItemsListContainer {
397
446
  // // TODO: clear event handler on elements if set
398
447
  super.clear();
399
448
  this.#_selects.clear();
400
- this.#_visibles.clear();
401
449
  if (this.#_options.showStubsIfEmpty) {
402
450
  // show default stub-item
403
451
  this.#_status.isStubItemShown = this.#_stubs.showDefItem();
@@ -425,24 +473,22 @@ class THtmlItemsListController extends THtmlItemsListContainer {
425
473
  }
426
474
 
427
475
  addItem(item, opt){
428
- let index = super.addItem(item, false);
429
- let newItem = index !== -1 ? super.getItem(index) : null;
430
- if (newItem) {
431
- const _options = this.#_options;
476
+ const index = super.addItem(item, false);
477
+ if (index !== -1) {
478
+ const { autoHideNewItems, showStubsIfEmpty } = this.#_options;
432
479
  const _status = this.#_status;
433
- if (!_status.catchEventOnHost) {
480
+ const { catchEventOnHost, isStubItemShown } = _status;
481
+ if (!catchEventOnHost) {
434
482
  // set event handler on element if it is not set on host
435
- newItem.addEventListener('click', this.#_on_will_select_item);
483
+ item.addEventListener('click', this.#_on_will_select_item);
436
484
  };
437
485
  // TODO: consider if a "boolean" <opt> is enough
438
- let setItemAsCur = typeof opt === 'boolean' ? opt : false;
439
- if (_options.autoHideNewItems && !setItemAsCur) {
440
- super.hideItem(index);
441
- } else {
486
+ const forceCI = typeof opt === 'boolean' ? opt : false;
487
+ if (!autoHideNewItems) {
442
488
  if (index === 0) {
443
489
  if (
444
- _options.showStubsIfEmpty
445
- && _status.isStubItemShown
490
+ showStubsIfEmpty
491
+ && isStubItemShown
446
492
  && this.#_stubs.hideDefItem()
447
493
  ) {
448
494
  // hide default stub-item
@@ -450,15 +496,15 @@ class THtmlItemsListController extends THtmlItemsListContainer {
450
496
  };
451
497
  this.#_triggerEvent('first-item-added', {
452
498
  index: index,
453
- item: newItem,
499
+ item: item,
454
500
  });
455
501
  };
456
502
  this.#_triggerEvent('item-added', {
457
503
  index: index,
458
- item: newItem,
504
+ item: item,
459
505
  });
460
506
  };
461
- if (setItemAsCur) this.setCurIndex(index);
507
+ if (forceCI) this.setCurIndex(index);
462
508
  };
463
509
  return index;
464
510
  }
@@ -474,7 +520,6 @@ class THtmlItemsListController extends THtmlItemsListContainer {
474
520
  item.removeEventListener('click', this.#_on_will_select_item);
475
521
  };
476
522
  this.#_selects.delete(item);
477
- this.#_visibles.delete(item);
478
523
  if (this.isEmpty()) {
479
524
  if (_options.showStubsIfEmpty) {
480
525
  // show default stub-item
@@ -492,7 +537,7 @@ class THtmlItemsListController extends THtmlItemsListContainer {
492
537
  }
493
538
 
494
539
  selectItem(index, opt){
495
- let item = super.getItem(index);
540
+ const item = super.getItem(index);
496
541
  let isSUCCEED = selectHtmlElement(item);
497
542
  if (isSUCCEED) {
498
543
  this.#_selects.add(item);
@@ -500,13 +545,14 @@ class THtmlItemsListController extends THtmlItemsListContainer {
500
545
  index: Number(index),
501
546
  item: item,
502
547
  });
503
- if (readAsBool(opt, false)) isSUCCEED = this.setCurIndex(index);
548
+ const forceCI = typeof opt === 'boolean' ? opt : false;
549
+ if (forceCI) isSUCCEED = this.setCurIndex(index);
504
550
  };
505
551
  return isSUCCEED;
506
552
  }
507
553
 
508
554
  unselectItem(index){
509
- let item = super.getItem(index);
555
+ const item = super.getItem(index);
510
556
  let isSUCCEED = unselectHtmlElement(item);
511
557
  if (isSUCCEED) {
512
558
  this.#_selects.delete(item);
@@ -519,10 +565,9 @@ class THtmlItemsListController extends THtmlItemsListContainer {
519
565
  }
520
566
 
521
567
  hideItem(index){
522
- let item = super.getItem(index);
568
+ const item = super.getItem(index);
523
569
  let isSUCCEED = hideHtmlElement(item);
524
570
  if (isSUCCEED) {
525
- this.#_visibles.delete(item);
526
571
  this.#_triggerEvent('item-hidden', {
527
572
  index: Number(index),
528
573
  item: item,
@@ -532,10 +577,9 @@ class THtmlItemsListController extends THtmlItemsListContainer {
532
577
  }
533
578
 
534
579
  showItem(index){
535
- let item = super.getItem(index);
580
+ const item = super.getItem(index);
536
581
  let isSUCCEED = showHtmlElement(item);
537
582
  if (isSUCCEED) {
538
- this.#_visibles.add(item);
539
583
  this.#_triggerEvent('item-shown', {
540
584
  index: Number(index),
541
585
  item: item,
@@ -554,7 +598,6 @@ class THtmlItemsListController extends THtmlItemsListContainer {
554
598
  /* NOTE:
555
599
  * for current you can't reset or set new one on the same event
556
600
  */
557
- //console.log('THtmlItemsListController.on() ==> event:['+name+'] - already exists...');
558
601
  };
559
602
  };
560
603
  }
@@ -1,4 +1,4 @@
1
- // [v0.1.013-20220903]
1
+ // [v0.1.014-20220922]
2
2
 
3
3
  // === module init block ===
4
4
 
@@ -134,6 +134,23 @@ function valueToClassList(value, opt){
134
134
  return result;
135
135
  };
136
136
 
137
+ function valueToIDString(value){
138
+ let result = '';
139
+ switch (typeof value) {
140
+ case 'number':
141
+ if (Number.isNaN(value) || value < 0) break;
142
+ if (Number.isInteger(value)) result = value.toString();
143
+ break;
144
+ case 'string':
145
+ result = value.trim();
146
+ // // TODO: check if a valid character sequencies
147
+ break;
148
+ default:
149
+ break;
150
+ };
151
+ return result;
152
+ };
153
+
137
154
  function createNewHtmlElement(tagName, opt){
138
155
  let _tag = typeof tagName === 'string' ? tagName.trim() : '';
139
156
  if (_tag === '') return null;
@@ -147,8 +164,7 @@ function createNewHtmlElement(tagName, opt){
147
164
  data: dset,
148
165
  } = opt;
149
166
  // set an element id
150
- id = typeof id === 'string' ? id.trim() : '';
151
- if (id !== '') item.setAttribute('id', id);
167
+ if ((id = valueToIDString(id)) !== '') item.setAttribute('id', id);
152
168
  // set an element text context
153
169
  if (typeof text === 'string') {
154
170
  item.appendChild(document.createTextNode(text));
@@ -226,3 +242,4 @@ exports.valueToClassList = valueToClassList;
226
242
 
227
243
  // experimental
228
244
  exports.createNewHtmlElement = createNewHtmlElement;
245
+ exports.valueToIDString = valueToIDString;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cntwg/html-helper",
3
- "version": "0.0.14",
3
+ "version": "0.0.15",
4
4
  "description": "A base HTML-helper library for js",
5
5
  "author": "ygracs <cs70th-om@rambler.ru>",
6
6
  "license": "MIT",
@@ -40,6 +40,7 @@
40
40
  "@ygracs/bsfoc-lib-js": "^0.1.2"
41
41
  },
42
42
  "devDependencies": {
43
- "jest": "^27.5.1"
43
+ "jest": "^29.0.3",
44
+ "jest-environment-jsdom": "^29.0.3"
44
45
  }
45
46
  }
package/CHANGELOG.md DELETED
@@ -1,123 +0,0 @@
1
- #### *v0.0.14*
2
-
3
- Pre-release version.
4
-
5
- > - `html-ctrls-list.md` updated;
6
- > - some fixes in `html-ctrls/list-stubs.js` module;
7
- > - alter behavior for the `loadItems` method of a `THtmlStubItemsSet` class in case when its `options` parameter is set to `boolean` (see `html-ctrls-list.md` for details);
8
- > - added method `rstDefItem` to `THtmlStubItemsSet` class;
9
- > - added `THtmlButtonsControllerARCSet` class;
10
-
11
- #### *v0.0.13*
12
-
13
- Pre-release version.
14
-
15
- > - `html-helper-lib.md` updated;
16
- > - `html-ctrls-btn.md` updated;
17
- > - `html-ctrls-list.md` updated;
18
- > - some fixes in `html-helper-lib.js` module;
19
- > - some fixes in `html-ctrls-btn.js` module;
20
- > - some fixes in `html-ctrls-list.js` module;
21
- > - move definition of `THtmlStubItemsSet` from `html-ctrls-list.js` module to `html-ctrls/lists-stubs.js` submodule;
22
- > - add function `valueToClassList` into `html-helper-lib.js` module;
23
- > - updated dependency on `@ygracs/bsfoc-lib-js` module to v0.1.2.
24
-
25
- #### *v0.0.12*
26
-
27
- Pre-release version.
28
-
29
- > - updated dependency on `@ygracs/bsfoc-lib-js` module to v0.1.1.
30
-
31
- #### *v0.0.11*
32
-
33
- Pre-release version.
34
-
35
- > - `html-ctrls-btn.md` updated;
36
- > - `html-ctrls-list.md` updated;
37
- > - some fixes in `html-ctrls-list.js` module;
38
- > - some fixes in `html-ctrls-btn.js` module.
39
-
40
- #### *v0.0.10*
41
-
42
- Pre-release version.
43
-
44
- > - `html-ctrls-btn.md` updated;
45
- > - added `THtmlListButtonsController` class;
46
-
47
- #### *v0.0.9*
48
-
49
- Pre-release version.
50
-
51
- > - switch dependency from `@cntwg/bsfoc-lib-js` module to `@ygracs/bsfoc-lib-js` module.
52
-
53
- #### *v0.0.8*
54
-
55
- Pre-release version.
56
-
57
- > - updated dependency on `@cntwg/bsfoc-lib-js` module to v0.0.11.
58
-
59
- #### *v0.0.7*
60
-
61
- Pre-release version.
62
-
63
- > - updated dependency on `@cntwg/bsfoc-lib-js` module to v0.0.10;
64
- > - some fixes in `html-helper-lib.js` module;
65
- > - some fixes in `html-ctrls-list.js` module.
66
-
67
- #### *v0.0.6*
68
-
69
- Pre-release version.
70
-
71
- > - `html-ctrls-btn.md` updated;
72
- > - `html-ctrls-list.md` updated;
73
- > - `html-helper-lib.md` updated;
74
- > - some fixes in `html-ctrls-btn.js` module;
75
- > - some fixes in `html-ctrls-list.js` module.
76
-
77
- #### *v0.0.5*
78
-
79
- Pre-release version.
80
-
81
- > - `$module/test/README.md` added;
82
- > - updated dependency on `@cntwg/bsfoc-lib-js` module to v0.0.8;
83
- > - `html-ctrls-list.md` updated;
84
- > - added `minIndex` property to `THtmlItemsListContainer` class;
85
- > - some fixes in `html-ctrls-list.js` module;
86
- > - added 'classes' property for module exports;
87
- > - MIT-license applied.
88
-
89
- #### *v0.0.4*
90
-
91
- Pre-release version.
92
-
93
- > - updated dependency on `@cntwg/bsfoc-lib-js` module to v0.0.5.
94
-
95
- #### *v0.0.3*
96
-
97
- Pre-release version.
98
-
99
- > - added `activateHTMLElements` function;
100
- > - added `inactivateHTMLElements` function;
101
- > - added `createNewHtmlElement` function (experimental);
102
- > - `html-helper-lib.md` updated;
103
- > - `html-ctrls-btn.md` updated;
104
- > - `html-ctrls-list.md` updated.
105
-
106
- #### *v0.0.2*
107
-
108
- Pre-release version.
109
-
110
- > - `README.md` added;
111
- > - `html-helper-lib.md` added to `doc`;
112
- > - `html-ctrls-btn.md` added to `doc`;
113
- > - `html-ctrls-list.md` added to `doc`;
114
- > - added `CSS_CLASS_ACTIVE` constant to `CSS_CLASS_STRING`;
115
- > - added `isActiveHTMLElement` function;
116
- > - added `markHtmlElementAsActive` function;
117
- > - added `unmarkActiveHtmlElement` function;
118
- > - `html-btn-ctrl.js` was renamed `html-ctrls-btn.js`;
119
- > - `html-list-ctrl.js` was renamed `html-ctrls-list.js`.
120
-
121
- #### *v0.0.1*
122
-
123
- Pre-release version.