-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdom.js
116 lines (103 loc) · 2.94 KB
/
dom.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
const device = require('tns-core-modules/platform').device;
const View = require('tns-core-modules/ui/core/view/view').View;
const ViewUtil = require('./view-util');
const viewUtil = new ViewUtil(device);
const eventsMap = {
button: ['tap'],
listView: ['itemTap'],
textField: ['returnPress']
//TODO other component events...
};
global['Node'] = View;
// Object.defineProperty(View.prototype, 'parentNode', {
// get: function parentNode() {
// console.log('parent ', this.parent);
// return this.parent;
// }
// });
View.prototype.appendChild = function(child, index) {
viewUtil.insertChild(this, child, index);
child['parentNode'] = this;
return child;
};
View.prototype.replaceChild = function(newChild, oldChild) {
var index = this._subViews.indexOf(oldChild);
this.appendChild(newChild, index);
viewUtil.removeChild(this, oldChild);
return this;
};
View.prototype.insertBefore = function(newChild, oldChild) {
var index = this._subViews.indexOf(oldChild);
return this.appendChild(newChild, index);
};
Object.defineProperty(View.prototype, 'nextSibling', {
get: function nextSibling() {
if (this.parentNode) {
const parentNodes = this.parentNode._subViews;
return parentNodes[parentNodes.indexOf(this) + 1] || null;
}
return null;
}
});
Object.defineProperty(View.prototype, 'previousSibling', {
get: function previousSibling() {
if (this.parentNode) {
const parentNodes = this.parentNode._subViews;
return parentNodes[parentNodes.indexOf(this) - 1] || null;
}
return null;
}
});
Object.defineProperty(View.prototype, 'lastChild', {
get: function lastChild() {
return this._subViews[this._subViews.length - 1];
}
});
Object.defineProperty(View.prototype, 'childNodes', {
get: function childNodes() {
return this._subViews;
}
});
//TODO textContent
Object.defineProperty(View.prototype, 'textContent', {
get: function textContent() {
return 'textContent';
},
set: function textContent(value) {
console.log(value);
}
});
global['document'] = {
createElement(tag) {
const view = viewUtil.createView(tag);
view['prototype'] = View.prototype;
eventsMap[tag] &&
eventsMap[tag].forEach(evt => {
let nativeEvent = evt,
viewEvent = evt;
if (typeof evt != 'string') {
nativeEvent = Object.keys(evt)[0];
viewEvent = evt[nativeEvent];
}
view.on(nativeEvent, e => {
view[viewEvent] && view[viewEvent].call(view, e);
});
});
return view;
},
createComment(text) {
const view = viewUtil.createComment();
view['prototype'] = View.prototype;
return view;
},
createTextNode(text) {
const view = viewUtil.createText();
view.nodeType = 3;
view['prototype'] = View.prototype;
return view;
}
};
global['document'].createElementNS = global['document'].createElement;
View.prototype.setAttribute = function(attr, val) {
this[attr] = val;
};