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

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

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

Powered by Google App Engine
This is Rietveld 408576698