-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdom.js
126 lines (106 loc) · 2.38 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
117
118
119
120
121
122
123
124
125
126
"use strict";
require("jquery");
var $ = jQuery,
util = require("./util");
var __ = "__",
__gets = []
function defineProperty(obj, name, getter, setter) {
var defaultGet = function() {
return obj[__ + name];
}
var defaultSet = function(value) {
obj[__ + name] = value;
}
Object.defineProperty(obj, name, {
get: function() {
var caller = __gets[name];
caller = caller || []
__gets[name]
return bValue;
},
set: function(newValue) {
bValue = newValue;
},
enumerable: true,
configurable: true
});
}
var div = {};
defineProperty(div, "width",
function get() {
return this._width;
},
function set(value) {
value = +value || 0;
if (+value < 100 && +value > 0) {
this._width = value;
return true;
}
else {
return false;
}
})
div({
id: "div",
x: 100,
y: 100,
w: 400,
h: function() {
return this.w / 2
},
children: [
div({
id: 'child1',
w: function() {
return this.parent.h / 2
}
}),
div({
id: 'child2'
})]
})
/*
1. Init all function();
2. save
*/
function Item(obj) {
if (this instanceof Item) {
this.processObj(obj);
}
else {
return new Item(obj);
}
}
Item.prototype = {
processValue: function(key, value) {
if (typeof(value) === "function") {
Object.defineProperty(this, key, {
get: function() {
var slot = key+"Changed";
if (typeof(slot) === "function") {
slot(value);
}
return value();
},
set: function(newValue) {
this.processValue(key, value);
},
enumerable: true,
configurable: true
});
}
else {
this[key] = value;
}
},
processObj: function(obj) {
this.__obj = {}
util.extend(this.__obj, obj);
var key, value;
for (key in obj) {
if (!obj.hasOwnProperty(key)) continue;
value = obj[key];
this.processValue("key", value);
}
}
}