summaryrefslogtreecommitdiffstats
path: root/chromium/third_party/catapult/tracing/tracing/ui/tracks/multi_row_track.html
blob: 8b5fd837f0d530deeb3a972fabf7c7f69bf970a2 (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
<!DOCTYPE html>
<!--
Copyright (c) 2013 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/utils.html">
<link rel="import" href="/tracing/model/model_settings.html">
<link rel="import" href="/tracing/ui/base/ui.html">
<link rel="import" href="/tracing/ui/tracks/container_track.html">

<script>
'use strict';

tr.exportTo('tr.ui.tracks', function() {
  /**
   * A track that displays a group of objects in multiple rows.
   * @constructor
   * @extends {ContainerTrack}
   */
  const MultiRowTrack = tr.ui.b.define(
      'multi-row-track', tr.ui.tracks.ContainerTrack);

  MultiRowTrack.prototype = {

    __proto__: tr.ui.tracks.ContainerTrack.prototype,

    decorate(viewport) {
      tr.ui.tracks.ContainerTrack.prototype.decorate.call(this, viewport);
      this.tooltip_ = '';
      this.heading_ = '';

      this.groupingSource_ = undefined;
      this.itemsToGroup_ = undefined;

      this.defaultToCollapsedWhenSubRowCountMoreThan = 1;

      this.currentSubRowsWithHeadings_ = undefined;
      this.expanded_ = true;
    },

    get itemsToGroup() {
      return this.itemsToGroup_;
    },

    setItemsToGroup(itemsToGroup, opt_groupingSource) {
      this.itemsToGroup_ = itemsToGroup;
      this.groupingSource_ = opt_groupingSource;
      this.currentSubRowsWithHeadings_ = undefined;
      this.updateContents_();
      this.updateExpandedStateFromGroupingSource_();
    },

    /**
     * Opt-out from using buildSubRows_() and provide prebuilt rows.
     * Array of {row: [rowItems...], heading} dicts is expected as an argument.
     */
    setPrebuiltSubRows(groupingSource, subRowsWithHeadings) {
      this.itemsToGroup_ = undefined;
      this.groupingSource_ = groupingSource;
      this.currentSubRowsWithHeadings_ = subRowsWithHeadings;
      this.updateContents_();
      this.updateExpandedStateFromGroupingSource_();
    },

    get heading() {
      return this.heading_;
    },

    set heading(h) {
      this.heading_ = h;
      this.updateHeadingAndTooltip_();
    },

    get tooltip() {
      return this.tooltip_;
    },

    set tooltip(t) {
      this.tooltip_ = t;
      this.updateHeadingAndTooltip_();
    },

    get subRows() {
      return this.currentSubRowsWithHeadings_.map(elem => elem.row);
    },

    get hasVisibleContent() {
      return this.children.length > 0;
    },

    get expanded() {
      return this.expanded_;
    },

    set expanded(expanded) {
      if (this.expanded_ === expanded) return;

      this.expanded_ = expanded;
      this.expandedStateChanged_();
    },

    onHeadingClicked_(e) {
      if (this.subRows.length <= 1) return;

      this.expanded = !this.expanded;

      if (this.groupingSource_) {
        const modelSettings = new tr.model.ModelSettings(
            this.groupingSource_.model);
        modelSettings.setSettingFor(this.groupingSource_, 'expanded',
            this.expanded);
      }

      e.stopPropagation();
    },

    updateExpandedStateFromGroupingSource_() {
      if (this.groupingSource_) {
        const numSubRows = this.subRows.length;
        const modelSettings = new tr.model.ModelSettings(
            this.groupingSource_.model);
        if (numSubRows > 1) {
          let defaultExpanded;
          if (numSubRows > this.defaultToCollapsedWhenSubRowCountMoreThan) {
            defaultExpanded = false;
          } else {
            defaultExpanded = true;
          }
          this.expanded = modelSettings.getSettingFor(
              this.groupingSource_, 'expanded', defaultExpanded);
        } else {
          this.expanded = undefined;
        }
      }
    },

    expandedStateChanged_() {
      const minH = Math.max(2, Math.ceil(18 / this.children.length));
      const h = (this.expanded_ ? 18 : minH) + 'px';

      for (let i = 0; i < this.children.length; i++) {
        this.children[i].height = h;
        if (i === 0) {
          this.children[i].arrowVisible = true;
        }
        this.children[i].expanded = this.expanded;
      }

      if (this.children.length === 1) {
        this.children[0].expanded = true;
        this.children[0].arrowVisible = false;
      }
    },

    updateContents_() {
      tr.ui.tracks.ContainerTrack.prototype.updateContents_.call(this);
      this.detach();  // Clear sub-tracks.

      if (this.currentSubRowsWithHeadings_ === undefined) {
        // No prebuilt rows, build it.
        if (this.itemsToGroup_ === undefined) {
          return;
        }
        const subRows = this.buildSubRows_(this.itemsToGroup_);
        this.currentSubRowsWithHeadings_ = subRows.map(row => {
          return {row, heading: undefined};
        });
      }
      if (this.currentSubRowsWithHeadings_ === undefined ||
          this.currentSubRowsWithHeadings_.length === 0) {
        return;
      }

      const addSubTrackEx = (items, opt_heading) => {
        const track = this.addSubTrack_(items);
        if (opt_heading !== undefined) {
          track.heading = opt_heading;
        }
        track.addEventListener(
            'heading-clicked', this.onHeadingClicked_.bind(this));
      };

      if (this.currentSubRowsWithHeadings_[0].heading !== undefined &&
          this.currentSubRowsWithHeadings_[0].heading !== this.heading_) {
        // Create an empty row to render the group's title there.
        addSubTrackEx([]);
      }

      for (const subRowWithHeading of this.currentSubRowsWithHeadings_) {
        const subRow = subRowWithHeading.row;
        if (subRow.length === 0) {
          continue;
        }
        addSubTrackEx(subRow, subRowWithHeading.heading);
      }

      this.updateHeadingAndTooltip_();
      this.expandedStateChanged_();
    },

    updateHeadingAndTooltip_() {
      if (!Polymer.dom(this).firstChild) return;

      Polymer.dom(this).firstChild.heading = this.heading_;
      Polymer.dom(this).firstChild.tooltip = this.tooltip_;
    },

    /**
     * Breaks up the list of slices into N rows, each of which is a list of
     * slices that are non overlapping.
     */
    buildSubRows_(itemsToGroup) {
      throw new Error('Not implemented');
    },

    addSubTrack_(subRowItems) {
      throw new Error('Not implemented');
    },

    areArrayContentsSame_(a, b) {
      if (!a || !b) return false;

      if (!a.length || !b.length) return false;

      if (a.length !== b.length) return false;

      for (let i = 0; i < a.length; ++i) {
        if (a[i] !== b[i]) return false;
      }
      return true;
    }
  };

  return {
    MultiRowTrack,
  };
});
</script>