@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,210 +0,0 @@
1
- // [v0.1.054-20250509]
2
-
3
- // === module init block ===
4
-
5
- const {
6
- isPlainObject,
7
- } = require('@ygracs/bsfoc-lib-js');
8
-
9
- const {
10
- CSS_CLASS_STRING,
11
- } = require('../html-helper-lib.js');
12
-
13
- const {
14
- isHTMLButton,
15
- THtmlButtonsSet,
16
- } = require('./buttons.js');
17
-
18
- const {
19
- pushEventHandler, triggerEventHandler,
20
- readOnClickEventInfo,
21
- } = require('./mod-hfunc.js');
22
-
23
- const {
24
- CSS_CLASS_DISABLED,
25
- } = CSS_CLASS_STRING;
26
-
27
- // === module extra block (function definitions) ===
28
-
29
- // === module main block ===
30
-
31
- /***
32
- * (* constant definitions *)
33
- */
34
-
35
- const BTS_DEF_GROUP_NAME = 'all';
36
-
37
- /***
38
- * (* function definitions *)
39
- */
40
-
41
- /***
42
- * (* class definitions *)
43
- */
44
-
45
- /**
46
- * @classdesc This class implements an interface of a pre-defined
47
- * buttons set that is used in pair with a list elements.
48
- * A set provide a buttons: <next>, <previous>, <first> and <last>.
49
- */
50
- class THtmlListButtonsController {
51
- /** @type {THtmlButtonsSet} */
52
- #_btnSet;
53
- /** @type {Map<string, Function>} */
54
- #_events;
55
-
56
- /**
57
- * @typedef {Object} listButtonsSetDesc
58
- * @property {?HTMLElement} btnFirst - button 'move to first'
59
- * @property {?HTMLElement} btnPrev - button 'move to previous'
60
- * @property {?HTMLElement} btnNext - button 'move to next'
61
- * @property {?HTMLElement} btnLast - button 'move to last'
62
- * @description A description for list buttons set.
63
- */
64
-
65
- /**
66
- * @param {listButtonsSetDesc} [opt] - an options
67
- * @description Creates an instance of a buttons set.
68
- */
69
- constructor(opt) {
70
- // load controls
71
- /** @type {listButtonsSetDesc} */
72
- let {
73
- btnFirst,
74
- btnPrev,
75
- btnNext,
76
- btnLast,
77
- } = isPlainObject(opt) ? opt : {};
78
- if (!isHTMLButton(btnFirst)) btnFirst = null;
79
- if (!isHTMLButton(btnPrev)) btnPrev = null;
80
- if (!isHTMLButton(btnNext)) btnNext = null;
81
- if (!isHTMLButton(btnLast)) btnLast = null;
82
- // init buttons set
83
- const _btnSet = new THtmlButtonsSet();
84
- this.#_btnSet = _btnSet;
85
- _btnSet.addGroup('move_frwd');
86
- _btnSet.addGroup('move_bkwd');
87
- _btnSet.addGroup('dsbl_ctrls');
88
- if (btnFirst && btnLast) {
89
- _btnSet.addItem('btn_frst', btnFirst, 'move_bkwd');
90
- _btnSet.addItem('btn_last', btnLast, 'move_frwd');
91
- btnFirst.addEventListener('click', e => this.#_on_btn_pressed(e, 'first'));
92
- btnLast.addEventListener('click', e => this.#_on_btn_pressed(e, 'last'));
93
- } else {
94
- _btnSet.addItem('btn_frst', btnFirst, 'dsbl_ctrls');
95
- _btnSet.addItem('btn_last', btnLast, 'dsbl_ctrls');
96
- };
97
- if (btnPrev && btnNext) {
98
- _btnSet.addItem('btn_prev', btnPrev, 'move_bkwd');
99
- _btnSet.addItem('btn_next', btnNext, 'move_frwd');
100
- btnPrev.addEventListener('click', e => this.#_on_btn_pressed(e, 'prev'));
101
- btnNext.addEventListener('click', e => this.#_on_btn_pressed(e, 'next'));
102
- } else {
103
- _btnSet.addItem('btn_prev', btnPrev, 'dsbl_ctrls');
104
- _btnSet.addItem('btn_next', btnNext, 'dsbl_ctrls');
105
- };
106
- _btnSet.disableGroup('move_frwd');
107
- _btnSet.disableGroup('move_bkwd');
108
- _btnSet.disableGroup('dsbl_ctrls');
109
- // init events controller
110
- this.#_events = new Map();
111
- }
112
-
113
- /**
114
- * @param {object} e - event
115
- * @param {string} key - button ID
116
- * @returns {void}
117
- * @private
118
- */
119
- #_on_btn_pressed = (e, key) => {
120
- //console.log(`THtmlListButtonsController._on_btn_pressed().key(${key}) ==> was called...`);
121
- //e.preventDefault(); /* need to reconsider reason for use */
122
- const { item, onClickNum } = readOnClickEventInfo(e);
123
- if (
124
- item instanceof HTMLElement
125
- && (onClickNum === 0 || onClickNum === 1)
126
- && !item.classList.contains(CSS_CLASS_DISABLED)
127
- ) {
128
- this.#_triggerEvent(`btn-${key}-pressed`, e);
129
- };
130
- }
131
-
132
- /**
133
- * @param {string} name - event name
134
- * @param {...any} args
135
- * @returns {void}
136
- * @private
137
- * @see triggerEventHandler
138
- */
139
- #_triggerEvent = (name, ...args) => {
140
- triggerEventHandler(this.#_events, name, ...args);
141
- }
142
-
143
- /**
144
- * @returns {void}
145
- * @description Disables all buttons.
146
- */
147
- disableAll() {
148
- const _btnSet = this.#_btnSet;
149
- _btnSet.disableGroup('move_bkwd');
150
- _btnSet.disableGroup('move_frwd');
151
- }
152
-
153
- /**
154
- * @returns {void}
155
- * @description Disables all buttons in 'move_bkwd' group.
156
- */
157
- disableBkwd() {
158
- this.#_btnSet.disableGroup('move_bkwd');
159
- }
160
-
161
- /**
162
- * @returns {void}
163
- * @description Disables all buttons in 'move_frwd' group.
164
- */
165
- disableFrwd() {
166
- this.#_btnSet.disableGroup('move_frwd');
167
- }
168
-
169
- /**
170
- * @returns {void}
171
- * @description Enables all buttons.
172
- */
173
- enableAll() {
174
- const _btnSet = this.#_btnSet;
175
- _btnSet.enableGroup('move_bkwd');
176
- _btnSet.enableGroup('move_frwd');
177
- }
178
-
179
- /**
180
- * @returns {void}
181
- * @description Enables all buttons in 'move_bkwd' group.
182
- */
183
- enableBkwd() {
184
- this.#_btnSet.enableGroup('move_bkwd');
185
- }
186
-
187
- /**
188
- * @returns {void}
189
- * @description Enables all buttons in 'move_frwd' group.
190
- */
191
- enableFrwd() {
192
- this.#_btnSet.enableGroup('move_frwd');
193
- }
194
-
195
- /**
196
- * @param {string} name - event name
197
- * @param {func} evnt - callback function
198
- * @returns {void}
199
- * @description Sets a callback function to handle event.
200
- */
201
- on(name, evnt) {
202
- pushEventHandler(this.#_events, name, evnt);
203
- }
204
- };
205
-
206
- // === module exports block ===
207
-
208
- exports.THtmlListButtonsController = THtmlListButtonsController;
209
-
210
- exports.BTS_DEF_GROUP_NAME = BTS_DEF_GROUP_NAME;
@@ -1,297 +0,0 @@
1
- // [v0.1.049-20250505]
2
-
3
- // === module init block ===
4
-
5
- const {
6
- isArray, isObject, isPlainObject,
7
- } = require('@ygracs/bsfoc-lib-js');
8
-
9
- const {
10
- isHTMLElement,
11
- } = require('../html-helper-lib.js');
12
-
13
- // === module extra block (helper functions) ===
14
-
15
- // === module main block ===
16
-
17
- /***
18
- * (* constant definitions *)
19
- */
20
-
21
- /***
22
- * (* function definitions *)
23
- */
24
-
25
- /***
26
- * (* class definitions *)
27
- */
28
-
29
- /**
30
- * @typedef {Object} OPT_ldstubs
31
- * @property {string} [defaultItem] - <*deprecated*> a default element name
32
- * @property {boolean} [useClear=true] - to clear before load an elements
33
- * @property {boolean} [force=false] - run in a force mode
34
- * @description A settings to load a stub-elements.
35
- * @todo [since v0.0.26] `defaultItem`-option deprecated
36
- */
37
-
38
- /**
39
- * @typedef {Object} OBJ_stubEList
40
- * @property {Array} items - array of `name`-`element` pairs
41
- * @property {string} [defaultItem] - a default element name
42
- * @description A stub-elements description list.
43
- */
44
-
45
- /**
46
- * @typedef {Object} OPT_stubconsett
47
- * @property {(any[])} items - array of `name`-`element` pairs
48
- * @property {string} [defaultItem] - a default element name
49
- * @property {boolean} [force=false] - run in a force mode
50
- * @description A an options set for a `THtmlStubItemsSet`-class constructor
51
- */
52
-
53
- /**
54
- * @classdesc This class implements an interface of a stub-items set
55
- */
56
- class THtmlStubItemsSet {
57
- /** @type {?HTMLElement} */
58
- #_host;
59
- /** @type {Map<string, HTMLElement>} */
60
- #_items;
61
- /** @type {string} */
62
- #_defItem;
63
- #_shownItem = null;
64
-
65
- /**
66
- * @param {HTMLElement} host - host element
67
- * @param {(array|OPT_stubconsett|OBJ_stubEList)} [opt] - options
68
- * @description Creates an instance of a stub-items set
69
- * @todo [since v0.0.26] use of an `array`-type as a value of an `opt` is deprecated
70
- */
71
- constructor(host, opt) {
72
- this.#_host = isHTMLElement(host) ? host : null;
73
- this.#_items = new Map();
74
- this.#_defItem = '';
75
- this.#_shownItem = null;
76
- if (isObject(opt)) {
77
- /** @type {(OPT_stubconsett|OBJ_stubEList)} */
78
- const obj = isArray(opt) ? { items: opt } : opt;
79
- const { items, defaultItem, force } = obj;
80
- this.loadItems({
81
- items,
82
- defaultItem,
83
- }, {
84
- force,
85
- });
86
- };
87
- }
88
-
89
- /**
90
- * Defines a default element name
91
- * @type {string}
92
- */
93
- get defItem() {
94
- return this.#_defItem;
95
- }
96
-
97
- set defItem(name) {
98
- this.setDefItem(name);
99
- }
100
-
101
- /**
102
- * Contains a Qty of an elements
103
- * @type {number}
104
- * @readonly
105
- */
106
- get count() {
107
- return this.#_items.size;
108
- }
109
-
110
- /**
111
- * @returns {void}
112
- * @description Clears an instance content.
113
- */
114
- clear() {
115
- // // TODO: check if any item is shown
116
- this.#_items.clear();
117
- this.#_defItem = '';
118
- this.#_shownItem = null;
119
- }
120
-
121
- /**
122
- * @param {string} name - an element name
123
- * @returns {boolean}
124
- * @description Sets a default item.
125
- */
126
- setDefItem(name) {
127
- let isACCEPTED = false;
128
- if (typeof name === 'string') {
129
- const _name = name.trim();
130
- isACCEPTED = this.#_items.has(_name);
131
- if (isACCEPTED) this.#_defItem = _name;
132
- };
133
- return isACCEPTED;
134
- }
135
-
136
- /**
137
- * @returns {void}
138
- * @description Resets a default item.
139
- */
140
- rstDefItem() {
141
- // // TODO: check if item is shown
142
- this.#_defItem = '';
143
- }
144
-
145
- /**
146
- * @param {string} name - an element name
147
- * @returns {boolean}
148
- * @description Checks if an item exists.
149
- */
150
- hasItem(name) {
151
- const _name = typeof name === 'string' ? name.trim() : '';
152
- return this.#_items.has(_name);
153
- }
154
-
155
- /**
156
- * @returns {boolean}
157
- * @description Checks if any items exists.
158
- */
159
- hasItems() {
160
- return this.#_items.size > 0;
161
- }
162
-
163
- /**
164
- * @param {string} name - an element name
165
- * @returns {?HTMLElement}
166
- * @description Returns an item by its name.
167
- */
168
- getItem(name) {
169
- const _name = typeof name === 'string' ? name.trim() : '';
170
- const item = this.#_items.has(_name) ? this.#_items.get(_name) : null;
171
- return isHTMLElement(item) ? item : null;
172
- }
173
-
174
- /**
175
- * @param {string} name - an element name
176
- * @param {HTMLElement} item - an element
177
- * @param {boolean} [opt=false] - flag indicating a force mode
178
- * @returns {boolean}
179
- * @description Adds an item to an instance members.
180
- */
181
- addItem(name, item, opt) {
182
- const _name = typeof name === 'string' ? name.trim() : '';
183
- let isSUCCEED = false;
184
- if (_name !== '' && isHTMLElement(item)) {
185
- const force = typeof opt === 'boolean' ? opt : false;
186
- if (force || !this.#_items.has(_name)) {
187
- this.#_items.set(_name, item);
188
- isSUCCEED = true;
189
- };
190
- };
191
- return isSUCCEED;
192
- }
193
-
194
- /**
195
- * @param {string} name - an element name
196
- * @returns {boolean}
197
- * @description Deletes an item from an instance members.
198
- */
199
- delItem(name) {
200
- let _name = '';
201
- let isSUCCEED = (
202
- typeof name === 'string'
203
- && ((_name = name.trim()) !== '')
204
- );
205
- if (isSUCCEED) {
206
- this.#_items.delete(_name);
207
- // // TODO: check if item is shown
208
- };
209
- return isSUCCEED;
210
- }
211
-
212
- /**
213
- * @param {string} name - an element name
214
- * @returns {boolean}
215
- * @description Shows an item.
216
- */
217
- showItem(name) {
218
- const item = this.getItem(name);
219
- const isSUCCEED = this.#_host && item ? true : false;
220
- if (isSUCCEED) this.#_host.append(item);
221
- return isSUCCEED;
222
- }
223
-
224
- /**
225
- * @returns {boolean}
226
- * @description Shows a default item.
227
- */
228
- showDefItem() {
229
- return this.showItem(this.#_defItem);
230
- }
231
-
232
- /**
233
- * @param {string} name - an element name
234
- * @returns {boolean}
235
- * @description Hides an item.
236
- */
237
- hideItem(name) {
238
- const item = this.getItem(name);
239
- const isSUCCEED = this.#_host && item ? true : false;
240
- if (isSUCCEED && this.#_host.contains(item)) item.remove();
241
- return isSUCCEED;
242
- }
243
-
244
- /**
245
- * @returns {boolean}
246
- * @description Hides a default item.
247
- */
248
- hideDefItem() {
249
- return this.hideItem(this.#_defItem);
250
- }
251
-
252
- /**
253
- * @param {(Array|OBJ_stubEList)} data - an array of entries or special object
254
- * @param {(boolean|OPT_ldstubs)} [opt] - an options
255
- * @returns {number}
256
- * @description Adds an items to an instance members.
257
- * @todo [since 0.0.25] deprecate use of `opt` as `boolean`
258
- * @todo [since v0.0.26] for `OPT_ldstubs` a `defaultItem`-option deprecated
259
- */
260
- loadItems(data, opt) {
261
- /** @type {OBJ_stubEList} */
262
- let {
263
- items,
264
- defaultItem,
265
- } = isPlainObject(data) ? data : { items: data };
266
- /** @type {OPT_ldstubs} */
267
- let _options = isPlainObject(opt) ? opt : { useClear: opt };
268
- let {
269
- useClear,
270
- force,
271
- } = _options;
272
- if (typeof useClear !== 'boolean') useClear = true;
273
- if (typeof force !== 'boolean') force = false;
274
- if (defaultItem === undefined) defaultItem = _options.defaultItem; /* deprecated */
275
- const setDefs = defaultItem !== undefined;
276
- let count = 0;
277
- if (isArray(items)) {
278
- if (useClear) this.clear();
279
- items.forEach((obj) => {
280
- if (isArray(obj)) {
281
- const [ name, item ] = obj;
282
- if (this.addItem(name, item, force)) count++;
283
- } else if (isPlainObject(obj)) {
284
- const { name, item } = obj;
285
- if (this.addItem(name, item, force)) count++;
286
- };
287
- });
288
- };
289
- if (setDefs && count > 0) this.setDefItem(defaultItem);
290
- return count;
291
- }
292
-
293
- };
294
-
295
- // === module exports block ===
296
-
297
- exports.THtmlStubItemsSet = THtmlStubItemsSet;
@@ -1,60 +0,0 @@
1
- // [v0.1.063-20250220]
2
-
3
- // === module init block ===
4
-
5
- const {
6
- pushEventHandler, triggerEventHandler,
7
- } = require('../event-hfunc.js');
8
-
9
- // === module extra block (helper functions) ===
10
-
11
- // === module main block ===
12
-
13
- /***
14
- * (* constant definitions *)
15
- */
16
-
17
- /***
18
- * (* function definitions *)
19
- */
20
-
21
- /**
22
- * @function readOnClickEventInfo
23
- * @param {object}
24
- * @returns {object}
25
- * @inner
26
- */
27
- function readOnClickEventInfo(e) {
28
- let item = null;
29
- const { eventPhase, detail: onClickNum } = e;
30
- //console.log('CHECK: e => ditail:['+onClickNum+']');
31
- //console.log('CHECK: e => phase:['+eventPhase+']');
32
- switch (eventPhase) {
33
- //*case 1:
34
- //* // NOTE: currently on eventPhase = 2 and 3
35
- case 2: {
36
- /**/// capturing stage
37
- item = e.target;
38
- break;
39
- }
40
- case 3: {
41
- /**/// bubblig stage
42
- item = e.currentTarget;
43
- break;
44
- }
45
- default: {}
46
- };
47
- return { item, onClickNum };
48
- };
49
-
50
- /***
51
- * (* class definitions *)
52
- */
53
-
54
- // === module exports block ===
55
-
56
- exports.readOnClickEventInfo = readOnClickEventInfo;
57
-
58
- // re-exported
59
- exports.pushEventHandler = pushEventHandler;
60
- exports.triggerEventHandler = triggerEventHandler;