Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(178)

Side by Side Diff: chrome/browser/resources/engagement/engagement_table.js

Issue 1407363010: Make table in chrome:site-engagement sortable. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: address comments Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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]*:\/\//, '');
Dan Beam 2015/10/29 00:50:00 nit: why not use URL? that way, if HTTP was used
calamity 2015/10/29 02:46:01 Oh cool. These are only ever origins anyway, so I
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.
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);
Dan Beam 2015/10/29 00:50:00 don't use extra parens, especially around return
calamity 2015/10/29 02:46:01 Done.
96
97 if (sortKey == 'score')
98 return val1 - val2;
99
100 console.log('Unsupported sort key:' + sortKey);
Dan Beam 2015/10/29 00:50:00 should this be assertNotReached()?
calamity 2015/10/29 02:46:01 Done.
101 return 0;
102 },
20 }); 103 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698