@cocreate/unique 1.11.0 → 1.11.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 +14 -0
- package/package.json +4 -4
- package/src/client.js +23 -1
- package/src/index.js +2 -1
- package/src/server.js +13 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,17 @@
|
|
|
1
|
+
## [1.11.2](https://github.com/CoCreate-app/CoCreate-unique/compare/v1.11.1...v1.11.2) (2023-05-19)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* update packages to latest version. This commit updates various packages in the dependencies section of the package.json file to their latest published versions, thereby fixing multiple bugs and improving overall performance. ([233efdf](https://github.com/CoCreate-app/CoCreate-unique/commit/233efdfa04d76a7823e028c4ab9e13e174822587))
|
|
7
|
+
|
|
8
|
+
## [1.11.1](https://github.com/CoCreate-app/CoCreate-unique/compare/v1.11.0...v1.11.1) (2023-05-18)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Bug Fixes
|
|
12
|
+
|
|
13
|
+
* Refactor module imports for client file ([c0b1f24](https://github.com/CoCreate-app/CoCreate-unique/commit/c0b1f246e5adc69d953f62447b4fd4a5c8b6ae9e))
|
|
14
|
+
|
|
1
15
|
# [1.11.0](https://github.com/CoCreate-app/CoCreate-unique/compare/v1.10.8...v1.11.0) (2023-05-17)
|
|
2
16
|
|
|
3
17
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cocreate/unique",
|
|
3
|
-
"version": "1.11.
|
|
3
|
+
"version": "1.11.2",
|
|
4
4
|
"description": "A simple unique component in vanilla javascript. Easily configured using HTML5 attributes and/or JavaScript API.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"unique",
|
|
@@ -59,8 +59,8 @@
|
|
|
59
59
|
"webpack-log": "^3.0.1"
|
|
60
60
|
},
|
|
61
61
|
"dependencies": {
|
|
62
|
-
"@cocreate/crud-client": "^1.
|
|
63
|
-
"@cocreate/docs": "^1.7.
|
|
64
|
-
"@cocreate/uuid": "^1.4.
|
|
62
|
+
"@cocreate/crud-client": "^1.21.1",
|
|
63
|
+
"@cocreate/docs": "^1.7.10",
|
|
64
|
+
"@cocreate/uuid": "^1.4.9"
|
|
65
65
|
}
|
|
66
66
|
}
|
package/src/client.js
CHANGED
|
@@ -2,18 +2,30 @@ import observer from '@cocreate/observer';
|
|
|
2
2
|
import crud from '@cocreate/crud-client';
|
|
3
3
|
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
* Initializes input event listeners for the specified elements. This is a convenience method for calling setInputEvent on each element.
|
|
7
|
+
*
|
|
8
|
+
* @param elements - The HTMLElement or array of HTMLElements to listen
|
|
9
|
+
*/
|
|
5
10
|
function init(elements) {
|
|
11
|
+
// Returns an array of unique elements
|
|
6
12
|
if (!elements)
|
|
7
13
|
elements = document.querySelectorAll('[unique]')
|
|
14
|
+
// If elements is not an array of elements returns an array of elements.
|
|
8
15
|
else if (!Array.isArray(elements))
|
|
9
16
|
elements = [elements]
|
|
10
17
|
for (let element of elements)
|
|
11
18
|
setInputEvent(element)
|
|
12
19
|
}
|
|
13
20
|
|
|
21
|
+
/**
|
|
22
|
+
* Sets the input event to prevent duplicate. This is necessary because IE doesn't support unique = " true " in HTML5.
|
|
23
|
+
*
|
|
24
|
+
* @param input - The input element to listen for changes on. Note that it's an object
|
|
25
|
+
*/
|
|
14
26
|
function setInputEvent(input) {
|
|
15
27
|
let delayTimer
|
|
16
|
-
input.addEventListener('input', function (
|
|
28
|
+
input.addEventListener('input', function () {
|
|
17
29
|
input.setAttribute('unique', true);
|
|
18
30
|
|
|
19
31
|
clearTimeout(delayTimer);
|
|
@@ -23,6 +35,11 @@ function setInputEvent(input) {
|
|
|
23
35
|
});
|
|
24
36
|
}
|
|
25
37
|
|
|
38
|
+
/**
|
|
39
|
+
* Checks if a value is unique. This is a helper function for CRUD operations that need to be performed on documents in order to make sure they are unique.
|
|
40
|
+
*
|
|
41
|
+
* @param element - The element that is being checked for uniqueness. It should have the attribute ` name `
|
|
42
|
+
*/
|
|
26
43
|
async function isUnique(element) {
|
|
27
44
|
let name = element.getAttribute('name');
|
|
28
45
|
let value = element.getValue();
|
|
@@ -44,14 +61,19 @@ async function isUnique(element) {
|
|
|
44
61
|
name,
|
|
45
62
|
unique: true
|
|
46
63
|
};
|
|
64
|
+
|
|
65
|
+
// If a document is returned, unique is set to false
|
|
47
66
|
if (data.document && data.document.length) {
|
|
48
67
|
response.unique = false;
|
|
49
68
|
}
|
|
69
|
+
|
|
70
|
+
// If indexedb response is unique is true, check server response
|
|
50
71
|
if (response.unique) {
|
|
51
72
|
delete request.db
|
|
52
73
|
response = await crud.socket.send('isUnique', request)
|
|
53
74
|
}
|
|
54
75
|
|
|
76
|
+
// Set unique attribute on the element
|
|
55
77
|
if (response.unique)
|
|
56
78
|
element.setAttribute('unique', true);
|
|
57
79
|
else
|
package/src/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
+
// Check if browser, return client or server file
|
|
1
2
|
(function (root, factory) {
|
|
2
3
|
if (typeof define === 'function' && define.amd) {
|
|
3
|
-
define(["./client"], function (CoCreateUnique) {
|
|
4
|
+
define(["./client.js"], function (CoCreateUnique) {
|
|
4
5
|
return factory(CoCreateUnique)
|
|
5
6
|
});
|
|
6
7
|
} else if (typeof module === 'object' && module.exports) {
|
package/src/server.js
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
class CoCreateUnique {
|
|
2
|
+
/**
|
|
3
|
+
* @param crud
|
|
4
|
+
*/
|
|
2
5
|
constructor(crud) {
|
|
3
6
|
this.wsManager = crud.wsManager
|
|
4
7
|
this.crud = crud
|
|
@@ -13,6 +16,14 @@ class CoCreateUnique {
|
|
|
13
16
|
}
|
|
14
17
|
|
|
15
18
|
|
|
19
|
+
/**
|
|
20
|
+
* Checks if a document is unique by sending a message to the socket. This is a blocking call so it returns a Promise
|
|
21
|
+
*
|
|
22
|
+
* @param socket - the socket to send the message to
|
|
23
|
+
* @param data - the data to read from the database usually an object
|
|
24
|
+
*
|
|
25
|
+
* @return { Promise } - resolves with the response from the server or rejects with an error if there was a
|
|
26
|
+
*/
|
|
16
27
|
async isUnique(socket, data) {
|
|
17
28
|
const self = this
|
|
18
29
|
try {
|
|
@@ -21,10 +32,11 @@ class CoCreateUnique {
|
|
|
21
32
|
unique: true,
|
|
22
33
|
uid: data.uid
|
|
23
34
|
};
|
|
35
|
+
// If the document is unique
|
|
24
36
|
if (data.document.length) {
|
|
25
37
|
response.unique = false;
|
|
26
38
|
}
|
|
27
|
-
self.wsManager.send(socket, 'isUnique', response);
|
|
39
|
+
return self.wsManager.send(socket, 'isUnique', response);
|
|
28
40
|
})
|
|
29
41
|
} catch (error) {
|
|
30
42
|
console.log(error);
|