summaryrefslogtreecommitdiff
path: root/chromium/third_party/catapult/tracing/tracing/value/ui/generic_table_view.html
blob: f9becaee0e4a79c9cc449ab518420bdae49a1671 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
<!DOCTYPE html>
<!--
Copyright (c) 2015 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
-->
<link rel="import" href="/tracing/base/iteration_helpers.html">
<link rel="import" href="/tracing/base/statistics.html">
<link rel="import" href="/tracing/ui/analysis/generic_object_view.html">
<link rel="import" href="/tracing/ui/base/table.html">
<link rel="import" href="/tracing/value/generic_table.html">
<link rel="import" href="/tracing/value/ui/array_of_numbers_span.html">

<polymer-element name="tr-v-ui-generic-table-view">
  <template>
    <style>
    :host {
    display: flex;
    }
    #table {
      flex: 1 1 auto;
      align-self: stretch;
    }
    </style>
    <tr-ui-b-table id="table"></tr-ui-b-table>
  </template>
</polymer-element>

<script>
'use strict';

tr.exportTo('tr.v.ui', function() {
  var TEXT_COLUMN_MODE = 1;
  var NUMERIC_COLUMN_MODE = 2;
  var ELEMENT_COLUMN_MODE = 3;

  function isNumeric(value) {
    // TODO(nduca): Also consider other units that are numeric.
    if ((typeof value) === 'number')
      return true;
    else if (value instanceof Number)
      return true;
    return false;
  }

  function GenericTableViewTotalsItem(opt_values) {
    if (opt_values !== undefined)
      this.values = opt_values;
    else
      this.values = [];
  }

  function GenericTableViewColumnDescriptor(fieldName, firstFieldValue) {
    this.title = fieldName;
    this.fieldName = fieldName;

    this.updateModeGivenValue(firstFieldValue);
  }

  GenericTableViewColumnDescriptor.prototype = {
    get columnMode() {
      return this.columnMode_;
    },

    get isInNumericMode() {
      return this.columnMode_ === NUMERIC_COLUMN_MODE;
    },

    cmp: function(a, b) {
      if (this.columnMode_ === ELEMENT_COLUMN_MODE)
        return 0;

      return tr.b.comparePossiblyUndefinedValues(a, b, function(a, b) {
        var vA = a[this.fieldName];
        var vB = b[this.fieldName];
        return tr.b.comparePossiblyUndefinedValues(vA, vB, function(vA, vB) {
          if (vA.localeCompare)
            return vA.localeCompare(vB);
          return vA - vB;
        }, this);
      }, this);
    },

    updateModeGivenValue: function(fieldValue) {
      if (this.columnMode_ === undefined) {
        if (fieldValue === undefined || fieldValue === null)
          return;

        if (isNumeric(fieldValue)) {
          this.columnMode_ = NUMERIC_COLUMN_MODE;
          return;
        }

        if (fieldValue instanceof HTMLElement) {
          this.columnMode_ = ELEMENT_COLUMN_MODE;
          return;
        }

        this.columnMode_ = TEXT_COLUMN_MODE;
        return;
      }

      // Undefineds & nulls shouldn't change the mode.
      if (fieldValue === undefined || fieldValue === null)
        return;

      // If we were already in numeric mode, then we don't
      // need to put it into numeric mode again. And, if we were
      // previously in text mode, then we can't go into numeric mode now.
      if (isNumeric(fieldValue))
        return;

      if (fieldValue instanceof HTMLElement) {
        this.columnMode_ = ELEMENT_COLUMN_MODE;
        return;
      }

      if (this.columnMode_ === NUMERIC_COLUMN_MODE)
        this.columnMode_ = TEXT_COLUMN_MODE;
    },

    value: function(item) {
      var fieldValue = item[this.fieldName];
      if (fieldValue instanceof GenericTableViewTotalsItem) {
        var span = document.createElement('tr-v-ui-array-of-numbers-span');
        span.summaryMode = tr.v.ui.ArrayOfNumbersSummaryModes.TOTAL_MODE;
        span.numbers = fieldValue.values;
        return span;
      }

      if (fieldValue === undefined)
        return '-';

      if (fieldValue instanceof HTMLElement)
        return fieldValue;

      if (fieldValue instanceof Object) {
        var gov = document.createElement('tr-ui-a-generic-object-view');
        gov.object = fieldValue;
        return gov;
      }

      // TODO(nduca): Use units objects if applicable.
      return fieldValue;
    }
  };

  Polymer('tr-v-ui-generic-table-view', {
    created: function() {
      this.items_ = undefined;
      this.importantColumNames_ = [];
    },

    get items() {
      return this.items_;
    },

    set items(itemsOrGenericTable) {
      if (itemsOrGenericTable === undefined) {
        this.items_ = undefined;
      } else if (itemsOrGenericTable instanceof Array) {
        this.items_ = itemsOrGenericTable;
      } else if (itemsOrGenericTable instanceof tr.v.GenericTable) {
        this.items_ = itemsOrGenericTable.items;
      }
      this.updateContents_();
    },

    get importantColumNames() {
      return this.importantColumNames_;
    },

    set importantColumNames(importantColumNames) {
      this.importantColumNames_ = importantColumNames;
      this.updateContents_();
    },

    createColumns_: function() {
      var columnsByName = {};
      this.items_.forEach(function(item) {
        tr.b.iterItems(item, function(itemFieldName, itemFieldValue) {
          var colDesc = columnsByName[itemFieldName];
          if (colDesc !== undefined) {
            colDesc.updateModeGivenValue(itemFieldValue);
            return;
          }

          colDesc = new GenericTableViewColumnDescriptor(
              itemFieldName, itemFieldValue);
          columnsByName[itemFieldName] = colDesc;
        }, this);
      }, this);

      var columns = tr.b.dictionaryValues(columnsByName);
      if (columns.length === 0)
        return undefined;

      // Sort by name.
      var isColumnNameImportant = {};
      var importantColumNames = this.importantColumNames || [];
      importantColumNames.forEach(function(icn) {
        isColumnNameImportant[icn] = true;
      });
      columns.sort(function(a, b) {
        var iA = isColumnNameImportant[a.title] ? 1 : 0;
        var iB = isColumnNameImportant[b.title] ? 1 : 0;
        if ((iB - iA) !== 0)
          return iB - iA;
        return a.title.localeCompare(b.title);
      });

      // Set sizes. This is convoluted by the fact that the first
      // table column must have fixed size.
      var colWidthPercentage;
      if (columns.length == 1)
        colWidthPercentage = '100%';
      else
        colWidthPercentage = (100 / (columns.length - 1)).toFixed(3) + '%';
      columns[0].width = '250px';
      for (var i = 1; i < columns.length; i++)
        columns[i].width = colWidthPercentage;

      return columns;
    },

    createFooterRowsIfNeeded_: function(columns) {
      // Make totals row if needed.
      var hasColumnThatIsNumeric = columns.some(function(column) {
        return column.isInNumericMode;
      });
      if (!hasColumnThatIsNumeric)
        return [];

      var totalsItems = {};
      columns.forEach(function(column) {
        if (!column.isInNumericMode)
          return;
        var totalsItem = new GenericTableViewTotalsItem();
        this.items_.forEach(function(item) {
          var fieldValue = item[column.fieldName];
          if (fieldValue === undefined || fieldValue === null)
            return;
          totalsItem.values.push(fieldValue);
        });
        totalsItems[column.fieldName] = totalsItem;
      }, this);

      return [totalsItems];
    },

    updateContents_: function() {
      var columns;
      if (this.items_ !== undefined)
        columns = this.createColumns_();

      if (!columns) {
        this.$.table.tableColumns = [];
        this.$.table.tableRows = [];
        this.$.table.footerRows = [];
        return;
      }

      this.$.table.tableColumns = columns;
      this.$.table.tableRows = this.items_;
      this.$.table.footerRows = this.createFooterRowsIfNeeded_(columns);
      this.$.table.rebuild();
    },

    get selectionMode() {
      return this.$.table.selectionMode;
    },

    set selectionMode(selectionMode) {
      this.$.table.selectionMode = selectionMode;
    },

    get rowHighlightStyle() {
      return this.$.table.rowHighlightStyle;
    },

    set rowHighlightStyle(rowHighlightStyle) {
      this.$.table.rowHighlightStyle = rowHighlightStyle;
    },

    get cellHighlightStyle() {
      return this.$.table.cellHighlightStyle;
    },

    set cellHighlightStyle(cellHighlightStyle) {
      this.$.table.cellHighlightStyle = cellHighlightStyle;
    }
  });

  return {
    GenericTableViewTotalsItem: GenericTableViewTotalsItem,
    GenericTableViewColumnDescriptor: GenericTableViewColumnDescriptor
  };
});
</script>