native-document 1.0.16 → 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.
@@ -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,106 +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
- return function(target, name) {
50
- target[name] = element;
51
- return element;
52
- };
53
- }
54
- if(property === 'unmountChildren') {
55
- return () => {
56
- for(let i = 0, length = element.children.length; i < length; i++) {
57
- let elementchildren = element.children[i];
58
- if(!elementchildren.$ndProx) {
59
- elementchildren.nd?.remove();
60
- }
61
- elementchildren = null;
62
- }
63
- };
64
- }
65
- if(property === 'remove') {
66
- return function() {
67
- element.nd.unmountChildren();
68
- lifecycle = null;
69
- element.$ndProx = null;
70
- delete element.nd?.on?.prevent;
71
- delete element.nd?.on;
72
- delete element.nd;
73
- };
74
- }
75
- if(property === 'hasLifecycle') {
76
- return lifecycle !== null;
77
- }
78
- if(property === 'lifecycle') {
79
- if(lifecycle) {
80
- return lifecycle;
81
- }
82
- let $observer = null;
83
- lifecycle = function(states) {
84
- $observer = $observer || DocumentObserver.watch(element);
85
-
86
- states.mounted && $observer.mounted(states.mounted);
87
- states.unmounted && $observer.unmounted(states.unmounted);
88
- return element;
89
- };
90
- return lifecycle;
91
- }
92
- if(property === 'mounted' || property === 'unmounted') {
93
- return function(callback) {
94
- element.nd.lifecycle({ [property]: callback});
95
- return element;
96
- };
97
- }
98
- },
99
- set(target, p, newValue, receiver) {
100
-
101
- },
102
- configurable: true
103
- });
104
- return this.$ndProx;
9
+ this.$nd = new NDElement(this);
10
+ this.$nd.nd = this.$nd;
11
+ return this.$nd;
105
12
  }
106
13
  });