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