Web Inspector: REGRESSION (r248873): Debugger: pressing delete on a breakpoint will...
[WebKit-https.git] / Source / WebInspectorUI / UserInterface / Views / URLBreakpointTreeElement.js
1 /*
2  * Copyright (C) 2019 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.URLBreakpointTreeElement = class URLBreakpointTreeElement extends WI.GeneralTreeElement
27 {
28     constructor(breakpoint, {className, title} = {})
29     {
30         console.assert(breakpoint instanceof WI.URLBreakpoint);
31
32         let classNames = ["breakpoint", "url"];
33         if (className)
34             classNames.push(className);
35
36         let subtitle;
37         if (!title) {
38             title = WI.UIString("URL");
39             if (breakpoint.type === WI.URLBreakpoint.Type.Text)
40                 subtitle = doubleQuotedString(breakpoint.url);
41             else
42                 subtitle = "/" + breakpoint.url + "/";
43         }
44
45         super(classNames, title, subtitle, breakpoint);
46
47         this.status = WI.ImageUtilities.useSVGSymbol("Images/Breakpoint.svg");
48         this.status.className = WI.BreakpointTreeElement.StatusImageElementStyleClassName;
49
50         this.tooltipHandledSeparately = true;
51     }
52
53     // Protected
54
55     onattach()
56     {
57         super.onattach();
58
59         this.representedObject.addEventListener(WI.URLBreakpoint.Event.DisabledStateChanged, this._updateStatus, this);
60         WI.debuggerManager.addEventListener(WI.DebuggerManager.Event.BreakpointsEnabledDidChange, this._updateStatus, this);
61
62         this._boundStatusImageElementClicked = this._statusImageElementClicked.bind(this);
63         this._boundStatusImageElementFocused = this._statusImageElementFocused.bind(this);
64         this._boundStatusImageElementMouseDown = this._statusImageElementMouseDown.bind(this);
65
66         this.status.addEventListener("click", this._boundStatusImageElementClicked);
67         this.status.addEventListener("focus", this._boundStatusImageElementFocused);
68         this.status.addEventListener("mousedown", this._boundStatusImageElementMouseDown);
69
70         this._updateStatus();
71     }
72
73     ondetach()
74     {
75         super.ondetach();
76
77         this.representedObject.removeEventListener(null, null, this);
78         WI.debuggerManager.removeEventListener(null, null, this);
79
80         this.status.removeEventListener("click", this._boundStatusImageElementClicked);
81         this.status.removeEventListener("focus", this._boundStatusImageElementFocused);
82         this.status.removeEventListener("mousedown", this._boundStatusImageElementMouseDown);
83
84         this._boundStatusImageElementClicked = null;
85         this._boundStatusImageElementFocused = null;
86         this._boundStatusImageElementMouseDown = null;
87     }
88
89     ondelete()
90     {
91         // We set this flag so that TreeOutlines that will remove this
92         // BreakpointTreeElement will know whether it was deleted from
93         // within the TreeOutline or from outside it (e.g. TextEditor).
94         this.__deletedViaDeleteKeyboardShortcut = true;
95
96         WI.domDebuggerManager.removeURLBreakpoint(this.representedObject);
97
98         return true;
99     }
100
101     onenter()
102     {
103         this._toggleBreakpoint();
104         return true;
105     }
106
107     onspace()
108     {
109         this._toggleBreakpoint();
110         return true;
111     }
112
113     populateContextMenu(contextMenu, event)
114     {
115         let breakpoint = this.representedObject;
116         let label = breakpoint.disabled ? WI.UIString("Enable Breakpoint") : WI.UIString("Disable Breakpoint");
117         contextMenu.appendItem(label, this._toggleBreakpoint.bind(this));
118
119         contextMenu.appendItem(WI.UIString("Delete Breakpoint"), () => {
120             WI.domDebuggerManager.removeURLBreakpoint(breakpoint);
121         });
122     }
123
124     // Private
125
126     _statusImageElementClicked(event)
127     {
128         this._toggleBreakpoint();
129     }
130
131     _statusImageElementFocused(event)
132     {
133         // Prevent tree outline focus.
134         event.stopPropagation();
135     }
136
137     _statusImageElementMouseDown(event)
138     {
139         // Prevent tree element selection.
140         event.stopPropagation();
141     }
142
143     _toggleBreakpoint()
144     {
145         this.representedObject.disabled = !this.representedObject.disabled;
146     }
147
148     _updateStatus()
149     {
150         if (!this.status)
151             return;
152
153         this.status.classList.toggle(WI.BreakpointTreeElement.StatusImageDisabledStyleClassName, this.representedObject.disabled);
154         this.status.classList.toggle(WI.BreakpointTreeElement.StatusImageResolvedStyleClassName, WI.debuggerManager.breakpointsEnabled);
155     }
156 };