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