@cocreate/unique 1.2.33 → 1.3.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 +7 -0
- package/package.json +1 -1
- package/src/client.js +65 -0
- package/src/index.js +14 -65
- package/src/server.js +45 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
# [1.3.0](https://github.com/CoCreate-app/CoCreate-unique/compare/v1.2.33...v1.3.0) (2022-03-03)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Features
|
|
5
|
+
|
|
6
|
+
* manage server and client scripts ([fc75bda](https://github.com/CoCreate-app/CoCreate-unique/commit/fc75bda105c0427dd3890ea9a76d55e596824d3a))
|
|
7
|
+
|
|
1
8
|
## [1.2.33](https://github.com/CoCreate-app/CoCreate-unique/compare/v1.2.32...v1.2.33) (2022-02-24)
|
|
2
9
|
|
|
3
10
|
|
package/package.json
CHANGED
package/src/client.js
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import crud from '@cocreate/crud-client';
|
|
2
|
+
import uuid from '@cocreate/uuid';
|
|
3
|
+
|
|
4
|
+
const CoCreateUnique = {
|
|
5
|
+
|
|
6
|
+
selector: `input[unique], textarea[unique], contenteditable[unique]`,
|
|
7
|
+
requestAttr: "data-unique_request_id",
|
|
8
|
+
|
|
9
|
+
init: function(container) {
|
|
10
|
+
|
|
11
|
+
let mainContainer = container || document;
|
|
12
|
+
|
|
13
|
+
if (!mainContainer.querySelectorAll) {
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
let items = mainContainer.querySelectorAll(this.selector)
|
|
18
|
+
const self = this;
|
|
19
|
+
|
|
20
|
+
items.forEach((item) => {
|
|
21
|
+
const request_id = uuid.generate();
|
|
22
|
+
item.setAttribute(self.requestAttr, request_id);
|
|
23
|
+
self.setInputEvent(item)
|
|
24
|
+
})
|
|
25
|
+
},
|
|
26
|
+
|
|
27
|
+
initListener: function() {
|
|
28
|
+
const self = this;
|
|
29
|
+
crud.listen('checkedUnique', function(data) {
|
|
30
|
+
self.checkedUnique(data)
|
|
31
|
+
})
|
|
32
|
+
},
|
|
33
|
+
|
|
34
|
+
checkedUnique: function(data) {
|
|
35
|
+
const request_id = data['request_id'];
|
|
36
|
+
|
|
37
|
+
if (!request_id) return
|
|
38
|
+
|
|
39
|
+
const element = document.querySelector(`[${this.requestAttr}='${request_id}']`)
|
|
40
|
+
|
|
41
|
+
if (data['unique']) {
|
|
42
|
+
element.setAttribute('unique', true);
|
|
43
|
+
} else {
|
|
44
|
+
element.setAttribute('unique', false);
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
|
|
48
|
+
setInputEvent: function(input) {
|
|
49
|
+
const self = this;
|
|
50
|
+
input.addEventListener('input', function(e) {
|
|
51
|
+
let request_data = {};
|
|
52
|
+
let value = input.getValue(input);
|
|
53
|
+
request_data['organization_id'] = window.config.organization_Id;
|
|
54
|
+
request_data['apiKey'] = window.config.apiKey;
|
|
55
|
+
request_data['collection'] = input.getAttribute('collection');
|
|
56
|
+
request_data['name'] = input.getAttribute('name');
|
|
57
|
+
request_data['value'] = value || e.target.value;
|
|
58
|
+
request_data['request_id'] = input.getAttribute(self.requestAttr);
|
|
59
|
+
crud.send('checkUnique', request_data);
|
|
60
|
+
});
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
CoCreateUnique.init();
|
|
65
|
+
CoCreateUnique.initListener();
|
package/src/index.js
CHANGED
|
@@ -1,65 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
let items = mainContainer.querySelectorAll(this.selector)
|
|
18
|
-
const self = this;
|
|
19
|
-
|
|
20
|
-
items.forEach((item) => {
|
|
21
|
-
const request_id = uuid.generate();
|
|
22
|
-
item.setAttribute(self.requestAttr, request_id);
|
|
23
|
-
self.setInputEvent(item)
|
|
24
|
-
})
|
|
25
|
-
},
|
|
26
|
-
|
|
27
|
-
initListener: function() {
|
|
28
|
-
const self = this;
|
|
29
|
-
crud.listen('checkedUnique', function(data) {
|
|
30
|
-
self.checkedUnique(data)
|
|
31
|
-
})
|
|
32
|
-
},
|
|
33
|
-
|
|
34
|
-
checkedUnique: function(data) {
|
|
35
|
-
const request_id = data['request_id'];
|
|
36
|
-
|
|
37
|
-
if (!request_id) return
|
|
38
|
-
|
|
39
|
-
const element = document.querySelector(`[${this.requestAttr}='${request_id}']`)
|
|
40
|
-
|
|
41
|
-
if (data['unique']) {
|
|
42
|
-
element.setAttribute('unique', true);
|
|
43
|
-
} else {
|
|
44
|
-
element.setAttribute('unique', false);
|
|
45
|
-
}
|
|
46
|
-
},
|
|
47
|
-
|
|
48
|
-
setInputEvent: function(input) {
|
|
49
|
-
const self = this;
|
|
50
|
-
input.addEventListener('input', function(e) {
|
|
51
|
-
let request_data = {};
|
|
52
|
-
let value = input.getValue(input);
|
|
53
|
-
request_data['organization_id'] = window.config.organization_Id;
|
|
54
|
-
request_data['apiKey'] = window.config.apiKey;
|
|
55
|
-
request_data['collection'] = input.getAttribute('collection');
|
|
56
|
-
request_data['name'] = input.getAttribute('name');
|
|
57
|
-
request_data['value'] = value || e.target.value;
|
|
58
|
-
request_data['request_id'] = input.getAttribute(self.requestAttr);
|
|
59
|
-
crud.send('checkUnique', request_data);
|
|
60
|
-
});
|
|
61
|
-
},
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
CoCreateUnique.init();
|
|
65
|
-
CoCreateUnique.initListener();
|
|
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,45 @@
|
|
|
1
|
+
class CoCreateUnique {
|
|
2
|
+
constructor(wsManager, dbClient) {
|
|
3
|
+
this.wsManager = wsManager
|
|
4
|
+
this.dbClient = dbClient
|
|
5
|
+
this.init();
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
init() {
|
|
9
|
+
if (this.wsManager) {
|
|
10
|
+
this.wsManager.on('checkUnique',
|
|
11
|
+
(socket, data, roomInfo) => this.checkUnique(socket, data, roomInfo));
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
async checkUnique(socket, req_data) {
|
|
17
|
+
const self = this
|
|
18
|
+
const db = this.dbClient.db(req_data['organization_id']);
|
|
19
|
+
const collection = db.collection(req_data["collection"]);
|
|
20
|
+
const query = {
|
|
21
|
+
[req_data['name']]: req_data['value']
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
try {
|
|
25
|
+
collection.find(query).toArray(function(error, result) {
|
|
26
|
+
if (!error && result) {
|
|
27
|
+
let response = {
|
|
28
|
+
request_id: req_data['request_id'],
|
|
29
|
+
name: req_data['name'],
|
|
30
|
+
unique: true
|
|
31
|
+
};
|
|
32
|
+
if (result.length) {
|
|
33
|
+
response.unique = false;
|
|
34
|
+
}
|
|
35
|
+
self.wsManager.send(socket, 'checkedUnique', response, req_data['organization_id']);
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
} catch (error) {
|
|
39
|
+
console.log(error);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
module.exports = CoCreateUnique;
|