@cocreate/utils 1.10.23 → 1.12.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 +19 -0
- package/package.json +2 -2
- package/src/{index.old.js → archive.js} +29 -0
- package/src/index.js +14 -267
- package/src/utils.js +510 -0
- package/src/encodedecode.js +0 -111
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,22 @@
|
|
|
1
|
+
# [1.12.0](https://github.com/CoCreate-app/CoCreate-utils/compare/v1.11.0...v1.12.0) (2022-11-25)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Features
|
|
5
|
+
|
|
6
|
+
* functions for search, query and sorting objects and arrays ([a0ef76d](https://github.com/CoCreate-app/CoCreate-utils/commit/a0ef76d24969e3679159987b2f3c4633bc46c138))
|
|
7
|
+
|
|
8
|
+
# [1.11.0](https://github.com/CoCreate-app/CoCreate-utils/compare/v1.10.23...v1.11.0) (2022-11-25)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Bug Fixes
|
|
12
|
+
|
|
13
|
+
* add valueTypes to archive ([6fd51ff](https://github.com/CoCreate-app/CoCreate-utils/commit/6fd51ffe880699b5303be7f2452a363cc3737716))
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
### Features
|
|
17
|
+
|
|
18
|
+
* utils can be used in browser or server ([f8ad791](https://github.com/CoCreate-app/CoCreate-utils/commit/f8ad7913e5d0c658ca3ef58bda09082dde8b67ef))
|
|
19
|
+
|
|
1
20
|
## [1.10.23](https://github.com/CoCreate-app/CoCreate-utils/compare/v1.10.22...v1.10.23) (2022-11-24)
|
|
2
21
|
|
|
3
22
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cocreate/utils",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.12.0",
|
|
4
4
|
"description": "A simple utils component in vanilla javascript. Easily configured using HTML5 attributes and/or JavaScript API.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"utils",
|
|
@@ -61,6 +61,6 @@
|
|
|
61
61
|
"webpack-log": "^3.0.1"
|
|
62
62
|
},
|
|
63
63
|
"dependencies": {
|
|
64
|
-
"@cocreate/docs": "^1.4.
|
|
64
|
+
"@cocreate/docs": "^1.4.4"
|
|
65
65
|
}
|
|
66
66
|
}
|
|
@@ -443,6 +443,35 @@ async function complexSelector(comSelector, callback) {
|
|
|
443
443
|
return objectData;
|
|
444
444
|
}
|
|
445
445
|
|
|
446
|
+
function valueTypes(data) {
|
|
447
|
+
let object = {}
|
|
448
|
+
if ( typeof data === 'object' ) {
|
|
449
|
+
// update['$set'] = {}
|
|
450
|
+
for (let [key, value] of Object.entries(data)) {
|
|
451
|
+
let val;
|
|
452
|
+
let valueType = typeof value;
|
|
453
|
+
switch(valueType) {
|
|
454
|
+
case 'string':
|
|
455
|
+
val = value
|
|
456
|
+
break;
|
|
457
|
+
case 'number':
|
|
458
|
+
val = Number(value)
|
|
459
|
+
break;
|
|
460
|
+
case 'object':
|
|
461
|
+
if (Array.isArray(value))
|
|
462
|
+
val = new Array(...value)
|
|
463
|
+
else
|
|
464
|
+
val = new Object(value)
|
|
465
|
+
break;
|
|
466
|
+
default:
|
|
467
|
+
val = value
|
|
468
|
+
}
|
|
469
|
+
object[key] = val
|
|
470
|
+
}
|
|
471
|
+
return object;
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
|
|
446
475
|
|
|
447
476
|
export default {
|
|
448
477
|
getElementPath,
|
package/src/index.js
CHANGED
|
@@ -1,267 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
function
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
frameDocuments.set(frameDocument, '')
|
|
16
|
-
frameDocument.addEventListener('click', e => {
|
|
17
|
-
frameDocument.clickedElement = e.target;
|
|
18
|
-
});
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export function dotNotationToObject(data, obj = {}) {
|
|
24
|
-
try {
|
|
25
|
-
for (const [key, value] of Object.entries(data)) {
|
|
26
|
-
let newObject = obj
|
|
27
|
-
let oldObject = new Object(obj)
|
|
28
|
-
let keys = key.split('.');
|
|
29
|
-
let length = keys.length - 1
|
|
30
|
-
for (let i = 0; i < keys.length; i++) {
|
|
31
|
-
if (/\[([0-9]*)\]/g.test(keys[i])){
|
|
32
|
-
let [k, index] = keys[i].split('[');
|
|
33
|
-
index = index.slice(0, -1)
|
|
34
|
-
if (length == i){
|
|
35
|
-
newObject[k] = oldObject[k] || [];
|
|
36
|
-
newObject[k][index] = value;
|
|
37
|
-
}
|
|
38
|
-
else {
|
|
39
|
-
newObject[k] = oldObject[k] || [];
|
|
40
|
-
newObject[k][index] = oldObject[k][index] || {};
|
|
41
|
-
}
|
|
42
|
-
newObject = newObject[k][index]
|
|
43
|
-
oldObject = oldObject[k][index]
|
|
44
|
-
}
|
|
45
|
-
else {
|
|
46
|
-
if (length == i)
|
|
47
|
-
newObject[keys[i]] = value;
|
|
48
|
-
else
|
|
49
|
-
newObject[keys[i]] = oldObject[keys[i]] || {};
|
|
50
|
-
|
|
51
|
-
newObject = newObject[keys[i]]
|
|
52
|
-
oldObject = oldObject[keys[i]]
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
return obj
|
|
57
|
-
} catch (error) {
|
|
58
|
-
console.log("Error converting dot notation to object", error);
|
|
59
|
-
return false;
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
export function parseTextToHtml(text) {
|
|
64
|
-
let doc = new DOMParser().parseFromString(text, "text/html");
|
|
65
|
-
if (doc.head.children[0]) return doc.head.children[0];
|
|
66
|
-
else return doc.body.children[0];
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
export function cssPath(node, container) {
|
|
70
|
-
let pathSplits = [];
|
|
71
|
-
do {
|
|
72
|
-
if (!node || !node.tagName) return false;
|
|
73
|
-
let pathSplit = node.tagName.toLowerCase();
|
|
74
|
-
// if (node.tagName == "DOM-PARSER" || node.hasAttribute('contenteditable')){
|
|
75
|
-
// pathSplit = "[contenteditable]";
|
|
76
|
-
// node = '';
|
|
77
|
-
// }
|
|
78
|
-
if (node.id){
|
|
79
|
-
pathSplit += "#" + node.id;
|
|
80
|
-
node = '';
|
|
81
|
-
}
|
|
82
|
-
else {
|
|
83
|
-
// let eid = node.getAttribute('eid');
|
|
84
|
-
// if (/{{\s*([\w\W]+)\s*}}/g.test(eid)) {
|
|
85
|
-
// eid = false;
|
|
86
|
-
// }
|
|
87
|
-
// if (eid) {
|
|
88
|
-
// pathSplit += `[eid="${eid}"]`;
|
|
89
|
-
// node = '';
|
|
90
|
-
// }
|
|
91
|
-
// else {
|
|
92
|
-
// if (node.classList.length) {
|
|
93
|
-
// node.classList.forEach((item) => {
|
|
94
|
-
// if (item.indexOf(":") === -1) pathSplit += "." + item;
|
|
95
|
-
// });
|
|
96
|
-
// }
|
|
97
|
-
|
|
98
|
-
if (node.parentNode && node.parentNode.children.length > 1) {
|
|
99
|
-
// ToDo: improve array logic so ignores javascript generated html??
|
|
100
|
-
let children = []
|
|
101
|
-
for (let child of node.parentNode.children){
|
|
102
|
-
// if (!child.matches('.mirror'))
|
|
103
|
-
// children.push(child);
|
|
104
|
-
if (child.tagName == node.tagName)
|
|
105
|
-
children.push(child);
|
|
106
|
-
}
|
|
107
|
-
let index = Array.prototype.indexOf.call(
|
|
108
|
-
children,
|
|
109
|
-
node
|
|
110
|
-
);
|
|
111
|
-
// if (children.length > 1)
|
|
112
|
-
// pathSplit += `:nth-child(${index + 1})`;
|
|
113
|
-
pathSplit += `:nth-of-type(${index + 1})`;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
// pathSplits.unshift(pathSplit);
|
|
117
|
-
node = node.parentNode;
|
|
118
|
-
if (node == null || node.tagName == "HTML" || node.tagName == "DOM-PARSER" || node.nodeName == "#document" || node.hasAttribute('contenteditable'))
|
|
119
|
-
node = '';
|
|
120
|
-
}
|
|
121
|
-
// }
|
|
122
|
-
pathSplits.unshift(pathSplit);
|
|
123
|
-
} while (node);
|
|
124
|
-
let path = pathSplits.join(" > ")
|
|
125
|
-
if (path && path.includes('<')) {
|
|
126
|
-
let index = path.lastIndexOf(' >')
|
|
127
|
-
if (index != -1)
|
|
128
|
-
path = path.slice(0, index)
|
|
129
|
-
else{
|
|
130
|
-
index = path.lastIndexOf('<')
|
|
131
|
-
path = path.slice(0, index)
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
return path;
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
export function domParser(str) {
|
|
139
|
-
try {
|
|
140
|
-
var mainTag = str.match(/\<(?<tag>[a-z0-9]+)(.*?)?\>/).groups.tag;
|
|
141
|
-
} catch (e){
|
|
142
|
-
// console.log(e, 'find position: can not find the main tag');
|
|
143
|
-
}
|
|
144
|
-
let doc;
|
|
145
|
-
switch (mainTag) {
|
|
146
|
-
case 'html':
|
|
147
|
-
doc = new DOMParser().parseFromString(str, "text/html");
|
|
148
|
-
return doc.documentElement;
|
|
149
|
-
case 'body':
|
|
150
|
-
doc = new DOMParser().parseFromString(str, "text/html");
|
|
151
|
-
return doc.body;
|
|
152
|
-
case 'head':
|
|
153
|
-
doc = new DOMParser().parseFromString(str, "text/html");
|
|
154
|
-
return doc.head;
|
|
155
|
-
|
|
156
|
-
default:
|
|
157
|
-
let con = document.createElement('dom-parser');
|
|
158
|
-
con.innerHTML = str;
|
|
159
|
-
return con;
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
export function queryDocumentSelectorAll(selector) {
|
|
164
|
-
let elements = [];
|
|
165
|
-
|
|
166
|
-
if (selector) {
|
|
167
|
-
let selectors = [selector];
|
|
168
|
-
if (selector.indexOf(',') !== -1){
|
|
169
|
-
selectors = selector.split(',');
|
|
170
|
-
}
|
|
171
|
-
for (let selector of selectors){
|
|
172
|
-
let els;
|
|
173
|
-
if (selector.indexOf(';') !== -1) {
|
|
174
|
-
let targetDocument;
|
|
175
|
-
let [documentSelector, targetSelector] = selector.split(';');
|
|
176
|
-
if (['parent', 'parentDocument'].includes(documentSelector))
|
|
177
|
-
targetDocument = window.parent.document;
|
|
178
|
-
else {
|
|
179
|
-
let frame = document.querySelector(documentSelector);
|
|
180
|
-
if (frame)
|
|
181
|
-
targetDocument = frame.contentDocument;
|
|
182
|
-
}
|
|
183
|
-
if (targetDocument){
|
|
184
|
-
if (targetSelector)
|
|
185
|
-
els = targetDocument.querySelectorAll(targetSelector);
|
|
186
|
-
else
|
|
187
|
-
if (targetDocument.clickedElement)
|
|
188
|
-
els = [targetDocument.clickedElement];
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
else
|
|
192
|
-
els = document.querySelectorAll(selector);
|
|
193
|
-
if (els){
|
|
194
|
-
els = Array.prototype.slice.call(els);
|
|
195
|
-
elements = elements.concat(els);
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
return elements;
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
export function queryDocumentSelector(selector) {
|
|
203
|
-
if (selector) {
|
|
204
|
-
let selectors = [selector];
|
|
205
|
-
if (selector.indexOf(',') !== -1){
|
|
206
|
-
selectors = selector.split(',');
|
|
207
|
-
}
|
|
208
|
-
for (let selector of selectors){
|
|
209
|
-
let el;
|
|
210
|
-
if (selector.indexOf(';') !== -1) {
|
|
211
|
-
let targetDocument;
|
|
212
|
-
let [documentSelector, targetSelector] = selector.split(';');
|
|
213
|
-
if (['parent', 'parentDocument'].includes(documentSelector))
|
|
214
|
-
targetDocument = window.parent.document;
|
|
215
|
-
else {
|
|
216
|
-
let frame = document.querySelector(documentSelector);
|
|
217
|
-
if (frame)
|
|
218
|
-
targetDocument = frame.contentDocument;
|
|
219
|
-
}
|
|
220
|
-
if (targetDocument){
|
|
221
|
-
if (targetSelector)
|
|
222
|
-
el = targetDocument.querySelector(targetSelector);
|
|
223
|
-
else
|
|
224
|
-
if (targetDocument.clickedElement)
|
|
225
|
-
el = [targetDocument.clickedElement];
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
else
|
|
229
|
-
el = document.querySelector(selector);
|
|
230
|
-
if (el)
|
|
231
|
-
return el
|
|
232
|
-
}
|
|
233
|
-
return;
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
// export function computeStyles(el, properties) {
|
|
238
|
-
// let computed = window.getComputedStyle(el);
|
|
239
|
-
// let result = {};
|
|
240
|
-
// properties.forEach((property) => {
|
|
241
|
-
// result[property] = parseInt(computed[property]);
|
|
242
|
-
// });
|
|
243
|
-
// return result;
|
|
244
|
-
// }
|
|
245
|
-
|
|
246
|
-
// function checkParent(element, selectors){
|
|
247
|
-
// let parentElement;
|
|
248
|
-
// do {
|
|
249
|
-
// parentElement = element.parentElement.closest(selectors);
|
|
250
|
-
// if (parentElement) {
|
|
251
|
-
// element = parentElement;
|
|
252
|
-
// } else {
|
|
253
|
-
// return element;
|
|
254
|
-
// }
|
|
255
|
-
// } while (parentElement);
|
|
256
|
-
// }
|
|
257
|
-
|
|
258
|
-
clickedElement();
|
|
259
|
-
|
|
260
|
-
export default {
|
|
261
|
-
parseTextToHtml,
|
|
262
|
-
dotNotationToObject,
|
|
263
|
-
cssPath,
|
|
264
|
-
domParser,
|
|
265
|
-
queryDocumentSelector,
|
|
266
|
-
queryDocumentSelectorAll
|
|
267
|
-
};
|
|
1
|
+
(function (root, factory) {
|
|
2
|
+
if (typeof define === 'function' && define.amd) {
|
|
3
|
+
define(["./utils"], function(CoCreateUtils) {
|
|
4
|
+
return factory(CoCreateUtils)
|
|
5
|
+
});
|
|
6
|
+
} else if (typeof module === 'object' && module.exports) {
|
|
7
|
+
const CoCreateUtils = require("./utils.js")
|
|
8
|
+
module.exports = factory(CoCreateUtils);
|
|
9
|
+
} else {
|
|
10
|
+
root.returnExports = factory(root["./utils.js"]);
|
|
11
|
+
}
|
|
12
|
+
}(typeof self !== 'undefined' ? self : this, function (CoCreateUtils) {
|
|
13
|
+
return CoCreateUtils;
|
|
14
|
+
}));
|
package/src/utils.js
ADDED
|
@@ -0,0 +1,510 @@
|
|
|
1
|
+
(function (root, factory) {
|
|
2
|
+
if (typeof define === 'function' && define.amd) {
|
|
3
|
+
define([], function() {
|
|
4
|
+
return factory(true)
|
|
5
|
+
});
|
|
6
|
+
} else if (typeof module === 'object' && module.exports) {
|
|
7
|
+
module.exports = factory(false);
|
|
8
|
+
} else {
|
|
9
|
+
root.returnExports = factory(true);
|
|
10
|
+
}
|
|
11
|
+
}(typeof self !== 'undefined' ? self : this, function (isBrowser) {
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
/*globals DOMParser*/
|
|
15
|
+
function clickedElement() {
|
|
16
|
+
document.addEventListener('click', e => {
|
|
17
|
+
document.clickedElement = e.target;
|
|
18
|
+
});
|
|
19
|
+
let frameDocuments = window.top.frameDocuments;
|
|
20
|
+
if (!frameDocuments){
|
|
21
|
+
window.top.frameDocuments = new Map();
|
|
22
|
+
frameDocuments = window.top.frameDocuments;
|
|
23
|
+
}
|
|
24
|
+
let frames = document.querySelectorAll('iframe');
|
|
25
|
+
for (let frame of frames){
|
|
26
|
+
let frameDocument = frame.contentDocument;
|
|
27
|
+
if (!frameDocuments.has(frameDocument)){
|
|
28
|
+
frameDocuments.set(frameDocument, '')
|
|
29
|
+
frameDocument.addEventListener('click', e => {
|
|
30
|
+
frameDocument.clickedElement = e.target;
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function dotNotationToObject(data, obj = {}) {
|
|
37
|
+
try {
|
|
38
|
+
for (const [key, value] of Object.entries(data)) {
|
|
39
|
+
let newObject = obj
|
|
40
|
+
let oldObject = new Object(obj)
|
|
41
|
+
let keys = key.split('.');
|
|
42
|
+
let length = keys.length - 1
|
|
43
|
+
for (let i = 0; i < keys.length; i++) {
|
|
44
|
+
if (/\[([0-9]*)\]/g.test(keys[i])){
|
|
45
|
+
let [k, index] = keys[i].split('[');
|
|
46
|
+
index = index.slice(0, -1)
|
|
47
|
+
if (length == i){
|
|
48
|
+
newObject[k] = oldObject[k] || [];
|
|
49
|
+
newObject[k][index] = value;
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
newObject[k] = oldObject[k] || [];
|
|
53
|
+
newObject[k][index] = oldObject[k][index] || {};
|
|
54
|
+
}
|
|
55
|
+
newObject = newObject[k][index]
|
|
56
|
+
oldObject = oldObject[k][index]
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
if (length == i)
|
|
60
|
+
newObject[keys[i]] = value;
|
|
61
|
+
else
|
|
62
|
+
newObject[keys[i]] = oldObject[keys[i]] || {};
|
|
63
|
+
|
|
64
|
+
newObject = newObject[keys[i]]
|
|
65
|
+
oldObject = oldObject[keys[i]]
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return obj
|
|
70
|
+
} catch (error) {
|
|
71
|
+
console.log("Error converting dot notation to object", error);
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function parseTextToHtml(text) {
|
|
77
|
+
let doc = new DOMParser().parseFromString(text, "text/html");
|
|
78
|
+
if (doc.head.children[0]) return doc.head.children[0];
|
|
79
|
+
else return doc.body.children[0];
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function cssPath(node, container) {
|
|
83
|
+
let pathSplits = [];
|
|
84
|
+
do {
|
|
85
|
+
if (!node || !node.tagName) return false;
|
|
86
|
+
let pathSplit = node.tagName.toLowerCase();
|
|
87
|
+
// if (node.tagName == "DOM-PARSER" || node.hasAttribute('contenteditable')){
|
|
88
|
+
// pathSplit = "[contenteditable]";
|
|
89
|
+
// node = '';
|
|
90
|
+
// }
|
|
91
|
+
if (node.id){
|
|
92
|
+
pathSplit += "#" + node.id;
|
|
93
|
+
node = '';
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
// let eid = node.getAttribute('eid');
|
|
97
|
+
// if (/{{\s*([\w\W]+)\s*}}/g.test(eid)) {
|
|
98
|
+
// eid = false;
|
|
99
|
+
// }
|
|
100
|
+
// if (eid) {
|
|
101
|
+
// pathSplit += `[eid="${eid}"]`;
|
|
102
|
+
// node = '';
|
|
103
|
+
// }
|
|
104
|
+
// else {
|
|
105
|
+
// if (node.classList.length) {
|
|
106
|
+
// node.classList.forEach((item) => {
|
|
107
|
+
// if (item.indexOf(":") === -1) pathSplit += "." + item;
|
|
108
|
+
// });
|
|
109
|
+
// }
|
|
110
|
+
|
|
111
|
+
if (node.parentNode && node.parentNode.children.length > 1) {
|
|
112
|
+
// ToDo: improve array logic so ignores javascript generated html??
|
|
113
|
+
let children = []
|
|
114
|
+
for (let child of node.parentNode.children){
|
|
115
|
+
// if (!child.matches('.mirror'))
|
|
116
|
+
// children.push(child);
|
|
117
|
+
if (child.tagName == node.tagName)
|
|
118
|
+
children.push(child);
|
|
119
|
+
}
|
|
120
|
+
let index = Array.prototype.indexOf.call(
|
|
121
|
+
children,
|
|
122
|
+
node
|
|
123
|
+
);
|
|
124
|
+
// if (children.length > 1)
|
|
125
|
+
// pathSplit += `:nth-child(${index + 1})`;
|
|
126
|
+
pathSplit += `:nth-of-type(${index + 1})`;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// pathSplits.unshift(pathSplit);
|
|
130
|
+
node = node.parentNode;
|
|
131
|
+
if (node == null || node.tagName == "HTML" || node.tagName == "DOM-PARSER" || node.nodeName == "#document" || node.hasAttribute('contenteditable'))
|
|
132
|
+
node = '';
|
|
133
|
+
}
|
|
134
|
+
// }
|
|
135
|
+
pathSplits.unshift(pathSplit);
|
|
136
|
+
} while (node);
|
|
137
|
+
let path = pathSplits.join(" > ")
|
|
138
|
+
if (path && path.includes('<')) {
|
|
139
|
+
let index = path.lastIndexOf(' >')
|
|
140
|
+
if (index != -1)
|
|
141
|
+
path = path.slice(0, index)
|
|
142
|
+
else{
|
|
143
|
+
index = path.lastIndexOf('<')
|
|
144
|
+
path = path.slice(0, index)
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
return path;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function domParser(str) {
|
|
152
|
+
try {
|
|
153
|
+
var mainTag = str.match(/\<(?<tag>[a-z0-9]+)(.*?)?\>/).groups.tag;
|
|
154
|
+
} catch (e){
|
|
155
|
+
// console.log(e, 'find position: can not find the main tag');
|
|
156
|
+
}
|
|
157
|
+
let doc;
|
|
158
|
+
switch (mainTag) {
|
|
159
|
+
case 'html':
|
|
160
|
+
doc = new DOMParser().parseFromString(str, "text/html");
|
|
161
|
+
return doc.documentElement;
|
|
162
|
+
case 'body':
|
|
163
|
+
doc = new DOMParser().parseFromString(str, "text/html");
|
|
164
|
+
return doc.body;
|
|
165
|
+
case 'head':
|
|
166
|
+
doc = new DOMParser().parseFromString(str, "text/html");
|
|
167
|
+
return doc.head;
|
|
168
|
+
|
|
169
|
+
default:
|
|
170
|
+
let con = document.createElement('dom-parser');
|
|
171
|
+
con.innerHTML = str;
|
|
172
|
+
return con;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
function queryDocumentSelectorAll(selector) {
|
|
177
|
+
let elements = [];
|
|
178
|
+
|
|
179
|
+
if (selector) {
|
|
180
|
+
let selectors = [selector];
|
|
181
|
+
if (selector.indexOf(',') !== -1){
|
|
182
|
+
selectors = selector.split(',');
|
|
183
|
+
}
|
|
184
|
+
for (let selector of selectors){
|
|
185
|
+
let els;
|
|
186
|
+
if (selector.indexOf(';') !== -1) {
|
|
187
|
+
let targetDocument;
|
|
188
|
+
let [documentSelector, targetSelector] = selector.split(';');
|
|
189
|
+
if (['parent', 'parentDocument'].includes(documentSelector))
|
|
190
|
+
targetDocument = window.parent.document;
|
|
191
|
+
else {
|
|
192
|
+
let frame = document.querySelector(documentSelector);
|
|
193
|
+
if (frame)
|
|
194
|
+
targetDocument = frame.contentDocument;
|
|
195
|
+
}
|
|
196
|
+
if (targetDocument){
|
|
197
|
+
if (targetSelector)
|
|
198
|
+
els = targetDocument.querySelectorAll(targetSelector);
|
|
199
|
+
else
|
|
200
|
+
if (targetDocument.clickedElement)
|
|
201
|
+
els = [targetDocument.clickedElement];
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
else
|
|
205
|
+
els = document.querySelectorAll(selector);
|
|
206
|
+
if (els){
|
|
207
|
+
els = Array.prototype.slice.call(els);
|
|
208
|
+
elements = elements.concat(els);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
return elements;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
function queryDocumentSelector(selector) {
|
|
216
|
+
if (selector) {
|
|
217
|
+
let selectors = [selector];
|
|
218
|
+
if (selector.indexOf(',') !== -1){
|
|
219
|
+
selectors = selector.split(',');
|
|
220
|
+
}
|
|
221
|
+
for (let selector of selectors){
|
|
222
|
+
let el;
|
|
223
|
+
if (selector.indexOf(';') !== -1) {
|
|
224
|
+
let targetDocument;
|
|
225
|
+
let [documentSelector, targetSelector] = selector.split(';');
|
|
226
|
+
if (['parent', 'parentDocument'].includes(documentSelector))
|
|
227
|
+
targetDocument = window.parent.document;
|
|
228
|
+
else {
|
|
229
|
+
let frame = document.querySelector(documentSelector);
|
|
230
|
+
if (frame)
|
|
231
|
+
targetDocument = frame.contentDocument;
|
|
232
|
+
}
|
|
233
|
+
if (targetDocument){
|
|
234
|
+
if (targetSelector)
|
|
235
|
+
el = targetDocument.querySelector(targetSelector);
|
|
236
|
+
else
|
|
237
|
+
if (targetDocument.clickedElement)
|
|
238
|
+
el = [targetDocument.clickedElement];
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
else
|
|
242
|
+
el = document.querySelector(selector);
|
|
243
|
+
if (el)
|
|
244
|
+
return el
|
|
245
|
+
}
|
|
246
|
+
return;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
function searchData(data, filter) {
|
|
251
|
+
if (filter && filter.search) {
|
|
252
|
+
if (filter['search']['type'] == 'and') {
|
|
253
|
+
data = andSearch(data, filter['search']['value']);
|
|
254
|
+
} else {
|
|
255
|
+
data = orSearch(data, filter['search']['value']);
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
const total = data.length;
|
|
259
|
+
const startIndex = filter.startIndex;
|
|
260
|
+
const count = filter.count;
|
|
261
|
+
let result_data = [];
|
|
262
|
+
|
|
263
|
+
if (startIndex)
|
|
264
|
+
data = data.slice(startIndex, total);
|
|
265
|
+
if (count)
|
|
266
|
+
data = data.slice(0, count)
|
|
267
|
+
|
|
268
|
+
result_data = data;
|
|
269
|
+
filter['startIndex'] = startIndex
|
|
270
|
+
if (count)
|
|
271
|
+
filter['count'] = count
|
|
272
|
+
filter['total'] = total
|
|
273
|
+
return result_data
|
|
274
|
+
} else {
|
|
275
|
+
return data
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
//. or operator
|
|
280
|
+
function orSearch(results, search) {
|
|
281
|
+
var tmp
|
|
282
|
+
if (search && search.length > 0) {
|
|
283
|
+
|
|
284
|
+
tmp = results.filter(function(item) {
|
|
285
|
+
|
|
286
|
+
for (var key in item) {
|
|
287
|
+
var value = item[key];
|
|
288
|
+
var __status = false;
|
|
289
|
+
|
|
290
|
+
var str_value = value;
|
|
291
|
+
|
|
292
|
+
if (Array.isArray(str_value) || typeof str_value == 'number') {
|
|
293
|
+
str_value = str_value.toString();
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
if (typeof str_value == 'string') {
|
|
297
|
+
str_value = str_value.toUpperCase();
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
for (let i = 0; i < search.length; i++) {
|
|
301
|
+
if (typeof search[i] == 'string' && typeof str_value == 'string') {
|
|
302
|
+
if (str_value.indexOf(search[i].toUpperCase()) > -1) {
|
|
303
|
+
__status = true;
|
|
304
|
+
break;
|
|
305
|
+
}
|
|
306
|
+
} else {
|
|
307
|
+
if (value == search[i]) {
|
|
308
|
+
__status = true;
|
|
309
|
+
break;
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
if (__status) {
|
|
315
|
+
return true;
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
return false;
|
|
320
|
+
})
|
|
321
|
+
} else {
|
|
322
|
+
tmp = results;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
return tmp;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
|
|
329
|
+
//. and operator
|
|
330
|
+
function andSearch(results, search) {
|
|
331
|
+
var tmp
|
|
332
|
+
if (search && search.length > 0) {
|
|
333
|
+
|
|
334
|
+
tmp = results.filter(function(item) {
|
|
335
|
+
|
|
336
|
+
for (let i = 0; i < search.length; i++) {
|
|
337
|
+
var __status = false;
|
|
338
|
+
|
|
339
|
+
for (var key in item) {
|
|
340
|
+
var value = item[key];
|
|
341
|
+
|
|
342
|
+
if (typeof search[i] == 'string') {
|
|
343
|
+
|
|
344
|
+
if (Array.isArray(value) || typeof value == 'number' ) {
|
|
345
|
+
value = value.toString();
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
if (typeof value == 'string') {
|
|
349
|
+
value = value.toUpperCase();
|
|
350
|
+
if (value.indexOf(search[i].toUpperCase()) > -1) {
|
|
351
|
+
__status = true;
|
|
352
|
+
break;
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
} else {
|
|
357
|
+
if (value == search[i]) {
|
|
358
|
+
__status = true;
|
|
359
|
+
break;
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
if (!__status) {
|
|
365
|
+
return false;
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
return true;
|
|
370
|
+
})
|
|
371
|
+
} else {
|
|
372
|
+
tmp = results;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
return tmp;
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
function sortData(data, sorts) {
|
|
379
|
+
if (!Array.isArray(sorts))
|
|
380
|
+
sorts = [sorts]
|
|
381
|
+
for (let sort of sorts) {
|
|
382
|
+
let name = sort.name
|
|
383
|
+
if (name) {
|
|
384
|
+
data.sort((a, b) => {
|
|
385
|
+
if (!a[name])
|
|
386
|
+
a[name] = ''
|
|
387
|
+
if (!b[name])
|
|
388
|
+
b[name] = ''
|
|
389
|
+
if (sort.type == '-1') {
|
|
390
|
+
if (sort.valueType == 'number')
|
|
391
|
+
return b[name] - a[name]
|
|
392
|
+
else
|
|
393
|
+
return b[name].localeCompare(a[name])
|
|
394
|
+
} else {
|
|
395
|
+
if (sort.valueType == 'number')
|
|
396
|
+
return a[name] - b[name]
|
|
397
|
+
else
|
|
398
|
+
return a[name].localeCompare(b[name])
|
|
399
|
+
}
|
|
400
|
+
});
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
return data;
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
function queryData(item, query) {
|
|
407
|
+
//. $contain, $range, $eq, $ne, $lt, $lte, $gt, $gte, $in, $nin, $geoWithin
|
|
408
|
+
let flag = true;
|
|
409
|
+
if (!item || !query) {
|
|
410
|
+
return false;
|
|
411
|
+
}
|
|
412
|
+
if (Array.isArray(item)) return false;
|
|
413
|
+
for (let i = 0; i < query.length; i++) {
|
|
414
|
+
let fieldValue = item[query[i].name];
|
|
415
|
+
if (fieldValue == undefined)
|
|
416
|
+
fieldValue = ''
|
|
417
|
+
let values = query[i].value
|
|
418
|
+
if (!Array.isArray(values))
|
|
419
|
+
values = [values]
|
|
420
|
+
|
|
421
|
+
for (let value of values) {
|
|
422
|
+
switch (query[i].operator) {
|
|
423
|
+
case '$contain':
|
|
424
|
+
if (!fieldValue.includes(value))
|
|
425
|
+
flag = false;
|
|
426
|
+
break;
|
|
427
|
+
case '$range':
|
|
428
|
+
if (value !== null && value !== null) {
|
|
429
|
+
if (value[0] > fieldValue || value[1] <= fieldValue)
|
|
430
|
+
flag = false;
|
|
431
|
+
} else if (item.value[0] == null && value[1] >= fieldValue) {
|
|
432
|
+
flag = false;
|
|
433
|
+
} else if (item.value[1] == null && value[0] <= fieldValue) {
|
|
434
|
+
flag = false;
|
|
435
|
+
}
|
|
436
|
+
break;
|
|
437
|
+
case '$eq':
|
|
438
|
+
if (fieldValue != value) flag = false;
|
|
439
|
+
break;
|
|
440
|
+
case '$ne':
|
|
441
|
+
if (fieldValue == value) flag = false;
|
|
442
|
+
break;
|
|
443
|
+
case '$lt':
|
|
444
|
+
if (fieldValue >= value) flag = false;
|
|
445
|
+
break;
|
|
446
|
+
case '$lte':
|
|
447
|
+
if (fieldValue > value) flag = false;
|
|
448
|
+
break;
|
|
449
|
+
case '$gt':
|
|
450
|
+
if (fieldValue <= value) flag = false;
|
|
451
|
+
break;
|
|
452
|
+
case '$gte':
|
|
453
|
+
if (fieldValue < value) flag = false;
|
|
454
|
+
break;
|
|
455
|
+
case '$in':
|
|
456
|
+
if (!Array.isArray(fieldValue) || !fieldValue.some(x => value.includes(x))) flag = false;
|
|
457
|
+
break;
|
|
458
|
+
case '$nin':
|
|
459
|
+
if (Array.isArray(fieldValue) && fieldValue.some(x => value.includes(x))) flag = false;
|
|
460
|
+
break;
|
|
461
|
+
default:
|
|
462
|
+
// if (!Array.isArray(fieldValue) || !fieldValue.some(x => value.includes(x))) flag = false;
|
|
463
|
+
if (fieldValue && !fieldValue.includes(value)) flag = false;
|
|
464
|
+
break;
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
return flag;
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
|
|
472
|
+
// function computeStyles(el, properties) {
|
|
473
|
+
// let computed = window.getComputedStyle(el);
|
|
474
|
+
// let result = {};
|
|
475
|
+
// properties.forEach((property) => {
|
|
476
|
+
// result[property] = parseInt(computed[property]);
|
|
477
|
+
// });
|
|
478
|
+
// return result;
|
|
479
|
+
// }
|
|
480
|
+
|
|
481
|
+
// function checkParent(element, selectors){
|
|
482
|
+
// let parentElement;
|
|
483
|
+
// do {
|
|
484
|
+
// parentElement = element.parentElement.closest(selectors);
|
|
485
|
+
// if (parentElement) {
|
|
486
|
+
// element = parentElement;
|
|
487
|
+
// } else {
|
|
488
|
+
// return element;
|
|
489
|
+
// }
|
|
490
|
+
// } while (parentElement);
|
|
491
|
+
// }
|
|
492
|
+
|
|
493
|
+
if (isBrowser)
|
|
494
|
+
clickedElement();
|
|
495
|
+
|
|
496
|
+
return {
|
|
497
|
+
parseTextToHtml,
|
|
498
|
+
dotNotationToObject,
|
|
499
|
+
cssPath,
|
|
500
|
+
domParser,
|
|
501
|
+
queryDocumentSelector,
|
|
502
|
+
queryDocumentSelectorAll,
|
|
503
|
+
searchData,
|
|
504
|
+
andSearch,
|
|
505
|
+
orSearch,
|
|
506
|
+
sortData,
|
|
507
|
+
queryData
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
}));
|
package/src/encodedecode.js
DELETED
|
@@ -1,111 +0,0 @@
|
|
|
1
|
-
// function __mergeObject(target, source)
|
|
2
|
-
// {
|
|
3
|
-
// target = target || {};
|
|
4
|
-
// for (let key of Object.keys(source)) {
|
|
5
|
-
// if (source[key] instanceof Object) {
|
|
6
|
-
// Object.assign(source[key], __mergeObject(target[key], source[key]))
|
|
7
|
-
// }
|
|
8
|
-
// }
|
|
9
|
-
|
|
10
|
-
// Object.assign(target || {}, source)
|
|
11
|
-
// return target
|
|
12
|
-
// }
|
|
13
|
-
|
|
14
|
-
// function __createObject(data, path)
|
|
15
|
-
// {
|
|
16
|
-
// if (!path) return data;
|
|
17
|
-
|
|
18
|
-
// let keys = path.split('.')
|
|
19
|
-
// let newObject = data;
|
|
20
|
-
|
|
21
|
-
// for (var i = keys.length - 1; i >= 0; i--) {
|
|
22
|
-
// newObject = {[keys[i]]: newObject}
|
|
23
|
-
// }
|
|
24
|
-
// return newObject;
|
|
25
|
-
// }
|
|
26
|
-
|
|
27
|
-
// function __createArray(key, data)
|
|
28
|
-
// {
|
|
29
|
-
// try {
|
|
30
|
-
// let item = /([\w\W]+)\[(\d+)\]/gm.exec(key)
|
|
31
|
-
// if (item && item.length == 3) {
|
|
32
|
-
// let arrayKey = item[1];
|
|
33
|
-
// let index = parseInt(item[2]);
|
|
34
|
-
|
|
35
|
-
// if (!data[arrayKey] || !Array.isArray(data[arrayKey])) {
|
|
36
|
-
// data[arrayKey] = [];
|
|
37
|
-
// }
|
|
38
|
-
// data[arrayKey][index] = data[key];
|
|
39
|
-
// delete data[key];
|
|
40
|
-
// key = arrayKey;
|
|
41
|
-
// }
|
|
42
|
-
// } catch {
|
|
43
|
-
// console.log('create array error');
|
|
44
|
-
// }
|
|
45
|
-
// return key;
|
|
46
|
-
// }
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
// function isObject(item) {
|
|
51
|
-
// return (!!item) && (item.constructor === Object);
|
|
52
|
-
// }
|
|
53
|
-
// function isArray(item) {
|
|
54
|
-
// return (!!item) && (item.constructor === Array);
|
|
55
|
-
// }
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
// function decodeObject(data) {
|
|
59
|
-
// let keys = Object.keys(data)
|
|
60
|
-
// let objectData = {};
|
|
61
|
-
|
|
62
|
-
// keys.forEach((k) => {
|
|
63
|
-
// k = __createArray(k, data);
|
|
64
|
-
// if (k.split('.').length > 1) {
|
|
65
|
-
// let newData = __createObject(data[k], k);
|
|
66
|
-
// delete data[k];
|
|
67
|
-
|
|
68
|
-
// objectData = __mergeObject(objectData, newData);
|
|
69
|
-
// } else {
|
|
70
|
-
// objectData[k] = data[k];
|
|
71
|
-
// }
|
|
72
|
-
// })
|
|
73
|
-
// return objectData;
|
|
74
|
-
// }
|
|
75
|
-
|
|
76
|
-
// function encodeObject(data) {
|
|
77
|
-
// let keys = Object.keys(data);
|
|
78
|
-
// let newData = {};
|
|
79
|
-
// keys.forEach((k) => {
|
|
80
|
-
// let data_value = data[k];
|
|
81
|
-
// if (isObject(data[k])) {
|
|
82
|
-
// let new_obj = encodeObject(data[k]);
|
|
83
|
-
// let newKeys = Object.keys(new_obj);
|
|
84
|
-
// newKeys.forEach((newKey) => {
|
|
85
|
-
// let value = new_obj[newKey];
|
|
86
|
-
// if (isNaN(parseInt(newKey))) {
|
|
87
|
-
// newKey = `${k}.${newKey}`;
|
|
88
|
-
// } else {
|
|
89
|
-
// newKey = `${k}[${newKey}]`;
|
|
90
|
-
// }
|
|
91
|
-
|
|
92
|
-
// newData[newKey] = value;
|
|
93
|
-
// })
|
|
94
|
-
|
|
95
|
-
// } else if (isArray(data_value)){
|
|
96
|
-
// data_value.forEach((v, index) => {
|
|
97
|
-
// newData[`${k}[${index}]`] = v;
|
|
98
|
-
// })
|
|
99
|
-
// } else {
|
|
100
|
-
// newData[k] = data[k];
|
|
101
|
-
// }
|
|
102
|
-
// })
|
|
103
|
-
// return newData;
|
|
104
|
-
// }
|
|
105
|
-
|
|
106
|
-
module.exports = {
|
|
107
|
-
// decodeObject,
|
|
108
|
-
// encodeObject,
|
|
109
|
-
replaceArray
|
|
110
|
-
};
|
|
111
|
-
|