Web Inspector: Timelines UI redesign: use DataGridNode for TimelineView selection...
[WebKit-https.git] / Source / WebInspectorUI / UserInterface / Views / NetworkTimelineView.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.NetworkTimelineView = class NetworkTimelineView extends WebInspector.TimelineView
27 {
28     constructor(timeline, extraArguments)
29     {
30         super(timeline, extraArguments);
31
32         console.assert(timeline.type === WebInspector.TimelineRecord.Type.Network);
33
34         this.navigationSidebarTreeOutline.disclosureButtons = false;
35         this.navigationSidebarTreeOutline.element.classList.add("network");
36
37         let columns = {name: {}, domain: {}, type: {}, method: {}, scheme: {}, statusCode: {}, cached: {}, size: {}, transferSize: {}, requestSent: {}, latency: {}, duration: {}};
38
39         columns.name.title = WebInspector.UIString("Name");
40         columns.name.icon = true;
41         columns.name.width = "10%";
42
43         columns.domain.title = WebInspector.UIString("Domain");
44         columns.domain.width = "10%";
45
46         columns.type.title = WebInspector.UIString("Type");
47         columns.type.width = "8%";
48
49         var typeToLabelMap = new Map;
50         for (var key in WebInspector.Resource.Type) {
51             var value = WebInspector.Resource.Type[key];
52             typeToLabelMap.set(value, WebInspector.Resource.displayNameForType(value, true));
53         }
54
55         columns.type.scopeBar = WebInspector.TimelineDataGrid.createColumnScopeBar("network", typeToLabelMap);
56         this._scopeBar = columns.type.scopeBar;
57
58         columns.method.title = WebInspector.UIString("Method");
59         columns.method.width = "6%";
60
61         columns.scheme.title = WebInspector.UIString("Scheme");
62         columns.scheme.width = "6%";
63
64         columns.statusCode.title = WebInspector.UIString("Status");
65         columns.statusCode.width = "6%";
66
67         columns.cached.title = WebInspector.UIString("Cached");
68         columns.cached.width = "6%";
69
70         columns.size.title = WebInspector.UIString("Size");
71         columns.size.width = "8%";
72         columns.size.aligned = "right";
73
74         columns.transferSize.title = WebInspector.UIString("Transferred");
75         columns.transferSize.width = "8%";
76         columns.transferSize.aligned = "right";
77
78         columns.requestSent.title = WebInspector.UIString("Start Time");
79         columns.requestSent.width = "9%";
80         columns.requestSent.aligned = "right";
81
82         columns.latency.title = WebInspector.UIString("Latency");
83         columns.latency.width = "9%";
84         columns.latency.aligned = "right";
85
86         columns.duration.title = WebInspector.UIString("Duration");
87         columns.duration.width = "9%";
88         columns.duration.aligned = "right";
89
90         for (var column in columns)
91             columns[column].sortable = true;
92
93         this._dataGrid = new WebInspector.TimelineDataGrid(this.navigationSidebarTreeOutline, columns);
94         this._dataGrid.addEventListener(WebInspector.TimelineDataGrid.Event.FiltersDidChange, this._dataGridFiltersDidChange, this);
95         this._dataGrid.addEventListener(WebInspector.DataGrid.Event.SelectedNodeChanged, this._dataGridNodeSelected, this);
96         this._dataGrid.sortColumnIdentifierSetting = new WebInspector.Setting("network-timeline-view-sort", "requestSent");
97         this._dataGrid.sortOrderSetting = new WebInspector.Setting("network-timeline-view-sort-order", WebInspector.DataGrid.SortOrder.Ascending);
98
99         this.element.classList.add("network");
100         this.addSubview(this._dataGrid);
101
102         timeline.addEventListener(WebInspector.Timeline.Event.RecordAdded, this._networkTimelineRecordAdded, this);
103
104         this._pendingRecords = [];
105     }
106
107     // Public
108
109     get navigationSidebarTreeOutlineLabel()
110     {
111         return WebInspector.UIString("Resources");
112     }
113
114     get selectionPathComponents()
115     {
116         if (!this._dataGrid.selectedNode || this._dataGrid.selectedNode.hidden)
117             return null;
118
119         console.assert(this._dataGrid.selectedNode instanceof WebInspector.ResourceTimelineDataGridNode);
120
121         let pathComponent = new WebInspector.TimelineDataGridNodePathComponent(this._dataGrid.selectedNode, this._dataGrid.selectedNode.resource);
122         pathComponent.addEventListener(WebInspector.HierarchicalPathComponent.Event.SiblingWasSelected, this.dataGridNodePathComponentSelected, this);
123         return [pathComponent];
124     }
125
126     shown()
127     {
128         super.shown();
129
130         this._dataGrid.shown();
131     }
132
133     hidden()
134     {
135         this._dataGrid.hidden();
136
137         super.hidden();
138     }
139
140     closed()
141     {
142         console.assert(this.representedObject instanceof WebInspector.Timeline);
143         this.representedObject.removeEventListener(null, null, this);
144
145         this._dataGrid.closed();
146     }
147
148     matchTreeElementAgainstCustomFilters(treeElement)
149     {
150         return this._dataGrid.treeElementMatchesActiveScopeFilters(treeElement);
151     }
152
153     reset()
154     {
155         super.reset();
156
157         this._dataGrid.reset();
158
159         this._pendingRecords = [];
160     }
161
162     // Protected
163
164     canShowContentViewForTreeElement(treeElement)
165     {
166         if (treeElement instanceof WebInspector.ResourceTreeElement || treeElement instanceof WebInspector.ScriptTreeElement)
167             return true;
168         return super.canShowContentViewForTreeElement(treeElement);
169     }
170
171     showContentViewForTreeElement(treeElement)
172     {
173         if (treeElement instanceof WebInspector.ResourceTreeElement || treeElement instanceof WebInspector.ScriptTreeElement) {
174             WebInspector.showSourceCode(treeElement.representedObject);
175             return;
176         }
177
178         console.error("Unknown tree element selected.", treeElement);
179     }
180
181     dataGridNodePathComponentSelected(event)
182     {
183         let pathComponent = event.data.pathComponent;
184         console.assert(pathComponent instanceof WebInspector.TimelineDataGridNodePathComponent);
185
186         let dataGridNode = pathComponent.timelineDataGridNode;
187         console.assert(dataGridNode.dataGrid === this._dataGrid);
188
189         dataGridNode.revealAndSelect();
190     }
191
192     treeElementSelected(treeElement, selectedByUser)
193     {
194         if (this._dataGrid.shouldIgnoreSelectionEvent())
195             return;
196
197         super.treeElementSelected(treeElement, selectedByUser);
198     }
199
200     layout()
201     {
202         this._processPendingRecords();
203     }
204
205     // Private
206
207     _processPendingRecords()
208     {
209         if (!this._pendingRecords.length)
210             return;
211
212         for (var resourceTimelineRecord of this._pendingRecords) {
213             // Skip the record if it already exists in the tree.
214             var treeElement = this.navigationSidebarTreeOutline.findTreeElement(resourceTimelineRecord.resource);
215             if (treeElement)
216                 continue;
217
218             treeElement = new WebInspector.ResourceTreeElement(resourceTimelineRecord.resource);
219             var dataGridNode = new WebInspector.ResourceTimelineDataGridNode(resourceTimelineRecord, false, this);
220
221             this._dataGrid.addRowInSortOrder(treeElement, dataGridNode);
222         }
223
224         this._pendingRecords = [];
225     }
226
227     _networkTimelineRecordAdded(event)
228     {
229         var resourceTimelineRecord = event.data.record;
230         console.assert(resourceTimelineRecord instanceof WebInspector.ResourceTimelineRecord);
231
232         this._pendingRecords.push(resourceTimelineRecord);
233
234         this.needsLayout();
235     }
236
237     _dataGridFiltersDidChange(event)
238     {
239         this.timelineSidebarPanel.updateFilter();
240     }
241
242     _dataGridNodeSelected(event)
243     {
244         this.dispatchEventToListeners(WebInspector.ContentView.Event.SelectionPathComponentsDidChange);
245     }
246 };