@cocreate/unique 1.10.8 → 1.11.1
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 +22 -0
- package/package.json +1 -1
- package/src/client.js +94 -0
- package/src/index.js +15 -63
- package/src/server.js +48 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,25 @@
|
|
|
1
|
+
## [1.11.1](https://github.com/CoCreate-app/CoCreate-unique/compare/v1.11.0...v1.11.1) (2023-05-18)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* Refactor module imports for client file ([c0b1f24](https://github.com/CoCreate-app/CoCreate-unique/commit/c0b1f246e5adc69d953f62447b4fd4a5c8b6ae9e))
|
|
7
|
+
|
|
8
|
+
# [1.11.0](https://github.com/CoCreate-app/CoCreate-unique/compare/v1.10.8...v1.11.0) (2023-05-17)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Bug Fixes
|
|
12
|
+
|
|
13
|
+
* Refactor server.js to include the uid field in the response object for readDocument method. ([beb58e2](https://github.com/CoCreate-app/CoCreate-unique/commit/beb58e20f3f8e9b8ed7d5ffb646b3af0897ef787))
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
### Features
|
|
17
|
+
|
|
18
|
+
* Add "unique" attribute to input fields ([ebb0765](https://github.com/CoCreate-app/CoCreate-unique/commit/ebb076581ca1b64df4a27dd21f5d56287eae4674))
|
|
19
|
+
* Add unique input check and server implementation ([5469faa](https://github.com/CoCreate-app/CoCreate-unique/commit/5469faae84157d45b1fa583e1c20336cbeb8b500))
|
|
20
|
+
* Import Observer and pass unique attributes to target and callback. ([a63801d](https://github.com/CoCreate-app/CoCreate-unique/commit/a63801da65d9cbeca605bd5ba0f66e43d1dd74c1))
|
|
21
|
+
* Refactor client.js init() to take an element arg and handle arrays ([4e63aa0](https://github.com/CoCreate-app/CoCreate-unique/commit/4e63aa070d79ae817b4364582f23b3af2f772b2d))
|
|
22
|
+
|
|
1
23
|
## [1.10.8](https://github.com/CoCreate-app/CoCreate-unique/compare/v1.10.7...v1.10.8) (2023-05-10)
|
|
2
24
|
|
|
3
25
|
|
package/package.json
CHANGED
package/src/client.js
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import observer from '@cocreate/observer';
|
|
2
|
+
import crud from '@cocreate/crud-client';
|
|
3
|
+
|
|
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
|
+
*/
|
|
10
|
+
function init(elements) {
|
|
11
|
+
// Returns an array of unique elements
|
|
12
|
+
if (!elements)
|
|
13
|
+
elements = document.querySelectorAll('[unique]')
|
|
14
|
+
// If elements is not an array of elements returns an array of elements.
|
|
15
|
+
else if (!Array.isArray(elements))
|
|
16
|
+
elements = [elements]
|
|
17
|
+
for (let element of elements)
|
|
18
|
+
setInputEvent(element)
|
|
19
|
+
}
|
|
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
|
+
*/
|
|
26
|
+
function setInputEvent(input) {
|
|
27
|
+
let delayTimer
|
|
28
|
+
input.addEventListener('input', function () {
|
|
29
|
+
input.setAttribute('unique', true);
|
|
30
|
+
|
|
31
|
+
clearTimeout(delayTimer);
|
|
32
|
+
delayTimer = setTimeout(function () {
|
|
33
|
+
isUnique(input)
|
|
34
|
+
}, 1000);
|
|
35
|
+
});
|
|
36
|
+
}
|
|
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
|
+
*/
|
|
43
|
+
async function isUnique(element) {
|
|
44
|
+
let name = element.getAttribute('name');
|
|
45
|
+
let value = element.getValue();
|
|
46
|
+
let request = {
|
|
47
|
+
db: 'indexeddb',
|
|
48
|
+
collection: element.getAttribute('collection'),
|
|
49
|
+
filter: {
|
|
50
|
+
query: [{
|
|
51
|
+
name,
|
|
52
|
+
value,
|
|
53
|
+
operator: '$eq'
|
|
54
|
+
}]
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
let data = await crud.readDocument(request)
|
|
59
|
+
let response = {
|
|
60
|
+
element,
|
|
61
|
+
name,
|
|
62
|
+
unique: true
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
// If a document is returned, unique is set to false
|
|
66
|
+
if (data.document && data.document.length) {
|
|
67
|
+
response.unique = false;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// If indexedb response is unique is true, check server response
|
|
71
|
+
if (response.unique) {
|
|
72
|
+
delete request.db
|
|
73
|
+
response = await crud.socket.send('isUnique', request)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// Set unique attribute on the element
|
|
77
|
+
if (response.unique)
|
|
78
|
+
element.setAttribute('unique', true);
|
|
79
|
+
else
|
|
80
|
+
element.setAttribute('unique', false);
|
|
81
|
+
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
observer.init({
|
|
85
|
+
name: 'CoCreateUnique',
|
|
86
|
+
observe: ['addedNodes'],
|
|
87
|
+
target: '[unique]',
|
|
88
|
+
callback: mutation =>
|
|
89
|
+
init(mutation.target)
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
init();
|
|
93
|
+
|
|
94
|
+
export default { init, isUnique }
|
package/src/index.js
CHANGED
|
@@ -1,63 +1,15 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
const self = this;
|
|
17
|
-
|
|
18
|
-
items.forEach((item) => {
|
|
19
|
-
self.setInputEvent(item)
|
|
20
|
-
})
|
|
21
|
-
},
|
|
22
|
-
|
|
23
|
-
checkedUnique: function(data) {
|
|
24
|
-
const element = data.input
|
|
25
|
-
if (data['unique'])
|
|
26
|
-
element.setAttribute('unique', true);
|
|
27
|
-
else
|
|
28
|
-
element.setAttribute('unique', false);
|
|
29
|
-
},
|
|
30
|
-
|
|
31
|
-
setInputEvent: function(input) {
|
|
32
|
-
const self = this;
|
|
33
|
-
input.addEventListener('input', function(e) {
|
|
34
|
-
let name = input.getAttribute('name');
|
|
35
|
-
let value = input.getValue();
|
|
36
|
-
let request = {
|
|
37
|
-
collection: input.getAttribute('collection'),
|
|
38
|
-
filter: {
|
|
39
|
-
query: [{
|
|
40
|
-
name,
|
|
41
|
-
value,
|
|
42
|
-
operator: '$eq'
|
|
43
|
-
}]
|
|
44
|
-
}
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
crud.readDocument(request).then((data) => {
|
|
48
|
-
let response = {
|
|
49
|
-
input,
|
|
50
|
-
name,
|
|
51
|
-
unique: true
|
|
52
|
-
};
|
|
53
|
-
if (data.document && data.document.length) {
|
|
54
|
-
response.unique = false;
|
|
55
|
-
}
|
|
56
|
-
self.checkedUnique(response)
|
|
57
|
-
})
|
|
58
|
-
|
|
59
|
-
});
|
|
60
|
-
},
|
|
61
|
-
};
|
|
62
|
-
|
|
63
|
-
CoCreateUnique.init();
|
|
1
|
+
// Check if browser, return client or server file
|
|
2
|
+
(function (root, factory) {
|
|
3
|
+
if (typeof define === 'function' && define.amd) {
|
|
4
|
+
define(["./client.js"], function (CoCreateUnique) {
|
|
5
|
+
return factory(CoCreateUnique)
|
|
6
|
+
});
|
|
7
|
+
} else if (typeof module === 'object' && module.exports) {
|
|
8
|
+
const CoCreateUnique = require("./server.js")
|
|
9
|
+
module.exports = factory(CoCreateUnique);
|
|
10
|
+
} else {
|
|
11
|
+
root.returnExports = factory(root["./client.js"]);
|
|
12
|
+
}
|
|
13
|
+
}(typeof self !== 'undefined' ? self : this, function (CoCreateUnique) {
|
|
14
|
+
return CoCreateUnique;
|
|
15
|
+
}));
|
package/src/server.js
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
class CoCreateUnique {
|
|
2
|
+
/**
|
|
3
|
+
* @param crud
|
|
4
|
+
*/
|
|
5
|
+
constructor(crud) {
|
|
6
|
+
this.wsManager = crud.wsManager
|
|
7
|
+
this.crud = crud
|
|
8
|
+
this.init();
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
init() {
|
|
12
|
+
if (this.wsManager) {
|
|
13
|
+
this.wsManager.on('isUnique',
|
|
14
|
+
(socket, data) => this.isUnique(socket, data));
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
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
|
+
*/
|
|
27
|
+
async isUnique(socket, data) {
|
|
28
|
+
const self = this
|
|
29
|
+
try {
|
|
30
|
+
this.crud.readDocument(data).then((data) => {
|
|
31
|
+
let response = {
|
|
32
|
+
unique: true,
|
|
33
|
+
uid: data.uid
|
|
34
|
+
};
|
|
35
|
+
// If the document is unique
|
|
36
|
+
if (data.document.length) {
|
|
37
|
+
response.unique = false;
|
|
38
|
+
}
|
|
39
|
+
return self.wsManager.send(socket, 'isUnique', response);
|
|
40
|
+
})
|
|
41
|
+
} catch (error) {
|
|
42
|
+
console.log(error);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
module.exports = CoCreateUnique;
|