Chromium Code Reviews| 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 the URL. | |
| 9 * @param {string} url | |
| 10 */ | |
| 11 function stripScheme(url) { | |
| 12 return url.replace(/^[a-z]*:\/\//, ''); | |
| 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, | |
| 16 value: () => [] | |
| 17 }, | |
| 18 }, | 23 }, |
| 19 | 24 |
| 25 /** | |
| 26 * The table's current sort key. | |
| 27 * @type {string} | |
| 28 */ | |
| 29 sortKey_: null, | |
| 30 | |
| 31 /** | |
| 32 * Whether the table is in reverse sorting order. | |
| 33 * @type {boolean} | |
| 34 */ | |
| 35 sortReverse_: false, | |
| 36 | |
| 37 ready: function() { this.sortTable_(this.$$('#initial-sort-column')); }, | |
|
tsergeant
2015/10/27 23:35:34
Use `this.$['initial-sort-column']` for selecting
calamity
2015/10/28 04:26:50
Done.
| |
| 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 */ | |
| 53 sortTable_: function(th) { | |
| 54 this.removeSortIndicator_(); | |
| 55 var newSortKey = th.getAttribute('sort-key'); | |
| 56 this.sortReverse_ = (newSortKey == this.sortKey_) && !this.sortReverse_; | |
| 57 this.sortKey_ = newSortKey; | |
| 58 | |
| 59 this.$['engagement-table-items'].sort = | |
| 60 this.getTableSortFunction_(newSortKey, this.sortReverse_); | |
| 61 | |
| 62 this.addSortIndicator_(th, this.sortReverse_); | |
| 63 }, | |
| 64 | |
| 65 /** | |
| 66 * Returns a sorting function for SiteEngagementInfo objects that sorts based | |
| 67 * on sortKey. | |
| 68 * @param {string} sortKey The name of the property to sort by. | |
| 69 * @param {boolean} reverse Whether the sort should be in reverse order. | |
| 70 */ | |
| 71 getTableSortFunction_: function(sortKey, reverse) { | |
|
tsergeant
2015/10/27 23:35:34
If you make sortKey_ and sortReverse_ into propert
calamity
2015/10/28 04:26:50
Done.
| |
| 72 return function(a, b) { | |
| 73 return (reverse ? -1 : 1) * | |
| 74 this.compareTableItem_(sortKey, a, b); | |
| 75 }.bind(this); | |
| 76 }, | |
| 77 | |
| 78 /** | |
| 79 * Compares two SiteEngagementInfo objects based on the sortKey. | |
| 80 * @param {string} sortKey The name of the property to sort by. | |
| 81 */ | |
| 82 compareTableItem_: function(sortKey, a, b) { | |
| 83 var val1 = a[sortKey]; | |
| 84 var val2 = b[sortKey]; | |
| 85 | |
| 86 if (sortKey == 'origin') | |
| 87 return (stripScheme(val1) > stripScheme(val2) ? 1 : -1); | |
| 88 | |
| 89 if (sortKey == 'score') | |
| 90 return val1 - val2; | |
| 91 | |
| 92 console.log('Unsupported sort key:' + sortKey); | |
| 93 return 0; | |
| 94 }, | |
| 95 | |
| 96 /** | |
| 97 * Adds and indicator to the given table header that indicates the sorting | |
| 98 * direction. | |
| 99 * @param {HTMLElement} th The table header element to add the indicator to. | |
| 100 * @param {boolean} reverse Whether the sort is in reverse order. | |
| 101 */ | |
| 102 addSortIndicator_: function(th, reverse) { | |
| 103 var indicator = document.createElement('span'); | |
| 104 indicator.id = 'sort-indicator'; | |
| 105 indicator.textContent = reverse ? '\u25BC' : '\u25B2'; | |
| 106 th.appendChild(indicator); | |
| 107 }, | |
| 108 | |
| 109 /** | |
| 110 * Removes the sort arrow span if it exists. | |
| 111 */ | |
| 112 removeSortIndicator_: function() { | |
| 113 var indicator = this.$$('#sort-indicator'); | |
| 114 if (indicator) | |
| 115 indicator.parentNode.removeChild(indicator); | |
| 116 }, | |
| 20 }); | 117 }); |
| OLD | NEW |