1/*
2 * Copyright (C) 2008 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. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26WebInspector.BreakpointsSidebarPane = function()
27{
28    WebInspector.SidebarPane.call(this, WebInspector.UIString("Breakpoints"));
29
30    this.breakpoints = {};
31
32    this.listElement = document.createElement("ol");
33    this.listElement.className = "breakpoint-list";
34
35    this.emptyElement = document.createElement("div");
36    this.emptyElement.className = "info";
37    this.emptyElement.textContent = WebInspector.UIString("No Breakpoints");
38
39    this.bodyElement.appendChild(this.emptyElement);
40}
41
42WebInspector.BreakpointsSidebarPane.prototype = {
43    addBreakpoint: function(breakpoint)
44    {
45        if (this.breakpoints[breakpoint.id])
46            return;
47
48        this.breakpoints[breakpoint.id] = breakpoint;
49
50        breakpoint.addEventListener("enabled", this._breakpointEnableChanged, this);
51        breakpoint.addEventListener("disabled", this._breakpointEnableChanged, this);
52        breakpoint.addEventListener("text-changed", this._breakpointTextChanged, this);
53
54        this._appendBreakpointElement(breakpoint);
55
56        if (this.emptyElement.parentElement) {
57            this.bodyElement.removeChild(this.emptyElement);
58            this.bodyElement.appendChild(this.listElement);
59        }
60
61        if (!InspectorBackend.debuggerEnabled() || !breakpoint.sourceID)
62            return;
63
64        if (breakpoint.enabled)
65            InspectorBackend.addBreakpoint(breakpoint.sourceID, breakpoint.line, breakpoint.condition);
66    },
67
68    _appendBreakpointElement: function(breakpoint)
69    {
70        function checkboxClicked(event)
71        {
72            breakpoint.enabled = !breakpoint.enabled;
73
74            // without this, we'd switch to the source of the clicked breakpoint
75            event.stopPropagation();
76        }
77
78        function breakpointClicked()
79        {
80            var script = WebInspector.panels.scripts.scriptOrResourceForID(breakpoint.sourceID);
81            if (script)
82                WebInspector.panels.scripts.showScript(script, breakpoint.line);
83        }
84
85        var breakpointElement = document.createElement("li");
86        breakpoint._breakpointListElement = breakpointElement;
87        breakpointElement._breakpointObject = breakpoint;
88        breakpointElement.addEventListener("click", breakpointClicked, false);
89
90        var checkboxElement = document.createElement("input");
91        checkboxElement.className = "checkbox-elem";
92        checkboxElement.type = "checkbox";
93        checkboxElement.checked = breakpoint.enabled;
94        checkboxElement.addEventListener("click", checkboxClicked, false);
95        breakpointElement.appendChild(checkboxElement);
96
97        var labelElement = document.createTextNode(breakpoint.label);
98        breakpointElement.appendChild(labelElement);
99
100        var sourceTextElement = document.createElement("div");
101        sourceTextElement.textContent = breakpoint.sourceText;
102        sourceTextElement.className = "source-text monospace";
103        breakpointElement.appendChild(sourceTextElement);
104
105        var currentElement = this.listElement.firstChild;
106        while (currentElement) {
107            var currentBreak = currentElement._breakpointObject;
108            if (currentBreak.url > breakpoint.url) {
109                this.listElement.insertBefore(breakpointElement, currentElement);
110                return;
111            } else if (currentBreak.url == breakpoint.url && currentBreak.line > breakpoint.line) {
112                this.listElement.insertBefore(breakpointElement, currentElement);
113                return;
114            }
115            currentElement = currentElement.nextSibling;
116        }
117        this.listElement.appendChild(breakpointElement);
118    },
119
120    removeBreakpoint: function(breakpoint)
121    {
122        if (!this.breakpoints[breakpoint.id])
123            return;
124        delete this.breakpoints[breakpoint.id];
125
126        breakpoint.removeEventListener("enabled", null, this);
127        breakpoint.removeEventListener("disabled", null, this);
128        breakpoint.removeEventListener("text-changed", null, this);
129
130        var element = breakpoint._breakpointListElement;
131        element.parentElement.removeChild(element);
132
133        if (!this.listElement.firstChild) {
134            this.bodyElement.removeChild(this.listElement);
135            this.bodyElement.appendChild(this.emptyElement);
136        }
137
138        if (!InspectorBackend.debuggerEnabled() || !breakpoint.sourceID)
139            return;
140
141        InspectorBackend.removeBreakpoint(breakpoint.sourceID, breakpoint.line);
142    },
143
144    _breakpointEnableChanged: function(event)
145    {
146        var breakpoint = event.target;
147
148        var checkbox = breakpoint._breakpointListElement.firstChild;
149        checkbox.checked = breakpoint.enabled;
150
151        if (!InspectorBackend.debuggerEnabled() || !breakpoint.sourceID)
152            return;
153
154        if (breakpoint.enabled)
155            InspectorBackend.addBreakpoint(breakpoint.sourceID, breakpoint.line, breakpoint.condition);
156        else
157            InspectorBackend.removeBreakpoint(breakpoint.sourceID, breakpoint.line);
158    },
159
160    _breakpointTextChanged: function(event)
161    {
162        var breakpoint = event.target;
163
164        var sourceTextElement = breakpoint._breakpointListElement.firstChild.nextSibling.nextSibling;
165        sourceTextElement.textContent = breakpoint.sourceText;
166    }
167}
168
169WebInspector.BreakpointsSidebarPane.prototype.__proto__ = WebInspector.SidebarPane.prototype;
170