@cntwg/html-helper 0.0.21 → 0.0.23

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.013-20240705]
1
+ // [v0.1.017-20241119]
2
2
 
3
3
  // === module init block ===
4
4
 
@@ -12,7 +12,7 @@ const {
12
12
  isHTMLElement, createNewHtmlElement,
13
13
  valueToIDString, valueToClassList,
14
14
  readAsAttrValue,
15
- showHtmlElement, hideHtmlElement,
15
+ showHTMLElement, hideHTMLElement,
16
16
  } = require('../html-helper-lib.js');
17
17
 
18
18
  const {
@@ -27,8 +27,22 @@ const lpAllowedValues = new Map([
27
27
  [ 'default', 0 ],
28
28
  ]);
29
29
 
30
- function getInputFieldTypeDescr(value){
31
- let fldType = readAsString(value, true);
30
+ /**
31
+ * @typedef {Object} ifcTypeDesc
32
+ * @property {string} type - type
33
+ * @property {string} subtype - subtype
34
+ * @description An input field type description
35
+ */
36
+
37
+ /**
38
+ * @function getInputFieldTypeDescr
39
+ * @param {string} value
40
+ * @returns {ifcTypeDesc}
41
+ * @inner
42
+ * @description Returns field description by a given type.
43
+ */
44
+ function getInputFieldTypeDescr(value) {
45
+ let fldType = readAsString(value, { useTrim: true });
32
46
  if (fldType === '') fldType = 'default';
33
47
  let fldSubType = '';
34
48
  switch (fldType) {
@@ -44,17 +58,24 @@ function getInputFieldTypeDescr(value){
44
58
  case 'field':
45
59
  case 'default': {
46
60
  fldType = 'text';
61
+ fldSubType = 'field';
47
62
  break;
48
63
  }
49
64
  default: {
50
65
  fldType = '';
51
- break;
52
66
  }
53
67
  };
54
68
  return { type: fldType, subtype: fldSubType };
55
69
  };
56
70
 
57
- function getLabelPositionID(value){
71
+ /**
72
+ * @function getLabelPositionID
73
+ * @param {string} value
74
+ * @returns {number}
75
+ * @inner
76
+ * @description Returns label position ID by its name.
77
+ */
78
+ function getLabelPositionID(value) {
58
79
  let key = readAsString(value, true);
59
80
  if (!lpAllowedValues.has(key)) key = 'default';
60
81
  return lpAllowedValues.get(key);
@@ -75,32 +96,53 @@ function getLabelPositionID(value){
75
96
  */
76
97
 
77
98
  /**
78
- * @description This class implements an instance of an input field element
99
+ * @classdesc This class implements an interface of an input field element
79
100
  */
80
101
  class THtmlInputField {
81
- #_host = null;
82
- #_label = null;
83
- #_field = null;
102
+ /** @property {?HTMLElement} */
103
+ #_host;// = null;
104
+ /** @property {?HTMLElement} */
105
+ #_label;// = null;
106
+ /** @property {?HTMLElement} */
107
+ #_field;// = null;
84
108
  #_id = '';
85
109
  #_value = '';
110
+ /** @property {object} */
86
111
  #_options = null;
87
- #_status = null;
88
- #_events = null;
112
+ /**
113
+ * @typedef {Object} statIFCtrl
114
+ * @property {string} fldType
115
+ * @property {string} fldSubType
116
+ * @property {boolean} isReadOnly
117
+ * @property {boolean} isModified
118
+ * @inner
119
+ * @description A controller status
120
+ */
121
+ /** @property {statIFCtrl} */
122
+ #_status;// = null;
123
+ /** @property {Map} */
124
+ #_events;// = null;
89
125
 
90
126
  /**
91
- * @param {ID_STRING} name - <*reserved*>
92
- * @param {object} eholds
93
- * @param {HTMLElement} eholds.host
94
- * @param {HTMLElement} eholds.label
95
- * @param {HTMLElement} eholds.field
127
+ * @typedef {Object} ifcElementsDesc
128
+ * @property {HTMLElement} host - host element
129
+ * @property {HTMLElement} label - label element
130
+ * @property {HTMLElement} field - field element
131
+ * @description A description for input field elements set.
132
+ */
133
+
134
+ /**
135
+ * @param {string} name - <*reserved*>
136
+ * @param {ifcElementsDesc} eholds - elements set
96
137
  * @param {object} [opt]
97
- * @param {ID_STRING} [opt.idPref]
98
- * @param {(string|array)} [opt.baseClassID]
138
+ * @param {string} [opt.idPref]
139
+ * @param {(string|string[])} [opt.baseClassID]
99
140
  * @param {string} [opt.hint]
100
- * @param {object} [opt.type]
101
- * @param {bool} [opt.readonly=true]
141
+ * @param {string} [opt.type]
142
+ * @param {boolean} [opt.readonly=true]
143
+ * @description Creates an instance of an input field element
102
144
  */
103
- constructor(name, eholds, opt){
145
+ constructor(name, eholds, opt) {
104
146
  // load elements
105
147
  const _eholds = isPlainObject(eholds) ? eholds : {};
106
148
  let { host, label, field } = _eholds;
@@ -126,6 +168,7 @@ class THtmlInputField {
126
168
  // save options
127
169
  this.#_options = _options;
128
170
  // save status
171
+ /** @type {statIFCtrl} */
129
172
  this.#_status = {
130
173
  fldType,
131
174
  fldSubType,
@@ -142,31 +185,39 @@ class THtmlInputField {
142
185
  }
143
186
 
144
187
  /**
145
- * @property {bool}
188
+ * @property {boolean}
146
189
  * @readonly
190
+ * @description Indicates whether element value was modified.
147
191
  */
148
- get isModified(){
192
+ get isModified() {
149
193
  return this.#_status.isModified;
150
194
  }
151
195
 
152
196
  /**
197
+ * @since v0.0.23
153
198
  * @property {string}
199
+ * @readonly
200
+ * @description Returns an input element origin type.
154
201
  */
155
- get value(){
202
+ get type() {
203
+ return this.#_status.fldType;
204
+ }
205
+
206
+ /**
207
+ * @property {string}
208
+ * @fires THtmlInputField#value-empty
209
+ * @description Containes an element value.
210
+ */
211
+ get value() {
156
212
  const field = this.#_field;
157
- let result = '';
158
- if (field) {
159
- const { fldType } = this.#_status;
160
- result = fldType === 'checkbox' ? field.checked : field.value;
161
- };
213
+ const result = this.type === 'checkbox' ? field.checked : field.value;
162
214
  return result;
163
215
  }
164
216
 
165
- set value(value){
217
+ set value(value) {
166
218
  const field = this.#_field;
167
219
  if (value !== undefined) {
168
- const { fldType } = this.#_status;
169
- if (fldType === 'checkbox') {
220
+ if (this.type === 'checkbox') {
170
221
  field.checked = value;
171
222
  this.#_status.isModified = false;
172
223
  } else {
@@ -190,9 +241,9 @@ class THtmlInputField {
190
241
  }
191
242
 
192
243
  /**
193
- * @param {ID_STRING}
194
- * @param {...any}
195
- * @returns {none}
244
+ * @param {string} name - event name
245
+ * @param {...any} args
246
+ * @returns {void}
196
247
  * @private
197
248
  * @see triggerEventHandler
198
249
  */
@@ -201,8 +252,8 @@ class THtmlInputField {
201
252
  };
202
253
 
203
254
  /**
204
- * @param {object}
205
- * @returns {none}
255
+ * @param {object} e
256
+ * @returns {void}
206
257
  * @private
207
258
  * @fires THtmlInputField#value-changed
208
259
  */
@@ -213,8 +264,8 @@ class THtmlInputField {
213
264
  // // TODO:
214
265
  /**
215
266
  * @event THtmlInputField#value-changed
216
- * @type {object}
217
- * @property {ID_STRING} type
267
+ * @type {Object}
268
+ * @property {string} type - event type
218
269
  * @property {object} target
219
270
  */
220
271
  this.#_triggerEvent('value-changed', {
@@ -224,8 +275,8 @@ class THtmlInputField {
224
275
  };
225
276
 
226
277
  /**
227
- * @param {object}
228
- * @returns {none}
278
+ * @param {object} e
279
+ * @returns {void}
229
280
  * @private
230
281
  * @fires THtmlInputField#value-empty
231
282
  */
@@ -238,8 +289,8 @@ class THtmlInputField {
238
289
  if (target.value === '') {
239
290
  /**
240
291
  * @event THtmlInputField#value-empty
241
- * @type {object}
242
- * @property {ID_STRING} type
292
+ * @type {Object}
293
+ * @property {string} type - event type
243
294
  * @property {object} target
244
295
  */
245
296
  this.#_triggerEvent('value-empty', {
@@ -250,8 +301,8 @@ class THtmlInputField {
250
301
  };
251
302
 
252
303
  /**
253
- * @param {object}
254
- * @returns {none}
304
+ * @param {object} e
305
+ * @returns {void}
255
306
  * @private
256
307
  */
257
308
  #_switchFieldStatusOnBlur = (e) => {
@@ -261,11 +312,11 @@ class THtmlInputField {
261
312
  };
262
313
 
263
314
  /**
264
- * @param {HTMLElement}
265
- * @returns {bool}
315
+ * @param {HTMLElement} target - target element
316
+ * @returns {boolean}
266
317
  * @description Tries to attach an element to a given target.
267
318
  */
268
- attachTo(target){
319
+ attachTo(target) {
269
320
  const host = this.#_host;
270
321
  let result = false;
271
322
  if (
@@ -279,58 +330,53 @@ class THtmlInputField {
279
330
  }
280
331
 
281
332
  /**
282
- * @param {none}
283
- * @returns {bool}
333
+ * @returns {boolean}
284
334
  * @description Checks if a value of an element is not set.
285
335
  */
286
- isEmpty(){
336
+ isEmpty() {
287
337
  return isEmptyString(this.value);
288
338
  }
289
339
 
290
340
  /**
291
- * @param {none}
292
- * @returns {bool}
341
+ * @returns {boolean}
293
342
  * @description Checks if a value of an element is set.
294
343
  */
295
- isNotEmpty(){
344
+ isNotEmpty() {
296
345
  return isNotEmptyString(this.value);
297
346
  }
298
347
 
299
348
  /**
300
- * @param {none}
301
- * @returns {none}
349
+ * @returns {void}
302
350
  * @description Shows an element.
303
351
  */
304
- show(){
305
- showHtmlElement(this.#_host);
352
+ show() {
353
+ showHTMLElement(this.#_host);
306
354
  }
307
355
 
308
356
  /**
309
- * @param {none}
310
- * @returns {none}
357
+ * @returns {void}
311
358
  * @description Hides an element.
312
359
  */
313
- hide(){
314
- hideHtmlElement(this.#_host);
360
+ hide() {
361
+ hideHTMLElement(this.#_host);
315
362
  }
316
363
 
317
364
  /**
318
- * @param {none}
319
- * @returns {none}
365
+ * @returns {void}
320
366
  * @description not implemented yet.
321
367
  */
322
- clear(){
368
+ clear() {
323
369
  // // TODO:
324
370
  }
325
371
 
326
372
  /**
327
- * @param {ID_STRING}
328
- * @param {object}
373
+ * @param {string} name - <*reserved*>
374
+ * @param {object} opt
329
375
  * @returns {?THtmlInputField}
330
376
  * @static
331
377
  * @description Creates a new element.
332
378
  */
333
- static create(name, opt){
379
+ static create(name, opt) {
334
380
  const eholds = THtmlInputField.createElement(name, opt, true);
335
381
  const { field } = eholds;
336
382
  return (
@@ -341,23 +387,23 @@ class THtmlInputField {
341
387
  }
342
388
 
343
389
  /**
344
- * @param {ID_STRING}
390
+ * @param {string} name
345
391
  * @param {object} [opt]
346
392
  * @param {object} [opt.host]
347
- * @param {ID_STRING} [opt.idPref]
393
+ * @param {string} [opt.idPref]
348
394
  * @param {object} [opt.label]
349
395
  * @param {object} [opt.labelPosition]
350
- * @param {bool} [opt.useDelim]
396
+ * @param {boolean} [opt.useDelim]
351
397
  * @param {object} [opt.baseClassID]
352
398
  * @param {string} [opt.hint]
353
399
  * @param {object} [opt.type]
354
- * @param {bool} [opt.readonly]
355
- * @param {bool} [doRetChild=false]
356
- * @returns {(HTMLElement|object)}
400
+ * @param {boolean} [opt.readonly]
401
+ * @param {boolean} [doRetChild=false]
402
+ * @returns {(HTMLElement|ifcElementsDesc)}
357
403
  * @static
358
404
  * @description Creates a new elements.
359
405
  */
360
- static createElement(name, opt, doRetChild){
406
+ static createElement(name, opt, doRetChild) {
361
407
  const id = valueToIDString(name);
362
408
  const _options = isPlainObject(opt) ? opt : {};
363
409
  let fldSubType = '';
@@ -448,12 +494,12 @@ class THtmlInputField {
448
494
  }
449
495
 
450
496
  /**
451
- * @param {ID_STRING}
452
- * @param {func}
453
- * @returns {none}
497
+ * @param {string} name - event name
498
+ * @param {func} evnt - callback function
499
+ * @returns {void}
454
500
  * @description Sets a callback function to handle event.
455
501
  */
456
- on(name, evnt){
502
+ on(name, evnt) {
457
503
  pushEventHandler(this.#_events, name, evnt);
458
504
  }
459
505