Web Inspector: hook up grid row filtering in the new Timelines UI
[WebKit-https.git] / Source / WebInspectorUI / UserInterface / Views / LayoutTimelineView.js
1 /*
2  * Copyright (C) 2014, 2015 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 WebInspector.LayoutTimelineView = class LayoutTimelineView extends WebInspector.TimelineView
27 {
28     constructor(timeline, extraArguments)
29     {
30         super(timeline, extraArguments);
31
32         console.assert(timeline.type === WebInspector.TimelineRecord.Type.Layout, timeline);
33
34         let columns = {type: {}, name: {}, location: {}, width: {}, height: {}, startTime: {}, totalTime: {}};
35
36         columns.name.title = WebInspector.UIString("Type");
37         columns.name.width = "15%";
38
39         var typeToLabelMap = new Map;
40         for (var key in WebInspector.LayoutTimelineRecord.EventType) {
41             var value = WebInspector.LayoutTimelineRecord.EventType[key];
42             typeToLabelMap.set(value, WebInspector.LayoutTimelineRecord.displayNameForEventType(value));
43         }
44
45         columns.type.scopeBar = WebInspector.TimelineDataGrid.createColumnScopeBar("layout", typeToLabelMap);
46         columns.type.hidden = true;
47
48         columns.name.disclosure = true;
49         columns.name.icon = true;
50
51         this._scopeBar = columns.type.scopeBar;
52
53         columns.location.title = WebInspector.UIString("Initiator");
54         columns.location.width = "25%";
55
56         columns.width.title = WebInspector.UIString("Width");
57         columns.width.width = "8%";
58
59         columns.height.title = WebInspector.UIString("Height");
60         columns.height.width = "8%";
61
62         columns.startTime.title = WebInspector.UIString("Start Time");
63         columns.startTime.width = "8%";
64         columns.startTime.aligned = "right";
65
66         columns.totalTime.title = WebInspector.UIString("Duration");
67         columns.totalTime.width = "8%";
68         columns.totalTime.aligned = "right";
69
70         for (var column in columns)
71             columns[column].sortable = true;
72
73         this._dataGrid = new WebInspector.LayoutTimelineDataGrid(columns);
74         this._dataGrid.addEventListener(WebInspector.DataGrid.Event.SelectedNodeChanged, this._dataGridSelectedNodeChanged, this);
75
76         this.setupDataGrid(this._dataGrid);
77
78         this._dataGrid.sortColumnIdentifierSetting = new WebInspector.Setting("layout-timeline-view-sort", "startTime");
79         this._dataGrid.sortOrderSetting = new WebInspector.Setting("layout-timeline-view-sort-order", WebInspector.DataGrid.SortOrder.Ascending);
80
81         this._hoveredTreeElement = null;
82         this._hoveredDataGridNode = null;
83         this._showingHighlight = false;
84         this._showingHighlightForRecord = null;
85
86         this._dataGrid.element.addEventListener("mouseover", this._mouseOverDataGrid.bind(this));
87         this._dataGrid.element.addEventListener("mouseleave", this._mouseLeaveDataGrid.bind(this));
88
89         this.element.classList.add("layout");
90         this.addSubview(this._dataGrid);
91
92         timeline.addEventListener(WebInspector.Timeline.Event.RecordAdded, this._layoutTimelineRecordAdded, this);
93
94         this._pendingRecords = [];
95     }
96
97     // Public
98
99     get selectionPathComponents()
100     {
101         let dataGridNode = this._dataGrid.selectedNode;
102         if (!dataGridNode || dataGridNode.hidden)
103             return null;
104
105         let pathComponents = [];
106
107         while (dataGridNode && !dataGridNode.root) {
108             console.assert(dataGridNode instanceof WebInspector.TimelineDataGridNode);
109             if (dataGridNode.hidden)
110                 return null;
111
112             let pathComponent = new WebInspector.TimelineDataGridNodePathComponent(dataGridNode);
113             pathComponent.addEventListener(WebInspector.HierarchicalPathComponent.Event.SiblingWasSelected, this.dataGridNodePathComponentSelected, this);
114             pathComponents.unshift(pathComponent);
115             dataGridNode = dataGridNode.parent;
116         }
117
118         return pathComponents;
119     }
120
121     shown()
122     {
123         super.shown();
124
125         this._updateHighlight();
126
127         this._dataGrid.shown();
128     }
129
130     hidden()
131     {
132         this._hideHighlightIfNeeded();
133
134         this._dataGrid.hidden();
135
136         super.hidden();
137     }
138
139     closed()
140     {
141         console.assert(this.representedObject instanceof WebInspector.Timeline);
142         this.representedObject.removeEventListener(null, null, this);
143
144         this._dataGrid.closed();
145     }
146
147     reset()
148     {
149         super.reset();
150
151         this._hideHighlightIfNeeded();
152
153         this._dataGrid.reset();
154
155         this._pendingRecords = [];
156     }
157
158     // Protected
159
160     dataGridNodePathComponentSelected(event)
161     {
162         let dataGridNode = event.data.pathComponent.timelineDataGridNode;
163         console.assert(dataGridNode.dataGrid === this._dataGrid);
164
165         dataGridNode.revealAndSelect();
166     }
167
168     filterDidChange()
169     {
170         super.filterDidChange();
171
172         this._updateHighlight();
173     }
174
175     treeElementSelected(treeElement, selectedByUser)
176     {
177         if (this._dataGrid.shouldIgnoreSelectionEvent())
178             return;
179
180         super.treeElementSelected(treeElement, selectedByUser);
181
182         this._updateHighlight();
183     }
184
185     layout()
186     {
187         this._processPendingRecords();
188     }
189
190     // Private
191
192     _processPendingRecords()
193     {
194         if (!this._pendingRecords.length)
195             return;
196
197         for (var layoutTimelineRecord of this._pendingRecords) {
198             let dataGridNode = new WebInspector.LayoutTimelineDataGridNode(layoutTimelineRecord, this.zeroTime);
199
200             this._dataGrid.addRowInSortOrder(null, dataGridNode);
201
202             let stack = [{children: layoutTimelineRecord.children, parentDataGridNode: dataGridNode, index: 0}];
203             while (stack.length) {
204                 let entry = stack.lastValue;
205                 if (entry.index >= entry.children.length) {
206                     stack.pop();
207                     continue;
208                 }
209
210                 let childRecord = entry.children[entry.index];
211                 console.assert(childRecord.type === WebInspector.TimelineRecord.Type.Layout, childRecord);
212
213                 let childDataGridNode = new WebInspector.LayoutTimelineDataGridNode(childRecord, this.zeroTime);
214                 console.assert(entry.parentDataGridNode, "entry without parent!");
215                 this._dataGrid.addRowInSortOrder(null, childDataGridNode, entry.parentDataGridNode);
216
217                 if (childDataGridNode && childRecord.children.length)
218                     stack.push({children: childRecord.children, parentDataGridNode: childDataGridNode, index: 0});
219                 ++entry.index;
220             }
221         }
222
223         this._pendingRecords = [];
224     }
225
226     _layoutTimelineRecordAdded(event)
227     {
228         var layoutTimelineRecord = event.data.record;
229         console.assert(layoutTimelineRecord instanceof WebInspector.LayoutTimelineRecord);
230
231         // Only add top-level records, to avoid processing child records multiple times.
232         if (layoutTimelineRecord.parent instanceof WebInspector.LayoutTimelineRecord)
233             return;
234
235         this._pendingRecords.push(layoutTimelineRecord);
236
237         this.needsLayout();
238     }
239
240     _updateHighlight()
241     {
242         var record = this._hoveredOrSelectedRecord();
243         if (!record) {
244             this._hideHighlightIfNeeded();
245             return;
246         }
247
248         this._showHighlightForRecord(record);
249     }
250
251     _showHighlightForRecord(record)
252     {
253         if (this._showingHighlightForRecord === record)
254             return;
255
256         this._showingHighlightForRecord = record;
257
258         const contentColor = {r: 111, g: 168, b: 220, a: 0.66};
259         const outlineColor = {r: 255, g: 229, b: 153, a: 0.66};
260
261         var quad = record.quad;
262         if (quad) {
263             DOMAgent.highlightQuad(quad.toProtocol(), contentColor, outlineColor);
264             this._showingHighlight = true;
265             return;
266         }
267
268         // This record doesn't have a highlight, so hide any existing highlight.
269         if (this._showingHighlight) {
270             this._showingHighlight = false;
271             DOMAgent.hideHighlight();
272         }
273     }
274
275     _hideHighlightIfNeeded()
276     {
277         this._showingHighlightForRecord = null;
278
279         if (this._showingHighlight) {
280             this._showingHighlight = false;
281             DOMAgent.hideHighlight();
282         }
283     }
284
285     _hoveredOrSelectedRecord()
286     {
287         if (this._hoveredDataGridNode)
288             return this._hoveredDataGridNode.record;
289
290         if (this._dataGrid.selectedNode && this._dataGrid.selectedNode.revealed)
291             return this._dataGrid.selectedNode.record;
292
293         return null;
294     }
295
296     _mouseOverDataGrid(event)
297     {
298         var hoveredDataGridNode = this._dataGrid.dataGridNodeFromNode(event.target);
299         if (!hoveredDataGridNode)
300             return;
301
302         this._hoveredDataGridNode = hoveredDataGridNode;
303         this._updateHighlight();
304     }
305
306     _mouseLeaveDataGrid(event)
307     {
308         this._hoveredDataGridNode = null;
309         this._updateHighlight();
310     }
311
312     _dataGridSelectedNodeChanged(event)
313     {
314         this._updateHighlight();
315     }
316 };