-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBindable.js
291 lines (270 loc) · 8.62 KB
/
Bindable.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// Copyright (c) 2018 Weaverize SAS <dev@weaverize.com>.
/**
* Class Mixin that extends a Polymer Class to add dynamic binding capabilities
* Adds a _bind method
* @param {*} superClass the class to extend
*/
var Bindable = function (superClass) {
return class extends superClass {
constructor() {
super();
//__data is where Polymer stores it's binded data
new Binder(this, "__data");
}
/**
* Sync the properties of two objects together both ways
* Shared value will the one of the object bound to
* @param {String} path dotted path of the property within this object
* @param {*} destObj object to sync
* @param {String} [destPath] dotted path to the destination property, path will be copied if not provided
* @returns {function} function to undo the binding
*/
_bind(path, destObj, destPath) {
//mock method replaced by Binder.bindPropertyBothWays at runtime
//used for documentation purposes
}
/**
* Binds a property of this object with a target's object property
* @param {String} path dotted path of the property within this object
* @param {*} destObj object to sync
* @param {String} [destPath] dotted path to the destination property, path will be copied if not provided
* @param {function} [setter] custom function to set a new value on the destination object
* @returns {function} function to undo the binding
*/
_bindOneWay(path, destObj, destPath, setter) {
//mock method replaced by Binder.bindProperty at runtime
//used for documentation purposes
}
}
}
/**
* Class that handles the dynamic binding
* @param {*} element target to make bindable
* @param {String} entrypoint name of the root property to make bindable
*/
var Binder = class {
constructor(element, entrypoint) {
//Map => { keys: proxy, values: [ { property : name, setting : function } ]
this.binds = new Map;
this.element = element;
this.entrypoint = entrypoint;
if (!this.element[this.entrypoint] || typeof this.element[this.entrypoint] != "object")
{
this.element[this.entrypoint] = {};
}
this.handler = {
set(obj, prop, value, parent) {
if (obj[prop] != value) {
//console.log("setting", prop, obj[prop], value);
Reflect.set(obj, prop, value);
if (value && typeof value == "object") {
obj[prop] = this.deepBind(obj[prop]);
}
this.notifyChanges(obj, prop, value, parent);
return true;
}
else {
//the value is unchanged, lets avoid propagation of unchanged
return Reflect.set(obj, prop, value);
}
},
deleteProperty(obj, prop) {
//FIXME: remove from this.binds the property
//don't know how to find the bind key corresponding to the current proxy...
//maybe by adding a reference to the original obj[prop] in this.binds
return Reflect.deleteProperty(obj, prop);
}
}
this.handler.set = this.handler.set.bind(this);
this.handler.deleteProperty = this.handler.deleteProperty.bind(this);
this.bindProperty = this.bindProperty.bind(this);
this.bindPropertyBothWays = this.bindPropertyBothWays.bind(this)
element[entrypoint] = this.deepBind(element[entrypoint]);
//to expose the binding at runtime uncomment:
element._binds = this.binds;
//making the default binding two-way by default seemed the default behavior
element._bind = this.bindPropertyBothWays;
element._bindOneWay = this.bindProperty;
this.addGetterAndSetter();
}
/**
* Adds a bindable property to the register
* @param {*} property
*/
register(property) {
if (this.binds.has(property)) {
console.log("this property already is registered as a bind", this.binds.get(property));
}
else {
this.binds.set(property, []);
}
}
/**
* Retrieves a property of an object using a path
* @param {*} obj object containing the property
* @param {String} path dotted path to the property
* @returns the object retrieved
*/
resolve(obj, path) {
var steps = path.split(".");
var crawled = obj;
if (path != "") {
for (var step of steps) {
crawled = crawled[step];
}
}
return crawled;
}
/**
* Retrieves the parent property of a property
* @param {*} obj object containing the property
* @param {String} path dotted path to the children property
* @returns the parent property of the children property
*/
resolveParent(obj, path) {
var steps = path.split(".");
steps.pop();
return this.resolve(obj, steps.join("."));
}
/**
* Setter factory that provides a function to set a value
* @param {*} obj Object to set
* @param {String} prop name of the property to set
* @returns function able to set the value
*/
makeSetter(obj, prop) {
return function (value) {
if (obj.get != undefined)
{
if (obj.get(prop) != value) {
obj.set(prop, value);
}
if (obj.notifyPath != undefined)
{
obj.notifyPath(prop);
}
}
else
{
var parent = this.resolveParent(obj, prop);
var property = prop.split(".").pop();
if (parent[property] != value)
{
parent[property] = value;
}
}
}
}
/**
* Sync the properties of two objects together both ways
* Shared value will the one of the object bound to
* @param {String} path dotted path of the property within this object
* @param {*} destObj object to sync
* @param {String} [destPath] dotted path to the destination property, path will be copied if not provided
* @returns {function} function to undo the binding
*/
bindPropertyBothWays(path, destObj, destPath) {
var destPath = destPath || path;
var undos = [];
undos.push(this.bindProperty(path, destObj, destPath));
if (typeof destObj._bind == "function") {
undos.push(destObj._bindOneWay(destPath, this.element, path));
}
else
{
console.log(destObj, "is not Bindable, binding will be one way only.\nUse ._bindOneWay() or make it Bindable to avoid this message.");
}
return function () {
undos.forEach(function (undo) {
undo();
});
}
}
/**
* Binds a property of this object with a target's object property
* @param {String} path dotted path of the property within this object
* @param {*} destObj object to sync
* @param {String} [destPath] dotted path to the destination property, path will be copied if not provided
* @param {function} [setter] custom function to set a new value on the destination object
* @returns {function} function to undo the binding
*/
bindProperty(path, destObj, destPath, setter) {
var destPath = destPath || path;
var parent = this.resolveParent(this.element[this.entrypoint], path);
var property = path.split(".").pop();
if (parent && this.binds.has(parent)) {
var setting = {
property: property,
setter: setter || this.makeSetter(destObj, destPath)
};
setting.setter = setting.setter.bind(this);
setting.setter(parent[property]);
var binds = this.binds.get(parent);
binds.push(setting);
//unbinding:
return function () {
if (binds.indexOf(setting) != -1) {
binds.splice(binds.indexOf(setting), 1);
}
};
}
else {
console.log("this property doesn't exist or cannot be binded (is it notify: true ?)", parent, property);
}
}
/**
* Notifies if necessary of the change to a bindable property's value
* @param {*} obj proxy's target object
* @param {*} prop name of the property that was modified
* @param {*} value new value of the property
* @param {*} proxy the proxy that intercepted the call
*/
notifyChanges(obj, prop, value, proxy) {
if (this.binds.has(proxy)) {
var watchers = this.binds.get(proxy);
for (var watcher of watchers) {
if (watcher.property == prop) {
watcher.setter(value);
}
}
}
else {
console.log("something went really wrong !");
}
}
/**
* Crawls recursively within a tree of object and replace the object by proxies
* Ignores non object property
* FIXME: /!\ Doesn't handle cycling in object /!\
* @param {*} property replace it by a proxy
* @returns the proxy replacement to the property
*/
deepBind(property) {
if (property && typeof property == "object") {
for (var subprop in property) {
if (property[subprop] && typeof property[subprop] == "object") {
property[subprop] = this.deepBind(property[subprop]);
}
}
var proxy = new Proxy(property, this.handler);
this.register(proxy);
return proxy;
}
}
addGetterAndSetter() {
if (this.element.get == undefined)
{
this.element.get = function(path) {
return this.resolve(this.element[this.entrypoint], path);
}.bind(this);
}
if (this.element.set == undefined)
{
this.element.set = function(path, value) {
var parent = this.resolveParent(this.element[this.entrypoint], path);
var property = path.split(".").pop();
parent[property] = value;
}.bind(this);
}
}
}