@cntwg/html-helper 0.0.26 → 0.1.0

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,414 +0,0 @@
1
- // [v0.1.062-20250509]
2
-
3
- // === module init block ===
4
-
5
- const {
6
- isNotEmptyString,
7
- isPlainObject, valueToArray,
8
- } = require('@ygracs/bsfoc-lib-js');
9
-
10
- const {
11
- TItemsICollection,
12
- } = require('@ygracs/lists-lib-js');
13
-
14
- const {
15
- isHTMLElement,
16
- valueToIDString,
17
- lockHTMLElement, unlockHTMLElement,
18
- CSS_CLASS_STRING,
19
- } = require('../html-helper-lib.js');
20
-
21
- const {
22
- pushEventHandler, triggerEventHandler,
23
- readOnClickEventInfo,
24
- } = require('./mod-hfunc.js');
25
-
26
- const {
27
- CSS_CLASS_DISABLED,
28
- } = CSS_CLASS_STRING;
29
-
30
- // === module extra block (helper functions) ===
31
-
32
- // === module main block ===
33
-
34
- /***
35
- * (* constant definitions *)
36
- */
37
-
38
- const BTS_DEF_GROUP_NAME = 'all';
39
-
40
- const etlHTagInputForBtn = new Set([
41
- 'button', 'submit', 'reset', 'image',
42
- ]);
43
-
44
- /***
45
- * (* function definitions *)
46
- */
47
-
48
- /**
49
- * @function isHTMLButton
50
- * @param {any} obj - element to be verified
51
- * @returns {boolean}
52
- * @description Checks if the given object is an HTML-button.
53
- */
54
- function isHTMLButton(obj) {
55
- let result = false;
56
- if (isHTMLElement(obj)) {
57
- switch (obj.tagName.toLowerCase()) {
58
- case 'button': {
59
- result = true;
60
- break;
61
- }
62
- case 'input': {
63
- result = etlHTagInputForBtn.has(obj.type.toLowerCase());
64
- break;
65
- }
66
- default: {}
67
- };
68
- };
69
- return result;
70
- }
71
-
72
- /***
73
- * (* class definitions *)
74
- */
75
-
76
- /**
77
- * @classdesc This class implements an interface of a buttons set
78
- */
79
- class THtmlButtonsSet {
80
- /** @type {TItemsICollection} */
81
- #_btnCol;
82
-
83
- /**
84
- * @description Creates an instance of a buttons set
85
- */
86
- constructor() {
87
- // define default functions for manipulators
88
- const __user_index_getter = (value) => {
89
- return valueToIDString(value);
90
- };
91
- const __user_item_getter = (value) => {
92
- return THtmlButtonsSet.isHTMLButton(value) ? value : null;
93
- };
94
- this.#_btnCol = new TItemsICollection({
95
- userFn: {
96
- checkIndexFn: isNotEmptyString,
97
- readIndexFn: __user_index_getter,
98
- checkItemFn: THtmlButtonsSet.isHTMLButton,
99
- readItemFn: __user_item_getter,
100
- },
101
- });
102
- }
103
-
104
- /**
105
- * Contains a list of an element names
106
- * @type {string[]}
107
- * @readonly
108
- * @see TItemsICollection.ItemNames
109
- */
110
- get ItemNames() {
111
- return this.#_btnCol.ItemNames;
112
- }
113
-
114
- /**
115
- * Contains a list of an elements group names
116
- * @type {string[]}
117
- * @readonly
118
- * @see TItemsICollection.CategoryNames
119
- */
120
- get GroupNames() {
121
- return this.#_btnCol.CategoryNames;
122
- }
123
-
124
- /**
125
- * @param {string} name - element name
126
- * @returns {boolean}
127
- * @see TItemsICollection.hasItem
128
- * @description Checks whether a target item with a given name exists.
129
- */
130
- hasItem(name) {
131
- return this.#_btnCol.hasItem(name);
132
- }
133
-
134
- /**
135
- * @param {string} name - element name
136
- * @param {any} item - element
137
- * @param {...string} [group] - name of a target groups
138
- * @returns {boolean}
139
- * @see TItemsICollection.addItem
140
- * @description Adds buttons to a set members.
141
- */
142
- addItem(...args) {
143
- return this.#_btnCol.addItem(...args);
144
- }
145
-
146
- /**
147
- * @param {string} name - element name
148
- * @param {...string} [group] - name of a target groups
149
- * @returns {boolean}
150
- * @see TItemsICollection.addItemToCategory
151
- * @description Adds button to a listed groups.
152
- */
153
- addItemToGroup(...args) {
154
- return this.#_btnCol.addItemToCategory(...args);
155
- }
156
-
157
- /**
158
- * @param {string} name - element name
159
- * @returns {boolean}
160
- * @see TItemsICollection.delItem
161
- * @description Deletes button from a set members.
162
- */
163
- delItem(name) {
164
- return this.#_btnCol.delItem(name);
165
- }
166
-
167
- /**
168
- * @param {string} name - element name
169
- * @param {...string} [group] - name of a target groups
170
- * @returns {boolean}
171
- * @see TItemsICollection.delItemFromGroup
172
- * @description Deletes button from a listed groups.
173
- */
174
- delItemFromGroup(...args) {
175
- return this.#_btnCol.delItemFromGroup(...args);
176
- }
177
-
178
- /**
179
- * @param {string} name - old name
180
- * @param {string} value - new name
181
- * @returns {boolean}
182
- * @see TItemsICollection.renameItem
183
- * @description Renames button in a set.
184
- */
185
- renameItem(name, value) {
186
- return this.#_btnCol.renameItem(name, value, false);
187
- }
188
-
189
- /**
190
- * @param {string} name - element name
191
- * @returns {void}
192
- * @description Tries to enable button.
193
- */
194
- enableItem(name) {
195
- unlockHTMLElement(this.#_btnCol.getItem(name));
196
- }
197
-
198
- /**
199
- * @param {string} name - element name
200
- * @returns {void}
201
- * @description Tries to disable button.
202
- */
203
- disableItem(name) {
204
- lockHTMLElement(this.#_btnCol.getItem(name));
205
- }
206
-
207
- /**
208
- * @param {string} name - group name
209
- * @returns {boolean}
210
- * @see TItemsICollection.hasCategory
211
- * @description Checks whether a target group with a given name exists.
212
- */
213
- hasGroup(name) {
214
- return this.#_btnCol.hasCategory(name);
215
- }
216
-
217
- /**
218
- * @param {string} name - group name
219
- * @returns {boolean}
220
- * @see TItemsICollection.addCategory
221
- * @description Adds a group with a given name.
222
- */
223
- addGroup(name) {
224
- return this.#_btnCol.addCategory(name);
225
- }
226
-
227
- /**
228
- * @param {string} name - group name
229
- * @returns {boolean}
230
- * @see TItemsICollection.delCategory
231
- * @description Deletes a group with a given name.
232
- */
233
- delGroup(name) {
234
- return this.#_btnCol.delCategory(name);
235
- }
236
-
237
- /**
238
- * @param {string} name - old name
239
- * @param {string} value - new name
240
- * @returns {boolean}
241
- * @see TItemsICollection.renameCategory
242
- * @description Renames group in a set.
243
- */
244
- renameGroup(name, value) {
245
- return this.#_btnCol.renameCategory(name, value, false);
246
- }
247
-
248
- /**
249
- * @param {string} value - group name
250
- * @returns {void}
251
- * @description Enables all buttons in a given group.
252
- */
253
- enableGroup(value) {
254
- const group = this.#_btnCol.getCategory(value);
255
- valueToArray(group).forEach(item => unlockHTMLElement(item));
256
- }
257
-
258
- /**
259
- * @param {string} value - group name
260
- * @returns {void}
261
- * @description Disables all buttons in a given group.
262
- */
263
- disableGroup(value) {
264
- const group = this.#_btnCol.getCategory(value);
265
- valueToArray(group).forEach(item => lockHTMLElement(item));
266
- }
267
-
268
- /**
269
- * Returns a function wich do a validation check.
270
- * @type {isHTMLButton}
271
- * @static
272
- */
273
- static get isHTMLButton() {
274
- return isHTMLButton;
275
- }
276
-
277
- };
278
-
279
- /**
280
- * @classdesc This class implements an interface of a pre-defined buttons set.
281
- * A set provide a three button: <apply>, <reset> and <cancel>.
282
- */
283
- class THtmlButtonsControllerARCSet {
284
- /** @type {THtmlButtonsSet} */
285
- #_btnSet;
286
- /** @type {Map<string, Function>} */
287
- #_events;
288
-
289
- /**
290
- * @typedef {Object} arcButtonsDesc
291
- * @property {?HTMLElement} btnApply - button 'apply'
292
- * @property {?HTMLElement} btnReset - button 'reset'
293
- * @property {?HTMLElement} btnCancel - button 'cancel'
294
- * @description A description for ARC-buttons set.
295
- */
296
-
297
- /**
298
- * @param {arcButtonsDesc} opt - an option
299
- * @description Creates an instance of the buttons set.
300
- */
301
- constructor(opt) {
302
- // load controls
303
- /** @type {arcButtonsDesc} */
304
- let {
305
- btnApply,
306
- btnReset,
307
- btnCancel,
308
- } = isPlainObject(opt) ? opt : {};
309
- if (!isHTMLButton(btnApply)) btnApply = null;
310
- if (!isHTMLButton(btnReset)) btnReset = null;
311
- if (!isHTMLButton(btnCancel)) btnCancel = null;
312
- // init buttons set
313
- const _btnSet = new THtmlButtonsSet();
314
- this.#_btnSet = _btnSet;
315
- _btnSet.addGroup('main');
316
- _btnSet.addGroup('all');
317
- if (btnApply) {
318
- _btnSet.addItem('btn_apply', btnApply, 'main', 'all');
319
- btnApply.addEventListener('click', e => this.#_on_btn_pressed(e, 'apply'));
320
- };
321
- if (btnReset) {
322
- _btnSet.addItem('btn_reset', btnReset, 'main', 'all');
323
- btnReset.addEventListener('click', e => this.#_on_btn_pressed(e, 'reset'));
324
- };
325
- if (btnCancel) {
326
- _btnSet.addItem('btn_cancel', btnCancel, 'all');
327
- btnCancel.addEventListener('click', e => this.#_on_btn_pressed(e, 'cancel'));
328
- };
329
- _btnSet.disableGroup('main');
330
- // init events controller
331
- this.#_events = new Map();
332
- }
333
-
334
- /**
335
- * @param {object} e - event
336
- * @param {string} key - button ID
337
- * @returns {void}
338
- * @private
339
- */
340
- #_on_btn_pressed = (e, key) => {
341
- //console.log(`THtmlListButtonsController._on_btn_pressed().key(${key}) ==> was called...`);
342
- //e.preventDefault(); /* need to reconsider reason for use */
343
- const { item, onClickNum } = readOnClickEventInfo(e);
344
- if (
345
- item instanceof HTMLElement
346
- && (onClickNum === 0 || onClickNum === 1)
347
- && !item.classList.contains(CSS_CLASS_DISABLED)
348
- ) {
349
- this.#_triggerEvent(`btn-${key}-pressed`, e);
350
- };
351
- }
352
-
353
- /**
354
- * @param {string} name - event name
355
- * @param {...any} args
356
- * @returns {void}
357
- * @private
358
- * @see triggerEventHandler
359
- */
360
- #_triggerEvent = (name, ...args) => {
361
- triggerEventHandler(this.#_events, name, ...args);
362
- }
363
-
364
- /**
365
- * @returns {void}
366
- * @description Disables all buttons in 'main' group.
367
- */
368
- disableMain() {
369
- this.#_btnSet.disableGroup('main');
370
- }
371
-
372
- /**
373
- * @returns {void}
374
- * @description Enables all buttons in 'main' group.
375
- */
376
- enableMain() {
377
- this.#_btnSet.enableGroup('main');
378
- }
379
-
380
- /**
381
- * @returns {void}
382
- * @description Disables all buttons.
383
- */
384
- disableAll() {
385
- this.#_btnSet.disableGroup('all');
386
- }
387
-
388
- /**
389
- * @returns {void}
390
- * @description Enables all buttons.
391
- */
392
- enableAll() {
393
- this.#_btnSet.enableGroup('all');
394
- }
395
-
396
- /**
397
- * @param {string} name - event name
398
- * @param {func} evnt - a callback function
399
- * @returns {void}
400
- * @description Sets a callback function to handle event.
401
- */
402
- on(name, evnt) {
403
- pushEventHandler(this.#_events, name, evnt);
404
- }
405
- };
406
-
407
- // === module exports block ===
408
-
409
- module.exports.THtmlButtonsSet = THtmlButtonsSet;
410
- module.exports.THtmlButtonsControllerARCSet = THtmlButtonsControllerARCSet;
411
-
412
- module.exports.BTS_DEF_GROUP_NAME = BTS_DEF_GROUP_NAME;
413
-
414
- module.exports.isHTMLButton = isHTMLButton;