forked from Saulis/iron-data-table
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata-table-cell.html
158 lines (131 loc) · 4.5 KB
/
data-table-cell.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
<dom-module id="data-table-cell">
<template>
<style is="custom-style">
:host {
flex: 1 0 100px;
padding: 0 24px 0 24px;
min-height: 10px; /* Prevent iron-list from looping when item height is really small */
height: 48px;
display: flex;
align-items: center;
overflow: hidden;
transition: flex-basis 200ms, flex-grow 200ms;
}
:host([header]) {
height: 56px;
}
:host([hidden]) {
display: none;
}
</style>
<content></content>
</template>
<script>
Polymer({
is: 'data-table-cell',
properties: {
bind: Object,
order: Number,
header: Boolean,
width: String,
flex: Number,
column: {
type: Object,
observer: '_columnChanged'
},
beforeBind: {
type: Object,
value: function() {
return function(data, cell) {};
}
},
_instance: {
type: Object,
computed: '_templatize(column, _templatizer)'
},
_templatizer: Object
},
observers: ['_bindData(_instance, bind)'],
attached: function() {
if (!Polymer.Settings.useNativeShadow) {
// cell is supposed to be placed outside the local dom of iron-data-table.
Polymer.StyleTransformer.dom(this, 'iron-data-table', this._scopeCssViaAttr, true);
if (this.domHost) {
Polymer.StyleTransformer.dom(this, this.domHost.tagName.toLowerCase(), this._scopeCssViaAttr, false);
}
}
// in some cases the _columnChanged observer is run before the cell has
// been turned into a html element and the inline styles don't get applied.
// Applying them after attaching fixes makes sure they're applied.
this._setColumnStyleProperties(this.column);
// TODO: for some reason this needs to be done async to work with Shadow
// DOM. Left the sync call there also to prevent flickering with Shady DOM.
this.async(function() {
this._setColumnStyleProperties(this.column);
});
},
_setColumnStyleProperties: function(column) {
this.style.flexGrow = column.flex;
this.style.flexDirection = column.alignRight ? 'row-reverse' : 'row';
this.style.flexBasis = column.width;
this.style.flexGrow = column.flex;
},
_orderChanged: function(e) {
this.style.order = e.detail.value;
},
_widthChanged: function(e) {
this.style.flexBasis = e.detail.value;
},
_flexChanged: function(e) {
this.style.flexGrow = e.detail.value;
},
_hiddenChanged: function(e) {
this.hidden = e.detail.value;
},
_templatizeRowChanged: function(e) {
if (!this.header) {
this._templatizer = e.detail.value;
}
},
_templatizeHeaderChanged: function(e) {
if (this.header) {
this._templatizer = e.detail.value;
}
},
_templatize: function(column, templatizer) {
return templatizer.bind(column)(this);
},
_bindData: function(instance, data) {
if (!this.header) {
this.beforeBind(data, this);
}
instance.bind(data);
},
_columnChanged: function(column, oldColumn) {
this._setColumnStyleProperties(column);
this._addListener(column, oldColumn, 'width');
this._addListener(column, oldColumn, 'flex');
this._addListener(column, oldColumn, 'order');
this.hidden = column.hidden;
this._addListener(column, oldColumn, 'hidden');
if (this.header && column.templatizeHeader) {
this._templatizer = column.templatizeHeader;
} else if (column.templatizeRow) {
this._templatizer = column.templatizeRow;
}
this._addListener(column, oldColumn, 'templatizeRow');
this._addListener(column, oldColumn, 'templatizeHeader');
},
// Adding listeners manually *seems* to work better with forwarded parent props
// than normal data-binding. Something to do with notifying the paths correctly.
_addListener: function(column, oldColumn, prop) {
var eventName = Polymer.CaseMap.camelToDashCase(prop) + '-changed';
var observerName = '_' + prop + 'Changed';
if (oldColumn) {
this.unlisten(oldColumn, eventName);
}
this.listen(column, eventName, observerName);
}
});
</script>
</dom-module>