@cntwg/html-helper 0.0.14
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.
- package/CHANGELOG.md +123 -0
- package/LICENSE +9 -0
- package/README.md +32 -0
- package/doc/html-ctrls-btn.md +222 -0
- package/doc/html-ctrls-list.md +405 -0
- package/doc/html-helper-lib.md +118 -0
- package/index.js +54 -0
- package/lib/html-ctrls/lists-btns.js +277 -0
- package/lib/html-ctrls/lists-stubs.js +185 -0
- package/lib/html-ctrls-btn.js +341 -0
- package/lib/html-ctrls-list.js +568 -0
- package/lib/html-helper-lib.js +228 -0
- package/package.json +45 -0
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
// [v0.1.042-20220912]
|
|
2
|
+
|
|
3
|
+
// === module init block ===
|
|
4
|
+
|
|
5
|
+
const {
|
|
6
|
+
readAsString,
|
|
7
|
+
readAsNumber,
|
|
8
|
+
isNotEmptyString,
|
|
9
|
+
isPlainObject,
|
|
10
|
+
valueToArray,
|
|
11
|
+
TItemsICollection,
|
|
12
|
+
} = require('@ygracs/bsfoc-lib-js');
|
|
13
|
+
|
|
14
|
+
const {
|
|
15
|
+
CSS_CLASS_STRING,
|
|
16
|
+
isHTMLElement,
|
|
17
|
+
} = require('../html-helper-lib.js');
|
|
18
|
+
|
|
19
|
+
const {
|
|
20
|
+
isHTMLButton,
|
|
21
|
+
THtmlButtonsSet,
|
|
22
|
+
} = require('../html-ctrls-btn');
|
|
23
|
+
|
|
24
|
+
const {
|
|
25
|
+
CSS_CLASS_SELECTED,
|
|
26
|
+
CSS_CLASS_DISABLED,
|
|
27
|
+
CSS_CLASS_HIDDEN,
|
|
28
|
+
} = CSS_CLASS_STRING;
|
|
29
|
+
|
|
30
|
+
const BTS_DEF_GROUP_NAME = 'all';
|
|
31
|
+
|
|
32
|
+
// === module extra block (function definitions) ===
|
|
33
|
+
|
|
34
|
+
// === module main block (function definitions) ===
|
|
35
|
+
|
|
36
|
+
// === module main block (class definitions) ===
|
|
37
|
+
|
|
38
|
+
class THtmlListButtonsController {
|
|
39
|
+
#_btnSet = null;
|
|
40
|
+
#_events = null;
|
|
41
|
+
|
|
42
|
+
constructor(opt) {
|
|
43
|
+
// load options
|
|
44
|
+
const _options = isPlainObject(opt) ? opt : {};
|
|
45
|
+
// load controls
|
|
46
|
+
let {
|
|
47
|
+
btnFirst,
|
|
48
|
+
btnPrev,
|
|
49
|
+
btnNext,
|
|
50
|
+
btnLast,
|
|
51
|
+
btn_frst, //* will deprecate
|
|
52
|
+
btn_prev, //* will deprecate
|
|
53
|
+
btn_next, //* will deprecate
|
|
54
|
+
btn_last, //* will deprecate
|
|
55
|
+
} = _options;
|
|
56
|
+
if (btnFirst === undefined) btnFirst = btn_frst;
|
|
57
|
+
if (btnPrev === undefined) btnPrev = btn_prev;
|
|
58
|
+
if (btnNext === undefined) btnNext = btn_next;
|
|
59
|
+
if (btnLast === undefined) btnLast = btn_last;
|
|
60
|
+
if (!isHTMLButton(btnFirst)) btnFirst = null;
|
|
61
|
+
if (!isHTMLButton(btnPrev)) btnPrev = null;
|
|
62
|
+
if (!isHTMLButton(btnNext)) btnNext = null;
|
|
63
|
+
if (!isHTMLButton(btnLast)) btnLast = null;
|
|
64
|
+
const controls = {
|
|
65
|
+
btnFirst,
|
|
66
|
+
btnPrev,
|
|
67
|
+
btnNext,
|
|
68
|
+
btnLast,
|
|
69
|
+
};
|
|
70
|
+
// init buttons set
|
|
71
|
+
const _btnSet = new THtmlButtonsSet();
|
|
72
|
+
this.#_btnSet = _btnSet;
|
|
73
|
+
_btnSet.addGroup('move_frwd');
|
|
74
|
+
_btnSet.addGroup('move_bkwd');
|
|
75
|
+
_btnSet.addGroup('dsbl_ctrls');
|
|
76
|
+
if (btnFirst && btnLast) {
|
|
77
|
+
_btnSet.addItem('btn_frst', btnFirst, 'move_bkwd');
|
|
78
|
+
_btnSet.addItem('btn_last', btnLast, 'move_frwd');
|
|
79
|
+
btnFirst.addEventListener('click', this.#_on_btn_frst_pressed);
|
|
80
|
+
btnLast.addEventListener('click', this.#_on_btn_last_pressed);
|
|
81
|
+
} else {
|
|
82
|
+
_btnSet.addItem('btn_frst', btnFirst, 'dsbl_ctrls');
|
|
83
|
+
_btnSet.addItem('btn_last', btnLast, 'dsbl_ctrls');
|
|
84
|
+
};
|
|
85
|
+
if (btnPrev && btnNext) {
|
|
86
|
+
_btnSet.addItem('btn_prev', btnPrev, 'move_bkwd');
|
|
87
|
+
_btnSet.addItem('btn_next', btnNext, 'move_frwd');
|
|
88
|
+
btnPrev.addEventListener('click', this.#_on_btn_prev_pressed);
|
|
89
|
+
btnNext.addEventListener('click', this.#_on_btn_next_pressed);
|
|
90
|
+
} else {
|
|
91
|
+
_btnSet.addItem('btn_prev', btnPrev, 'dsbl_ctrls');
|
|
92
|
+
_btnSet.addItem('btn_next', btnNext, 'dsbl_ctrls');
|
|
93
|
+
};
|
|
94
|
+
_btnSet.disableGroup('move_frwd');
|
|
95
|
+
_btnSet.disableGroup('move_bkwd');
|
|
96
|
+
_btnSet.disableGroup('dsbl_ctrls');
|
|
97
|
+
// init events controller
|
|
98
|
+
this.#_events = new Map();
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
#_on_btn_frst_pressed = (e) => {
|
|
102
|
+
//console.log('THtmlListButtonsController._on_btn_frst_pressed() ==> was called...');
|
|
103
|
+
//e.preventDefault(); /* need to reconsider reason for use */
|
|
104
|
+
let curItem = null;
|
|
105
|
+
let on_click_num = readAsNumber(e.detail, 0);
|
|
106
|
+
//console.log('CHECK: e => ditail:['+on_click_num+']');
|
|
107
|
+
//console.log('CHECK: e => phase:['+e.eventPhase+']');
|
|
108
|
+
switch (e.eventPhase) {
|
|
109
|
+
//*case 1:
|
|
110
|
+
//* // NOTE: currently on eventPhase = 2 and 3
|
|
111
|
+
case 2:
|
|
112
|
+
/**/// capturing stage
|
|
113
|
+
curItem = e.target;
|
|
114
|
+
break;
|
|
115
|
+
case 3:
|
|
116
|
+
/**/// bubblig stage
|
|
117
|
+
curItem = e.currentTarget;
|
|
118
|
+
break;
|
|
119
|
+
default:
|
|
120
|
+
break;
|
|
121
|
+
};
|
|
122
|
+
if (
|
|
123
|
+
curItem instanceof HTMLElement
|
|
124
|
+
&& (on_click_num === 0 || on_click_num === 1)
|
|
125
|
+
&& !curItem.classList.contains(CSS_CLASS_DISABLED)
|
|
126
|
+
) {
|
|
127
|
+
this.#_triggerEvent('btn-first-pressed', e);
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
#_on_btn_last_pressed = (e) => {
|
|
132
|
+
//console.log('THtmlListButtonsController._on_btn_last_pressed() ==> was called...');
|
|
133
|
+
//e.preventDefault(); /* need to reconsider reason for use */
|
|
134
|
+
let curItem = null;
|
|
135
|
+
let on_click_num = readAsNumber(e.detail, 0);
|
|
136
|
+
//console.log('CHECK: e => ditail:['+on_click_num+']');
|
|
137
|
+
//console.log('CHECK: e => phase:['+e.eventPhase+']');
|
|
138
|
+
switch (e.eventPhase) {
|
|
139
|
+
//*case 1:
|
|
140
|
+
//* // NOTE: currently on eventPhase = 2 and 3
|
|
141
|
+
case 2:
|
|
142
|
+
/**/// capturing stage
|
|
143
|
+
curItem = e.target;
|
|
144
|
+
break;
|
|
145
|
+
case 3:
|
|
146
|
+
/**/// bubblig stage
|
|
147
|
+
curItem = e.currentTarget;
|
|
148
|
+
break;
|
|
149
|
+
default:
|
|
150
|
+
break;
|
|
151
|
+
};
|
|
152
|
+
if (
|
|
153
|
+
curItem instanceof HTMLElement
|
|
154
|
+
&& (on_click_num === 0 || on_click_num === 1)
|
|
155
|
+
&& !curItem.classList.contains(CSS_CLASS_DISABLED)
|
|
156
|
+
) {
|
|
157
|
+
this.#_triggerEvent('btn-last-pressed', e);
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
#_on_btn_prev_pressed = (e) => {
|
|
162
|
+
//console.log('THtmlListButtonsController._on_btn_prev_pressed() ==> was called...');
|
|
163
|
+
//e.preventDefault(); /* need to reconsider reason for use */
|
|
164
|
+
let curItem = null;
|
|
165
|
+
let on_click_num = readAsNumber(e.detail, 0);
|
|
166
|
+
//console.log('CHECK: e => ditail:['+on_click_num+']');
|
|
167
|
+
//console.log('CHECK: e => phase:['+e.eventPhase+']');
|
|
168
|
+
switch (e.eventPhase) {
|
|
169
|
+
//*case 1:
|
|
170
|
+
//* // NOTE: currently on eventPhase = 2 and 3
|
|
171
|
+
case 2:
|
|
172
|
+
/**/// capturing stage
|
|
173
|
+
curItem = e.target;
|
|
174
|
+
break;
|
|
175
|
+
case 3:
|
|
176
|
+
/**/// bubblig stage
|
|
177
|
+
curItem = e.currentTarget;
|
|
178
|
+
break;
|
|
179
|
+
default:
|
|
180
|
+
break;
|
|
181
|
+
};
|
|
182
|
+
if (
|
|
183
|
+
curItem instanceof HTMLElement
|
|
184
|
+
&& (on_click_num === 0 || on_click_num === 1)
|
|
185
|
+
&& !curItem.classList.contains(CSS_CLASS_DISABLED)
|
|
186
|
+
) {
|
|
187
|
+
this.#_triggerEvent('btn-prev-pressed', e);
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
#_on_btn_next_pressed = (e) => {
|
|
192
|
+
//console.log('THtmlListButtonsController._on_btn_next_pressed() ==> was called...');
|
|
193
|
+
//e.preventDefault(); /* need to reconsider reason for use */
|
|
194
|
+
let curItem = null;
|
|
195
|
+
let on_click_num = readAsNumber(e.detail, 0);
|
|
196
|
+
//console.log('CHECK: e => ditail:['+on_click_num+']');
|
|
197
|
+
//console.log('CHECK: e => phase:['+e.eventPhase+']');
|
|
198
|
+
switch (e.eventPhase) {
|
|
199
|
+
//*case 1:
|
|
200
|
+
//* // NOTE: currently on eventPhase = 2 and 3
|
|
201
|
+
case 2:
|
|
202
|
+
/**/// capturing stage
|
|
203
|
+
curItem = e.target;
|
|
204
|
+
break;
|
|
205
|
+
case 3:
|
|
206
|
+
/**/// bubblig stage
|
|
207
|
+
curItem = e.currentTarget;
|
|
208
|
+
break;
|
|
209
|
+
default:
|
|
210
|
+
break;
|
|
211
|
+
};
|
|
212
|
+
if (
|
|
213
|
+
curItem instanceof HTMLElement
|
|
214
|
+
&& (on_click_num === 0 || on_click_num === 1)
|
|
215
|
+
&& !curItem.classList.contains(CSS_CLASS_DISABLED)
|
|
216
|
+
) {
|
|
217
|
+
this.#_triggerEvent('btn-next-pressed', e);
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
#_triggerEvent = (name, ...args) => {
|
|
222
|
+
const _name = typeof name === 'string' ? name.trim() : '';
|
|
223
|
+
if (_name !== '') {
|
|
224
|
+
const _events = this.#_events;
|
|
225
|
+
if (_events.has(_name)) _events.get(name)(...args);
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
disableAll(){
|
|
230
|
+
const _btnSet = this.#_btnSet;
|
|
231
|
+
_btnSet.disableGroup('move_bkwd');
|
|
232
|
+
_btnSet.disableGroup('move_frwd');
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
disableBkwd(){
|
|
236
|
+
this.#_btnSet.disableGroup('move_bkwd');
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
disableFrwd(){
|
|
240
|
+
this.#_btnSet.disableGroup('move_frwd');
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
enableAll(){
|
|
244
|
+
const _btnSet = this.#_btnSet;
|
|
245
|
+
_btnSet.enableGroup('move_bkwd');
|
|
246
|
+
_btnSet.enableGroup('move_frwd');
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
enableBkwd(){
|
|
250
|
+
this.#_btnSet.enableGroup('move_bkwd');
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
enableFrwd(){
|
|
254
|
+
this.#_btnSet.enableGroup('move_frwd');
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
on(name, evnt){
|
|
258
|
+
const _name = typeof name === 'string' ? name.trim() : '';
|
|
259
|
+
if (_name !== '' && typeof evnt === 'function') {
|
|
260
|
+
const _events = this.#_events;
|
|
261
|
+
if (!_events.has(_name)) {
|
|
262
|
+
_events.set(_name, evnt);
|
|
263
|
+
} else {
|
|
264
|
+
/* NOTE:
|
|
265
|
+
* for current you can't reset or set new one on the same event
|
|
266
|
+
*/
|
|
267
|
+
//console.log('THtmlListButtonsController.on() ==> event:['+name+'] - already exists...');
|
|
268
|
+
};
|
|
269
|
+
};
|
|
270
|
+
}
|
|
271
|
+
};
|
|
272
|
+
|
|
273
|
+
// === module exports block ===
|
|
274
|
+
|
|
275
|
+
exports.THtmlListButtonsController = THtmlListButtonsController;
|
|
276
|
+
|
|
277
|
+
exports.BTS_DEF_GROUP_NAME = BTS_DEF_GROUP_NAME;
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
// [v0.1.038-20220912]
|
|
2
|
+
|
|
3
|
+
// === module init block ===
|
|
4
|
+
|
|
5
|
+
const {
|
|
6
|
+
//readAsBool, readAsNumber, readAsString,
|
|
7
|
+
//isEmptyString, isNotEmptyString,
|
|
8
|
+
isArray, isObject, isPlainObject,
|
|
9
|
+
//TItemsListEx,
|
|
10
|
+
} = require('@ygracs/bsfoc-lib-js');
|
|
11
|
+
|
|
12
|
+
const {
|
|
13
|
+
isHTMLElement,
|
|
14
|
+
//isSelectedHTMLElement, isHiddenHTMLElement,
|
|
15
|
+
//showHtmlElement, hideHtmlElement,
|
|
16
|
+
//selectHtmlElement, unselectHtmlElement,
|
|
17
|
+
CSS_CLASS_STRING,
|
|
18
|
+
} = require('../html-helper-lib.js');
|
|
19
|
+
|
|
20
|
+
/*const {
|
|
21
|
+
CSS_CLASS_SELECTED,
|
|
22
|
+
CSS_CLASS_DISABLED,
|
|
23
|
+
CSS_CLASS_HIDDEN,
|
|
24
|
+
} = CSS_CLASS_STRING;*/
|
|
25
|
+
|
|
26
|
+
// === module extra block (helper functions) ===
|
|
27
|
+
|
|
28
|
+
// === module main block (function definitions) ===
|
|
29
|
+
|
|
30
|
+
// === module main block (class definitions) ===
|
|
31
|
+
|
|
32
|
+
class THtmlStubItemsSet {
|
|
33
|
+
#_host = null;
|
|
34
|
+
#_items = null;
|
|
35
|
+
#_defItem = '';
|
|
36
|
+
#_shownItem = null;
|
|
37
|
+
|
|
38
|
+
constructor(host, items) {
|
|
39
|
+
this.#_host = isHTMLElement(host) ? host : null;
|
|
40
|
+
this.#_items = new Map();
|
|
41
|
+
this.#_defItem = '';
|
|
42
|
+
this.#_shownItem = null;
|
|
43
|
+
if (isObject(items)) this.loadItems(items);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
get defItem(){
|
|
47
|
+
return this.#_defItem;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
set defItem(name){
|
|
51
|
+
this.setDefItem(name);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
get count(){
|
|
55
|
+
return this.#_items.size;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
clear(){
|
|
59
|
+
// // TODO: check if any item is shown
|
|
60
|
+
this.#_items.clear();
|
|
61
|
+
this.#_defItem = '';
|
|
62
|
+
this.#_shownItem = null;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
setDefItem(name){
|
|
66
|
+
let isACCEPTED = false;
|
|
67
|
+
if (typeof name === 'string') {
|
|
68
|
+
const _name = name.trim();
|
|
69
|
+
isACCEPTED = this.#_items.has(_name);
|
|
70
|
+
if (isACCEPTED) this.#_defItem = _name;
|
|
71
|
+
};
|
|
72
|
+
return isACCEPTED;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
rstDefItem(){
|
|
76
|
+
// // TODO: check if item is shown
|
|
77
|
+
this.#_defItem = '';
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
hasItem(name){
|
|
81
|
+
const _name = typeof name === 'string' ? name.trim() : '';
|
|
82
|
+
return this.#_items.has(_name);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
hasItems(){
|
|
86
|
+
return this.#_items.size > 0;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
getItem(name){
|
|
90
|
+
const _name = typeof name === 'string' ? name.trim() : '';
|
|
91
|
+
const item = this.#_items.has(_name) ? this.#_items.get(_name) : null;
|
|
92
|
+
return isHTMLElement(item) ? item : null;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
addItem(name, item, opt){
|
|
96
|
+
const _name = typeof name === 'string' ? name.trim() : '';
|
|
97
|
+
let isSUCCEED = false;
|
|
98
|
+
if (_name !== '' && isHTMLElement(item)) {
|
|
99
|
+
const force = typeof opt === 'boolean' ? opt : false;
|
|
100
|
+
if (force || !this.#_items.has(_name)) {
|
|
101
|
+
this.#_items.set(_name, item);
|
|
102
|
+
isSUCCEED = true;
|
|
103
|
+
};
|
|
104
|
+
};
|
|
105
|
+
return isSUCCEED;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
delItem(name){
|
|
109
|
+
let _name = '';
|
|
110
|
+
let isSUCCEED = (
|
|
111
|
+
typeof name === 'string'
|
|
112
|
+
&& ((_name = name.trim()) !== '')
|
|
113
|
+
);
|
|
114
|
+
if (isSUCCEED) {
|
|
115
|
+
this.#_items.delete(_name);
|
|
116
|
+
// // TODO: check if item is shown
|
|
117
|
+
};
|
|
118
|
+
return isSUCCEED;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
showItem(name){
|
|
122
|
+
const item = this.getItem(name);
|
|
123
|
+
const isSUCCEED = this.#_host && item ? true : false;
|
|
124
|
+
if (isSUCCEED) this.#_host.append(item);
|
|
125
|
+
return isSUCCEED;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
showDefItem(){
|
|
129
|
+
return this.showItem(this.#_defItem);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
hideItem(name){
|
|
133
|
+
const item = this.getItem(name);
|
|
134
|
+
const isSUCCEED = this.#_host && item ? true : false;
|
|
135
|
+
if (isSUCCEED && this.#_host.contains(item)) item.remove();
|
|
136
|
+
return isSUCCEED;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
hideDefItem(){
|
|
140
|
+
return this.hideItem(this.#_defItem);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
loadItems(list, opt){
|
|
144
|
+
let _options = {};
|
|
145
|
+
if (typeof opt === 'boolean') {
|
|
146
|
+
_options.useClear = opt;
|
|
147
|
+
} else if (isPlainObject(opt)) {
|
|
148
|
+
_options = opt;
|
|
149
|
+
};
|
|
150
|
+
let {
|
|
151
|
+
defaultItem,
|
|
152
|
+
useClear,
|
|
153
|
+
force,
|
|
154
|
+
load_as_new, //* will deprecate
|
|
155
|
+
} = _options;
|
|
156
|
+
if (useClear === undefined) useClear = load_as_new;
|
|
157
|
+
useClear = typeof useClear === 'boolean' ? useClear : true;
|
|
158
|
+
force = typeof force === 'boolean' ? force : false;
|
|
159
|
+
let _list = list;
|
|
160
|
+
if (isPlainObject(_list)) {
|
|
161
|
+
({ items: _list, defaultItem } = _list);
|
|
162
|
+
};
|
|
163
|
+
const setDefs = defaultItem !== undefined;
|
|
164
|
+
let count = 0;
|
|
165
|
+
if (isArray(_list)) {
|
|
166
|
+
if (useClear) this.clear();
|
|
167
|
+
_list.forEach((obj) => {
|
|
168
|
+
if (isArray(obj)) {
|
|
169
|
+
const [ name, item ] = obj;
|
|
170
|
+
if (this.addItem(name, item, force)) count++;
|
|
171
|
+
} else if (isPlainObject(obj)) {
|
|
172
|
+
const { name, item } = obj;
|
|
173
|
+
if (this.addItem(name, item, force)) count++;
|
|
174
|
+
};
|
|
175
|
+
});
|
|
176
|
+
};
|
|
177
|
+
if (setDefs && count > 0) this.setDefItem(defaultItem);
|
|
178
|
+
return count;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
// === module exports block ===
|
|
184
|
+
|
|
185
|
+
exports.THtmlStubItemsSet = THtmlStubItemsSet;
|