native-document 1.0.103 → 1.0.105
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/dist/native-document.components.min.js +62 -65
- package/dist/native-document.dev.js +135 -132
- package/dist/native-document.dev.js.map +1 -1
- package/dist/native-document.min.js +1 -1
- package/index.d.ts +3 -2
- package/index.js +0 -1
- package/package.json +2 -2
- package/src/core/data/ObservableItem.js +0 -9
- package/src/core/elements/anchor.js +23 -25
- package/src/core/elements/control/for-each-array.js +53 -32
- package/src/core/elements/index.js +2 -8
- package/src/core/utils/validator.js +11 -6
- package/src/core/wrappers/prototypes/nd-element-extensions.js +1 -5
- package/src/router/Route.js +2 -2
- package/types/service.d.ts +1 -1
|
@@ -4,6 +4,9 @@ import Validator from "../../utils/validator";
|
|
|
4
4
|
import { ElementCreator } from "../../wrappers/ElementCreator";
|
|
5
5
|
import NativeDocumentError from "../../errors/NativeDocumentError";
|
|
6
6
|
|
|
7
|
+
|
|
8
|
+
const CREATE_AND_CACHE_ACTIONS = new Set(['clear', 'push', 'unshift', 'replace']);
|
|
9
|
+
|
|
7
10
|
/**
|
|
8
11
|
* Renders items from an ObservableArray with optimized array-specific updates.
|
|
9
12
|
* Provides index observables and handles array mutations efficiently.
|
|
@@ -64,50 +67,67 @@ export function ForEachArray(data, callback, configs = {}) {
|
|
|
64
67
|
if(removeChild) {
|
|
65
68
|
const child = cacheItem.child;
|
|
66
69
|
child?.remove();
|
|
67
|
-
cache.delete(
|
|
70
|
+
cache.delete(item);
|
|
68
71
|
}
|
|
69
72
|
cacheItem.indexObserver?.cleanup();
|
|
70
73
|
};
|
|
71
74
|
|
|
72
|
-
const
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
return;
|
|
75
|
+
const createAndCache = (item) => {
|
|
76
|
+
const child = ElementCreator.getChild(callback(item, null));
|
|
77
|
+
if(process.env.NODE_ENV === 'development') {
|
|
78
|
+
if(!child) {
|
|
79
|
+
throw new NativeDocumentError("ForEachArray child can't be null or undefined!");
|
|
80
|
+
}
|
|
79
81
|
}
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
82
|
+
cache.set(item, { child, indexObserver: null });
|
|
83
|
+
return child;
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
const createWithIndexAndCache = (item, indexKey) => {
|
|
87
|
+
const indexObserver = Observable(indexKey);
|
|
88
|
+
const child = ElementCreator.getChild(callback(item, indexObserver));
|
|
89
|
+
if(process.env.NODE_ENV === 'development') {
|
|
90
|
+
if(!child) {
|
|
91
|
+
throw new NativeDocumentError("ForEachArray child can't be null or undefined!");
|
|
83
92
|
}
|
|
84
|
-
removeCacheItem(itemAsKey, false);
|
|
85
93
|
}
|
|
86
|
-
cache.
|
|
87
|
-
|
|
94
|
+
cache.set(item, { child, indexObserver });
|
|
95
|
+
return child;
|
|
96
|
+
};
|
|
88
97
|
|
|
89
|
-
const
|
|
98
|
+
const getOrCreate = (item, indexKey) => {
|
|
90
99
|
const cacheItem = cache.get(item);
|
|
91
100
|
if(cacheItem) {
|
|
92
101
|
cacheItem.indexObserver?.set(indexKey);
|
|
93
|
-
|
|
94
|
-
if(child) {
|
|
95
|
-
return child;
|
|
96
|
-
}
|
|
97
|
-
cache.delete(item);
|
|
102
|
+
return cacheItem.child;
|
|
98
103
|
}
|
|
104
|
+
return createAndCache(item, indexKey);
|
|
105
|
+
};
|
|
99
106
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
if(
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
indexObserver
|
|
106
|
-
});
|
|
107
|
-
return child;
|
|
107
|
+
let buildItem = createAndCache;
|
|
108
|
+
const selectBuildStrategy = (action = null) => {
|
|
109
|
+
if(CREATE_AND_CACHE_ACTIONS.includes(action)) {
|
|
110
|
+
buildItem = isIndexRequired ? createWithIndexAndCache : createAndCache;
|
|
111
|
+
return;
|
|
108
112
|
}
|
|
113
|
+
buildItem = cache.size ? getOrCreate : (isIndexRequired ? createWithIndexAndCache : createAndCache);
|
|
114
|
+
};
|
|
115
|
+
|
|
109
116
|
|
|
110
|
-
|
|
117
|
+
const cleanCache = (items) => {
|
|
118
|
+
if(!isIndexRequired) {
|
|
119
|
+
cache.clear();
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
if(configs.shouldKeepItemsInCache) {
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
for (const [itemAsKey, _] of cache.entries()) {
|
|
126
|
+
if(items && items.includes(itemAsKey)) {
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
removeCacheItem(itemAsKey, false);
|
|
130
|
+
}
|
|
111
131
|
};
|
|
112
132
|
|
|
113
133
|
const removeByItem = function(item, fragment) {
|
|
@@ -125,10 +145,10 @@ export function ForEachArray(data, callback, configs = {}) {
|
|
|
125
145
|
return;
|
|
126
146
|
}
|
|
127
147
|
child.remove();
|
|
128
|
-
}
|
|
148
|
+
};
|
|
129
149
|
|
|
130
150
|
const Actions = {
|
|
131
|
-
toFragment(items
|
|
151
|
+
toFragment(items){
|
|
132
152
|
const fragment = document.createDocumentFragment();
|
|
133
153
|
for(let i = 0, length = items.length; i < length; i++) {
|
|
134
154
|
fragment.appendChild(buildItem(items[i], lastNumberOfItems));
|
|
@@ -136,7 +156,7 @@ export function ForEachArray(data, callback, configs = {}) {
|
|
|
136
156
|
}
|
|
137
157
|
return fragment;
|
|
138
158
|
},
|
|
139
|
-
add(items
|
|
159
|
+
add(items) {
|
|
140
160
|
element.appendElement(Actions.toFragment(items));
|
|
141
161
|
},
|
|
142
162
|
replace(items) {
|
|
@@ -251,6 +271,7 @@ export function ForEachArray(data, callback, configs = {}) {
|
|
|
251
271
|
clear();
|
|
252
272
|
return;
|
|
253
273
|
}
|
|
274
|
+
selectBuildStrategy(operations?.action);
|
|
254
275
|
|
|
255
276
|
if(!operations?.action) {
|
|
256
277
|
if(lastNumberOfItems === 0) {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import HtmlElementWrapper from "../wrappers/HtmlElementWrapper";
|
|
2
2
|
|
|
3
3
|
export * from './control/for-each';
|
|
4
4
|
export * from './control/for-each-array';
|
|
@@ -19,15 +19,9 @@ export * from './table';
|
|
|
19
19
|
/**
|
|
20
20
|
* Creates an empty `DocumentFragment` wrapper.
|
|
21
21
|
* Useful for grouping elements without adding a DOM node.
|
|
22
|
-
* @param {NdChild} children
|
|
23
22
|
* @type {function(GlobalAttributes=, NdChild|NdChild[]=): DocumentFragment}
|
|
24
|
-
* @returns {Anchor}
|
|
25
23
|
*/
|
|
26
|
-
export const Fragment = (
|
|
27
|
-
const fragment = Anchor('Fragment');
|
|
28
|
-
fragment.append(children);
|
|
29
|
-
return fragment;
|
|
30
|
-
};
|
|
24
|
+
export const Fragment = HtmlElementWrapper('');
|
|
31
25
|
|
|
32
26
|
|
|
33
27
|
|
|
@@ -11,6 +11,13 @@ const COMMON_NODE_TYPES = {
|
|
|
11
11
|
DOCUMENT_FRAGMENT: 11
|
|
12
12
|
};
|
|
13
13
|
|
|
14
|
+
const VALID_TYPES = {
|
|
15
|
+
[COMMON_NODE_TYPES.ELEMENT]: true,
|
|
16
|
+
[COMMON_NODE_TYPES.TEXT]: true,
|
|
17
|
+
[COMMON_NODE_TYPES.DOCUMENT_FRAGMENT]: true,
|
|
18
|
+
[COMMON_NODE_TYPES.COMMENT]: true
|
|
19
|
+
};
|
|
20
|
+
|
|
14
21
|
const Validator = {
|
|
15
22
|
isObservable(value) {
|
|
16
23
|
return value?.__$isObservable;
|
|
@@ -61,12 +68,10 @@ const Validator = {
|
|
|
61
68
|
return !(typeof value !== 'object' || value === null || Array.isArray(value) || value.constructor.name !== 'Object')
|
|
62
69
|
},
|
|
63
70
|
isElement(value) {
|
|
64
|
-
return value &&
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
value.nodeType === COMMON_NODE_TYPES.COMMENT
|
|
69
|
-
);
|
|
71
|
+
return value && VALID_TYPES[value.nodeType];
|
|
72
|
+
},
|
|
73
|
+
isDOMNode(value) {
|
|
74
|
+
return VALID_TYPES[value.nodeType];
|
|
70
75
|
},
|
|
71
76
|
isFragment(value) {
|
|
72
77
|
return value?.nodeType === COMMON_NODE_TYPES.DOCUMENT_FRAGMENT;
|
|
@@ -7,11 +7,7 @@ import Validator from "../../utils/validator";
|
|
|
7
7
|
import ObservableChecker from "../../data/ObservableChecker";
|
|
8
8
|
|
|
9
9
|
String.prototype.toNdElement = function () {
|
|
10
|
-
|
|
11
|
-
if(Validator.isString(formattedChild)) {
|
|
12
|
-
return ElementCreator.createStaticTextNode(null, formattedChild);
|
|
13
|
-
}
|
|
14
|
-
return ElementCreator.getChild(null, formattedChild);
|
|
10
|
+
return ElementCreator.createStaticTextNode(null, this);
|
|
15
11
|
};
|
|
16
12
|
|
|
17
13
|
Element.prototype.toNdElement = function () {
|
package/src/router/Route.js
CHANGED
|
@@ -77,13 +77,13 @@ export function Route($path, $component, $options = {}) {
|
|
|
77
77
|
*
|
|
78
78
|
* @param {string} path
|
|
79
79
|
*/
|
|
80
|
-
this.match = function(path)
|
|
80
|
+
this.match = function(path) {
|
|
81
81
|
path = '/'+trim(path, '/');
|
|
82
82
|
const match = getPattern().exec(path);
|
|
83
83
|
if(!match) return false;
|
|
84
84
|
const params = {};
|
|
85
85
|
|
|
86
|
-
|
|
86
|
+
getPattern().exec(path).forEach((value, index) => {
|
|
87
87
|
if(index < 1) return;
|
|
88
88
|
const name = $paramsNames[index - 1];
|
|
89
89
|
params[name] = value;
|
package/types/service.d.ts
CHANGED