-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbindable.js
242 lines (194 loc) · 5.89 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
var createSpec = require('spec-js'),
EventEmitter = require('events').EventEmitter,
merge = require('./flatMerge'),
statham = require('statham'),
jsonConverter = require('./jsonConverter');
function clone(object){
var copy = jsonConverter(object, object.__serialiseExclude__, object.__serialiseInclude__);
if(!copy){
return;
}
Object.keys(copy).forEach(function(key){
var item = copy[key];
if(!item || typeof item !== 'object'){
return;
}else if(typeof copy[key]._clone === 'function'){
copy[key] = item._clone();
}else{
copy[key] = clone(item);
}
if(copy[key] === undefined){
delete copy[key];
}
});
return copy;
}
var stack = [];
function eventually(fn){
stack.push(fn);
if(stack.length === 1){
setTimeout(function(){
while(stack.length){
stack.pop()();
}
},100);
}
}
function getItemPath(item){
var gedi = item.gaffa.gedi,
paths = [],
referenceItem = item;
while(referenceItem){
// item.path should be a child ref after item.sourcePath
if(referenceItem.path != null){
paths.push(referenceItem.path);
if(gedi.paths.isAbsolute(referenceItem.path)){
break;
}
}
// item.sourcePath is most root level path
if(referenceItem.sourcePath != null){
paths.push(gedi.paths.create(referenceItem.sourcePath));
}
referenceItem = referenceItem.parent;
}
return gedi.paths.resolve.apply(this, paths.reverse());
}
var iuid = 0;
function Bindable(){
this.setMaxListeners(1000);
// instance unique ID
this.__iuid = iuid++;
}
Bindable = createSpec(Bindable, EventEmitter);
Bindable.bindables = {};
Bindable.getByIuid = function(id){
return this.bindables[id];
};
Bindable.prototype.getPath = function(){
return getItemPath(this);
};
Bindable.prototype.getDataAtPath = function(){
if(!this.gaffa){
return;
}
return this.gaffa.model.get(getItemPath(this));
};
Bindable.prototype.toJSON = function(){
var tempObject = jsonConverter(this, this.__serialiseExclude__, this.__serialiseInclude__);
return tempObject;
};
Bindable.prototype._clone = function(){
return new this.constructor(clone(this));
};
function setupCleanup(bindable, parent){
var onDebind = bindable.debind.bind(bindable);
parent.once('debind', onDebind);
bindable.once('debind', function(){
bindable.parent.removeListener('debind', onDebind);
});
var onDestroy = bindable.destroy.bind(bindable);
parent.once('destroy', onDestroy);
bindable.once('destroy', function(){
bindable.parent.removeListener('destroy', onDestroy);
});
}
Bindable.prototype.bind = function(parent, scope){
if(scope){
this.scope = merge(scope, this.scope);
}
if(parent && !parent._bound){
console.warn('Attempted to bind to a parent who was not bound.');
return;
}
if(this._bound){
this.debind();
// console.warn('Attempted to bind an already bound item.');
}
if(parent){
this.gaffa = parent.gaffa;
this.parent = parent;
this._parentId = parent.__iuid;
setupCleanup(this, parent);
}
this.updatePath();
if('path' in this && !this.path){
this._invalidPath = true;
return;
}
this._bound = true;
Bindable.bindables[this.__iuid] = this;
this.emit('bind', parent, scope);
};
Bindable.prototype.getSourcePath = function(){
return this.gaffa.gedi.paths.resolve(this.parent && this.parent.getPath(), this.sourcePath);
};
Bindable.prototype.updatePath = function(){
if(!this.pathBinding || this._pathBindingBound){
return;
}
this._pathBindingBound = true;
var bindable = this,
absoluteSourcePath = this.getSourcePath(),
lastPath = this.path,
gaffa = this.gaffa;
function setPath(valueTokens){
var newPath;
if(valueTokens){
var valueToken = valueTokens[valueTokens.length - 1];
newPath = valueToken.sourcePathInfo && valueToken.sourcePathInfo.path;
}
if(newPath === lastPath){
return;
}
lastPath = newPath;
if(!bindable._bound && !bindable._invalidPath){
bindable.path = newPath;
return;
}
bindable.debind();
bindable.path = newPath;
if(newPath == null){
bindable._invalidPath = true;
return;
}
bindable._invalidPath = false;
bindable.bind(bindable.parent, bindable.scope);
}
setPath(gaffa.gedi.get(this.pathBinding, absoluteSourcePath, bindable.scope, true));
function handlePathChange(event){
setPath(gaffa.model.get(bindable.pathBinding, bindable.parent, bindable.scope, true));
}
gaffa.gedi.bind(this.pathBinding, handlePathChange, absoluteSourcePath);
this.on('destroy', function(){
gaffa.gedi.debind(bindable.pathBinding, handlePathChange, absoluteSourcePath);
});
};
Bindable.prototype.debind = function(){
var bindable = this;
if(!this._bound){
// ToDo: This happens with actions, resolve.
return;
}
this.emit('debind');
this._bound = false;
delete Bindable.bindables[this.__iuid];
};
Bindable.prototype.destroy = function(){
var bindable = this;
this._destroyed = true;
if(this._bound){
this.debind();
}
// Destroy bindables asynchonously.
eventually(function(){
bindable.emit('destroy');
bindable.removeAllListeners();
// Let any children bound to 'destroy' do their thing before actually destroying this.
eventually(function(){
bindable.gaffa = null;
bindable.parent = null;
});
});
};
module.exports = Bindable;