1904b0193dfa8529ce8c3d14e8d11581b3c06c64
[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
99     onenter()
100     {
101         this._toggleBreakpoint();
102         return true;
103     }
104
105     onspace()
106     {
107         this._toggleBreakpoint();
108         return true;
109     }
110
111     populateContextMenu(contextMenu, event)
112     {
113         let breakpoint = this.representedObject;
114         let label = breakpoint.disabled ? WI.UIString("Enable Breakpoint") : WI.UIString("Disable Breakpoint");
115         contextMenu.appendItem(label, this._toggleBreakpoint.bind(this));
116
117         contextMenu.appendItem(WI.UIString("Delete Breakpoint"), () => {
118             WI.domDebuggerManager.removeURLBreakpoint(breakpoint);
119         });
120     }
121
122     // Private
123
124     _statusImageElementClicked(event)
125     {
126         this._toggleBreakpoint();
127     }
128
129     _statusImageElementFocused(event)
130     {
131         // Prevent tree outline focus.
132         event.stopPropagation();
133     }
134
135     _statusImageElementMouseDown(event)
136     {
137         // Prevent tree element selection.
138         event.stopPropagation();
139     }
140
141     _toggleBreakpoint()
142     {
143         this.representedObject.disabled = !this.representedObject.disabled;
144     }
145
146     _updateStatus()
147     {
148         if (!this.status)
149             return;
150
151         this.status.classList.toggle(WI.BreakpointTreeElement.StatusImageDisabledStyleClassName, this.representedObject.disabled);
152         this.status.classList.toggle(WI.BreakpointTreeElement.StatusImageResolvedStyleClassName, WI.debuggerManager.breakpointsEnabled);
153     }
154 };