native-document 1.0.0 → 1.0.2
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/package.json +1 -1
- package/readme.md +10 -0
- package/src/data/Observable.js +0 -1
- package/src/elements/content-formatter.js +1 -1
- package/src/elements/control/show-if.js +1 -1
- package/src/router/Router.js +1 -2
- package/src/router/RouterComponent.js +1 -3
- package/src/utils/debug-manager.js +4 -7
- package/src/wrappers/AttributesWrapper.js +42 -12
- package/src/wrappers/constants.js +2 -0
- package/dist/native-document.dev.js +0 -2346
- package/dist/native-document.min.js +0 -1
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -32,6 +32,16 @@ curl -o native-document.min.js https://raw.githubusercontent.com/afrocodeur/nati
|
|
|
32
32
|
<script src="https://cdn.jsdelivr.net/gh/afrocodeur/native-document@latest/dist/native-document.min.js"></script>
|
|
33
33
|
```
|
|
34
34
|
|
|
35
|
+
```bash
|
|
36
|
+
#Use degit
|
|
37
|
+
|
|
38
|
+
npx degit afrocodeur/native-document-vite my-project
|
|
39
|
+
cd my-project
|
|
40
|
+
npm install
|
|
41
|
+
npm start
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
|
|
35
45
|
|
|
36
46
|
### Basic Example
|
|
37
47
|
|
package/src/data/Observable.js
CHANGED
|
@@ -2,7 +2,7 @@ import HtmlElementWrapper from "../wrappers/HtmlElementWrapper";
|
|
|
2
2
|
|
|
3
3
|
export const Div = HtmlElementWrapper('div');
|
|
4
4
|
export const Span = HtmlElementWrapper('span');
|
|
5
|
-
export const Label = HtmlElementWrapper('
|
|
5
|
+
export const Label = HtmlElementWrapper('label');
|
|
6
6
|
export const P = HtmlElementWrapper('p');
|
|
7
7
|
export const Paragraph = P;
|
|
8
8
|
export const Strong = HtmlElementWrapper('strong');
|
|
@@ -11,7 +11,7 @@ import DebugManager from "../../utils/debug-manager.js";
|
|
|
11
11
|
* @param {string|null} comment
|
|
12
12
|
* @returns {DocumentFragment}
|
|
13
13
|
*/
|
|
14
|
-
export const ShowIf = function(condition, child, comment) {
|
|
14
|
+
export const ShowIf = function(condition, child, comment = null) {
|
|
15
15
|
if(!(Validator.isObservable(condition))) {
|
|
16
16
|
return DebugManager.warn('ShowIf', "ShowIf : condition must be an Observable / "+comment, condition);
|
|
17
17
|
}
|
package/src/router/Router.js
CHANGED
|
@@ -174,7 +174,6 @@ export default function Router($options = {}) {
|
|
|
174
174
|
$currentState.query = query;
|
|
175
175
|
$currentState.path = path;
|
|
176
176
|
|
|
177
|
-
console.log($currentState.query)
|
|
178
177
|
const middlewares = [...route.middlewares(), trigger];
|
|
179
178
|
let currentIndex = 0;
|
|
180
179
|
const request = { ...$currentState };
|
|
@@ -195,7 +194,7 @@ Router.routers = {};
|
|
|
195
194
|
|
|
196
195
|
/**
|
|
197
196
|
*
|
|
198
|
-
* @param {{mode: 'memory'|'history'|'hash', name
|
|
197
|
+
* @param {{mode: 'memory'|'history'|'hash', name?:string, entry?: string}} options
|
|
199
198
|
* @param {Function} callback
|
|
200
199
|
* @param {Element} container
|
|
201
200
|
*/
|
|
@@ -19,12 +19,10 @@ export function RouterComponent(router, container) {
|
|
|
19
19
|
const { route, params, query, path } = state;
|
|
20
20
|
if($cache.has(path)) {
|
|
21
21
|
const cacheNode = $cache.get(path);
|
|
22
|
-
console.log(cacheNode);
|
|
23
22
|
updateContainer(cacheNode);
|
|
24
23
|
return;
|
|
25
24
|
}
|
|
26
|
-
const Component = route.component()
|
|
27
|
-
console.log({ params, query })
|
|
25
|
+
const Component = route.component();
|
|
28
26
|
const node = Component({ params, query });
|
|
29
27
|
$cache.set(path, node);
|
|
30
28
|
updateContainer(node);
|
|
@@ -1,8 +1,5 @@
|
|
|
1
|
-
// Build configuration
|
|
2
|
-
const isProd = process.env.NODE_ENV === 'production';
|
|
3
|
-
|
|
4
1
|
const DebugManager = {
|
|
5
|
-
enabled:
|
|
2
|
+
enabled: false,
|
|
6
3
|
|
|
7
4
|
enable() {
|
|
8
5
|
this.enabled = true;
|
|
@@ -13,7 +10,7 @@ const DebugManager = {
|
|
|
13
10
|
this.enabled = false;
|
|
14
11
|
},
|
|
15
12
|
|
|
16
|
-
log
|
|
13
|
+
log(category, message, data) {
|
|
17
14
|
if (!this.enabled) return;
|
|
18
15
|
console.group(`🔍 [${category}] ${message}`);
|
|
19
16
|
if (data) console.log(data);
|
|
@@ -21,12 +18,12 @@ const DebugManager = {
|
|
|
21
18
|
console.groupEnd();
|
|
22
19
|
},
|
|
23
20
|
|
|
24
|
-
warn
|
|
21
|
+
warn(category, message, data) {
|
|
25
22
|
if (!this.enabled) return;
|
|
26
23
|
console.warn(`⚠️ [${category}] ${message}`, data);
|
|
27
24
|
},
|
|
28
25
|
|
|
29
|
-
error
|
|
26
|
+
error(category, message, error) {
|
|
30
27
|
console.error(`❌ [${category}] ${message}`, error);
|
|
31
28
|
}
|
|
32
29
|
};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import Validator from "../utils/validator";
|
|
2
2
|
import NativeDocumentError from "../errors/NativeDocumentError";
|
|
3
|
-
|
|
3
|
+
import {BOOLEAN_ATTRIBUTES} from "./constants.js";
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
*
|
|
@@ -52,6 +52,40 @@ function bindStyleAttribute(element, data) {
|
|
|
52
52
|
}
|
|
53
53
|
}
|
|
54
54
|
|
|
55
|
+
/**
|
|
56
|
+
*
|
|
57
|
+
* @param {HTMLElement} element
|
|
58
|
+
* @param {string} attributeName
|
|
59
|
+
* @param {boolean|number|Observable} value
|
|
60
|
+
*/
|
|
61
|
+
function bindBooleanAttribute(element, attributeName, value) {
|
|
62
|
+
element[attributeName] = Boolean(Validator.isObservable(value) ? value.val() : value);
|
|
63
|
+
if(Validator.isObservable(value)) {
|
|
64
|
+
value.subscribe(newValue => {
|
|
65
|
+
element[attributeName] = Boolean(newValue);
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
*
|
|
73
|
+
* @param {HTMLElement} element
|
|
74
|
+
* @param {string} attributeName
|
|
75
|
+
* @param {Observable} value
|
|
76
|
+
*/
|
|
77
|
+
function bindAttributeWithObservable(element, attributeName, value) {
|
|
78
|
+
value.subscribe(newValue => element.setAttribute(attributeName, newValue));
|
|
79
|
+
element.setAttribute(attributeName, value.val());
|
|
80
|
+
if(attributeName === 'value') {
|
|
81
|
+
if(['checkbox', 'radio'].includes(element.type)) {
|
|
82
|
+
element.addEventListener('input', () => value.set(element.checked));
|
|
83
|
+
} else {
|
|
84
|
+
element.addEventListener('input', () => value.set(element.value));
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
55
89
|
/**
|
|
56
90
|
*
|
|
57
91
|
* @param {HTMLElement} element
|
|
@@ -62,22 +96,18 @@ export default function AttributesWrapper(element, attributes) {
|
|
|
62
96
|
Validator.validateAttributes(attributes);
|
|
63
97
|
|
|
64
98
|
if(!Validator.isObject(attributes)) {
|
|
65
|
-
console.log(attributes);
|
|
66
99
|
throw new NativeDocumentError('Attributes must be an object');
|
|
67
100
|
}
|
|
68
101
|
|
|
69
|
-
for(let
|
|
102
|
+
for(let key in attributes) {
|
|
103
|
+
const attributeName = key.toLowerCase();
|
|
70
104
|
const value = attributes[attributeName];
|
|
105
|
+
if(BOOLEAN_ATTRIBUTES.includes(attributeName)) {
|
|
106
|
+
bindBooleanAttribute(element, attributeName, value);
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
71
109
|
if(Validator.isObservable(value)) {
|
|
72
|
-
|
|
73
|
-
element.setAttribute(attributeName, value.val());
|
|
74
|
-
if(attributeName === 'value') {
|
|
75
|
-
if(['checkbox', 'radio'].includes(element.type)) {
|
|
76
|
-
element.addEventListener('input', () => value.set(element.checked));
|
|
77
|
-
} else {
|
|
78
|
-
element.addEventListener('input', () => value.set(element.value));
|
|
79
|
-
}
|
|
80
|
-
}
|
|
110
|
+
bindAttributeWithObservable(element, attributeName, value);
|
|
81
111
|
continue;
|
|
82
112
|
}
|
|
83
113
|
if(attributeName === 'class' && Validator.isJson(value)) {
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
|
|
2
|
+
export const BOOLEAN_ATTRIBUTES = ['checked', 'selected', 'disabled', 'readonly', 'required', 'autofocus', 'multiple', 'autocomplete', 'hidden', 'contenteditable', 'spellcheck', 'translate', 'draggable', 'async', 'defer', 'autoplay', 'controls', 'loop', 'muted', 'download', 'reversed', 'open', 'default', 'formnovalidate', 'novalidate', 'scoped', 'itemscope', 'allowfullscreen', 'allowpaymentrequest', 'playsinline'];
|