@cocreate/unique 1.20.1 → 1.20.2
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 +7 -0
- package/package.json +1 -1
- package/src/client.js +54 -61
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
## [1.20.2](https://github.com/CoCreate-app/CoCreate-unique/compare/v1.20.1...v1.20.2) (2025-04-11)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* update observer observe param to type and and attributeName to attributeFilter ([81b8d45](https://github.com/CoCreate-app/CoCreate-unique/commit/81b8d45850eb719076fa25334ad3954ec1326fff))
|
|
7
|
+
|
|
1
8
|
## [1.20.1](https://github.com/CoCreate-app/CoCreate-unique/compare/v1.20.0...v1.20.1) (2024-12-09)
|
|
2
9
|
|
|
3
10
|
|
package/package.json
CHANGED
package/src/client.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import observer from
|
|
2
|
-
import crud from
|
|
3
|
-
|
|
1
|
+
import observer from "@cocreate/observer";
|
|
2
|
+
import crud from "@cocreate/crud-client";
|
|
4
3
|
|
|
5
4
|
/**
|
|
6
5
|
* Initializes elements to ensure their values are unique. This function targets elements with the 'unique' attribute and uses CRUD operations to check if the element's value already exists.
|
|
@@ -11,81 +10,75 @@ import crud from '@cocreate/crud-client';
|
|
|
11
10
|
* - If null or omitted, all elements in the document with the 'unique' attribute are queried and processed for uniqueness checks.
|
|
12
11
|
*/
|
|
13
12
|
function init(elements) {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
elements = [elements]
|
|
18
|
-
for (let element of elements)
|
|
19
|
-
setInputEvent(element)
|
|
13
|
+
if (!elements) elements = document.querySelectorAll("[unique]");
|
|
14
|
+
else if (!Array.isArray(elements)) elements = [elements];
|
|
15
|
+
for (let element of elements) setInputEvent(element);
|
|
20
16
|
}
|
|
21
17
|
|
|
22
18
|
/**
|
|
23
|
-
* Sets the input event to prevent duplicate. This is necessary because IE doesn't support unique = " true " in HTML5.
|
|
24
|
-
*
|
|
25
|
-
* @param input - The input element to listen for changes on. Note that it's an object
|
|
26
|
-
*/
|
|
19
|
+
* Sets the input event to prevent duplicate. This is necessary because IE doesn't support unique = " true " in HTML5.
|
|
20
|
+
*
|
|
21
|
+
* @param input - The input element to listen for changes on. Note that it's an object
|
|
22
|
+
*/
|
|
27
23
|
function setInputEvent(input) {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
24
|
+
let delayTimer;
|
|
25
|
+
input.addEventListener("input", function () {
|
|
26
|
+
input.setAttribute("unique", true);
|
|
31
27
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
28
|
+
clearTimeout(delayTimer);
|
|
29
|
+
delayTimer = setTimeout(function () {
|
|
30
|
+
isUnique(input);
|
|
31
|
+
}, 1000);
|
|
32
|
+
});
|
|
37
33
|
}
|
|
38
34
|
|
|
39
35
|
/**
|
|
40
|
-
* Checks if a value is unique. This is a helper function for CRUD operations that need to be performed on objects in order to make sure they are unique.
|
|
41
|
-
*
|
|
42
|
-
* @param element - The element that is being checked for uniqueness. It should have the attribute ` name `
|
|
43
|
-
*/
|
|
36
|
+
* Checks if a value is unique. This is a helper function for CRUD operations that need to be performed on objects in order to make sure they are unique.
|
|
37
|
+
*
|
|
38
|
+
* @param element - The element that is being checked for uniqueness. It should have the attribute ` name `
|
|
39
|
+
*/
|
|
44
40
|
async function isUnique(element) {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
data = await crud.send(data)
|
|
41
|
+
let key = element.getAttribute("key");
|
|
42
|
+
let value = await element.getValue();
|
|
43
|
+
let data = {
|
|
44
|
+
method: "object.read",
|
|
45
|
+
storage: "indexeddb",
|
|
46
|
+
array: element.getAttribute("array"),
|
|
47
|
+
$filter: {
|
|
48
|
+
query: {
|
|
49
|
+
[key]: value
|
|
50
|
+
},
|
|
51
|
+
limit: 1
|
|
52
|
+
}
|
|
53
|
+
};
|
|
60
54
|
|
|
61
|
-
|
|
62
|
-
// If a object is returned, unique is set to false
|
|
63
|
-
if (data.object && data.object.length) {
|
|
64
|
-
response.unique = false;
|
|
65
|
-
} else
|
|
66
|
-
response.unique = true
|
|
55
|
+
data = await crud.send(data);
|
|
67
56
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
}
|
|
57
|
+
let response = {};
|
|
58
|
+
// If a object is returned, unique is set to false
|
|
59
|
+
if (data.object && data.object.length) {
|
|
60
|
+
response.unique = false;
|
|
61
|
+
} else response.unique = true;
|
|
74
62
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
63
|
+
// If indexedb response is unique is true, check server response
|
|
64
|
+
if (response.unique) {
|
|
65
|
+
delete data.storage;
|
|
66
|
+
data.method = "isUnique";
|
|
67
|
+
response = await crud.socket.send(data);
|
|
68
|
+
}
|
|
78
69
|
|
|
70
|
+
// Set unique attribute on the element
|
|
71
|
+
element.setAttribute("unique", response.unique);
|
|
72
|
+
return response.unique;
|
|
79
73
|
}
|
|
80
74
|
|
|
81
75
|
observer.init({
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
init(mutation.target)
|
|
76
|
+
name: "CoCreateUnique",
|
|
77
|
+
types: ["addedNodes"],
|
|
78
|
+
selector: "[unique]",
|
|
79
|
+
callback: (mutation) => init(mutation.target)
|
|
87
80
|
});
|
|
88
81
|
|
|
89
82
|
init();
|
|
90
83
|
|
|
91
|
-
export default { init, isUnique }
|
|
84
|
+
export default { init, isUnique };
|