native-document 1.0.15 → 1.0.17

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.
@@ -24,9 +24,9 @@ export const createTextNode = function(value) {
24
24
  export default function HtmlElementWrapper(name, customWrapper) {
25
25
  const $tagName = name.toLowerCase();
26
26
 
27
- const builder = function(attributes, children = null) {
27
+ return function(attributes, children = null) {
28
28
  try {
29
- if(Validator.isValidChildren(attributes)) {
29
+ if(!Validator.isJson(attributes)) {
30
30
  const tempChildren = children;
31
31
  children = attributes;
32
32
  attributes = tempChildren;
@@ -42,9 +42,5 @@ export default function HtmlElementWrapper(name, customWrapper) {
42
42
  DebugManager.error('ElementCreation', `Error creating ${$tagName}`, error);
43
43
  }
44
44
  };
45
-
46
- builder.hold = (children, attributes) => (() => builder(children, attributes));
47
-
48
- return builder;
49
45
  }
50
46
 
@@ -0,0 +1,81 @@
1
+ import DocumentObserver from "./DocumentObserver";
2
+ import { EVENTS } from "../utils/events";
3
+
4
+ export function NDElement(element) {
5
+ this.$element = element;
6
+ this.$observer = null;
7
+ }
8
+
9
+ for(const event of EVENTS) {
10
+ const eventName = event.toLowerCase();
11
+ NDElement.prototype['on'+event] = function(callback) {
12
+ this.$element.addEventListener(eventName, callback);
13
+ return this;
14
+ };
15
+ NDElement.prototype['onPrevent'+event] = function(callback) {
16
+ this.$element.addEventListener(eventName, function(event) {
17
+ event.preventDefault();
18
+ callback(event);
19
+ });
20
+ return this;
21
+ };
22
+ NDElement.prototype['onStop'+event] = function(callback) {
23
+ this.$element.addEventListener(eventName, function(event) {
24
+ event.stopPropagation();
25
+ callback(event);
26
+ });
27
+ return this;
28
+ };
29
+ NDElement.prototype['onPreventStop'+event] = function(callback) {
30
+ this.$element.addEventListener(eventName, function(event) {
31
+ event.stopPropagation();
32
+ event.preventDefault();
33
+ callback(event);
34
+ });
35
+ return this;
36
+ };
37
+ }
38
+
39
+ NDElement.prototype.ref = function(target, name) {
40
+ target[name] = element;
41
+ return this;
42
+ };
43
+
44
+ NDElement.prototype.unmountChildren = function() {
45
+ let element = this.$element;
46
+ for(let i = 0, length = element.children.length; i < length; i++) {
47
+ let elementchildren = element.children[i];
48
+ if(!elementchildren.$ndProx) {
49
+ elementchildren.nd?.remove();
50
+ }
51
+ elementchildren = null;
52
+ }
53
+ element = null;
54
+ return this;
55
+ };
56
+
57
+ NDElement.prototype.remove = function() {
58
+ let element = this.$element;
59
+ element.nd.unmountChildren();
60
+ element.$ndProx = null;
61
+ delete element.nd?.on?.prevent;
62
+ delete element.nd?.on;
63
+ delete element.nd;
64
+ element = null;
65
+ return this;
66
+ };
67
+
68
+ NDElement.prototype.lifecycle = function(states) {
69
+ this.$observer = this.$observer || DocumentObserver.watch(this.$element);
70
+
71
+ states.mounted && this.$observer.mounted(states.mounted);
72
+ states.unmounted && this.$observer.unmounted(states.unmounted);
73
+ return this;
74
+ };
75
+ NDElement.prototype.mounted = function(callback) {
76
+ return this.lifecycle({ mounted: callback });
77
+ };
78
+
79
+ NDElement.prototype.mounted = function(callback) {
80
+ return this.lifecycle({ unmounted: callback });
81
+ };
@@ -1,109 +1,13 @@
1
- import DocumentObserver from "./DocumentObserver";
1
+ import { NDElement } from "./NDElement";
2
2
 
3
3
  Object.defineProperty(HTMLElement.prototype, 'nd', {
4
4
  get() {
5
- if(this.$ndProx) {
6
- return this.$ndProx;
5
+ if(this.$nd) {
6
+ return this.$nd;
7
7
  }
8
- let element = this;
9
- let lifecycle = null;
10
8
 
11
- this.$ndProx = new Proxy({}, {
12
- get(target, property) {
13
- if(/^on[A-Z]/.test(property)) {
14
- const event = property.replace(/^on/, '').toLowerCase();
15
- const shouldPrevent = event.toLowerCase().startsWith('prevent');
16
- let eventName = event.replace(/^prevent/i, '');
17
- const shouldStop = event.toLowerCase().startsWith('stop');
18
- eventName = eventName.replace(/^stop/i, '');
19
-
20
- return function(callback) {
21
- if(shouldPrevent && !shouldStop) {
22
- element.addEventListener(eventName, function(event) {
23
- event.preventDefault();
24
- callback(event);
25
- });
26
- return element;
27
- }
28
- if(!shouldPrevent && shouldStop) {
29
- element.addEventListener(eventName, function(event) {
30
- event.stopPropagation();
31
- callback(event);
32
- });
33
- return element;
34
- }
35
- if(shouldPrevent && shouldStop) {
36
- element.addEventListener(eventName, function(event) {
37
- event.preventDefault();
38
- event.stopPropagation();
39
- callback(event);
40
- });
41
- return element;
42
- }
43
- element.addEventListener(eventName, callback);
44
- return element;
45
- };
46
- return fn;
47
- }
48
- if(property === 'ref') {
49
- if(ref) {
50
- return ref;
51
- }
52
- return function(target, name) {
53
- target[name] = element;
54
- return element;
55
- };
56
- }
57
- if(property === 'unmountChildren') {
58
- return () => {
59
- for(let i = 0, length = element.children.length; i < length; i++) {
60
- let elementchildren = element.children[i];
61
- if(!elementchildren.$ndProx) {
62
- elementchildren.nd?.remove();
63
- }
64
- elementchildren = null;
65
- }
66
- };
67
- }
68
- if(property === 'remove') {
69
- return function() {
70
- element.nd.unmountChildren();
71
- lifecycle = null;
72
- element.$ndProx = null;
73
- delete element.nd?.on?.prevent;
74
- delete element.nd?.on;
75
- delete element.nd;
76
- };
77
- }
78
- if(property === 'hasLifecycle') {
79
- return lifecycle !== null;
80
- }
81
- if(property === 'lifecycle') {
82
- if(lifecycle) {
83
- return lifecycle;
84
- }
85
- let $observer = null;
86
- lifecycle = function(states) {
87
- $observer = $observer || DocumentObserver.watch(element);
88
-
89
- states.mounted && $observer.mounted(states.mounted);
90
- states.unmounted && $observer.unmounted(states.unmounted);
91
- return element;
92
- };
93
- return lifecycle;
94
- }
95
- if(property === 'mounted' || property === 'unmounted') {
96
- return function(callback) {
97
- element.nd.lifecycle({ [property]: callback});
98
- return element;
99
- };
100
- }
101
- },
102
- set(target, p, newValue, receiver) {
103
-
104
- },
105
- configurable: true
106
- });
107
- return this.$ndProx;
9
+ this.$nd = new NDElement(this);
10
+ this.$nd.nd = this.$nd;
11
+ return this.$nd;
108
12
  }
109
13
  });