@cntwg/html-helper 0.0.18 → 0.0.19

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 CHANGED
@@ -1,3 +1,14 @@
1
+ #### *v0.0.19*
2
+
3
+ Pre-release version.
4
+
5
+ > - `html-helper-lib.md` updated;
6
+ > - updated dependency on `@ygracs/bsfoc-lib-js` module to v0.1.3;
7
+ > - add functions `lockHtmlElement` and `unlockHtmlElement` into `html-helper-lib.js` module;
8
+ > - change behavior for `createNewHtmlElement` function in handling `options` parameter (*see docs for details*);
9
+ > - added `THtmlInputField` class (experimental);
10
+ > - some other fixes.
11
+
1
12
  #### *v0.0.18*
2
13
 
3
14
  Pre-release version.
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
- >|***rev.*:**|0.1.3|
1
+ >|***rev.*:**|0.1.4|
2
2
  >|:---|---:|
3
- >|date:|2023-04-09|
3
+ >|date:|2023-06-29|
4
4
 
5
5
  ## Introduction
6
6
 
@@ -34,3 +34,8 @@ This module contains the following components:
34
34
  - `THtmlStubItemsSet`.
35
35
 
36
36
  > *For more details see `html-ctrls-list.md` in module `doc` directory*.
37
+
38
+ - classes for working with HTML-elements maintaining an input fields support such as:
39
+ - `THtmlInputField` (experimental, beta stage).
40
+
41
+ > *For more details see `html-ctrls-fields.md` in module `doc` directory*.
@@ -1,6 +1,6 @@
1
- >|***rev.*:**|0.1.8|
1
+ >|***rev.*:**|0.1.9|
2
2
  >|:---|---:|
3
- >|date:|2022-09-12|
3
+ >|date:|2023-07-04|
4
4
 
5
5
  ## Introduction
6
6
 
@@ -77,7 +77,7 @@ This method adds a group given by `name`. If succeed `true` is returned.
77
77
 
78
78
  ##### **delGroup(name)**
79
79
 
80
- This method deletes the gpoup given by `name`. If succeed `true` is returned.
80
+ This method deletes the group given by `name`. If succeed `true` is returned.
81
81
 
82
82
  ##### **renameGroup(oldName, newName)**
83
83
 
@@ -108,13 +108,13 @@ The class constructor creates a new instance of the class. It receives an `optio
108
108
  |option name|option type|default value|description|
109
109
  |:---|---|---:|:---|
110
110
  |`btnFirst`|`HTMLElement`|---|define HTML-button which will select the first element|
111
- |`btn_frst`|`HTMLElement`|---|*will deprecate (use `btnFirst` instead)*|
111
+ |`btn_frst`|`HTMLElement`|---|\<*deprecate*> *(use `btnFirst` instead)*|
112
112
  |`btnPrev`|`HTMLElement`|---|define HTML-button which will select the previous element|
113
- |`btn_prev`|`HTMLElement`|---|*will deprecate (use `btnPrev` instead)*|
113
+ |`btn_prev`|`HTMLElement`|---|\<*deprecate*> *(use `btnPrev` instead)*|
114
114
  |`btnNext`|`HTMLElement`|---|define HTML-button which will select the next element|
115
- |`btn_next`|`HTMLElement`|---|*will deprecate (use `btnNext` instead)*|
115
+ |`btn_next`|`HTMLElement`|---|\<*deprecate*> *(use `btnNext` instead)*|
116
116
  |`btnLast`|`HTMLElement`|---|define HTML-button which will select the last element|
117
- |`btn_last`|`HTMLElement`|---|*will deprecate (use `btnLast` instead)*|
117
+ |`btn_last`|`HTMLElement`|---|\<*deprecate*> *(use `btnLast` instead)*|
118
118
 
119
119
 
120
120
  #### class properties
@@ -0,0 +1,140 @@
1
+ >|***rev.*:**|0.0.4|
2
+ >|:---|---:|
3
+ >|date:|2023-07-04|
4
+
5
+ ## Introduction
6
+
7
+ This paper describes a classes provided by `html-ctrls-fields.js` module.
8
+
9
+ ## Module classes
10
+
11
+ ### **THtmlInputField**
12
+
13
+ This class provide a container useful when dealing with an elements such an input fields or a checkboxes placed on an HTML-form.
14
+
15
+ #### class constructor
16
+
17
+ The class constructor creates a new instance of the class. It receives a parameters listed below:
18
+
19
+ |parameter name|value type|default value|description|
20
+ |:---|---|---:|:---|
21
+ |`name`|`string`|---|\<*reserved*>|
22
+ |`eholds`|`object`|---|contains ref-list for container elements|
23
+ |`options`|`object`|---|contains a container settings|
24
+
25
+ The `eholds` structure has listed below:
26
+
27
+ |parameter name|value type|default value|description|
28
+ |:---|---|---:|:---|
29
+ |`host`|`HTMLElement`|---|points to a host element which holds all other items|
30
+ |`label`|`HTMLElement`|---|points to a label item|
31
+ |`field`|`HTMLElement`|---|points to a field item|
32
+
33
+ The `options` structure has listed below:
34
+
35
+ |option name|value type|default value|description|
36
+ |:---|---|---:|:---|
37
+ |`idPref`|---|---|\<*reserved*>|
38
+ |`baseClassID`|---|---|\<*reserved*>|
39
+ |`hint`|---|---|\<*reserved*>|
40
+ |`fldType`|---|---|\<*reserved*>|
41
+ |`isReadOnly`|---|---|\<*reserved*>|
42
+
43
+ #### class properties
44
+
45
+ The table below describes a properties of a `THtmlInputField` class:
46
+
47
+ |property name|property type|read only|description|
48
+ |:---|---|---|:---|
49
+ |`value`|`string` or `boolean`|no|contains a container value|
50
+ |`isModified`|`boolean`|yes|show whether or not the field content was modified|
51
+
52
+ #### class methods
53
+
54
+ ##### **clear()**
55
+
56
+ > Note: \<*reserved*>
57
+
58
+ ##### **isEmpty()**
59
+
60
+ This method returns `true` if the container field element contains an empty string.
61
+
62
+ ##### **isNotEmpty()**
63
+
64
+ This method returns `true` if the container field element contains a non-empty string.
65
+
66
+ ##### **hideItem()**
67
+
68
+ This method hides a container on the Web-form.
69
+
70
+ ##### **showItem()**
71
+
72
+ This method shows a container on the Web-form.
73
+
74
+ ##### **attachTo(target)**
75
+
76
+ This method attaches a container to a parent HTML-element given by a `target` parameter. If succeed `true` returned.
77
+
78
+ #### class methods (*static*)
79
+
80
+ ##### **create(name\[, options])**
81
+
82
+ This method creates an instance of the class. If failed `null` returned.
83
+
84
+ The `name` parameter represents an ID of the created container.
85
+
86
+ For details of `options` parameter see a `createElement` method description.
87
+
88
+ ##### **createElement(name\[, options\[, bRetChild]])**
89
+
90
+ This method creates a container with all elements needed for input field component to function.
91
+
92
+ The `name` parameter represents an ID of the created container.
93
+
94
+ The `options` structure has listed below:
95
+
96
+ |option name|value type|default value|description|
97
+ |:---|---|---:|:---|
98
+ |`host`|`HTMLElement`|---|an HTML-element which is a host for all container elements|
99
+ |`idPref`|`string`|---|a prefix which modified a container ID, if given|
100
+ |`label`|`string`|---|a text for the field label|
101
+ |`labelPosition`|`string`|---|defines a position to place a label|
102
+ |`useDelim`|`boolean`|`true`|defines whether a delimeter is used within a text of a label|
103
+ |`baseClassID`|`object`|---|containes a list of class name attributes for container elements|
104
+ |`hint`|`string`|---|defines a helper string which is shown when a field content is empty|
105
+ |`fldType`|`string`|---|defines a field type|
106
+ |`isReadOnly`|`boolean`|`true`|defines whether a field content can be modified|
107
+
108
+ The `baseClassID` structure has listed below:
109
+
110
+ |option name|value type|default value|description|
111
+ |:---|---|---:|:---|
112
+ |`main`|---|---|containes a list of class name attributes for all elements|
113
+ |`host`|---|---|containes a list of class name attributes for container host|
114
+ |`label`|---|---|containes a list of class name attributes for label element|
115
+ |`field`|---|---|containes a list of class name attributes for field element|
116
+
117
+ The `bRetChild` parameter, if given, changes the result value of the method. It must be of a `boolean` type. If value is `false` the only host element is returned as a result (a default behavoir). If `true`, an `object` of the following structure is returned:
118
+
119
+ |property name|value type|description|
120
+ |:---|---|:---|
121
+ |`host`|`HTMLElement`|a container host element|
122
+ |`label`|`HTMLElement`|a container label element|
123
+ |`field`|`HTMLElement`|a container field element|
124
+
125
+ #### class methods (*special*)
126
+
127
+ ##### **on(name, event)**
128
+
129
+ This method sets event handler addressed by `name` parameter for the class instance.
130
+
131
+ > Note: for current implementation you can't reset or set new event handler (*especially another one*) if a handler with the same name exists already.
132
+
133
+ #### class events
134
+
135
+ The class generates the events listed in the table bellow:
136
+
137
+ |event name|description|
138
+ |:---|:---|
139
+ |`value-changed`|fired when value is changed|
140
+ |`value-empty`|fired due editing of the field when its value become an empty string|
@@ -1,6 +1,6 @@
1
- >|***rev.*:**|0.1.14|
1
+ >|***rev.*:**|0.1.15|
2
2
  >|:---|---:|
3
- >|date:|2023-04-04|
3
+ >|date:|2023-07-04|
4
4
 
5
5
  ## Introduction
6
6
 
@@ -409,7 +409,7 @@ The `options` parameter is an `object` that contains the following set of parame
409
409
  |option name|value type|default value|description|
410
410
  |:---|---|---:|:---|
411
411
  |`useClear`|`boolean`|`true`|if set to `true`, before the load of the items, the list is cleared|
412
- |`load_as_new`|`boolean`|`true`|*will deprecate (use `useClear` instead)*|
412
+ |`load_as_new`|`boolean`|`true`|\<*deprecate*> *(use `useClear` instead)*|
413
413
  |`force`|`boolean`|`false`|if set to `true`, the item which is already present will be replaced with a new one|
414
414
 
415
415
  If the `options` parameter is a `boolean` value it treats as a `options.useClear` option.
@@ -1,6 +1,6 @@
1
- >|***rev.*:**|0.1.5|
1
+ >|***rev.*:**|0.1.8|
2
2
  >|:---|---:|
3
- >|date:|2023-03-30|
3
+ >|date:|2023-06-12|
4
4
 
5
5
  ## Introduction
6
6
 
@@ -74,6 +74,14 @@ This function applies the class attribute defined by `CSS_CLASS_CURRENT` for a g
74
74
 
75
75
  This function removes the class attribute defined by `CSS_CLASS_CURRENT` for a given HTML-element. If succeed `true` is returned.
76
76
 
77
+ #### **unlockHtmlElement(object)**
78
+
79
+ This function enables a given HTML-element.
80
+
81
+ #### **lockHtmlElement(args)**
82
+
83
+ This function disables a given HTML-element.
84
+
77
85
  #### **activateHtmlElements(args)**
78
86
 
79
87
  This function enables all HTML-elements listed by `args`.
@@ -94,24 +102,25 @@ The `options` parameter if sets to `true` force the function to remove all dupli
94
102
 
95
103
  > Note: Purpose of those functions will be discussed and some may be deprecate and make obsolete or functionality may be altered. So use it with cautions in mind.
96
104
 
97
- #### **createNewHtmlElement(tagName, options)**
105
+ #### **createNewHtmlElement(tagName\[, options])**
98
106
 
99
107
  This function helps to simplify the process of creating a new HTML-element. It receives the tag name (`tagName` parameter) and the options as a second parameter:
100
108
 
101
109
  |parameter name|value type|default value|description|
102
110
  |:---|---|---:|:---|
103
- |`tagName`|`string`|`undefined`|defines a tag name of the element.|
104
- |`options`|`object`|---|contains a set of a settings.|
111
+ |`tagName`|`string`|`undefined`|defines a tag name of the element|
112
+ |`options`|`object`|---|contains a set of a settings|
105
113
 
106
114
  The `options` structure has listed below:
107
115
 
108
116
  |option name|value type|default value|description|
109
117
  |:---|---|---:|:---|
110
- |`id`|`string`|`undefined`|defines ID-attribute of an element.|
111
- |`text`|`string`|`undefined`|defines a text context of an element.|
112
- |`attr`|---|`undefined`|defines an attributes of an element.|
113
- |`class`|---|`undefined`|defines a class-attributes of an element.|
114
- |`data`|---|`undefined`|defines a data-attributes of an element.|
118
+ |`id`|`string`|`undefined`|defines ID-attribute of an element|
119
+ |`text`|`string`|`undefined`|defines a text context of an element|
120
+ |`attr`|---|`undefined`|defines an attributes of an element|
121
+ |`class`|---|`undefined`|\<deprecate> (*use `classNames` instead*)|
122
+ |`classNames`|---|`undefined`|defines a class-attributes of an element|
123
+ |`data`|---|`undefined`|defines a data-attributes of an element|
115
124
 
116
125
  If succeed, the function returned `HTMLElement` and `null` if failed.
117
126
 
package/index.js CHANGED
@@ -1,10 +1,11 @@
1
- // [v0.1.016-20230409]
1
+ // [v0.1.018-20230629]
2
2
 
3
3
  // === module init block ===
4
4
 
5
5
  const html_helper = require('./lib/html-helper-lib.js');
6
6
  const html_list = require('./lib/html-ctrls/list.js');
7
7
  const html_bts = require('./lib/html-ctrls/buttons.js');
8
+ const html_flds = require('./lib/html-ctrls/fields.js');
8
9
 
9
10
  const html_list_btn = require('./lib/html-ctrls/lists-btn.js');
10
11
 
@@ -28,6 +29,8 @@ exports.markHtmlElementAsCurrent = html_helper.markHtmlElementAsCurrent;
28
29
  exports.unmarkCurrentHtmlElement = html_helper.unmarkCurrentHtmlElement;
29
30
  exports.markHtmlElementAsActive = html_helper.markHtmlElementAsActive;
30
31
  exports.unmarkActiveHtmlElement = html_helper.unmarkActiveHtmlElement;
32
+ exports.lockHtmlElement = html_helper.lockHtmlElement;
33
+ exports.unlockHtmlElement = html_helper.unlockHtmlElement;
31
34
  exports.activateHtmlElements = html_helper.activateHtmlElements;
32
35
  exports.inactivateHtmlElements = html_helper.inactivateHtmlElements;
33
36
  exports.valueToClassList = html_helper.valueToClassList;
@@ -40,6 +43,9 @@ exports.THtmlButtonsSet = html_bts.THtmlButtonsSet;
40
43
  exports.THtmlButtonsControllerARCSet = html_bts.THtmlButtonsControllerARCSet;
41
44
  exports.BTS_DEF_GROUP_NAME = html_bts.BTS_DEF_GROUP_NAME;
42
45
 
46
+ // experimental
47
+ exports.THtmlInputField = html_flds.THtmlInputField;
48
+
43
49
  exports.THtmlListButtonsController = html_list_btn.THtmlListButtonsController;
44
50
 
45
51
  // experimental
@@ -54,6 +60,8 @@ exports.component = {
54
60
  THtmlButtonsSet: exports.THtmlButtonsSet,
55
61
  THtmlListButtonsController: exports.THtmlListButtonsController,
56
62
  THtmlButtonsControllerARCSet: exports.THtmlButtonsControllerARCSet,
63
+ // experimental
64
+ THtmlInputField: exports.THtmlInputField,
57
65
  };
58
66
  // will deprecate
59
67
  exports.classes = exports.component;
@@ -1,28 +1,29 @@
1
- // [v0.1.047-20230408]
1
+ // [v0.1.049-20230614]
2
2
 
3
3
  // === module init block ===
4
4
 
5
5
  const {
6
- //readAsString,
7
- //readAsNumber,
8
6
  isNotEmptyString,
9
7
  isPlainObject, valueToArray,
10
8
  TItemsICollection,
11
9
  } = require('@ygracs/bsfoc-lib-js');
12
10
 
13
11
  const {
14
- isHTMLElement, CSS_CLASS_STRING,
12
+ isHTMLElement,
15
13
  valueToIDString,
14
+ lockHtmlElement, unlockHtmlElement,
15
+ CSS_CLASS_STRING,
16
16
  } = require('../html-helper-lib.js');
17
17
 
18
18
  const {
19
19
  pushEventHandler, triggerEventHandler,
20
+ readOnClickEventInfo,
20
21
  } = require('./mod-hfunc.js');
21
22
 
22
23
  const {
23
- CSS_CLASS_SELECTED,
24
+ //CSS_CLASS_SELECTED,
24
25
  CSS_CLASS_DISABLED,
25
- CSS_CLASS_HIDDEN,
26
+ //CSS_CLASS_HIDDEN,
26
27
  } = CSS_CLASS_STRING;
27
28
 
28
29
  const BTS_DEF_GROUP_NAME = 'all';
@@ -53,32 +54,6 @@ function isHTMLButton(obj){
53
54
  return result;
54
55
  }
55
56
 
56
- function readOnClickEventInfo(e) {
57
- let item = null;
58
- //let on_click_num = readAsNumber(e.detail, 0);
59
- const { eventPhase, detail: onClickNum } = e;
60
- //console.log('CHECK: e => ditail:['+onClickNum+']');
61
- //console.log('CHECK: e => phase:['+eventPhase+']');
62
- switch (eventPhase) {
63
- //*case 1:
64
- //* // NOTE: currently on eventPhase = 2 and 3
65
- case 2: {
66
- /**/// capturing stage
67
- item = e.target;
68
- break;
69
- }
70
- case 3: {
71
- /**/// bubblig stage
72
- item = e.currentTarget;
73
- break;
74
- }
75
- default: {
76
- break;
77
- }
78
- };
79
- return { item, onClickNum };
80
- };
81
-
82
57
  // === module main block (function definitions) ===
83
58
 
84
59
  // === module main block (class definitions) ===
@@ -137,19 +112,11 @@ class THtmlButtonsSet {
137
112
  }
138
113
 
139
114
  enableItem(name){
140
- const item = this.#_btnCol.getItem(name);
141
- if (item) {
142
- item.classList.remove(CSS_CLASS_DISABLED);
143
- item.disabled = false;
144
- };
115
+ unlockHtmlElement(this.#_btnCol.getItem(name));
145
116
  }
146
117
 
147
118
  disableItem(name){
148
- const item = this.#_btnCol.getItem(name);
149
- if (item) {
150
- item.classList.add(CSS_CLASS_DISABLED);
151
- item.disabled = true;
152
- };
119
+ lockHtmlElement(this.#_btnCol.getItem(name));
153
120
  }
154
121
 
155
122
  hasGroup(name){
@@ -170,22 +137,12 @@ class THtmlButtonsSet {
170
137
 
171
138
  enableGroup(value){
172
139
  const group = this.#_btnCol.getCategory(value);
173
- valueToArray(group).forEach((item) => {
174
- if (item) {
175
- item.classList.remove(CSS_CLASS_DISABLED);
176
- item.disabled = false;
177
- };
178
- });
140
+ valueToArray(group).forEach(item => unlockHtmlElement(item));
179
141
  }
180
142
 
181
143
  disableGroup(value){
182
144
  const group = this.#_btnCol.getCategory(value);
183
- valueToArray(group).forEach((item) => {
184
- if (item) {
185
- item.classList.add(CSS_CLASS_DISABLED);
186
- item.disabled = true;
187
- };
188
- });
145
+ valueToArray(group).forEach(item => lockHtmlElement(item));
189
146
  }
190
147
 
191
148
  static get isHTMLButton(){
@@ -210,11 +167,11 @@ class THtmlButtonsControllerARCSet {
210
167
  if (!isHTMLButton(btnApply)) btnApply = null;
211
168
  if (!isHTMLButton(btnReset)) btnReset = null;
212
169
  if (!isHTMLButton(btnCancel)) btnCancel = null;
213
- const controls = {
170
+ /*const controls = {
214
171
  btnApply,
215
172
  btnReset,
216
173
  btnCancel,
217
- };
174
+ };*/ // // TODO: ???
218
175
  // init buttons set
219
176
  const _btnSet = new THtmlButtonsSet();
220
177
  this.#_btnSet = _btnSet;
@@ -222,23 +179,23 @@ class THtmlButtonsControllerARCSet {
222
179
  _btnSet.addGroup('all');
223
180
  if (btnApply) {
224
181
  _btnSet.addItem('btn_apply', btnApply, 'main', 'all');
225
- btnApply.addEventListener('click', this.#_on_btn_apply_pressed);
182
+ btnApply.addEventListener('click', e => this.#_on_btn_pressed(e, 'apply'));
226
183
  };
227
184
  if (btnReset) {
228
185
  _btnSet.addItem('btn_reset', btnReset, 'main', 'all');
229
- btnReset.addEventListener('click', this.#_on_btn_reset_pressed);
186
+ btnReset.addEventListener('click', e => this.#_on_btn_pressed(e, 'reset'));
230
187
  };
231
188
  if (btnCancel) {
232
189
  _btnSet.addItem('btn_cancel', btnCancel, 'all');
233
- btnCancel.addEventListener('click', this.#_on_btn_cancel_pressed);
190
+ btnCancel.addEventListener('click', e => this.#_on_btn_pressed(e, 'cancel'));
234
191
  };
235
192
  _btnSet.disableGroup('main');
236
193
  // init events controller
237
194
  this.#_events = new Map();
238
195
  }
239
196
 
240
- #_on_btn_apply_pressed = (e) => {
241
- //console.log('THtmlListButtonsController._on_btn_apply_pressed() ==> was called...');
197
+ #_on_btn_pressed = (e, key) => {
198
+ //console.log(`THtmlListButtonsController._on_btn_pressed().key(${key}) ==> was called...`);
242
199
  //e.preventDefault(); /* need to reconsider reason for use */
243
200
  const { item, onClickNum } = readOnClickEventInfo(e);
244
201
  if (
@@ -246,33 +203,7 @@ class THtmlButtonsControllerARCSet {
246
203
  && (onClickNum === 0 || onClickNum === 1)
247
204
  && !item.classList.contains(CSS_CLASS_DISABLED)
248
205
  ) {
249
- this.#_triggerEvent('btn-apply-pressed', e);
250
- };
251
- }
252
-
253
- #_on_btn_reset_pressed = (e) => {
254
- //console.log('THtmlListButtonsController._on_btn_reset_pressed() ==> was called...');
255
- //e.preventDefault(); /* need to reconsider reason for use */
256
- const { item, onClickNum } = readOnClickEventInfo(e);
257
- if (
258
- item instanceof HTMLElement
259
- && (onClickNum === 0 || onClickNum === 1)
260
- && !item.classList.contains(CSS_CLASS_DISABLED)
261
- ) {
262
- this.#_triggerEvent('btn-reset-pressed', e);
263
- };
264
- }
265
-
266
- #_on_btn_cancel_pressed = (e) => {
267
- //console.log('THtmlListButtonsController._on_btn_cancel_pressed() ==> was called...');
268
- //e.preventDefault(); /* need to reconsider reason for use */
269
- const { item, onClickNum } = readOnClickEventInfo(e);
270
- if (
271
- item instanceof HTMLElement
272
- && (onClickNum === 0 || onClickNum === 1)
273
- && !item.classList.contains(CSS_CLASS_DISABLED)
274
- ) {
275
- this.#_triggerEvent('btn-cancel-pressed', e);
206
+ this.#_triggerEvent(`btn-${key}-pressed`, e);
276
207
  };
277
208
  }
278
209
 
@@ -309,5 +240,3 @@ exports.THtmlButtonsControllerARCSet = THtmlButtonsControllerARCSet;
309
240
  exports.BTS_DEF_GROUP_NAME = BTS_DEF_GROUP_NAME;
310
241
 
311
242
  exports.isHTMLButton = isHTMLButton;
312
-
313
- exports.readOnClickEventInfo = readOnClickEventInfo;
@@ -0,0 +1,342 @@
1
+ // [v0.1.011-20230704]
2
+
3
+ // === module init block ===
4
+
5
+ const {
6
+ readAsBool, readAsString,
7
+ isEmptyString, isNotEmptyString,
8
+ isPlainObject,
9
+ } = require('@ygracs/bsfoc-lib-js');
10
+
11
+ const {
12
+ isHTMLElement, createNewHtmlElement,
13
+ valueToIDString, valueToClassList,
14
+ readAsAttrValue,
15
+ showHtmlElement, hideHtmlElement,
16
+ } = require('../html-helper-lib.js');
17
+
18
+ const {
19
+ pushEventHandler, triggerEventHandler,
20
+ } = require('./mod-hfunc.js');
21
+
22
+ // === module extra block (helper functions) ===
23
+
24
+ const lpAllowedValues = new Map([
25
+ [ 'before', 0 ],
26
+ [ 'after', 1 ],
27
+ [ 'default', 0 ],
28
+ ]);
29
+
30
+ function getInputFieldTypeDescr(value){
31
+ let fldType = readAsString(value, '', true);
32
+ if (fldType === '') fldType = 'default';
33
+ let fldSubType = '';
34
+ switch (fldType) {
35
+ case 'text':
36
+ case 'checkbox': {
37
+ break;
38
+ }
39
+ case 'switch': {
40
+ fldType = 'checkbox';
41
+ fldSubType = 'switch';
42
+ break;
43
+ }
44
+ case 'field':
45
+ case 'default': {
46
+ fldType = 'text';
47
+ break;
48
+ }
49
+ default: {
50
+ fldType = '';
51
+ break;
52
+ }
53
+ };
54
+ return { type: fldType, subtype: fldSubType };
55
+ };
56
+
57
+ function getLabelPositionID(value){
58
+ let key = readAsString(value, '', true);
59
+ if (!lpAllowedValues.has(key)) key = 'default';
60
+ return lpAllowedValues.get(key);
61
+ };
62
+
63
+ // === module main block ===
64
+
65
+ /***
66
+ * (* constant definitions *)
67
+ */
68
+
69
+ /***
70
+ * (* function definitions *)
71
+ */
72
+
73
+ /***
74
+ * (* class definitions *)
75
+ */
76
+
77
+ class THtmlInputField {
78
+ #_host = null;
79
+ #_label = null;
80
+ #_field = null;
81
+ #_id = '';
82
+ #_value = '';
83
+ #_options = null;
84
+ #_status = null;
85
+ #_events = null;
86
+
87
+ constructor(name, eholds, opt){
88
+ // load elements
89
+ const _eholds = isPlainObject(eholds) ? eholds : {};
90
+ let { host, label, field } = _eholds;
91
+ // save elements
92
+ if (!isHTMLElement(host)) host = null;
93
+ if (!isHTMLElement(label)) label = null;
94
+ if (!isHTMLElement(field)) field = null;
95
+ this.#_host = host;
96
+ this.#_label = label;
97
+ this.#_field = field;
98
+ // load options
99
+ const _options = isPlainObject(opt) ? opt : {};
100
+ let {
101
+ idPref,
102
+ baseClassID,
103
+ hint,
104
+ type: fldType,
105
+ readonly: isReadOnly,
106
+ } = _options;
107
+ let fldSubType = '';
108
+ ({ type: fldType, subtype: fldSubType } = getInputFieldTypeDescr(fldType));
109
+ isReadOnly = readAsBool(isReadOnly, true);
110
+ // save options
111
+ this.#_options = _options;
112
+ // save status
113
+ this.#_status = {
114
+ fldType,
115
+ fldSubType,
116
+ isReadOnly,
117
+ isModified: false,
118
+ };
119
+ // init an event instance controller
120
+ if (field) {
121
+ field.addEventListener('change', this.#_saveFieldData);
122
+ field.addEventListener('input', this.#_switchFieldStatusOnInput);
123
+ field.addEventListener('blur', this.#_switchFieldStatusOnBlur);
124
+ };
125
+ this.#_events = new Map();
126
+ }
127
+
128
+ get isModified(){
129
+ return this.#_status.isModified;
130
+ }
131
+
132
+ get value(){
133
+ const field = this.#_field;
134
+ let result = '';
135
+ if (field) {
136
+ const { fldType } = this.#_status;
137
+ result = fldType === 'checkbox' ? field.checked : field.value;
138
+ };
139
+ return result;
140
+ }
141
+
142
+ set value(value){
143
+ const field = this.#_field;
144
+ if (value !== undefined) {
145
+ const { fldType } = this.#_status;
146
+ if (fldType === 'checkbox') {
147
+ field.checked = value;
148
+ this.#_status.isModified = false;
149
+ } else {
150
+ let _value = readAsString(value, '', {
151
+ boolToString: true,
152
+ numberToString: true,
153
+ });
154
+ field.value = _value;
155
+ this.#_status.isModified = false;
156
+ if (_value === '') {
157
+ this.#_triggerEvent('value-empty', {
158
+ type: 'value-empty',
159
+ target: null,
160
+ });
161
+ };
162
+ };
163
+ };
164
+ }
165
+
166
+ #_triggerEvent = (name, ...args) => {
167
+ triggerEventHandler(this.#_events, name, ...args);
168
+ };
169
+
170
+ #_saveFieldData = (e) => {
171
+ e.preventDefault();
172
+ const target = e.target;
173
+ //***/console.log(':'+e.type+':'+target.getAttribute('id')+':\n:saveFieldData() => received OnChange-event');
174
+ // // TODO:
175
+ this.#_triggerEvent('value-changed', {
176
+ type: 'value-changed',
177
+ target,
178
+ });
179
+ };
180
+
181
+ #_switchFieldStatusOnInput = (e) => {
182
+ e.preventDefault();
183
+ const target = e.target;
184
+ //***/console.log(':'+e.type+':'+target.getAttribute('id')+':\n:switchFieldStatusOnInput() => received OnInput-event');
185
+ this.#_status.isModified = true;
186
+ // // TODO:
187
+ if (target.value === '') {
188
+ this.#_triggerEvent('value-empty', {
189
+ type: 'value-empty',
190
+ target,
191
+ });
192
+ };
193
+ };
194
+
195
+ #_switchFieldStatusOnBlur = (e) => {
196
+ e.preventDefault();
197
+ //***/console.log(':'+e.type+':'+e.target.getAttribute('id')+':\n:switchFieldStatusOnBlur() => received OnBlur-event');
198
+ // // TODO:
199
+ };
200
+
201
+ attachTo(target){
202
+ const host = this.#_host;
203
+ let result = false;
204
+ if (
205
+ isHTMLElement(target)
206
+ && isHTMLElement(host)
207
+ ) {
208
+ target.append(host);
209
+ result = true;
210
+ };
211
+ return result;
212
+ }
213
+
214
+ isEmpty(){
215
+ return isEmptyString(this.value);
216
+ }
217
+
218
+ isNotEmpty(){
219
+ return isNotEmptyString(this.value);
220
+ }
221
+
222
+ show(){
223
+ showHtmlElement(this.#_host);
224
+ }
225
+
226
+ hide(){
227
+ hideHtmlElement(this.#_host);
228
+ }
229
+
230
+ clear(){
231
+ // // TODO:
232
+ }
233
+
234
+ static create(name, opt){
235
+ const eholds = THtmlInputField.createElement(name, opt, true);
236
+ const { field } = eholds;
237
+ return (
238
+ isHTMLElement(field)
239
+ ? new THtmlInputField(name, eholds, opt)
240
+ : null
241
+ );
242
+ }
243
+
244
+ static createElement(name, opt, doRetChild){
245
+ const id = valueToIDString(name);
246
+ const _options = isPlainObject(opt) ? opt : {};
247
+ let fldSubType = '';
248
+ let {
249
+ host,
250
+ idPref,
251
+ label,
252
+ labelPosition,
253
+ useDelim,
254
+ baseClassID,
255
+ hint,
256
+ type: fldType,
257
+ readonly: isReadOnly,
258
+ } = _options;
259
+ if (!isPlainObject(baseClassID)) baseClassID = {};
260
+ let {
261
+ main: bcidMain,
262
+ host: bcidHost,
263
+ label: bcidLabel,
264
+ field: bcidField,
265
+ } = baseClassID;
266
+ bcidMain = valueToClassList(bcidMain);
267
+ bcidHost = valueToClassList(bcidHost);
268
+ bcidLabel = valueToClassList(bcidLabel);
269
+ bcidField = valueToClassList(bcidField);
270
+ idPref = valueToIDString(idPref);
271
+ hint = readAsString(hint, '', true);
272
+ ({ type: fldType, subtype: fldSubType } = getInputFieldTypeDescr(fldType));
273
+ if (fldSubType === 'switch') bcidField.push('switch');
274
+ labelPosition = getLabelPositionID(labelPosition);
275
+ if (typeof useDelim !== 'boolean') useDelim = true;
276
+ label = readAsString(label, '', true);
277
+ if (label !== '') label = `${label}${useDelim ? ':' : ''}`;
278
+ isReadOnly = readAsBool(isReadOnly, true);
279
+ let fldID = idPref === '' ? id : `${idPref}-${id}`;
280
+ if (!isHTMLElement(host)) {
281
+ host = createNewHtmlElement('div', {
282
+ class: bcidHost,
283
+ });
284
+ };
285
+ let lblHolder = null;
286
+ let fldHolder = null;
287
+ let items = [];
288
+ if (host) {
289
+ lblHolder = host.querySelector(`label[for="${fldID}"]`);
290
+ if (!lblHolder) {
291
+ lblHolder = createNewHtmlElement('label', {
292
+ text: label,
293
+ attr: {
294
+ for: fldID,
295
+ },
296
+ class: bcidLabel,
297
+ });
298
+ };
299
+ fldHolder = host.querySelector(`input#${fldID}`);
300
+ if (!fldHolder) {
301
+ fldHolder = createNewHtmlElement('input', {
302
+ id: fldID,
303
+ attr: {
304
+ type: fldType,
305
+ placeholder: hint,
306
+ },
307
+ class: bcidField,
308
+ });
309
+ };
310
+ if (fldHolder) {
311
+ if (isReadOnly) {
312
+ const attr = fldType === 'checkbox' ? 'disabled' : 'readOnly';
313
+ fldHolder[attr] = true;
314
+ };
315
+ if (lblHolder) items.push(lblHolder);
316
+ items.push(fldHolder);
317
+ };
318
+ const len = items.length;
319
+ if (len > 0) {
320
+ if (len === 2 && labelPosition === 1) {
321
+ items = [ items[1], items[0] ];
322
+ };
323
+ host.append(...items);
324
+ };
325
+ };
326
+ // // TODO:
327
+ return (
328
+ readAsBool(doRetChild, false)
329
+ ? { host, label: lblHolder, field: fldHolder }
330
+ : host
331
+ );
332
+ }
333
+
334
+ on(name, evnt){
335
+ pushEventHandler(this.#_events, name, evnt);
336
+ }
337
+
338
+ };
339
+
340
+ // === module exports block ===
341
+
342
+ module.exports.THtmlInputField = THtmlInputField;
@@ -1,35 +1,30 @@
1
- // [v0.1.045-20230408]
1
+ // [v0.1.047-20230614]
2
2
 
3
3
  // === module init block ===
4
4
 
5
5
  const {
6
- //readAsString,
7
- readAsNumber,
8
- //isNotEmptyString,
9
6
  isPlainObject,
10
- //valueToArray,
11
- //TItemsICollection,
12
7
  } = require('@ygracs/bsfoc-lib-js');
13
8
 
14
9
  const {
15
- CSS_CLASS_STRING,
16
10
  isHTMLElement,
11
+ CSS_CLASS_STRING,
17
12
  } = require('../html-helper-lib.js');
18
13
 
19
14
  const {
20
15
  isHTMLButton,
21
16
  THtmlButtonsSet,
22
- readOnClickEventInfo,
23
17
  } = require('./buttons.js');
24
18
 
25
19
  const {
26
20
  pushEventHandler, triggerEventHandler,
21
+ readOnClickEventInfo,
27
22
  } = require('./mod-hfunc.js');
28
23
 
29
24
  const {
30
- CSS_CLASS_SELECTED,
25
+ //CSS_CLASS_SELECTED,
31
26
  CSS_CLASS_DISABLED,
32
- CSS_CLASS_HIDDEN,
27
+ //CSS_CLASS_HIDDEN,
33
28
  } = CSS_CLASS_STRING;
34
29
 
35
30
  const BTS_DEF_GROUP_NAME = 'all';
@@ -66,12 +61,12 @@ class THtmlListButtonsController {
66
61
  if (!isHTMLButton(btnPrev)) btnPrev = null;
67
62
  if (!isHTMLButton(btnNext)) btnNext = null;
68
63
  if (!isHTMLButton(btnLast)) btnLast = null;
69
- const controls = {
64
+ /*const controls = {
70
65
  btnFirst,
71
66
  btnPrev,
72
67
  btnNext,
73
68
  btnLast,
74
- };
69
+ };*/ // // TODO: ???
75
70
  // init buttons set
76
71
  const _btnSet = new THtmlButtonsSet();
77
72
  this.#_btnSet = _btnSet;
@@ -81,8 +76,8 @@ class THtmlListButtonsController {
81
76
  if (btnFirst && btnLast) {
82
77
  _btnSet.addItem('btn_frst', btnFirst, 'move_bkwd');
83
78
  _btnSet.addItem('btn_last', btnLast, 'move_frwd');
84
- btnFirst.addEventListener('click', this.#_on_btn_frst_pressed);
85
- btnLast.addEventListener('click', this.#_on_btn_last_pressed);
79
+ btnFirst.addEventListener('click', e => this.#_on_btn_pressed(e, 'first'));
80
+ btnLast.addEventListener('click', e => this.#_on_btn_pressed(e, 'last'));
86
81
  } else {
87
82
  _btnSet.addItem('btn_frst', btnFirst, 'dsbl_ctrls');
88
83
  _btnSet.addItem('btn_last', btnLast, 'dsbl_ctrls');
@@ -90,8 +85,8 @@ class THtmlListButtonsController {
90
85
  if (btnPrev && btnNext) {
91
86
  _btnSet.addItem('btn_prev', btnPrev, 'move_bkwd');
92
87
  _btnSet.addItem('btn_next', btnNext, 'move_frwd');
93
- btnPrev.addEventListener('click', this.#_on_btn_prev_pressed);
94
- btnNext.addEventListener('click', this.#_on_btn_next_pressed);
88
+ btnPrev.addEventListener('click', e => this.#_on_btn_pressed(e, 'prev'));
89
+ btnNext.addEventListener('click', e => this.#_on_btn_pressed(e, 'next'));
95
90
  } else {
96
91
  _btnSet.addItem('btn_prev', btnPrev, 'dsbl_ctrls');
97
92
  _btnSet.addItem('btn_next', btnNext, 'dsbl_ctrls');
@@ -103,47 +98,8 @@ class THtmlListButtonsController {
103
98
  this.#_events = new Map();
104
99
  }
105
100
 
106
- #_on_btn_frst_pressed = (e) => {
107
- //console.log('THtmlListButtonsController._on_btn_frst_pressed() ==> was called...');
108
- //e.preventDefault(); /* need to reconsider reason for use */
109
- const { item, onClickNum } = readOnClickEventInfo(e);
110
- if (
111
- item instanceof HTMLElement
112
- && (onClickNum === 0 || onClickNum === 1)
113
- && !item.classList.contains(CSS_CLASS_DISABLED)
114
- ) {
115
- this.#_triggerEvent('btn-first-pressed', e);
116
- };
117
- }
118
-
119
- #_on_btn_last_pressed = (e) => {
120
- //console.log('THtmlListButtonsController._on_btn_last_pressed() ==> 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-last-pressed', e);
129
- };
130
- }
131
-
132
- #_on_btn_prev_pressed = (e) => {
133
- //console.log('THtmlListButtonsController._on_btn_prev_pressed() ==> was called...');
134
- //e.preventDefault(); /* need to reconsider reason for use */
135
- const { item, onClickNum } = readOnClickEventInfo(e);
136
- if (
137
- item instanceof HTMLElement
138
- && (onClickNum === 0 || onClickNum === 1)
139
- && !item.classList.contains(CSS_CLASS_DISABLED)
140
- ) {
141
- this.#_triggerEvent('btn-prev-pressed', e);
142
- };
143
- }
144
-
145
- #_on_btn_next_pressed = (e) => {
146
- //console.log('THtmlListButtonsController._on_btn_next_pressed() ==> was called...');
101
+ #_on_btn_pressed = (e, key) => {
102
+ //console.log(`THtmlListButtonsController._on_btn_pressed().key(${key}) ==> was called...`);
147
103
  //e.preventDefault(); /* need to reconsider reason for use */
148
104
  const { item, onClickNum } = readOnClickEventInfo(e);
149
105
  if (
@@ -151,7 +107,7 @@ class THtmlListButtonsController {
151
107
  && (onClickNum === 0 || onClickNum === 1)
152
108
  && !item.classList.contains(CSS_CLASS_DISABLED)
153
109
  ) {
154
- this.#_triggerEvent('btn-next-pressed', e);
110
+ this.#_triggerEvent(`btn-${key}-pressed`, e);
155
111
  };
156
112
  }
157
113
 
@@ -1,4 +1,4 @@
1
- // [v0.1.059-20230408]
1
+ // [v0.1.060-20230614]
2
2
 
3
3
  // === module init block ===
4
4
 
@@ -25,9 +25,36 @@ function triggerEventHandler(pool, name, ...args){
25
25
  if (pool.has(_name)) pool.get(_name)(...args);
26
26
  };
27
27
  };
28
+
29
+ function readOnClickEventInfo(e) {
30
+ let item = null;
31
+ const { eventPhase, detail: onClickNum } = e;
32
+ //console.log('CHECK: e => ditail:['+onClickNum+']');
33
+ //console.log('CHECK: e => phase:['+eventPhase+']');
34
+ switch (eventPhase) {
35
+ //*case 1:
36
+ //* // NOTE: currently on eventPhase = 2 and 3
37
+ case 2: {
38
+ /**/// capturing stage
39
+ item = e.target;
40
+ break;
41
+ }
42
+ case 3: {
43
+ /**/// bubblig stage
44
+ item = e.currentTarget;
45
+ break;
46
+ }
47
+ default: {
48
+ break;
49
+ }
50
+ };
51
+ return { item, onClickNum };
52
+ };
53
+
28
54
  // === module main block (class definitions) ===
29
55
 
30
56
  // === module exports block ===
31
57
 
32
58
  exports.pushEventHandler = pushEventHandler;
33
59
  exports.triggerEventHandler = triggerEventHandler;
60
+ exports.readOnClickEventInfo = readOnClickEventInfo;
@@ -1,4 +1,4 @@
1
- // [v0.1.016-20230330]
1
+ // [v0.1.019-20230614]
2
2
 
3
3
  // === module init block ===
4
4
 
@@ -85,39 +85,53 @@ function unmarkActiveHtmlElement(obj){
85
85
  return isSUCCEED;
86
86
  };
87
87
 
88
+ function lockHtmlElement(obj){
89
+ let isSUCCEED = isHTMLElement(obj);
90
+ if (isSUCCEED) {
91
+ const { classList, tagName } = obj;
92
+ classList.add(CSS_CLASS_DISABLED);
93
+ switch (tagName.toLowerCase()) {
94
+ case 'button':
95
+ case 'input': {
96
+ obj.disabled = true;
97
+ break;
98
+ }
99
+ default: {
100
+ break;
101
+ }
102
+ };
103
+ };
104
+ return isSUCCEED;
105
+ };
106
+
107
+ function unlockHtmlElement(obj){
108
+ let isSUCCEED = isHTMLElement(obj);
109
+ if (isSUCCEED) {
110
+ const { classList, tagName } = obj;
111
+ classList.remove(CSS_CLASS_DISABLED);
112
+ switch (tagName.toLowerCase()) {
113
+ case 'button':
114
+ case 'input': {
115
+ obj.disabled = false;
116
+ break;
117
+ }
118
+ default: {
119
+ break;
120
+ }
121
+ };
122
+ };
123
+ return isSUCCEED;
124
+ };
125
+
88
126
  function inactivateHtmlElements(...args) {
89
127
  for (let item of args) {
90
- if (isHTMLElement(item)) {
91
- item.classList.add(CSS_CLASS_DISABLED);
92
- switch (item.tagName.toLowerCase()) {
93
- case 'button':
94
- case 'input': {
95
- item.disabled = true;
96
- break;
97
- }
98
- default: {
99
- break;
100
- }
101
- };
102
- };
128
+ lockHtmlElement(item);
103
129
  };
104
130
  };
105
131
 
106
132
  function activateHtmlElements(...args) {
107
133
  for (let item of args) {
108
- if (isHTMLElement(item)) {
109
- item.classList.remove(CSS_CLASS_DISABLED);
110
- switch (item.tagName.toLowerCase()) {
111
- case 'button':
112
- case 'input': {
113
- item.disabled = false;
114
- break;
115
- }
116
- default: {
117
- break;
118
- }
119
- };
120
- };
134
+ unlockHtmlElement(item);
121
135
  };
122
136
  };
123
137
 
@@ -192,6 +206,7 @@ function createNewHtmlElement(tagName, opt){
192
206
  id,
193
207
  text,
194
208
  attr,
209
+ classNames,
195
210
  class: _class,
196
211
  data: dset,
197
212
  } = opt;
@@ -217,6 +232,7 @@ function createNewHtmlElement(tagName, opt){
217
232
  };
218
233
  };
219
234
  // set a class-attributes of the element
235
+ if (_class === undefined) _class = classNames;
220
236
  if (_class !== undefined) {
221
237
  const cl = valueToClassList(_class, true);
222
238
  if (cl.length > 0) item.classList.add(...cl);
@@ -267,6 +283,8 @@ exports.markHtmlElementAsCurrent = markHtmlElementAsCurrent;
267
283
  exports.unmarkCurrentHtmlElement = unmarkCurrentHtmlElement;
268
284
  exports.markHtmlElementAsActive = markHtmlElementAsActive;
269
285
  exports.unmarkActiveHtmlElement = unmarkActiveHtmlElement;
286
+ exports.lockHtmlElement = lockHtmlElement;
287
+ exports.unlockHtmlElement = unlockHtmlElement;
270
288
  exports.activateHtmlElements = activateHtmlElements;
271
289
  exports.inactivateHtmlElements = inactivateHtmlElements;
272
290
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cntwg/html-helper",
3
- "version": "0.0.18",
3
+ "version": "0.0.19",
4
4
  "description": "A base HTML-helper library for js",
5
5
  "author": "ygracs <cs70th-om@rambler.ru>",
6
6
  "license": "MIT",
@@ -13,11 +13,13 @@
13
13
  "doc/html-helper-lib.md",
14
14
  "doc/html-ctrls-list.md",
15
15
  "doc/html-ctrls-btn.md",
16
+ "doc/html-ctrls-fields.md",
16
17
  "lib/html-helper-lib.js",
17
18
  "lib/html-ctrls/lists-stubs.js",
18
19
  "lib/html-ctrls/lists-btn.js",
19
20
  "lib/html-ctrls/list.js",
20
21
  "lib/html-ctrls/buttons.js",
22
+ "lib/html-ctrls/fields.js",
21
23
  "lib/html-ctrls/mod-hfunc.js",
22
24
  "index.js",
23
25
  "CHANGELOG.md"
@@ -40,11 +42,15 @@
40
42
  "test-bc1:bs": "jest THtmlButtonsControllerARCSet/base",
41
43
  "test-bc1:ec": "jest THtmlButtonsControllerARCSet/events"
42
44
  },
45
+ "imports": {
46
+ "#lib/*": "./lib/*"
47
+ },
43
48
  "dependencies": {
44
- "@ygracs/bsfoc-lib-js": "^0.1.2"
49
+ "@ygracs/bsfoc-lib-js": "^0.1.3"
45
50
  },
46
51
  "devDependencies": {
47
- "jest": "^29.0.3",
48
- "jest-environment-jsdom": "^29.0.3"
52
+ "jest": "^29.5.0",
53
+ "jest-environment-jsdom": "^29.5.0",
54
+ "minimist": "^1.2.8"
49
55
  }
50
56
  }