OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 'use strict'; | 5 'use strict'; |
6 | 6 |
7 /** | |
8 * Strips a scheme from an origin. | |
9 * @param {string} origin | |
10 */ | |
Dan Beam
2015/10/29 22:03:16
@return
calamity
2015/10/30 03:56:27
Done.
| |
11 function stripScheme(origin) { | |
12 return new URL(origin).host; | |
13 } | |
14 | |
7 Polymer({ | 15 Polymer({ |
8 is: 'engagement-table', | 16 is: 'engagement-table', |
9 properties: { | 17 properties: { |
10 /** | 18 /** |
11 * A list of engagement info objects. | 19 * A list of engagement info objects. |
12 * @type !Array<!SiteEngagementInfo> | 20 * @type !Array<!SiteEngagementInfo> |
13 */ | 21 */ |
14 engagementInfo: { | 22 engagementInfo: {type: Array, value: () => []}, |
15 type: Array, | 23 |
16 value: () => [] | 24 /** |
17 }, | 25 * The table's current sort key. |
26 * @type {string} | |
27 * @private | |
28 */ | |
29 sortKey_: {type: String, value: 'score'}, | |
30 | |
31 /** | |
32 * Whether the table is in reverse sorting order. | |
33 * @type {boolean} | |
34 * @private | |
35 */ | |
36 sortReverse: {type: Boolean, value: true, reflectToAttribute: true}, | |
18 }, | 37 }, |
19 | 38 |
39 /** | |
40 * @param {Event} e | |
41 */ | |
42 handleSortColumnTap: function(e) { | |
43 this.sortTable_(e.currentTarget); | |
44 e.preventDefault(); | |
45 }, | |
46 | |
47 /** | |
48 * Sorts the engagement table based on the provided sort column header. Sort | |
49 * columns have a 'sort-key' attribute that is a property name of a | |
50 * SiteEngagementInfo. | |
51 * @param {HTMLElement} th The column to sort by. | |
52 * @private | |
53 */ | |
54 sortTable_: function(th) { | |
55 // Remove the old sort indicator column. | |
56 var sortColumn = this.$$('.sort-column'); | |
57 if (sortColumn) | |
58 sortColumn.className = ''; | |
59 | |
60 // Updating these properties recomputes the template sort function. | |
61 var newSortKey = th.getAttribute('sort-key'); | |
62 if (this.sortKey_) | |
63 this.sortReverse = (newSortKey == this.sortKey_) && !this.sortReverse; | |
64 | |
65 this.sortKey_ = newSortKey; | |
66 | |
67 // Specify the new sort indicator column. | |
68 th.className = 'sort-column'; | |
69 }, | |
70 | |
71 /** | |
72 * Returns a sorting function for SiteEngagementInfo objects that sorts based | |
73 * on sortKey. | |
74 * @param {string} sortKey The name of the property to sort by. | |
75 * @param {boolean} reverse Whether the sort should be in reverse order. | |
76 * @private | |
77 */ | |
78 getTableSortFunction_: function(sortKey, reverse) { | |
79 return function(a, b) { | |
80 return (reverse ? -1 : 1) * | |
81 this.compareTableItem_(sortKey, a, b); | |
82 }.bind(this); | |
83 }, | |
84 | |
85 /** | |
86 * Compares two SiteEngagementInfo objects based on the sortKey. | |
87 * @param {string} sortKey The name of the property to sort by. | |
Dan Beam
2015/10/29 22:03:16
@return
calamity
2015/10/30 03:56:27
Done.
| |
88 * @private | |
89 */ | |
90 compareTableItem_: function(sortKey, a, b) { | |
91 var val1 = a[sortKey]; | |
92 var val2 = b[sortKey]; | |
93 | |
94 if (sortKey == 'origin') | |
95 return stripScheme(val1) > stripScheme(val2) ? 1 : -1; | |
96 | |
97 if (sortKey == 'score') | |
98 return val1 - val2; | |
99 | |
100 assertNotReached('Unsupported sort key: ' + sortKey); | |
101 return 0; | |
102 }, | |
20 }); | 103 }); |
OLD | NEW |