forked from vaadin/vaadin-grid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvaadin-grid-templatizer.html
166 lines (140 loc) · 4.72 KB
/
vaadin-grid-templatizer.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
<link rel="import" href="../polymer/polymer.html">
<script>
window.vaadin = window.vaadin || {};
vaadin.elements = vaadin.elements || {};
vaadin.elements.grid = vaadin.elements.grid || {};
vaadin.elements.grid.Templatizer = Polymer({
is: 'vaadin-grid-templatizer',
behaviors: [Polymer.Templatizer],
properties: {
dataHost: Object,
template: Object,
_templateInstances: {
type: Array,
value: function() {
return [];
}
},
_parentPathValues: {
value: function() {
return {};
}
}
},
observers: [
'_templateInstancesChanged(_templateInstances.*, _parentPathValues.*)'
],
created: function() {
// needed for V2 to enable event.model on declarative event listeners.
this._parentModel = true;
this._instanceProps = {
expanded: true,
index: true,
item: true,
selected: true
};
},
createInstance: function() {
this._ensureTemplatized();
var instance = this.stamp({});
this.addInstance(instance);
return instance;
},
addInstance: function(instance) {
if (this._templateInstances.indexOf(instance) === -1) {
this.push('_templateInstances', instance);
}
},
removeInstance: function(instance) {
var index = this._templateInstances.indexOf(instance);
this.splice('_templateInstances', index, 1);
},
_ensureTemplatized: function() {
// Avoid multiple templatize for the template
if (!this.template._templatized) {
this.template._templatized = true;
this.templatize(this.template);
// TODO: hack to avoid: https://github.com/Polymer/polymer/issues/3307
this._parentProps = this._parentProps || {};
// TODO: V1 only. For some reason, parent properties are not forwarded
// initially, they need manual forwarding.
if (!Polymer.Element) {
Object.keys(this._parentProps).forEach(function(prop) {
if (this._parentProps[prop]) {
// this._forwardParentProp(prop, this.dataHost[prop]);
}
}, this);
}
}
},
_forwardInstanceProp: function(inst, prop, value) {
// fire notification event only when a prop is changed through a user-action.
// e.g. 'expanded' is different from the originally bound '__expanded__' value.
if (inst['__' + prop + '__'] !== undefined &&
inst['__' + prop + '__'] !== value) {
this.fire('template-instance-changed', {
prop: prop,
value: value,
inst: inst
});
}
},
_forwardInstancePath: function(inst, path, value) {
// TODO: assuming we're currently just listening to [[item.xxxx]] properties
// which affect only cells on the current row.
if (path.indexOf('item.') === 0 && !this._suppressItemChangeEvent) {
this.fire('item-changed', {
item: inst.item,
// stripping 'item.' from path.
path: path.substring(5),
value: value
});
}
},
_notifyInstancePropV2: function(inst, prop, value) {
this._forwardInstanceProp(inst, prop, value);
this._forwardInstancePath(inst, prop, value);
},
_forwardParentProp: function(prop, value) {
this._parentPathValues[prop] = value;
this._templateInstances.forEach(function(inst) {
inst.set(prop, value);
}, this);
},
_forwardParentPath: function(path, value) {
this.set(['_parentPathValues', path], value);
this._templateInstances.forEach(function(inst) {
inst.notifyPath(path, value);
}, this);
},
_forwardHostPropV2: function(prop, value) {
this._forwardParentProp(prop, value);
// TODO: _forwardedParentPropsChanged isn't triggered for some reason in
// all cases in Hybrid mode. Try removing this after running pure P2.
if (this._templateInstances) {
this._templateInstances.forEach(function(inst) {
inst.notifyPath(prop, value);
}, this);
}
},
_templateInstancesChanged: function(t, p) {
var index, count;
if (t.path === '_templateInstances') {
// Iterate all instances
index = 0;
count = this._templateInstances.length;
} else if (t.path === '_templateInstances.splices') {
// Iterate only new instances
index = t.value.index;
count = t.value.addedCount;
} else {
return;
}
Object.keys(this._parentPathValues || {}).forEach(function(keyName) {
for (var i = index; i < index + count; i++) {
this._templateInstances[i].set(keyName, this._parentPathValues[keyName]);
}
}, this);
}
});
</script>