-
Notifications
You must be signed in to change notification settings - Fork 9
/
brainy-table-templatizer-behavior.html
executable file
·172 lines (145 loc) · 4.61 KB
/
brainy-table-templatizer-behavior.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
<link rel="import" href="../polymer/polymer.html">
<script>
var Brainy = window.Brainy || {};
/**
* A base behavior implemented by cells and row details instances.
*
* @polymerBehavior Brainy.TableTemplatizerBehavior
*/
Brainy.TableTemplatizerBehaviorImpl = {
properties: {
expanded: Boolean,
index: Number,
item: Object,
table: Object,
template: Object,
// singleton
_instances: {
type: Array,
value: []
},
_instance: {
type: Object,
computed: '_templatize(template)'
}
},
observers: [
'_expandedChanged(_instance, expanded)',
'_indexChanged(_instance, index)',
'_itemChanged(_instance, item)',
'_itemPathChanged(_instance, item.*)'
],
created: function() {
this._instanceProps = {
column: true,
expanded: true,
index: true,
item: true
};
},
_templatize: function(template) {
this.templatize(template);
// fix _rootDataHost to the context where template has been defined
if (template._rootDataHost) {
this._getRootDataHost = function() {
return template._rootDataHost;
};
}
var instance = this.stamp();
this._instances.push(instance);
Polymer.dom(this).appendChild(instance.root);
return instance;
},
// this overrides the default from Polymer.Templatizer
stamp: function(model) {
model = model || {};
if (this._parentProps) {
var templatized = this._templatized;
for (var prop in this._parentProps) {
if (model[prop] === undefined) {
model[prop] = templatized[this._parentPropPrefix + prop];
// this is a fix for missing initial bindings on subsequent stamps
// https://github.com/Polymer/polymer/issues/3755
if (model[prop] === undefined) {
model[prop] = this._getBinding(prop, this.dataHost);
}
}
}
}
return new this.ctor(model, this);
},
_getBinding: function(prop, dataHost) {
var data = dataHost && dataHost.__data__;
if (!data) {
return undefined;
}
if (prop in data) {
return data[prop];
}
return this._getBinding(prop, dataHost.dataHost);
},
_expandedChanged: function(instance, expanded) {
// store original expanded value to detect when value change has
// originated from within the template.
this._expanded = expanded;
instance.expanded = expanded;
},
_indexChanged: function(instance, index) {
instance.index = index;
},
_itemChanged: function(instance, item) {
instance.item = item;
},
_itemPathChanged: function(instance, item) {
// TODO: hack to avoid: https://github.com/Polymer/polymer/issues/3307
this._parentProps = this._parentProps || {};
instance.notifyPath(item.path, item.value);
},
_forwardParentProp: function(prop, value) {
// TODO: Bug in Polymer, seems to two-way bind any parent property only
// to the last template instance created. We need to push the property
// to all instances manually.
this._instances.forEach(function(inst) {
inst[prop] = value;
});
},
_forwardParentPath: function(path, value) {
// TODO: Bug in Polymer, seems to two-way bind any parent property only
// to the last template instance created. We need to push the property
// to all instances manually.
this._instances.forEach(function(inst) {
inst._notifyPath(path, value, true);
});
},
_forwardInstanceProp: function(inst, prop, value) {
if (prop === 'expanded' && inst.item && this._expanded !== value && !this.table._isDetailDisabled(inst.item)) {
if (value) {
this.table.expandItem(inst.item);
} else {
this.table.collapseItem(inst.item);
}
}
},
_forwardInstancePath: function(inst, path, value) {
if (path.indexOf('item') === 0) {
// instance.notifyPath above will call _forwardInstancePath recursively,
// so need to debounce to avoid firing the same event multiple times.
this.table.debounce('item-changed', function() {
// stripping 'item.' from path.
this.table.fire('item-changed', {
item: inst.item,
path: path.substring(5),
value: value
});
}.bind(this));
}
}
};
/**
* @polymerBehavior
*/
Brainy.TableTemplatizerBehavior = [
Polymer.Templatizer,
Brainy.TableTemplatizerBehaviorImpl
];
</script>