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

Side by Side Diff: appengine/swarming/elements/res/imp/common/dynamic-table.html

Issue 2269643002: Extract shared filters and aliasing code (Closed) Base URL: https://chromium.googlesource.com/external/github.com/luci/luci-py@master
Patch Set: Address nit Created 4 years, 4 months 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
(Empty)
1 <!--
2 Copyright 2016 The LUCI Authors. All rights reserved.
3 Use of this source code is governed under the Apache License, Version 2.0
4 that can be found in the LICENSE file.
5
6 This file contains most of the logic needed to create a dynamic table. It is b roken up into two
7 parts, a style dom-module called "dynamic-table-style" and a behavior called
8 SwarmingBehaviors.DynamicTableBehavior. This behavior ties together filtering, sorting and column
9 content. It also offers a few utilities to make creating the table easier. A c lient of these two
10 parts needs to create the templates to actually draw the <table>,<tr> and so o n. See
11 bot-list.html and task-list.html for examples.
12
13 A client should use the provided style set as follows:
14
15 <link rel="import" href="/res/imp/common/dynamic-table.html">
16 ...
17 <template>
18 <style include="dynamic-table-style">
19 ...
20
21 This behavior has already defined the following properties, which a client sho uld bind to:
22 _columns, Array<String>, The columns that should be shown.
23 _items, Array<Object>, Those elements that may be displayed and/or sorted, d epending on the
24 settings.
25 _filter, Function, Given an element from _items, return a boolean if the ite m should be shown.
26 This function will be bound to this element.
27 _sortstr, String, A String representation of the current state of sorting li ke
28 [name]:["asc", "desc"].
29 _verbose, Boolean, If the verbose contents of the table should be shown.
30
31 A client must define the following properties:
32 _columnMap: Object, a mapping of column name to a function that will return the content for a
33 given bot. These functions are bound to this element. If a column is not listed here, a sane
34 default will be used (see _column()).
35 _headerMap: Object, a mapping of column name to the displayed text for a col umn.
36 _specialColumns, Array<String> A list of "special" column names, that is, co lumns which will
37 have html in them, provided by the client. non-special (i.e. plain colun ns) just contain
38 text and will have their content provided by _attribute (see below).
39 _specialSort, Object, A mapping of column name to a function that implements custom sorting
40 rules. The function will be given (dir, a, b) and is expected to return an int, as a normal
41 sort comparison function would. Otherwise, natural comparison of a and b is used
42 (see _compare()).
43
44 A client must define the following methods:
45 _attribute(i, col, default): Given the item i, return an array of values for the column "col",
46 or an array containting just the default, if not. This is only used as a default when a
47 column does not appear in _columnMap.
48
49 This behavior provides the following properties:
50 _filteredSortedItems, Array<Object>, The list of items that should shown, af ter filtering and
51 sorting.
52 _plainColumns, Array<String>, the list of columns with any special columns s tripped out.
53
54 This behavior provides the following methods:
55 _column(col, item): Return the text content of item for a column.
56 _header(col): Return the header for a column, defaulting to the column name.
57 _hide(col): Return a boolean based on whether to hide this column.
58 _sortChange(event): Update the sorting based on an event created by sort-tog gle.
59 -->
60
61 <dom-module id="dynamic-table-style">
62 <template>
63 <style>
64 table {
65 border-collapse: collapse;
66 margin-left: 5px;
67 }
68 td, th {
69 border: 1px solid #DDD;
70 padding: 5px;
71 }
72 th {
73 position: relative;
74 }
75 sort-toggle {
76 position: absolute;
77 right: 0;
78 top: 0.4em;
79 }
80 </style>
81
82 </template>
83 </dom-module>
84
85 <script>
86 window.SwarmingBehaviors = window.SwarmingBehaviors || {};
87 (function(){
88 // This behavior wraps up all the shared swarming functionality.
89 SwarmingBehaviors.DynamicTableBehavior = {
90
91 properties: {
92
93 _columns: {
94 type: Array,
95 },
96
97 _filter: {
98 type: Function,
99 },
100
101 _filteredSortedItems: {
102 type: Array,
103 computed: "_filterAndSort(_items,_filter.*,_sort.*)"
104 },
105
106 _items: {
107 type: Array,
108 },
109
110 _plainColumns: {
111 type: Array,
112 computed: "_stripSpecial(_columns.*)",
113 },
114
115 // _sort is an Object {name:String, direction:String}.
116 _sort: {
117 type: Object,
118 computed: "_makeSortObject(_sortstr)",
119 },
120
121 _sortstr: {
122 type: String,
123 },
124
125 _verbose: {
126 type: Boolean,
127 }
128 },
129
130 _column: function(col, key) {
131 var f = this._columnMap[col];
132 if (!f) {
133 f = function(key) {
134 var c = this._attribute(key, col, "none");
135 if (this._verbose) {
136 return c.join(" | ");
137 }
138 return c[0];
139 }
140 }
141 return f.bind(this)(key);
142 },
143
144 _compare: function(a, b) {
145 if (!this._sort) {
146 return 0;
147 }
148 var dir = 1;
149 if (this._sort.direction === "desc") {
150 dir = -1;
151 }
152 var sort = this._specialSort[this._sort.name];
153 if (sort) {
154 return sort.bind(this)(dir, a, b);
155 }
156 // Default to a natural compare of the columns.
157 var aCol = this._column(this._sort.name, a);
158 var bCol = this._column(this._sort.name, b);
159
160 return dir * swarming.naturalCompare(aCol, bCol);
161 },
162
163 _filterAndSort: function() {
164 // We intentionally sort this._items (and not a copy) to allow users to
165 // "chain" sorts, that is, sort by one thing and then another, and
166 // have both orderings properly impact the list.
167 swarming.stableSort(this._items, this._compare.bind(this));
168 var items = this._items;
169 if (this._filter) {
170 items = items.filter(this._filter.bind(this));
171 }
172
173 return items;
174 },
175
176 _header: function(col){
177 return this._headerMap[col] || col;
178 },
179
180 _hide: function(col) {
181 return this._columns.indexOf(col) === -1;
182 },
183
184 _makeSortObject: function(sortstr){
185 if (!sortstr) {
186 return undefined;
187 }
188 var pieces = sortstr.split(":");
189 if (pieces.length != 2) {
190 // fail safe
191 return {name: "id", direction: "asc"};
192 }
193 return {
194 name: pieces[0],
195 direction: pieces[1],
196 }
197 },
198
199 _sortChange: function(e) {
200 // The event we get from sort-toggle tells us the name of what needs
201 // to be sorting and how to sort it.
202 if (!(e && e.detail && e.detail.name)) {
203 return;
204 }
205 // should trigger the computation of _sort and __filterAndSort
206 this.set("_sortstr", e.detail.name + ":" + e.detail.direction);
207 },
208 // _stripSpecial removes the special columns and sorts the remaining
209 // columns so they always appear in the same order, regardless of
210 // the order they are added.
211 _stripSpecial: function(){
212 return this._columns.filter(function(c) {
213 return this._specialColumns.indexOf(c) === -1;
214 }.bind(this)).sort();
215 },
216
217 };
218 })();
219 </script>
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698