@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.
@@ -0,0 +1,228 @@
1
+ // [v0.1.013-20220903]
2
+
3
+ // === module init block ===
4
+
5
+ const {
6
+ //isEmptyString, isNotEmptyString,
7
+ readAsString,
8
+ //readAsListS,
9
+ isArray, isObject, isPlainObject, valueToArray,
10
+ } = require('@ygracs/bsfoc-lib-js');
11
+
12
+ const CSS_CLASS_CURRENT = 'current';
13
+ const CSS_CLASS_SELECTED = 'selected';
14
+ const CSS_CLASS_ACTIVE = 'active';
15
+ const CSS_CLASS_DISABLED = 'disabled';
16
+ const CSS_CLASS_HIDDEN = 'hidden';
17
+
18
+ // === module extra block (helper functions) ===
19
+
20
+ // === module main block (function definitions) ===
21
+
22
+ function isHTMLElement(obj){ return obj instanceof HTMLElement; };
23
+
24
+ function isSelectedHTMLElement(obj){
25
+ return isHTMLElement(obj) && obj.classList.contains(CSS_CLASS_SELECTED);
26
+ };
27
+
28
+ function isCurrentHTMLElement(obj){
29
+ return isHTMLElement(obj) && obj.classList.contains(CSS_CLASS_CURRENT);
30
+ };
31
+
32
+ function isActiveHTMLElement(obj){
33
+ return isHTMLElement(obj) && obj.classList.contains(CSS_CLASS_ACTIVE);
34
+ };
35
+
36
+ function isHiddenHTMLElement(obj){
37
+ return isHTMLElement(obj) && obj.classList.contains(CSS_CLASS_HIDDEN);
38
+ };
39
+
40
+ function hideHtmlElement(obj){
41
+ const isSUCCEED = isHTMLElement(obj);
42
+ if (isSUCCEED) obj.classList.add(CSS_CLASS_HIDDEN);
43
+ return isSUCCEED;
44
+ };
45
+
46
+ function showHtmlElement(obj){
47
+ const isSUCCEED = isHTMLElement(obj);
48
+ if (isSUCCEED) obj.classList.remove(CSS_CLASS_HIDDEN);
49
+ return isSUCCEED;
50
+ };
51
+
52
+ function selectHtmlElement(obj){
53
+ const isSUCCEED = isHTMLElement(obj);
54
+ if (isSUCCEED) obj.classList.add(CSS_CLASS_SELECTED);
55
+ return isSUCCEED;
56
+ };
57
+
58
+ function unselectHtmlElement(obj){
59
+ const isSUCCEED = isHTMLElement(obj);
60
+ if (isSUCCEED) obj.classList.remove(CSS_CLASS_SELECTED);
61
+ return isSUCCEED;
62
+ };
63
+
64
+ function markHtmlElementAsCurrent(obj){
65
+ const isSUCCEED = isHTMLElement(obj);
66
+ if (isSUCCEED) obj.classList.add(CSS_CLASS_CURRENT);
67
+ return isSUCCEED;
68
+ };
69
+
70
+ function unmarkCurrentHtmlElement(obj){
71
+ const isSUCCEED = isHTMLElement(obj);
72
+ if (isSUCCEED) obj.classList.remove(CSS_CLASS_CURRENT);
73
+ return isSUCCEED;
74
+ };
75
+
76
+ function markHtmlElementAsActive(obj){
77
+ const isSUCCEED = isHTMLElement(obj);
78
+ if (isSUCCEED) obj.classList.add(CSS_CLASS_ACTIVE);
79
+ return isSUCCEED;
80
+ };
81
+
82
+ function unmarkActiveHtmlElement(obj){
83
+ const isSUCCEED = isHTMLElement(obj);
84
+ if (isSUCCEED) obj.classList.remove(CSS_CLASS_ACTIVE);
85
+ return isSUCCEED;
86
+ };
87
+
88
+ function inactivateHtmlElements(...args) {
89
+ for (let item of args) {
90
+ if (item instanceof HTMLElement) {
91
+ item.classList.add(CSS_CLASS_DISABLED);
92
+ switch (item.tagName.toLowerCase()) {
93
+ case 'button':
94
+ case 'input':
95
+ item.disabled = true; break;
96
+ default:
97
+ break;
98
+ };
99
+ };
100
+ };
101
+ };
102
+
103
+ function activateHtmlElements(...args) {
104
+ for (let item of args) {
105
+ if (item instanceof HTMLElement) {
106
+ item.classList.remove(CSS_CLASS_DISABLED);
107
+ switch (item.tagName.toLowerCase()) {
108
+ case 'button':
109
+ case 'input':
110
+ item.disabled = false; break;
111
+ default:
112
+ break;
113
+ };
114
+ };
115
+ };
116
+ };
117
+
118
+ function valueToClassList(value, opt){
119
+ let result = [];
120
+ let list = valueToArray(value);
121
+ list.forEach((elem) => {
122
+ let value = '';
123
+ if (
124
+ typeof elem === 'string'
125
+ && ((value = elem.trim()) !== '')
126
+ ) {
127
+ value = value.split(/\s+/);
128
+ if (value.length > 0) result.push(...value);
129
+ };
130
+ });
131
+ if (result.length > 0 && typeof opt === 'boolean' && opt) {
132
+ result = [ ...(new Set(result)) ];
133
+ };
134
+ return result;
135
+ };
136
+
137
+ function createNewHtmlElement(tagName, opt){
138
+ let _tag = typeof tagName === 'string' ? tagName.trim() : '';
139
+ if (_tag === '') return null;
140
+ let item = document.createElement(_tag.toLowerCase());
141
+ if (isPlainObject(opt)) {
142
+ let {
143
+ id,
144
+ text,
145
+ attr,
146
+ class: _class,
147
+ data: dset,
148
+ } = opt;
149
+ // set an element id
150
+ id = typeof id === 'string' ? id.trim() : '';
151
+ if (id !== '') item.setAttribute('id', id);
152
+ // set an element text context
153
+ if (typeof text === 'string') {
154
+ item.appendChild(document.createTextNode(text));
155
+ };
156
+ // set an attributes of the element
157
+ if (isObject(attr)) {
158
+ attr = isArray(attr) ? attr : Object.entries(attr);
159
+ for (let [ key, value ] of attr ) {
160
+ key = (typeof key === 'string') ? key.trim() : '';
161
+ if (key !== '') {
162
+ value = readAsString(value, '', {
163
+ useTrim: true,
164
+ numberToString: true,
165
+ boolToString: true,
166
+ });
167
+ if (value !== '') item.setAttribute(key.toLowerCase(), value);
168
+ }
169
+ };
170
+ };
171
+ // set a class-attributes of the element
172
+ if (_class !== undefined) {
173
+ const cl = valueToClassList(_class, true);
174
+ if (cl.length > 0) item.classList.add(...cl);
175
+ };
176
+ // set a data-attributes of the element
177
+ if (isObject(dset)) {
178
+ dset = isArray(dset) ? dset : Object.entries(dset);
179
+ for (let [ key, value ] of dset ) {
180
+ key = (typeof key === 'string') ? key.trim() : '';
181
+ if (key !== '') {
182
+ value = readAsString(value, '', {
183
+ useTrim: true,
184
+ numberToString: true,
185
+ boolToString: true,
186
+ });
187
+ if (value !== '') item.dataset[key] = value;
188
+ }
189
+ };
190
+ };
191
+ // set an event handlers for the element
192
+ let evnt = null;
193
+ };
194
+ return item;
195
+ };
196
+
197
+ // === module main block (class definitions) ===
198
+
199
+ // === module exports block ===
200
+
201
+ exports.CSS_CLASS_STRING = /*function(){ return*/ {
202
+ CSS_CLASS_CURRENT: CSS_CLASS_CURRENT,
203
+ CSS_CLASS_SELECTED: CSS_CLASS_SELECTED,
204
+ CSS_CLASS_ACTIVE: CSS_CLASS_ACTIVE,
205
+ CSS_CLASS_DISABLED: CSS_CLASS_DISABLED,
206
+ CSS_CLASS_HIDDEN: CSS_CLASS_HIDDEN
207
+ }/*}*/;
208
+
209
+ exports.isHTMLElement = isHTMLElement;
210
+ exports.isCurrentHTMLElement = isCurrentHTMLElement;
211
+ exports.isSelectedHTMLElement = isSelectedHTMLElement;
212
+ exports.isActiveHTMLElement = isActiveHTMLElement;
213
+ exports.isHiddenHTMLElement = isHiddenHTMLElement;
214
+ exports.showHtmlElement = showHtmlElement;
215
+ exports.hideHtmlElement = hideHtmlElement;
216
+ exports.selectHtmlElement = selectHtmlElement;
217
+ exports.unselectHtmlElement = unselectHtmlElement;
218
+ exports.markHtmlElementAsCurrent = markHtmlElementAsCurrent;
219
+ exports.unmarkCurrentHtmlElement = unmarkCurrentHtmlElement;
220
+ exports.markHtmlElementAsActive = markHtmlElementAsActive;
221
+ exports.unmarkActiveHtmlElement = unmarkActiveHtmlElement;
222
+ exports.activateHtmlElements = activateHtmlElements;
223
+ exports.inactivateHtmlElements = inactivateHtmlElements;
224
+
225
+ exports.valueToClassList = valueToClassList;
226
+
227
+ // experimental
228
+ exports.createNewHtmlElement = createNewHtmlElement;
package/package.json ADDED
@@ -0,0 +1,45 @@
1
+ {
2
+ "name": "@cntwg/html-helper",
3
+ "version": "0.0.14",
4
+ "description": "A base HTML-helper library for js",
5
+ "author": "ygracs <cs70th-om@rambler.ru>",
6
+ "license": "MIT",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://gitlab.com/cntwg/html-helper.git"
10
+ },
11
+ "main": "./index.js",
12
+ "files": [
13
+ "doc/html-helper-lib.md",
14
+ "doc/html-ctrls-list.md",
15
+ "doc/html-ctrls-btn.md",
16
+ "lib/html-helper-lib.js",
17
+ "lib/html-ctrls/lists-stubs.js",
18
+ "lib/html-ctrls/lists-btns.js",
19
+ "lib/html-ctrls-list.js",
20
+ "lib/html-ctrls-btn.js",
21
+ "index.js"
22
+ ],
23
+ "scripts": {
24
+ "test": "jest",
25
+ "test-bs": "jest base/",
26
+ "test-lc1": "jest THtmlItemsListContainer",
27
+ "test-lc2": "jest THtmlItemsListController",
28
+ "test-lc2:bs": "jest THtmlItemsListController/base",
29
+ "test-lc2:ec": "jest THtmlItemsListController/events",
30
+ "test-sis": "jest THtmlStubItemsSet",
31
+ "test-btn": "jest THtmlButtonsSet",
32
+ "test-lbc": "jest THtmlListButtonsController",
33
+ "test-lbc:bs": "jest THtmlListButtonsController/base",
34
+ "test-lbc:ec": "jest THtmlListButtonsController/events",
35
+ "test-bc1": "jest THtmlButtonsControllerARCSet",
36
+ "test-bc1:bs": "jest THtmlButtonsControllerARCSet/base",
37
+ "test-bc1:ec": "jest THtmlButtonsControllerARCSet/events"
38
+ },
39
+ "dependencies": {
40
+ "@ygracs/bsfoc-lib-js": "^0.1.2"
41
+ },
42
+ "devDependencies": {
43
+ "jest": "^27.5.1"
44
+ }
45
+ }