-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathaccessors.html
230 lines (203 loc) · 7 KB
/
accessors.html
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
<!--
@license
Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->
<script>
Polymer.Bind = {
// for prototypes (usually)
prepareModel: function(model) {
model._propertyEffects = {};
model._bindListeners = [];
// TODO(sjmiles): no mixin function?
var api = this._modelApi;
for (var n in api) {
model[n] = api[n];
}
},
_modelApi: {
_notifyChange: function(property) {
var eventName = Polymer.CaseMap.camelToDashCase(property) + '-changed';
// TODO(sjmiles): oops, `fire` doesn't exist at this layer
this.fire(eventName, {
value: this[property]
}, {bubbles: false});
},
// TODO(sjmiles): removing _notifyListener from here breaks accessors.html
// as a standalone lib. This is temporary, as standard/configure.html
// installs it's own version on Polymer.Base, and we need that to work
// right now.
// NOTE: exists as a hook for processing listeners
/*
_notifyListener: function(fn, e) {
// NOTE: pass e.target because e.target can get lost if this function
// is queued asynchrously
return fn.call(this, e, e.target);
},
*/
_propertySet: function(property, value, effects) {
var old = this.__data__[property];
if (old !== value) {
this.__data__[property] = value;
if (typeof value == 'object') {
this._clearPath(property);
}
if (this._propertyChanged) {
this._propertyChanged(property, value, old);
}
if (effects) {
this._effectEffects(property, value, effects, old);
}
}
return old;
},
_effectEffects: function(property, value, effects, old) {
effects.forEach(function(fx) {
//console.log(fx);
var fn = Polymer.Bind['_' + fx.kind + 'Effect'];
if (fn) {
fn.call(this, property, value, fx.effect, old);
}
}, this);
},
_clearPath: function(path) {
for (var prop in this.__data__) {
if (prop.indexOf(path + '.') === 0) {
this.__data__[prop] = undefined;
}
}
}
},
// a prepared model can acquire effects
ensurePropertyEffects: function(model, property) {
var fx = model._propertyEffects[property];
if (!fx) {
fx = model._propertyEffects[property] = [];
}
return fx;
},
addPropertyEffect: function(model, property, kind, effect) {
var fx = this.ensurePropertyEffects(model, property);
fx.push({
kind: kind,
effect: effect
});
},
createBindings: function(model) {
//console.group(model.is);
// map of properties to effects
var fx$ = model._propertyEffects;
if (fx$) {
// for each property with effects
for (var n in fx$) {
// array of effects
var fx = fx$[n];
// effects have priority
fx.sort(this._sortPropertyEffects);
// create accessors
this._createAccessors(model, n, fx);
}
}
//console.groupEnd();
},
_sortPropertyEffects: (function() {
// TODO(sjmiles): EFFECT_ORDER buried this way is not ideal,
// but presumably the sort method is going to be a hot path and not
// have a `this`. There is also a problematic dependency on effect.kind
// values here, which are otherwise pluggable.
var EFFECT_ORDER = {
'compute': 0,
'annotation': 1,
'computedAnnotation': 2,
'reflect': 3,
'notify': 4,
'observer': 5,
'complexObserver': 6,
'function': 7
};
return function(a, b) {
return EFFECT_ORDER[a.kind] - EFFECT_ORDER[b.kind];
};
})(),
// create accessors that implement effects
_createAccessors: function(model, property, effects) {
var defun = {
get: function() {
// TODO(sjmiles): elide delegation for performance, good ROI?
return this.__data__[property];
}
};
var setter = function(value) {
this._propertySet(property, value, effects);
};
// ReadOnly properties have a private setter only
// TODO(kschaaf): Per current Bind factoring, we shouldn't
// be interrogating the prototype here
if (model.getPropertyInfo && model.getPropertyInfo(property).readOnly) {
model['_set' + this.upper(property)] = setter;
} else {
defun.set = setter;
}
Object.defineProperty(model, property, defun);
},
upper: function(name) {
return name[0].toUpperCase() + name.substring(1);
},
_addAnnotatedListener: function(model, index, property, path, event) {
var fn = this._notedListenerFactory(property, path,
this._isStructured(path), this._isEventBogus);
var eventName = event ||
(Polymer.CaseMap.camelToDashCase(property) + '-changed');
model._bindListeners.push({
index: index,
property: property,
path: path,
changedFn: fn,
event: eventName
});
},
_isStructured: function(path) {
return path.indexOf('.') > 0;
},
_isEventBogus: function(e, target) {
return e.path && e.path[0] !== target;
},
_notedListenerFactory: function(property, path, isStructured, bogusTest) {
return function(e, target) {
if (!bogusTest(e, target)) {
if (e.detail && e.detail.path) {
this.notifyPath(this._fixPath(path, property, e.detail.path),
e.detail.value);
} else {
var value = target[property];
if (!isStructured) {
this[path] = target[property];
} else {
// TODO(kschaaf): dirty check avoids null references when the object has gone away
if (this.__data__[path] != value) {
this.set(path, value);
}
}
}
}
};
},
// for instances
prepareInstance: function(inst) {
inst.__data__ = Object.create(null);
},
setupBindListeners: function(inst) {
inst._bindListeners.forEach(function(info) {
// Property listeners:
// <node>.on.<property>-changed: <path]> = e.detail.value
//console.log('[_setupBindListener]: [%s][%s] listening for [%s][%s-changed]', this.localName, info.path, info.id || info.index, info.property);
var node = inst._nodes[info.index];
node.addEventListener(info.event, inst._notifyListener.bind(inst, info.changedFn));
});
}
};
</script>