-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcomputed.js
174 lines (159 loc) · 5.04 KB
/
computed.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/**
* @module liaison/computed
*/
define([
"decor/Observable",
"./ObservablePath",
"./Each"
], function (Observable, ObservablePath, Each) {
"use strict";
var EMPTY_OBJECT = {},
EMPTY_ARRAY = [],
REGEXP_COMPUTED_MARKER = /^_computed/,
REGEXP_SHADOW_PROP = /^_.*Attr$/,
getPrototypeOf = Object.getPrototypeOf;
function set(name, value) {
this[name] = value;
}
function callComputedCallback(callback, a) {
return callback.apply(this, a);
}
function mapPath(path) {
return new ObservablePath(this, path);
}
function Computed(callback, paths) {
this.callback = callback;
this.paths = paths;
}
Computed.prototype = {
_computedMarker: "_computed",
clone: function () {
// Returns an inactive state of clone
return new Computed(this.callback, this.paths).init(this.o, this.name);
},
init: function (o, name) {
this.remove();
o !== undefined && (this.o = o);
name !== undefined && (this.name = name);
return this;
},
activate: function () {
var o = this.o;
if (typeof o.getProps !== "function" || !REGEXP_SHADOW_PROP.test(this.name)) {
var sourcePaths = [],
entryPaths = [];
this.paths.forEach(function (path) {
if (path[0] !== "@") {
sourcePaths.push(path);
} else {
var index = sourcePaths.length - 1;
(entryPaths[index] = entryPaths[index] || []).push(path.substr(1));
}
}, this);
var sources = sourcePaths.map(mapPath, o),
callback = callComputedCallback.bind(o, this.callback);
this.source = new Each(sources, entryPaths, callback);
set.call(o, this.name, this.source.open((typeof o.set === "function" ? o.set : set).bind(o, this.name)));
}
return this;
},
remove: function () {
if (this.source) {
this.source.remove();
this.source = null;
}
}
};
/**
* @function module:liaison/computed
* @param {Function} callback The function to calculate computed property value.
* @param {...string} var_args The paths from the parent object.
* @returns
* The computed property object.
* The computed property is calculated every time one of the values of the paths changes.
* @example
* var o = wrapper.wrap({
* first: "John",
* last: "Doe",
* name: computed(function (first, last) {
* return first + " " + last;
* }, "first", "last")
* });
* computed.apply(o); // Makes computed properties under o active
* new ObservablePath(o, "name").observe(function (newValue, oldValue) {
* // "John Doe" comes to newValue
* // "Ben Doe" comes to oldValue
* });
* o.set("first", "Ben");
* @example
* var o = wrapper.wrap({
* items: [
* {Name: "Anne Ackerman"},
* {Name: "Ben Beckham"},
* {Name: "Chad Chapman"},
* {Name: "Irene Ira"}
* ],
* includeShortName: false,
* totalNameLength: computed(function (a, includeShortName) {
* return a.reduce(function (length, entry) {
* return length + (includeShortName || entry.Name.length >= 10 ? entry.Name.length : 0);
* }, 0);
* }, "items", "@Name", "includeShortName") // "@Name" let the computed array observe Name property in each array entries in o.items
* });
* computed.apply(o); // Makes computed properties under o active
* new ObservablePath(o, "totalNameLength").observe(function (newValue, oldValue) {
* // 57 comes to newValue
* // 36 comes to oldValue
* });
* o.items.push({Name: "John Jacklin"});
* o.set("includeShortName", true);
*/
var computed = function (callback) {
return new Computed(callback, EMPTY_ARRAY.slice.call(arguments, 1));
};
/**
* Look for computed properties under the given object and activate them by applying the object and the property name in thre tree.
* @function module:liaison/computed.apply
* @param o The object tree.
*/
computed.apply = function (o) {
var root = o,
tree = [],
handles = [];
function applyImpl(o) {
var proto,
index = tree.indexOf(o),
isRoot = root === o;
if (index < 0) {
tree.push(o);
if (Array.isArray(o)) {
o.forEach(function (entry, i) {
computed.test(entry) ? handles.push(entry.clone().init(o, i).activate()) : applyImpl(entry);
});
} else if (Observable.test(o) || o && typeof o === "object" && (isRoot || (proto = getPrototypeOf(o)) && !getPrototypeOf(proto))) {
for (var s in o) {
var value;
try {
value = o[s];
} catch (e) {
continue; // IE9 throws accessing some DOM properties
}
computed.test(value) ? handles.push(value.clone().init(o, s).activate()) : applyImpl(value);
}
}
tree.pop();
}
}
applyImpl(o);
return handles;
};
/**
* @function module:liaison/computed.test
* @param o A value
* @returns {boolean} True if the given value is a computed property.
*/
computed.test = function (o) {
return REGEXP_COMPUTED_MARKER.test((o || EMPTY_OBJECT)._computedMarker);
};
return computed;
});