-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlens-data-sort.html
144 lines (112 loc) · 3.63 KB
/
lens-data-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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../core-selector/core-selector.html">
<link rel="import" href="../lens-u-data-utility/lens-u-data-utility.html">
<!--
A Thelma component for sorting data by a column's values. Can sort both text and numbers in ascending or descending order.
##### Example
<lens-data-sort></lens-data-sort>
@element lens-data-sort
@blurb Sort data by the values of a column.
@status alpha
@homepage http://lenses.github.io/lens-data-sort
-->
<polymer-element name="lens-data-sort" attributes="input output sortColumn sortType">
<template>
<link rel="stylesheet" href="lens-data-sort.css">
<lens-u-data-utility id="data_utility"></lens-u-data-utility>
<div class="lens-container lens-data">
<p class="info">Sort rows.</p>
<label for="func_selector">Sort by column:</label>
<core-selector id="value_selector" selected="{{sortColumn}}" valueattr="label">
<template repeat="{{attr in _dataAttributes}}">
<div class="col" label="{{attr}}">{{attr}}</div>
</template>
</core-selector>
<label for="sort_selector">Sort order:</label>
<core-selector id="func_selector" id="" selected="{{sortType}}" valueattr="label">
<div class="col" label="asc">Ascending</div>
<div class="col" label="desc">Descending</div>
</core-selector>
</div>
</template>
<script src="../lodash/lodash.js"></script>
<script>
Polymer({
/**
* Input data
*
* @property input
* @type object
* @default undefined
*/
/**
* Output data
*
* @property output
* @type object
* @default undefined
*/
/**
* sortType is either 'asc' (ascending) or 'desc' (descending).
*
* @property sortType
* @type String
* @default 'asc'
*/
sortType: 'asc',
/**
* sortColumn is the name of the column that will be used to sort the array of data.
*
* @property sortColumn
* @type String
*/
ready: function () {
//open the collapse if it the component is included
if(this.included && !this.$.ctrl_collapse.opened) {
this.$.ctrl_collapse.toggle();
}
},
inputChanged: function() {
var self = this;
var firstItem = self.input[0];
if(!firstItem) {
return;
}
var attributes = Object.keys(firstItem);
if(JSON.stringify(self._dataAttributes) === JSON.stringify(attributes) ) {
//self._mapChartData();
}
else {
self._dataAttributes = attributes;
}
self._calculateOutput();
},
_calculateOutput: function() {
var self = this;
if(self.sortColumn)
{
self.output = _.sortBy(self.input, function(item) {
var value = item[self.sortColumn];
var isDate = self.$.data_utility.parseDate(value);
if(isDate) {
return isDate;
}
var numeric = self.$.data_utility.unformatStringIfNumeric(value);
return numeric;
});
if(self.sortType==='desc') {
self.output.reverse();
}
}
},
sortColumnChanged: function(e) {
var self= this;
self._calculateOutput();
},
sortTypeChanged: function(e) {
var self= this;
self._calculateOutput();
},
});
</script>
</polymer-element>