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, | 23 |
| 16 value: () => [] | 24 /** |
| 17 }, | 25 * The table's current sort key. |
| 26 * @type {string} | |
| 27 * @private | |
| 28 */ | |
| 29 sortKey_: {type: String}, | |
|
tsergeant
2015/10/28 04:57:03
As discussed, maybe set the default value here
calamity
2015/10/28 05:43:11
Done.
| |
| 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 ready: function() { this.sortTable_(this.$['initial-sort-column']); }, | |
| 40 | |
| 41 /** | |
| 42 * @param {Event} e | |
| 43 */ | |
| 44 handleSortColumnTap: function(e) { | |
| 45 this.sortTable_(e.currentTarget); | |
| 46 e.preventDefault(); | |
| 47 }, | |
| 48 | |
| 49 /** | |
| 50 * Sorts the engagement table based on the provided sort column header. Sort | |
| 51 * columns have a 'sort-key' attribute that is a property name of a | |
| 52 * SiteEngagementInfo. | |
| 53 * @param {HTMLElement} th The column to sort by. | |
| 54 * @private | |
| 55 */ | |
| 56 sortTable_: function(th) { | |
| 57 // Remove the old sort indicator column. | |
| 58 var sortColumn = this.$$('.sort-column'); | |
| 59 if (sortColumn) | |
| 60 sortColumn.className = ''; | |
| 61 | |
| 62 // Updating these properties recomputes the template sort function. | |
| 63 var newSortKey = th.getAttribute('sort-key'); | |
| 64 if (this.sortKey_) | |
| 65 this.sortReverse_ = (newSortKey == this.sortKey_) && !this.sortReverse_; | |
| 66 | |
| 67 this.sortKey_ = newSortKey; | |
| 68 | |
| 69 // Specify the new sort indicator column. | |
| 70 th.className = 'sort-column'; | |
| 71 }, | |
| 72 | |
| 73 /** | |
| 74 * Returns a sorting function for SiteEngagementInfo objects that sorts based | |
| 75 * on sortKey. | |
| 76 * @param {string} sortKey The name of the property to sort by. | |
| 77 * @param {boolean} reverse Whether the sort should be in reverse order. | |
| 78 * @private | |
| 79 */ | |
| 80 getTableSortFunction_: function(sortKey, reverse) { | |
| 81 return function(a, b) { | |
| 82 return (reverse ? -1 : 1) * | |
| 83 this.compareTableItem_(sortKey, a, b); | |
| 84 }.bind(this); | |
| 85 }, | |
| 86 | |
| 87 /** | |
| 88 * Compares two SiteEngagementInfo objects based on the sortKey. | |
| 89 * @param {string} sortKey The name of the property to sort by. | |
| 90 * @private | |
| 91 */ | |
| 92 compareTableItem_: function(sortKey, a, b) { | |
| 93 var val1 = a[sortKey]; | |
| 94 var val2 = b[sortKey]; | |
| 95 | |
| 96 if (sortKey == 'origin') | |
| 97 return (stripScheme(val1) > stripScheme(val2) ? 1 : -1); | |
| 98 | |
| 99 if (sortKey == 'score') | |
| 100 return val1 - val2; | |
| 101 | |
| 102 console.log('Unsupported sort key:' + sortKey); | |
| 103 return 0; | |
| 104 }, | |
| 20 }); | 105 }); |
| OLD | NEW |