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