-
Notifications
You must be signed in to change notification settings - Fork 20
/
px-data-grid-sorter.html
174 lines (148 loc) · 5.16 KB
/
px-data-grid-sorter.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
<!--
Copyright (c) 2018, General Electric
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<link rel="import" href="../polymer/polymer-element.html"/>
<link rel="import" href="px-data-grid-theme.html">
<link rel="import" href="../px-icon-set/px-icon.html">
<link rel="import" href="css/px-data-grid-sorter-styles.html">
<dom-module id="px-data-grid-sorter">
<template>
<style include="px-data-grid-sorter-styles"></style>
<div part="content">
<slot></slot>
</div>
<div part="indicators"><px-icon id="order-icon" class="order-icon" icon="[[_getDirectionIcon(direction)]]" hidden="[[!_isOrdered(_order, direction)]]"></px-icon>[[_getDisplayOrder(_order)]]</div>
</template>
<script>
{
/**
*
*/
class GridSorterElement extends Vaadin.ThemableMixin(Polymer.Element) {
static get is() {
return 'px-data-grid-sorter';
}
static get properties() {
return {
/**
* JS Path of the property in the item used for sorting the data.
*/
path: String,
/**
* How to sort the data.
* Possible values are `asc` to use an ascending algorithm, `desc` to sort the data in
* descending direction, or `null` for not sorting the data.
*/
direction: {
type: String,
reflectToAttribute: true,
notify: true,
value: null
},
_order: {
type: Number,
value: null
},
_isConnected: {
type: Boolean,
value: false
}
};
}
static get observers() {
return [
'_pathOrDirectionChanged(path, direction, _isConnected)',
'_directionOrOrderChanged(direction, _order)'
];
}
ready() {
super.ready();
this.addEventListener('click', this._onClick.bind(this));
}
connectedCallback() {
super.connectedCallback();
this._isConnected = true;
}
disconnectedCallback() {
super.disconnectedCallback();
this._isConnected = false;
}
_pathOrDirectionChanged(path, direction, isConnected) {
if (path === undefined || direction === undefined || isConnected === undefined) {
return;
}
if (isConnected) {
// Fire custom event that is handled differently than default sorter-changed
this.dispatchEvent(new CustomEvent('px-sorter-changed', {
bubbles: true,
composed: true,
detail: {
sorter: this
}
}));
}
}
_getDisplayOrder(order) {
return order === null ? '' : order + 1;
}
_getDirectionIcon(direction) {
if (direction === 'desc') {
return 'px-nav:down';
} else if (direction === 'asc') {
return 'px-nav:up';
} else {
return 'px-nav:unconfirmed';
}
}
_isOrdered() {
return this.direction !== null;
}
_onClick(e) {
const activeElement = this.getRootNode().activeElement;
if (this !== activeElement && this.contains(activeElement)) {
// Some focusable content inside the sorter was clicked, do nothing.
return;
}
e.preventDefault();
e.stopPropagation();
if (this.direction === 'asc') {
this.direction = 'desc';
} else if (this.direction === 'desc') {
this.direction = null;
} else {
this.direction = 'asc';
}
}
_directionOrOrderChanged(direction, order) {
if (direction === undefined || order === undefined) {
return;
}
// Safari has an issue with repainting shadow root element styles when a host attribute changes.
// Need this workaround (toggle any inline css property on and off) until the issue gets fixed.
var isSafari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
if (isSafari && this.root) {
this.root.querySelectorAll('*').forEach(function(el) {
el.style['-webkit-backface-visibility'] = 'visible';
el.style['-webkit-backface-visibility'] = '';
});
}
}
}
customElements.define(GridSorterElement.is, GridSorterElement);
/**
* @namespace Predix
*/
window.Predix = window.Predix || {};
Predix.GridSorterElement = GridSorterElement;
}
</script>
</dom-module>