1 // Copyright (c) 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 * @extends {WebInspector.SidebarPane}
8 * @param {!WebInspector.BreakpointManager} breakpointManager
9 * @param {function(!WebInspector.UISourceCode, number=, number=, boolean=)} showSourceLineDelegate
11 WebInspector
.JavaScriptBreakpointsSidebarPane = function(breakpointManager
, showSourceLineDelegate
)
13 WebInspector
.SidebarPane
.call(this, WebInspector
.UIString("Breakpoints"));
14 this.registerRequiredCSS("components/breakpointsList.css");
16 this._breakpointManager
= breakpointManager
;
17 this._showSourceLineDelegate
= showSourceLineDelegate
;
19 this.listElement
= createElementWithClass("ol", "breakpoint-list");
21 this.emptyElement
= this.element
.createChild("div", "info");
22 this.emptyElement
.textContent
= WebInspector
.UIString("No Breakpoints");
24 this._items
= new Map();
26 var breakpointLocations
= this._breakpointManager
.allBreakpointLocations();
27 for (var i
= 0; i
< breakpointLocations
.length
; ++i
)
28 this._addBreakpoint(breakpointLocations
[i
].breakpoint
, breakpointLocations
[i
].uiLocation
);
30 this._breakpointManager
.addEventListener(WebInspector
.BreakpointManager
.Events
.BreakpointAdded
, this._breakpointAdded
, this);
31 this._breakpointManager
.addEventListener(WebInspector
.BreakpointManager
.Events
.BreakpointRemoved
, this._breakpointRemoved
, this);
33 this.emptyElement
.addEventListener("contextmenu", this._emptyElementContextMenu
.bind(this), true);
36 WebInspector
.JavaScriptBreakpointsSidebarPane
.prototype = {
37 _emptyElementContextMenu: function(event
)
39 var contextMenu
= new WebInspector
.ContextMenu(event
);
40 this._appendBreakpointActiveItem(contextMenu
);
45 * @param {!WebInspector.ContextMenu} contextMenu
47 _appendBreakpointActiveItem: function(contextMenu
)
49 var breakpointActive
= this._breakpointManager
.breakpointsActive();
50 var breakpointActiveTitle
= breakpointActive
?
51 WebInspector
.UIString
.capitalize("Deactivate ^breakpoints") :
52 WebInspector
.UIString
.capitalize("Activate ^breakpoints");
53 contextMenu
.appendItem(breakpointActiveTitle
, this._breakpointManager
.setBreakpointsActive
.bind(this._breakpointManager
, !breakpointActive
));
57 * @param {!WebInspector.Event} event
59 _breakpointAdded: function(event
)
61 this._breakpointRemoved(event
);
63 var breakpoint
= /** @type {!WebInspector.BreakpointManager.Breakpoint} */ (event
.data
.breakpoint
);
64 var uiLocation
= /** @type {!WebInspector.UILocation} */ (event
.data
.uiLocation
);
65 this._addBreakpoint(breakpoint
, uiLocation
);
69 * @param {!WebInspector.BreakpointManager.Breakpoint} breakpoint
70 * @param {!WebInspector.UILocation} uiLocation
72 _addBreakpoint: function(breakpoint
, uiLocation
)
74 var element
= createElementWithClass("li", "cursor-pointer");
75 element
.addEventListener("contextmenu", this._breakpointContextMenu
.bind(this, breakpoint
), true);
76 element
.addEventListener("click", this._breakpointClicked
.bind(this, uiLocation
), false);
78 var checkboxLabel
= createCheckboxLabel(uiLocation
.linkText(), breakpoint
.enabled());
79 element
.appendChild(checkboxLabel
);
80 checkboxLabel
.addEventListener("click", this._breakpointCheckboxClicked
.bind(this, breakpoint
), false);
82 var snippetElement
= element
.createChild("div", "source-text monospace");
85 * @param {?string} content
86 * @this {WebInspector.JavaScriptBreakpointsSidebarPane}
88 function didRequestContent(content
)
90 var lineNumber
= uiLocation
.lineNumber
91 var columnNumber
= uiLocation
.columnNumber
;
92 var contentString
= new String(content
);
93 if (lineNumber
< contentString
.lineCount()) {
94 var lineText
= contentString
.lineAt(lineNumber
);
95 var maxSnippetLength
= 200;
96 var snippetStartIndex
= columnNumber
> 100 ? columnNumber
: 0;
97 snippetElement
.textContent
= lineText
.substr(snippetStartIndex
).trimEnd(maxSnippetLength
);
99 this.didReceiveBreakpointLineForTest(uiLocation
.uiSourceCode
);
102 uiLocation
.uiSourceCode
.requestContent(didRequestContent
.bind(this));
104 element
._data
= uiLocation
;
105 var currentElement
= this.listElement
.firstChild
;
106 while (currentElement
) {
107 if (currentElement
._data
&& this._compareBreakpoints(currentElement
._data
, element
._data
) > 0)
109 currentElement
= currentElement
.nextSibling
;
111 this._addListElement(element
, currentElement
);
113 var breakpointItem
= { element
: element
, checkbox
: checkboxLabel
.checkboxElement
};
114 this._items
.set(breakpoint
, breakpointItem
);
120 * @param {!WebInspector.UISourceCode} uiSourceCode
122 didReceiveBreakpointLineForTest: function(uiSourceCode
)
127 * @param {!WebInspector.Event} event
129 _breakpointRemoved: function(event
)
131 var breakpoint
= /** @type {!WebInspector.BreakpointManager.Breakpoint} */ (event
.data
.breakpoint
);
132 var breakpointItem
= this._items
.get(breakpoint
);
135 this._items
.remove(breakpoint
);
136 this._removeListElement(breakpointItem
.element
);
140 * @param {!WebInspector.BreakpointManager.Breakpoint} breakpoint
142 highlightBreakpoint: function(breakpoint
)
144 var breakpointItem
= this._items
.get(breakpoint
);
147 breakpointItem
.element
.classList
.add("breakpoint-hit");
148 this._highlightedBreakpointItem
= breakpointItem
;
151 clearBreakpointHighlight: function()
153 if (this._highlightedBreakpointItem
) {
154 this._highlightedBreakpointItem
.element
.classList
.remove("breakpoint-hit");
155 delete this._highlightedBreakpointItem
;
159 _breakpointClicked: function(uiLocation
, event
)
161 this._showSourceLineDelegate(uiLocation
.uiSourceCode
, uiLocation
.lineNumber
);
165 * @param {!WebInspector.BreakpointManager.Breakpoint} breakpoint
166 * @param {!Event} event
168 _breakpointCheckboxClicked: function(breakpoint
, event
)
170 // Breakpoint element has it's own click handler.
172 breakpoint
.setEnabled(event
.target
.checkboxElement
.checked
);
176 * @param {!WebInspector.BreakpointManager.Breakpoint} breakpoint
177 * @param {!Event} event
179 _breakpointContextMenu: function(breakpoint
, event
)
181 var breakpoints
= this._items
.valuesArray();
182 var contextMenu
= new WebInspector
.ContextMenu(event
);
183 contextMenu
.appendItem(WebInspector
.UIString
.capitalize("Remove ^breakpoint"), breakpoint
.remove
.bind(breakpoint
));
184 if (breakpoints
.length
> 1) {
185 var removeAllTitle
= WebInspector
.UIString
.capitalize("Remove ^all ^breakpoints");
186 contextMenu
.appendItem(removeAllTitle
, this._breakpointManager
.removeAllBreakpoints
.bind(this._breakpointManager
));
189 contextMenu
.appendSeparator();
190 this._appendBreakpointActiveItem(contextMenu
);
192 function enabledBreakpointCount(breakpoints
)
195 for (var i
= 0; i
< breakpoints
.length
; ++i
) {
196 if (breakpoints
[i
].checkbox
.checked
)
201 if (breakpoints
.length
> 1) {
202 var enableBreakpointCount
= enabledBreakpointCount(breakpoints
);
203 var enableTitle
= WebInspector
.UIString
.capitalize("Enable ^all ^breakpoints");
204 var disableTitle
= WebInspector
.UIString
.capitalize("Disable ^all ^breakpoints");
206 contextMenu
.appendSeparator();
208 contextMenu
.appendItem(enableTitle
, this._breakpointManager
.toggleAllBreakpoints
.bind(this._breakpointManager
, true), !(enableBreakpointCount
!= breakpoints
.length
));
209 contextMenu
.appendItem(disableTitle
, this._breakpointManager
.toggleAllBreakpoints
.bind(this._breakpointManager
, false), !(enableBreakpointCount
> 1));
215 _addListElement: function(element
, beforeElement
)
218 this.listElement
.insertBefore(element
, beforeElement
);
220 if (!this.listElement
.firstChild
) {
221 this.element
.removeChild(this.emptyElement
);
222 this.element
.appendChild(this.listElement
);
224 this.listElement
.appendChild(element
);
228 _removeListElement: function(element
)
230 this.listElement
.removeChild(element
);
231 if (!this.listElement
.firstChild
) {
232 this.element
.removeChild(this.listElement
);
233 this.element
.appendChild(this.emptyElement
);
237 _compare: function(x
, y
)
240 return x
< y
? -1 : 1;
244 _compareBreakpoints: function(b1
, b2
)
246 return this._compare(b1
.uiSourceCode
.originURL(), b2
.uiSourceCode
.originURL()) || this._compare(b1
.lineNumber
, b2
.lineNumber
);
251 this.listElement
.removeChildren();
252 if (this.listElement
.parentElement
) {
253 this.element
.removeChild(this.listElement
);
254 this.element
.appendChild(this.emptyElement
);
259 __proto__
: WebInspector
.SidebarPane
.prototype