-
Notifications
You must be signed in to change notification settings - Fork 92
/
paper-datatable-column.html
444 lines (415 loc) · 13.3 KB
/
paper-datatable-column.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
<script src="../whenever.js/whenever.js"></script>
<dom-module id="paper-datatable-column">
<template>
<style>
:host {
display: block;
}
</style>
<template id="arrayTemplate">
<template is="dom-repeat" items="{{value}}" as="arrayItem">
<div class="array-item">{{_pdt_getArrayItemLabel(column, arrayItem)}}</div>
</template>
</template>
<template id="editableInputTemplate">
<paper-input type="[[_pdt_getCorrectTypeForInput(column.type)]]" value="{{value}}" step="any" no-label-float></paper-input>
</template>
<template id="editableBooleanTemplate">
<paper-checkbox checked="{{value}}"></paper-checkbox>
</template>
</template>
<script>
(function () {
'use strict';
/*
This element is used inside of `<paper-datatable>` to declare the columns to be shown in the datatable.
@element paper-datatable-column
*/
Polymer({
is: 'paper-datatable-column',
properties: {
/**
* String displayed in `<th></th>`
*
* @attribute header
* @type String
* @required
*/
header: String,
/**
* The property to be used from `data` for this column
*
* @attribute property
* @type String
* @required
*/
property: String,
/**
* The type of the property (can be 'String', 'Number', 'Array', 'Date', etc.)
* Used for sorting and default display
*
* @attribute type
* @type String
* @default 'String'
*/
type: {
type: String,
value: 'String'
},
/**
* Tooltip shown when hovering the `<th>`
*
* @attribute tooltip
* @type String
*/
tooltip: {
type: String,
notify: true
},
/**
* Whether to show sorting UI on the column. Sorting only works automagically
* on simple data types.
*
* @attribute sortable
* @type Boolean
* @default false
*/
sortable: Boolean,
/**
* Whether the column is currently sorted. Should only be set on one column at a time
*
* @attribute sorted
* @type Boolean
* @default false
*/
sorted: {
type: Boolean,
notify: true,
observer: '_sortChanged',
reflectToAttribute: true
},
/**
* Whether the column is currently sorted. Should only be set on one column at a time
*
* @attribute sortDirection
* @type String
* @default 'asc'
*/
sortDirection: {
type: String,
value: 'asc',
observer: '_sortChanged'
},
/**
* If `type` is a simple type, then if this is true one of the default editable
* templates is used. For example if `type` is `String` every cell will contain
* a `<paper-input>`. This is practically just a convenience method so that you
* don't have to define every `<template>` on your own.
*
* If you do use a custom `<template>` this will prevent a `row-tap` from triggering
* a selection. So if you click in a `<paper-input>` it won't select the row.
*
* @attribute editable
* @type Boolean
* @default false
*/
editable: Boolean,
/**
* If `type` is an `Array` and the array consists of `Object`s it's a common need
* to display a single property of every object (in non-editable mode).
*
* Note that it's not possible to use this in combination with an editable dialog,
* as internally this is using a `<template>` and Polymer currently allows only
* one template to be templatized per element.
*
* @attribute arrayDisplayProp
* @type String
*/
arrayDisplayProp: String,
/**
* Style to be applied to every cell.
*
* @attribute cellStyle
* @type String
*/
cellStyle: {
type: String,
value: ''
},
/**
* Convenience attribute to align the header and cell content (e.g. 'center')
*
* @attribute align
* @type String
* @default 'left'
*/
align: {
type: String,
value: 'left'
},
/**
* Style to be applied to the header.
*
* @attribute style
* @type String
*/
_styleString: {
type: String,
value: function(){
var alignment = this.align || this.getAttribute('align') || 'left';
var minWidth = this.width || this.getAttribute('width') || 0;
minWidth += parseFloat(minWidth).toString() === minWidth ? 'px' : '';
var styleString = this.getAttribute('style') || '';
return 'text-align:'+alignment+';min-width:'+minWidth+';'+styleString;
}
},
/**
* If you have `undefined`'s in your `data` this method can be used to
* set a default, thus preventing auto-saves from triggering.
*
* @attribute default
* @type Object
*/
default: Object,
/**
* Can be overwritten to manually format the value in a non-editable state
*
* IMPORTANT: This is a property, not a method you should call directly.
*
* @attribute formatValue
* @type Function
* @default see code
*/
formatValue: Function,
/**
* Can be overwritten to manually sort the datatable if internal sorting is used. This is a normal
* function following the Javascript sort API.
*
* IMPORTANT: This is a property, not a method you should call directly.
*
* @attribute sort
* @type Function
* @default see code
*/
sort: Function,
/**
* Instead of rendering the `<template>` inline in the cell it will instead be rendered in a
* floating `<paper-datatable-edit-dialog>` after the user `tap`s on the cell.
*
* @attribute dialog
* @type Boolean
* @default false
*/
dialog: {
type: Boolean
},
/**
* Per material design spec it's often useful to indicate that a cell is editable if you're using
* `[dialog]` based editing. This will show a little pencil icon to the right of the cell, but can be
* a bit overwhelming if used on more than one column.
*
* @attribute editIcon
* @type Boolean
* @default false
*/
editIcon: {
type: Boolean
},
/**
* Removing and adding columns entirely from the DOM is a bit hard, so instead there is this
* convenience method to entirely disable a column.
*
* @attribute inactive
* @type Boolean
* @default false
*/
inactive: {
type: Boolean,
observer: '_requeryColumnList'
},
/**
* If you're using the `dynamic-columns` resize behavior this attribute is used to determining
* how important the column is when columns have to be hidden due to the window size. Set to -1
* if the column should always be shown. If columns have the same priority they will be assumed to
* be ordered by importance from left to right.
*/
resizePriority: {
type: Number,
value: 0
},
/**
* Mostly just a convenience attribute to set min-width, but required in conjuction with
* the `dynamic-columns` resize behavior
*
* @type Number in px or String
*/
width: Object
},
behaviors: [
Polymer.Templatizer
],
created: function(){
this.beenAttached = new Whenever();
},
ready: function() {
var template = Polymer.dom(this).querySelector('template');
if(!template && this.type.toLowerCase() == 'array'){
var template = this.$.arrayTemplate;
}else if(!template && this.editable && this.type.toLowerCase() == 'string'){
var template = this.$.editableInputTemplate;
}else if(!template && this.editable && this.type.toLowerCase() == 'number'){
var template = this.$.editableInputTemplate;
}else if(!template && this.editable && this.type.toLowerCase() == 'boolean'){
var template = this.$.editableBooleanTemplate;
}
if(template) {
this._instanceProps = {};
this._instanceProps.item = true;
this._instanceProps.value = true;
this._instanceProps.column = true;
this.templatize(template);
this.template = true;
}
},
_createCellInstance: function(model, notificationKey){
if(typeof model[this.property] == 'undefined' && typeof this.default !== 'undefined'){
var instance = this.stamp({item: model, column:this, value: this.default, _dataKey: notificationKey});
}else{
var instance = this.stamp({item: model, column:this, value: model[this.property], _dataKey: notificationKey});
}
return instance;
},
_formatValue: function(data){
data = this._cast(data);
if('formatValue' in this){
return this.formatValue(data);
}
if(typeof data == 'undefined'){
return '';
}
var value = this._cast(data);
if(this.type.toLowerCase() == 'string'){
return value;
}else if(this.type.toLowerCase() == 'number'){
return value;
}else if(this.type.toLowerCase() == 'boolean'){
return value ? 'Yes' : 'No';
}else if(this.type.toLowerCase() == 'date'){
var prependZero = function(val){
return val < 10 ? '0' + val : val;
}
return value.getUTCFullYear() +'/'+ prependZero(value.getUTCMonth()+1) +'/'+ prependZero(value.getUTCDate()) + ' ' + prependZero(value.getUTCHours()) + ':' + prependZero(value.getUTCMinutes()) + ':' + prependZero(value.getUTCSeconds());
}else{
console.warn('Complex objects should implement their own template or format-value function.', data);
return '?';
}
},
_sort: function(valA, valB){
valA = this._cast(valA);
valB = this._cast(valB);
if('sort' in this){
return this.sort(valA, valB);
}
if(valA < valB) return -1;
if(valA > valB) return 1;
return 0;
},
_cast: function(value){
if(typeof value === 'undefined'){
if(typeof this.default !== 'undefined'){
value = JSON.parse(JSON.stringify(this.default));
}else{
value = '';
}
}
if(this.type.toLowerCase() == 'string'){
return value.toString();
}else if(this.type.toLowerCase() == 'number'){
return parseFloat(value);
}else if(this.type.toLowerCase() == 'boolean'){
return value ? true : false;
}else if(this.type.toLowerCase() == 'date'){
return new Date(value);
}else{
return value;
}
},
_sortChanged: function(){
if(this.sorted){
this.beenAttached.whenReady(function(){
this.parentNodeRef.sort(this);
}.bind(this));
}
},
_forwardParentProp: function(prop, value){
console.warn('_forwardParentProp',arguments);
},
_forwardParentPath: function(prop, value){
console.warn('_forwardParentPath',arguments);
},
_forwardInstanceProp: function(templateInstance, prop, value){
var path = prop.split('.');
var item = path.shift();
if(item == 'value'){
var parentPath = ['data', templateInstance.get('_dataKey'), this.property];
value = this._cast(value);
//console.log('_forwardInstanceProp',arguments);
//console.log('set', parentPath, 'to', value);
this.parentNodeRef.set(parentPath, value);
}else if(item == 'item'){
console.warn('_forwardInstanceProp',arguments);
}
},
_forwardInstancePath: function(templateInstance, prop, value){
var path = prop.split('.');
var item = path.shift();
if(item == 'value'){
var parentPath = ['data', templateInstance.get('_dataKey'), this.property].concat(path);
if(path.length == 0){
value = this._cast(value);
}
//console.log(parentPath, path.join('.'), value)
//console.log('_forwardInstancePath',arguments);
}else if(item == 'item'){
var parentPath = ['data', templateInstance.get('_dataKey')].concat(path);
//console.log(parentPath, value);
//console.log('_forwardInstancePath',arguments);
}
//console.log('set', parentPath, 'to', value);
this.parentNodeRef.set(parentPath, value);
},
_requeryColumnList: function(){
//only trigger this if ready, anything before that point will be handled automatically during
// initial initialization.
if(this.beenAttached.state.ready){
this.parentNodeRef._queryAndSetColumns();
}
},
/*
_getRootDataHost: function(){
return this;
},*/
_registerEvilFunctions: function(){
if(typeof this.dataHost === 'undefined' || typeof this.parentNodeRef.dataHost !== 'undefined') {
this.dataHost = this.parentNodeRef.dataHost;
}else if(typeof this.dataHost === 'undefined'){
console.warn('A hack is used to support some functionality of the `[editable]` and [`array-display-prop`] attribute. ' +
'This however requires the element to be located in a polymer element or `dom-bind`.')
}
this.dataHost._pdt_getArrayItemLabel = this._getArrayItemLabel;
this.dataHost._pdt_getCorrectTypeForInput = this._getCorrectTypeForInput;
},
_getArrayItemLabel: function(column, value){
return column.arrayDisplayProp ? value[column.arrayDisplayProp] : value;
},
_getCorrectTypeForInput: function(type){
if(type.toLowerCase() == 'string'){
return 'string';
}else if(type.toLowerCase() == 'number'){
return 'number';
}
}
});
})();
</script>
</dom-module>