Web Inspector: Convert TreeElement classes to ES6
[WebKit-https.git] / Source / WebInspectorUI / UserInterface / Views / BreakpointTreeElement.js
1 /*
2  * Copyright (C) 2013-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.BreakpointTreeElement = class BreakpointTreeElement extends WebInspector.GeneralTreeElement
27 {
28     constructor(breakpoint, className, title)
29     {
30         console.assert(breakpoint instanceof WebInspector.Breakpoint);
31
32         if (!className)
33             className = WebInspector.BreakpointTreeElement.GenericLineIconStyleClassName;
34
35         super(["breakpoint", className], title, null, breakpoint, false);
36
37         this._breakpoint = breakpoint;
38
39         this._listeners = new WebInspector.EventListenerSet(this, "BreakpointTreeElement listeners");
40         if (!title)
41             this._listeners.register(breakpoint, WebInspector.Breakpoint.Event.LocationDidChange, this._breakpointLocationDidChange);
42         this._listeners.register(breakpoint, WebInspector.Breakpoint.Event.DisabledStateDidChange, this._updateStatus);
43         this._listeners.register(breakpoint, WebInspector.Breakpoint.Event.AutoContinueDidChange, this._updateStatus);
44         this._listeners.register(breakpoint, WebInspector.Breakpoint.Event.ResolvedStateDidChange, this._updateStatus);
45         this._listeners.register(WebInspector.debuggerManager, WebInspector.DebuggerManager.Event.BreakpointsEnabledDidChange, this._updateStatus);
46
47         this._listeners.register(WebInspector.probeManager, WebInspector.ProbeManager.Event.ProbeSetAdded, this._probeSetAdded);
48         this._listeners.register(WebInspector.probeManager, WebInspector.ProbeManager.Event.ProbeSetRemoved, this._probeSetRemoved);
49
50         this._statusImageElement = document.createElement("img");
51         this._statusImageElement.className = WebInspector.BreakpointTreeElement.StatusImageElementStyleClassName;
52         this._listeners.register(this._statusImageElement, "mousedown", this._statusImageElementMouseDown);
53         this._listeners.register(this._statusImageElement, "click", this._statusImageElementClicked);
54
55         if (!title)
56             this._updateTitles();
57         this._updateStatus();
58
59         this.status = this._statusImageElement;
60         this.small = true;
61
62         this._iconAnimationLayerElement = document.createElement("span");
63         this.iconElement.appendChild(this._iconAnimationLayerElement);
64     }
65
66     // Public
67
68     get breakpoint()
69     {
70         return this._breakpoint;
71     }
72
73     get filterableData()
74     {
75         return {text: this.breakpoint.url};
76     }
77
78     ondelete()
79     {
80         if (!WebInspector.debuggerManager.isBreakpointRemovable(this._breakpoint))
81             return false;
82
83         WebInspector.debuggerManager.removeBreakpoint(this._breakpoint);
84         return true;
85     }
86
87     onenter()
88     {
89         this._breakpoint.cycleToNextMode();
90         return true;
91     }
92
93     onspace()
94     {
95         this._breakpoint.cycleToNextMode();
96         return true;
97     }
98
99     oncontextmenu(event)
100     {
101         var contextMenu = new WebInspector.ContextMenu(event);
102         this._breakpoint.appendContextMenuItems(contextMenu, this._statusImageElement);
103         contextMenu.show();
104     }
105
106     onattach()
107     {
108         WebInspector.GeneralTreeElement.prototype.onattach.call(this);
109
110         this._listeners.install();
111
112         for (var probeSet of WebInspector.probeManager.probeSets)
113             if (probeSet.breakpoint === this._breakpoint)
114                 this._addProbeSet(probeSet);
115     }
116
117     ondetach()
118     {
119         WebInspector.GeneralTreeElement.prototype.ondetach.call(this);
120
121         this._listeners.uninstall();
122
123         if (this._probeSet)
124             this._removeProbeSet(this._probeSet);
125     }
126
127     removeStatusImage()
128     {
129         this._statusImageElement.remove();
130         this._statusImageElement = null;
131     }
132
133     // Private
134
135     _updateTitles()
136     {
137         var sourceCodeLocation = this._breakpoint.sourceCodeLocation;
138
139         var displayLineNumber = sourceCodeLocation.displayLineNumber;
140         var displayColumnNumber = sourceCodeLocation.displayColumnNumber;
141         if (displayColumnNumber > 0)
142             this.mainTitle = WebInspector.UIString("Line %d:%d").format(displayLineNumber + 1, displayColumnNumber + 1); // The user visible line and column numbers are 1-based.
143         else
144             this.mainTitle = WebInspector.UIString("Line %d").format(displayLineNumber + 1); // The user visible line number is 1-based.
145
146         if (sourceCodeLocation.hasMappedLocation()) {
147             this.subtitle = sourceCodeLocation.formattedLocationString();
148
149             if (sourceCodeLocation.hasFormattedLocation())
150                 this.subtitleElement.classList.add(WebInspector.BreakpointTreeElement.FormattedLocationStyleClassName);
151             else
152                 this.subtitleElement.classList.remove(WebInspector.BreakpointTreeElement.FormattedLocationStyleClassName);
153
154             this.tooltip = this.mainTitle + " \u2014 " + WebInspector.UIString("originally %s").format(sourceCodeLocation.originalLocationString());
155         }
156     }
157
158     _updateStatus()
159     {
160         if (!this._statusImageElement)
161             return;
162
163         if (this._breakpoint.disabled)
164             this._statusImageElement.classList.add(WebInspector.BreakpointTreeElement.StatusImageDisabledStyleClassName);
165         else
166             this._statusImageElement.classList.remove(WebInspector.BreakpointTreeElement.StatusImageDisabledStyleClassName);
167
168         if (this._breakpoint.autoContinue)
169             this._statusImageElement.classList.add(WebInspector.BreakpointTreeElement.StatusImageAutoContinueStyleClassName);
170         else
171             this._statusImageElement.classList.remove(WebInspector.BreakpointTreeElement.StatusImageAutoContinueStyleClassName);
172
173         if (this._breakpoint.resolved && WebInspector.debuggerManager.breakpointsEnabled)
174             this._statusImageElement.classList.add(WebInspector.BreakpointTreeElement.StatusImageResolvedStyleClassName);
175         else
176             this._statusImageElement.classList.remove(WebInspector.BreakpointTreeElement.StatusImageResolvedStyleClassName);
177     }
178
179     _addProbeSet(probeSet)
180     {
181         console.assert(probeSet instanceof WebInspector.ProbeSet);
182         console.assert(probeSet.breakpoint === this._breakpoint);
183         console.assert(probeSet !== this._probeSet);
184
185         this._probeSet = probeSet;
186         probeSet.addEventListener(WebInspector.ProbeSet.Event.SamplesCleared, this._samplesCleared, this);
187         probeSet.dataTable.addEventListener(WebInspector.ProbeSetDataTable.Event.FrameInserted, this._dataUpdated, this);
188     }
189
190     _removeProbeSet(probeSet)
191     {
192         console.assert(probeSet instanceof WebInspector.ProbeSet);
193         console.assert(probeSet === this._probeSet);
194
195         probeSet.removeEventListener(WebInspector.ProbeSet.Event.SamplesCleared, this._samplesCleared, this);
196         probeSet.dataTable.removeEventListener(WebInspector.ProbeSetDataTable.Event.FrameInserted, this._dataUpdated, this);
197         delete this._probeSet;
198     }
199
200     _probeSetAdded(event)
201     {
202         var probeSet = event.data.probeSet;
203         if (probeSet.breakpoint === this._breakpoint)
204             this._addProbeSet(probeSet);
205     }
206
207     _probeSetRemoved(event)
208     {
209         var probeSet = event.data.probeSet;
210         if (probeSet.breakpoint === this._breakpoint)
211             this._removeProbeSet(probeSet);
212     }
213
214     _samplesCleared(event)
215     {
216         console.assert(this._probeSet);
217
218         var oldTable = event.data.oldTable;
219         oldTable.removeEventListener(WebInspector.ProbeSetDataTable.Event.FrameInserted, this._dataUpdated, this);
220         this._probeSet.dataTable.addEventListener(WebInspector.ProbeSetDataTable.Event.FrameInserted, this._dataUpdated, this);
221     }
222
223     _dataUpdated()
224     {
225         if (this.element.classList.contains(WebInspector.BreakpointTreeElement.ProbeDataUpdatedStyleClassName)) {
226             clearTimeout(this._removeIconAnimationTimeoutIdentifier);
227             this.element.classList.remove(WebInspector.BreakpointTreeElement.ProbeDataUpdatedStyleClassName);
228             // We want to restart the animation, which can only be done by removing the class,
229             // performing layout, and re-adding the class. Try adding class back on next run loop.
230             window.requestAnimationFrame(this._dataUpdated.bind(this));
231             return;
232         }
233
234         this.element.classList.add(WebInspector.BreakpointTreeElement.ProbeDataUpdatedStyleClassName);
235         this._removeIconAnimationTimeoutIdentifier = setTimeout(function() {
236             this.element.classList.remove(WebInspector.BreakpointTreeElement.ProbeDataUpdatedStyleClassName);
237         }.bind(this), WebInspector.BreakpointTreeElement.ProbeDataUpdatedAnimationDuration);
238     }
239
240
241     _breakpointLocationDidChange(event)
242     {
243         console.assert(event.target === this._breakpoint);
244
245         // The Breakpoint has a new display SourceCode. The sidebar will remove us, and ondetach() will clear listeners.
246         if (event.data.oldDisplaySourceCode === this._breakpoint.displaySourceCode)
247             return;
248
249         this._updateTitles();
250     }
251
252     _statusImageElementMouseDown(event)
253     {
254         // To prevent the tree element from selecting.
255         event.stopPropagation();
256     }
257
258     _statusImageElementClicked(event)
259     {
260         this._breakpoint.cycleToNextMode();
261     }
262 };
263
264 WebInspector.BreakpointTreeElement.GenericLineIconStyleClassName = "breakpoint-generic-line-icon";
265 WebInspector.BreakpointTreeElement.StatusImageElementStyleClassName = "status-image";
266 WebInspector.BreakpointTreeElement.StatusImageResolvedStyleClassName = "resolved";
267 WebInspector.BreakpointTreeElement.StatusImageAutoContinueStyleClassName = "auto-continue";
268 WebInspector.BreakpointTreeElement.StatusImageDisabledStyleClassName = "disabled";
269 WebInspector.BreakpointTreeElement.FormattedLocationStyleClassName = "formatted-location";
270 WebInspector.BreakpointTreeElement.ProbeDataUpdatedStyleClassName = "data-updated";
271
272 WebInspector.BreakpointTreeElement.ProbeDataUpdatedAnimationDuration = 400; // milliseconds