2e380de8a97e84e1a1318b8d02dee0fc5fe73a5d
[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         if (!WI.debuggerManager.isBreakpointRemovable(this._breakpoint)) {
81             if (this._breakpoint.disabled)
82                 InspectorFrontendHost.beep();
83             else
84                 this._breakpoint.disabled = true;
85             return;
86         }
87
88         // We set this flag so that TreeOutlines that will remove this
89         // BreakpointTreeElement will know whether it was deleted from
90         // within the TreeOutline or from outside it (e.g. TextEditor).
91         this.__deletedViaDeleteKeyboardShortcut = true;
92
93         WI.debuggerManager.removeBreakpoint(this._breakpoint);
94     }
95
96     onenter()
97     {
98         this._breakpoint.cycleToNextMode();
99         return true;
100     }
101
102     onspace()
103     {
104         this._breakpoint.cycleToNextMode();
105         return true;
106     }
107
108     onattach()
109     {
110         super.onattach();
111
112         this._listenerSet.install();
113
114         for (var probeSet of WI.debuggerManager.probeSets)
115             if (probeSet.breakpoint === this._breakpoint)
116                 this._addProbeSet(probeSet);
117     }
118
119     ondetach()
120     {
121         super.ondetach();
122
123         this._listenerSet.uninstall();
124
125         if (this._probeSet)
126             this._removeProbeSet(this._probeSet);
127     }
128
129     populateContextMenu(contextMenu, event)
130     {
131         WI.breakpointPopoverController.appendContextMenuItems(contextMenu, this._breakpoint, this.status);
132
133         super.populateContextMenu(contextMenu, event);
134     }
135
136     // Private
137
138     _updateTitles()
139     {
140         var sourceCodeLocation = this._breakpoint.sourceCodeLocation;
141
142         var displayLineNumber = sourceCodeLocation.displayLineNumber;
143         var displayColumnNumber = sourceCodeLocation.displayColumnNumber;
144         if (displayColumnNumber > 0)
145             this.mainTitle = WI.UIString("Line %d:%d").format(displayLineNumber + 1, displayColumnNumber + 1); // The user visible line and column numbers are 1-based.
146         else
147             this.mainTitle = WI.UIString("Line %d").format(displayLineNumber + 1); // The user visible line number is 1-based.
148
149         if (sourceCodeLocation.hasMappedLocation()) {
150             this.subtitle = sourceCodeLocation.formattedLocationString();
151
152             if (sourceCodeLocation.hasFormattedLocation())
153                 this.subtitleElement.classList.add(WI.BreakpointTreeElement.FormattedLocationStyleClassName);
154             else
155                 this.subtitleElement.classList.remove(WI.BreakpointTreeElement.FormattedLocationStyleClassName);
156
157             this.tooltip = this.mainTitle + " \u2014 " + WI.UIString("originally %s").format(sourceCodeLocation.originalLocationString());
158         }
159     }
160
161     _updateStatus()
162     {
163         if (!this.status)
164             return;
165
166         this.status.classList.toggle(WI.BreakpointTreeElement.StatusImageDisabledStyleClassName, this._breakpoint.disabled);
167         this.status.classList.toggle(WI.BreakpointTreeElement.StatusImageAutoContinueStyleClassName, this._breakpoint.autoContinue);
168         this.status.classList.toggle(WI.BreakpointTreeElement.StatusImageResolvedStyleClassName, this._breakpoint.resolved && WI.debuggerManager.breakpointsEnabled);
169     }
170
171     _addProbeSet(probeSet)
172     {
173         console.assert(probeSet instanceof WI.ProbeSet);
174         console.assert(probeSet.breakpoint === this._breakpoint);
175         console.assert(probeSet !== this._probeSet);
176
177         this._probeSet = probeSet;
178         probeSet.addEventListener(WI.ProbeSet.Event.SamplesCleared, this._samplesCleared, this);
179         probeSet.dataTable.addEventListener(WI.ProbeSetDataTable.Event.FrameInserted, this._dataUpdated, this);
180     }
181
182     _removeProbeSet(probeSet)
183     {
184         console.assert(probeSet instanceof WI.ProbeSet);
185         console.assert(probeSet === this._probeSet);
186
187         probeSet.removeEventListener(WI.ProbeSet.Event.SamplesCleared, this._samplesCleared, this);
188         probeSet.dataTable.removeEventListener(WI.ProbeSetDataTable.Event.FrameInserted, this._dataUpdated, this);
189         this._probeSet = null;
190     }
191
192     _probeSetAdded(event)
193     {
194         var probeSet = event.data.probeSet;
195         if (probeSet.breakpoint === this._breakpoint)
196             this._addProbeSet(probeSet);
197     }
198
199     _probeSetRemoved(event)
200     {
201         var probeSet = event.data.probeSet;
202         if (probeSet.breakpoint === this._breakpoint)
203             this._removeProbeSet(probeSet);
204     }
205
206     _samplesCleared(event)
207     {
208         console.assert(this._probeSet);
209
210         var oldTable = event.data.oldTable;
211         oldTable.removeEventListener(WI.ProbeSetDataTable.Event.FrameInserted, this._dataUpdated, this);
212         this._probeSet.dataTable.addEventListener(WI.ProbeSetDataTable.Event.FrameInserted, this._dataUpdated, this);
213     }
214
215     _dataUpdated()
216     {
217         if (this.element.classList.contains(WI.BreakpointTreeElement.ProbeDataUpdatedStyleClassName)) {
218             clearTimeout(this._removeIconAnimationTimeoutIdentifier);
219             this.element.classList.remove(WI.BreakpointTreeElement.ProbeDataUpdatedStyleClassName);
220             // We want to restart the animation, which can only be done by removing the class,
221             // performing layout, and re-adding the class. Try adding class back on next run loop.
222             window.requestAnimationFrame(this._dataUpdated.bind(this));
223             return;
224         }
225
226         this.element.classList.add(WI.BreakpointTreeElement.ProbeDataUpdatedStyleClassName);
227         this._removeIconAnimationTimeoutIdentifier = setTimeout(() => {
228             this.element.classList.remove(WI.BreakpointTreeElement.ProbeDataUpdatedStyleClassName);
229         }, WI.BreakpointTreeElement.ProbeDataUpdatedAnimationDuration);
230     }
231
232     _breakpointLocationDidChange(event)
233     {
234         console.assert(event.target === this._breakpoint);
235
236         // The Breakpoint has a new display SourceCode. The sidebar will remove us, and ondetach() will clear listeners.
237         if (event.data.oldDisplaySourceCode === this._breakpoint.displaySourceCode)
238             return;
239
240         this._updateTitles();
241     }
242
243     _statusImageElementMouseDown(event)
244     {
245         // To prevent the tree element from selecting.
246         event.stopPropagation();
247     }
248
249     _statusImageElementClicked(event)
250     {
251         this._breakpoint.cycleToNextMode();
252     }
253 };
254
255 WI.BreakpointTreeElement.GenericLineIconStyleClassName = "breakpoint-generic-line-icon";
256 WI.BreakpointTreeElement.StatusImageElementStyleClassName = "status-image";
257 WI.BreakpointTreeElement.StatusImageResolvedStyleClassName = "resolved";
258 WI.BreakpointTreeElement.StatusImageAutoContinueStyleClassName = "auto-continue";
259 WI.BreakpointTreeElement.StatusImageDisabledStyleClassName = "disabled";
260 WI.BreakpointTreeElement.FormattedLocationStyleClassName = "formatted-location";
261 WI.BreakpointTreeElement.ProbeDataUpdatedStyleClassName = "data-updated";
262
263 WI.BreakpointTreeElement.ProbeDataUpdatedAnimationDuration = 400; // milliseconds