forked from Saulis/iron-data-table
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data-table-column-sort.html
120 lines (102 loc) · 2.67 KB
/
data-table-column-sort.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
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../paper-icon-button/paper-icon-button.html">
<link rel="import" href="data-table-icons.html">
<dom-module id="data-table-column-sort">
<template>
<style>
:host {
display: block;
margin: 4px;
}
:host([hidden]) {
display: none;
}
paper-icon-button {
position: relative;
opacity: .84;
transition: all .2s;
}
paper-icon-button:hover,
paper-icon-button[focused] {
color: var(--default-primary-color);
}
paper-icon-button:not([direction]) {
opacity: .16;
}
paper-icon-button[direction='desc'] {
transform: rotate(-180deg);
}
paper-icon-button[hidden] {
display: none;
}
.order {
font-size: 9px;
font-weight: bold;
position: absolute;
right: 4px;
bottom: 8px;
}
</style>
<div style="position: relative">
<paper-icon-button id="sortIcon" on-tap="_sort" icon="data-table:arrow-upward" direction$="[[direction]]">
</paper-icon-button>
<div class="order">[[order]]</div>
</div>
</template>
<script>
Polymer({
is: 'data-table-column-sort',
properties: {
direction: {
type: String,
notify: true
},
path: String,
order: {
type: Number,
computed: '_order(path, sortOrder, sortOrder.length)'
},
sortOrder: Array
},
observers: ['_sortOrderChanged(sortOrder.*)'],
_order: function(path, sortOrder, length) {
if (length <= 1) {
return '';
}
for (var i = 0; i < length; i++) {
if (sortOrder[i].path === path) {
return i + 1;
}
}
},
_sortOrderChanged: function(sortOrder) {
// TODO: if sortOrder for this column has been removed from outside, direction is not updated.
if (sortOrder.base) {
sortOrder.base.forEach(function(sort) {
if (sort.path === this.path) {
this.direction = sort.direction;
return;
}
}.bind(this));
}
},
_sort: function() {
switch (this.direction) {
case 'asc':
this.direction = 'desc';
break;
case 'desc':
this.direction = null;
break;
default:
this.direction = 'asc';
break;
}
this.fire('sort-direction-changed', {
path: this.path,
direction: this.direction
});
}
});
</script>
</dom-module>