Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / trace-viewer / trace_viewer / tracing / tracks / multi_row_track.html
1 <!DOCTYPE html>
2 <!--
3 Copyright (c) 2013 The Chromium Authors. All rights reserved.
4 Use of this source code is governed by a BSD-style license that can be
5 found in the LICENSE file.
6 -->
7
8 <link rel="import" href="/tracing/tracks/container_track.html">
9 <link rel="import" href="/tracing/trace_model_settings.html">
10 <link rel="import" href="/tvcm/sorted_array_utils.html">
11 <link rel="import" href="/tvcm/ui.html">
12
13 <script>
14 'use strict';
15
16 tvcm.exportTo('tracing.tracks', function() {
17   var TraceModelSettings = tracing.TraceModelSettings;
18
19   /**
20    * A track that displays a group of objects in multiple rows.
21    * @constructor
22    * @extends {ContainerTrack}
23    */
24   var MultiRowTrack = tvcm.ui.define(
25       'multi-row-track', tracing.tracks.ContainerTrack);
26
27   MultiRowTrack.prototype = {
28
29     __proto__: tracing.tracks.ContainerTrack.prototype,
30
31     decorate: function(viewport) {
32       tracing.tracks.ContainerTrack.prototype.decorate.call(this, viewport);
33       this.tooltip_ = '';
34       this.heading_ = '';
35
36       this.groupingSource_ = undefined;
37       this.itemsToGroup_ = undefined;
38
39       this.defaultToCollapsedWhenSubRowCountMoreThan = 1;
40
41       this.itemsGroupedOnLastUpdateContents_ = undefined;
42
43       this.currentSubRows_ = [];
44       this.expanded_ = true;
45     },
46
47     get itemsToGroup() {
48       return this.itemsToGroup_;
49     },
50
51     setItemsToGroup: function(itemsToGroup, opt_groupingSource) {
52       this.itemsToGroup_ = itemsToGroup;
53       this.groupingSource_ = opt_groupingSource;
54       this.updateContents_();
55       this.updateExpandedStateFromGroupingSource_();
56     },
57
58     get heading() {
59       return this.heading_;
60     },
61
62     set heading(h) {
63       this.heading_ = h;
64       this.updateContents_();
65     },
66
67     get tooltip() {
68       return this.tooltip_;
69     },
70
71     set tooltip(t) {
72       this.tooltip_ = t;
73       this.updateContents_();
74     },
75
76     get subRows() {
77       return this.currentSubRows_;
78     },
79
80     get hasVisibleContent() {
81       return this.children.length > 0;
82     },
83
84     get expanded() {
85       return this.expanded_;
86     },
87
88     set expanded(expanded) {
89       expanded = expanded;
90       if (this.expanded_ == expanded)
91         return;
92       this.expanded_ = expanded;
93       this.expandedStateChanged_();
94     },
95
96     onHeadingClicked_: function(e) {
97       if (this.subRows.length <= 1)
98         return;
99       this.expanded = !this.expanded;
100
101       if (this.groupingSource_) {
102         var modelSettings = new TraceModelSettings(
103             this.groupingSource_.model);
104         modelSettings.setSettingFor(this.groupingSource_, 'expanded', this.expanded);
105       }
106
107       e.stopPropagation();
108     },
109
110     updateExpandedStateFromGroupingSource_: function() {
111       if (this.groupingSource_) {
112         var numSubRows = this.subRows.length;
113         var modelSettings = new TraceModelSettings(
114             this.groupingSource_.model);
115         if (numSubRows > 1) {
116           var defaultExpanded;
117           if (numSubRows > this.defaultToCollapsedWhenSubRowCountMoreThan) {
118             defaultExpanded = false;
119           } else {
120             defaultExpanded = true;
121           }
122           this.expanded = modelSettings.getSettingFor(
123               this.groupingSource_, 'expanded', defaultExpanded);
124         } else {
125           this.expanded = undefined;
126         }
127       }
128     },
129
130     expandedStateChanged_: function() {
131       var minH = Math.max(2, Math.ceil(18 / this.children.length));
132       var h = (this.expanded_ ? 18 : minH) + 'px';
133       for (var i = 0; i < this.children.length; i++)
134         this.children[i].height = h;
135
136       if (this.children.length > 0)
137         this.children[0].expanded = this.expanded;
138     },
139
140     updateContents_: function() {
141       tracing.tracks.ContainerTrack.prototype.updateContents_.call(this);
142       if (!this.itemsToGroup_) {
143         this.updateHeadingAndTooltip_();
144         this.currentSubRows_ = [];
145         return;
146       }
147
148       if (this.areArrayContentsSame_(this.itemsGroupedOnLastUpdateContents_,
149                                      this.itemsToGroup_)) {
150         this.updateHeadingAndTooltip_();
151         return;
152       }
153
154       this.itemsGroupedOnLastUpdateContents_ = this.itemsToGroup_;
155
156       this.detach();
157       if (!this.itemsToGroup_.length) {
158         this.currentSubRows_ = [];
159         return;
160       }
161       var subRows = this.buildSubRows_(this.itemsToGroup_);
162       this.currentSubRows_ = subRows;
163       for (var srI = 0; srI < subRows.length; srI++) {
164         var subRow = subRows[srI];
165         if (!subRow.length)
166           continue;
167         var track = this.addSubTrack_(subRow);
168         track.addEventListener(
169           'heading-clicked', this.onHeadingClicked_.bind(this));
170       }
171       this.updateHeadingAndTooltip_();
172       this.expandedStateChanged_();
173     },
174
175     updateHeadingAndTooltip_: function() {
176       if (!this.firstChild)
177         return;
178       this.firstChild.heading = this.heading_;
179       this.firstChild.tooltip = this.tooltip_;
180     },
181
182     /**
183      * Breaks up the list of slices into N rows, each of which is a list of
184      * slices that are non overlapping.
185      */
186     buildSubRows_: function(itemsToGroup) {
187       throw new Error('Not implemented');
188     },
189
190     addSubTrack_: function(subRowItems) {
191       throw new Error('Not implemented');
192     },
193
194     areArrayContentsSame_: function(a, b) {
195       if (!a || !b)
196         return false;
197       if (!a.length || !b.length)
198         return false;
199       if (a.length != b.length)
200         return false;
201       for (var i = 0; i < a.length; ++i) {
202         if (a[i] != b[i])
203           return false;
204       }
205       return true;
206     }
207   };
208
209   return {
210     MultiRowTrack: MultiRowTrack
211   };
212 });
213 </script>