@cocreate/elements 1.38.0 → 1.39.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.
- package/CHANGELOG.md +26 -0
- package/package.json +1 -1
- package/src/form.js +170 -150
- package/src/index.js +1437 -1295
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,29 @@
|
|
|
1
|
+
# [1.39.0](https://github.com/CoCreate-app/CoCreate-elements/compare/v1.38.1...v1.39.0) (2024-12-14)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* allow empty strings and array values ([03ccb89](https://github.com/CoCreate-app/CoCreate-elements/commit/03ccb89cc10262a981c3e66ee279be8a49a58b4b))
|
|
7
|
+
* delete element[i].hasRead in remove function ([e549c3d](https://github.com/CoCreate-app/CoCreate-elements/commit/e549c3d70b7b1701e425e1eadb828df681b619c4))
|
|
8
|
+
* filter.init('el') ([571b179](https://github.com/CoCreate-app/CoCreate-elements/commit/571b179b99b6724988b2a65002bc53d5ba005d02))
|
|
9
|
+
* getValueFromObject inorder to get values using dotNotation ([a101064](https://github.com/CoCreate-app/CoCreate-elements/commit/a101064413587e0a95b6423211f3084ad713e76f))
|
|
10
|
+
* handling crud updates when render element used ([88c5781](https://github.com/CoCreate-app/CoCreate-elements/commit/88c5781e7ce02a672e84bed1bf993bbfcd29f83e))
|
|
11
|
+
* moved observer out of loop as only needs to be declared once. ([4f9c63c](https://github.com/CoCreate-app/CoCreate-elements/commit/4f9c63cfdd3d4c6f065e9604535945420ab32b09))
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
### Features
|
|
15
|
+
|
|
16
|
+
* attribute observer organization_id and host ([28a9175](https://github.com/CoCreate-app/CoCreate-elements/commit/28a9175aa27f5c66205b350e6a4c14b19f014960))
|
|
17
|
+
* Attribute refrence to define whether the crud refrence data will be excluded ([fb1c35d](https://github.com/CoCreate-app/CoCreate-elements/commit/fb1c35de0ebb2cc484d02f367f6d542266f4b8ed))
|
|
18
|
+
|
|
19
|
+
## [1.38.1](https://github.com/CoCreate-app/CoCreate-elements/compare/v1.38.0...v1.38.1) (2024-12-09)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
### Bug Fixes
|
|
23
|
+
|
|
24
|
+
* formating ([b00cd97](https://github.com/CoCreate-app/CoCreate-elements/commit/b00cd97d4b930a6e2d31f0bb0fa99b4f3e9a507e))
|
|
25
|
+
* handling reset type value ([4e10403](https://github.com/CoCreate-app/CoCreate-elements/commit/4e104034219333cbd0f49fd8f46cd9e24bd2dd94))
|
|
26
|
+
|
|
1
27
|
# [1.38.0](https://github.com/CoCreate-app/CoCreate-elements/compare/v1.37.1...v1.38.0) (2024-11-04)
|
|
2
28
|
|
|
3
29
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cocreate/elements",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.39.0",
|
|
4
4
|
"description": "Fetch content into any html element by using html5 attributes to query data stored in CoCreate headless CMS. If data is changed it will instantly reflect in html tags as changes are broadcasted in realtime",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"elements",
|
package/src/form.js
CHANGED
|
@@ -1,190 +1,210 @@
|
|
|
1
|
-
import Observer from
|
|
2
|
-
import { getAttributeNames, ObjectId } from
|
|
3
|
-
import Action from
|
|
4
|
-
import elementPrototype from
|
|
5
|
-
|
|
1
|
+
import Observer from "@cocreate/observer";
|
|
2
|
+
import { getAttributeNames, ObjectId } from "@cocreate/utils";
|
|
3
|
+
import Action from "@cocreate/actions";
|
|
4
|
+
import elementPrototype from "@cocreate/element-prototype";
|
|
6
5
|
|
|
7
6
|
/**
|
|
8
|
-
* Initializes form elements. If no parameter is provided, or null is passed, it queries and initializes all form elements.
|
|
7
|
+
* Initializes form elements. If no parameter is provided, or null is passed, it queries and initializes all form elements.
|
|
9
8
|
* It can also initialize a single form element or an array of form elements.
|
|
10
|
-
*
|
|
9
|
+
*
|
|
11
10
|
* @param {(Element|Element[]|null)} [elements] - Optional. A single form element, an array of form elements, or null.
|
|
12
11
|
* - If null or omitted, the function queries and initializes all form elements.
|
|
13
12
|
* - If a single form element is provided, it initializes that element.
|
|
14
13
|
* - If an array of form elements is provided, each element in the array is initialized.
|
|
15
14
|
*/
|
|
16
15
|
function init(elements) {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
runObjectId(element);
|
|
34
|
-
setAttribute(element);
|
|
35
|
-
disableAutoFill(element);
|
|
36
|
-
setValue(element)
|
|
37
|
-
element.addEventListener('submit', function (event) {
|
|
38
|
-
if (!element.hasAttribute('action')) {
|
|
39
|
-
event.preventDefault();
|
|
40
|
-
}
|
|
41
|
-
});
|
|
42
|
-
}
|
|
16
|
+
if (!elements) elements = document.querySelectorAll("form");
|
|
17
|
+
else if (!Array.isArray(elements)) elements = [elements];
|
|
18
|
+
|
|
19
|
+
for (let element of elements) {
|
|
20
|
+
runObjectId(element);
|
|
21
|
+
setAttribute(element);
|
|
22
|
+
disableAutoFill(element);
|
|
23
|
+
setValue(element);
|
|
24
|
+
element.addEventListener("submit", function (event) {
|
|
25
|
+
if (!element.hasAttribute("action")) {
|
|
26
|
+
event.preventDefault();
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
}
|
|
43
30
|
}
|
|
44
31
|
|
|
45
32
|
/**
|
|
46
|
-
* @param form
|
|
47
|
-
*/
|
|
33
|
+
* @param form
|
|
34
|
+
*/
|
|
35
|
+
// TODO: runObjectId could potentially be removed and handled by element-prototype getAttribute $object_id
|
|
36
|
+
// Somthing to consider is that $object_id is not replaced by an _id causing getAttribute to return a new _id on each get
|
|
48
37
|
function runObjectId(form) {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
}
|
|
38
|
+
let elements = Array.from(form.querySelectorAll("[object='ObjectId()']"));
|
|
39
|
+
|
|
40
|
+
if (form.getAttribute("object") === "ObjectId()") {
|
|
41
|
+
elements.push(form);
|
|
42
|
+
}
|
|
43
|
+
for (let i = 0; i < elements.length; i++) {
|
|
44
|
+
let array = elements[i].getAttribute("array");
|
|
45
|
+
if (!array) continue;
|
|
46
|
+
elements[i].setAttribute("object", ObjectId().toString());
|
|
47
|
+
}
|
|
61
48
|
}
|
|
62
49
|
|
|
63
50
|
/**
|
|
64
|
-
* @param form
|
|
65
|
-
*/
|
|
51
|
+
* @param form
|
|
52
|
+
*/
|
|
66
53
|
function setAttribute(form, elements) {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
54
|
+
if (!elements)
|
|
55
|
+
elements = form.querySelectorAll(
|
|
56
|
+
"[organization_id], [host], [storage], [database], [array], [index], [object], [key]"
|
|
57
|
+
);
|
|
58
|
+
|
|
59
|
+
for (let attribute of form.attributes) {
|
|
60
|
+
let variable = window.CoCreateConfig.attributes[attribute.name];
|
|
61
|
+
|
|
62
|
+
// Set the attribute of all elements in the variable
|
|
63
|
+
if (variable) {
|
|
64
|
+
for (let el of elements) {
|
|
65
|
+
// Set the value of the attribute.
|
|
66
|
+
// TODO: skip-attribute naming convention, perhaps skip by defualt if storage, database, array not the same and use attribute to apply for cases where one _id will be used across 2 arrays
|
|
67
|
+
if (
|
|
68
|
+
!el.getAttribute(attribute.name) &&
|
|
69
|
+
!el.hasAttribute("skip-attribute")
|
|
70
|
+
) {
|
|
71
|
+
el.setAttribute(attribute.name, attribute.value);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
84
76
|
}
|
|
85
77
|
|
|
86
78
|
/**
|
|
87
|
-
* @param element
|
|
88
|
-
*/
|
|
79
|
+
* @param element
|
|
80
|
+
*/
|
|
89
81
|
function disableAutoFill(element) {
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
82
|
+
// Clear the text area and set the autocomplete attribute to off.
|
|
83
|
+
if (element.tagName == "TEXTAREA") {
|
|
84
|
+
element.value = "";
|
|
85
|
+
element.setAttribute("autocomplete", "off");
|
|
86
|
+
}
|
|
87
|
+
// Set the autocomplete attribute to off.
|
|
88
|
+
if (!element.hasAttribute("autocomplete")) {
|
|
89
|
+
element.setAttribute("autocomplete", "off");
|
|
90
|
+
}
|
|
99
91
|
}
|
|
100
92
|
|
|
101
93
|
/**
|
|
102
|
-
* @param form
|
|
103
|
-
*/
|
|
94
|
+
* @param form
|
|
95
|
+
*/
|
|
104
96
|
function reset(form) {
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
97
|
+
// Convert the form elements collection to an array
|
|
98
|
+
const formElementsArray = Array.from(form.elements);
|
|
99
|
+
|
|
100
|
+
// Query for additional elements with [object] or [key] attributes that are not already part of form controls
|
|
101
|
+
const customElements = Array.from(
|
|
102
|
+
form.querySelectorAll(
|
|
103
|
+
"[object], [key]:not(input):not(select):not(textarea):not(button)"
|
|
104
|
+
)
|
|
105
|
+
);
|
|
106
|
+
|
|
107
|
+
// Merge form elements and custom elements using the spread operator
|
|
108
|
+
const allElements = [...formElementsArray, ...customElements];
|
|
109
|
+
|
|
110
|
+
// Store the elements and their values in a map for restoration
|
|
111
|
+
const elementStates = new Map();
|
|
112
|
+
|
|
113
|
+
// Iterate over all elements and store their current state based on the 'reset' attribute
|
|
114
|
+
for (const element of allElements) {
|
|
115
|
+
if (["BUTTON", "FIELDSET"].includes(element.tagName)) continue;
|
|
116
|
+
// Get the reset attribute value, if any
|
|
117
|
+
const resetType = element.getAttribute("reset");
|
|
118
|
+
if (
|
|
119
|
+
element.hasAttribute("object") &&
|
|
120
|
+
(!resetType || resetType === "object")
|
|
121
|
+
)
|
|
122
|
+
element.setAttribute("object", "");
|
|
123
|
+
if (resetType === "false" || resetType === "object")
|
|
124
|
+
elementStates.set(
|
|
125
|
+
element,
|
|
126
|
+
element.getValue() ||
|
|
127
|
+
element.value ||
|
|
128
|
+
element.getAttribute("value")
|
|
129
|
+
);
|
|
130
|
+
if (!resetType || resetType === "value") element.setValue("");
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
if (form.hasAttribute("object")) form.setAttribute("object", "");
|
|
134
|
+
|
|
135
|
+
// Perform the default form reset
|
|
136
|
+
form.reset();
|
|
137
|
+
|
|
138
|
+
// Restore values based on the 'reset' attribute
|
|
139
|
+
elementStates.forEach((value, element) => {
|
|
140
|
+
element.setValue(value);
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
// Dispatch a custom reset event
|
|
144
|
+
document.dispatchEvent(
|
|
145
|
+
new CustomEvent("reset", {
|
|
146
|
+
detail: {}
|
|
147
|
+
})
|
|
148
|
+
);
|
|
146
149
|
}
|
|
147
150
|
|
|
148
151
|
function setValue(form) {
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
}
|
|
152
|
+
form.setValue = (value) => {
|
|
153
|
+
if (typeof value !== "object") elementPrototype.setValue(form, value);
|
|
154
|
+
else {
|
|
155
|
+
const inputs = form.querySelectorAll("[name], [key]");
|
|
156
|
+
inputs.forEach((element) => {
|
|
157
|
+
const key =
|
|
158
|
+
element.getAttribute("key") || element.getAttribute("name");
|
|
159
|
+
if (value[key]) {
|
|
160
|
+
element.setValue(value[key]);
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
};
|
|
163
165
|
}
|
|
164
166
|
|
|
165
167
|
Observer.init({
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
168
|
+
name: "CoCreateForm",
|
|
169
|
+
observe: ["addedNodes"],
|
|
170
|
+
selector: "form",
|
|
171
|
+
callback: (mutation) => init(mutation.target)
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
Observer.init({
|
|
175
|
+
name: "CoCreateForm",
|
|
176
|
+
observe: ["attributes"],
|
|
177
|
+
attributeName: getAttributeNames([
|
|
178
|
+
"organization_id",
|
|
179
|
+
"host",
|
|
180
|
+
"storage",
|
|
181
|
+
"database",
|
|
182
|
+
"array",
|
|
183
|
+
"object",
|
|
184
|
+
"index"
|
|
185
|
+
]),
|
|
186
|
+
selector: "form",
|
|
187
|
+
callback: (mutation) =>
|
|
188
|
+
mutation.target.tagName === "FORM" && setAttribute(mutation.target)
|
|
170
189
|
});
|
|
171
190
|
|
|
172
191
|
Observer.init({
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
192
|
+
name: "CoCreateFormElements",
|
|
193
|
+
observe: ["addedNodes"],
|
|
194
|
+
selector:
|
|
195
|
+
"[organization_id], [host], [storage], [database], [array], [index], [object], [key]",
|
|
196
|
+
callback: function (mutation) {
|
|
197
|
+
if (mutation.target.form)
|
|
198
|
+
setAttribute(mutation.target.form, [mutation.target]);
|
|
199
|
+
}
|
|
179
200
|
});
|
|
180
201
|
|
|
181
202
|
Action.init({
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
}
|
|
203
|
+
name: "reset",
|
|
204
|
+
endEvent: "reset",
|
|
205
|
+
callback: (action) => {
|
|
206
|
+
if (action.form) reset(action.form);
|
|
207
|
+
}
|
|
188
208
|
});
|
|
189
209
|
|
|
190
210
|
init();
|