597fd28564178fb82ed530ea83b3deb14101440d
[WebKit-https.git] / WebCore / inspector / front-end / ScriptView.js
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
26 WebInspector.ScriptView = function(script)
27 {
28     WebInspector.View.call(this);
29
30     this.element.addStyleClass("script-view");
31
32     this.script = script;
33
34     this._frameNeedsSetup = true;
35     this._sourceFrameSetup = false;
36     var canEditScripts = WebInspector.panels.scripts.canEditScripts();
37     this.sourceFrame = new WebInspector.SourceFrame(this.element, this._addBreakpoint.bind(this), this._removeBreakpoint.bind(this), canEditScripts ? this._editLine.bind(this) : null, this._continueToLine.bind(this));
38 }
39
40 WebInspector.ScriptView.prototype = {
41     show: function(parentElement)
42     {
43         WebInspector.View.prototype.show.call(this, parentElement);
44         this.setupSourceFrameIfNeeded();
45         this.sourceFrame.visible = true;
46         this.resize();
47     },
48
49     setupSourceFrameIfNeeded: function()
50     {
51         if (!this._frameNeedsSetup)
52             return;
53         delete this._frameNeedsSetup;
54
55         this.attach();
56
57         if (this.script.source)
58             this._sourceFrameSetupFinished();
59         else {
60             var callbackId = WebInspector.Callback.wrap(this._didGetScriptSource.bind(this))
61             InspectorBackend.getScriptSource(callbackId, this.script.sourceID);
62         }
63     },
64
65     _didGetScriptSource: function(source)
66     {
67         this.script.source = source || WebInspector.UIString("<source is not available>");
68         this._sourceFrameSetupFinished();
69     },
70
71     _sourceFrameSetupFinished: function()
72     {
73         this.sourceFrame.setContent("text/javascript", this._prependWhitespace(this.script.source));
74         this._sourceFrameSetup = true;
75     },
76
77     _prependWhitespace: function(content) {
78         var prefix = "";
79         for (var i = 0; i < this.script.startingLine - 1; ++i)
80             prefix += "\n";
81         return prefix + content;
82     },
83
84     attach: function()
85     {
86         if (!this.element.parentNode)
87             document.getElementById("script-resource-views").appendChild(this.element);
88     },
89
90     _continueToLine: function(line)
91     {
92         var scriptsPanel = WebInspector.panels.scripts;
93         if (scriptsPanel)
94             scriptsPanel.continueToLine(this.script.sourceID, line);
95     },
96
97     _addBreakpoint: function(line)
98     {
99         WebInspector.breakpointManager.setBreakpoint(this.script.sourceID, this.script.sourceURL, line, true, "");
100         if (!WebInspector.panels.scripts.breakpointsActivated)
101             WebInspector.panels.scripts.toggleBreakpointsClicked();
102     },
103
104     _editLineComplete: function(newBody)
105     {
106         this.script.source = newBody;
107         this.sourceFrame.updateContent(this._prependWhitespace(newBody));
108     },
109
110     _sourceIDForLine: function(line)
111     {
112         return this.script.sourceID;
113     },
114
115     // The following methods are pulled from SourceView, since they are
116     // generic and work with ScriptView just fine.
117
118     hide: WebInspector.SourceView.prototype.hide,
119     revealLine: WebInspector.SourceView.prototype.revealLine,
120     highlightLine: WebInspector.SourceView.prototype.highlightLine,
121     addMessage: WebInspector.SourceView.prototype.addMessage,
122     clearMessages: WebInspector.SourceView.prototype.clearMessages,
123     searchCanceled: WebInspector.SourceView.prototype.searchCanceled,
124     performSearch: WebInspector.SourceView.prototype.performSearch,
125     jumpToFirstSearchResult: WebInspector.SourceView.prototype.jumpToFirstSearchResult,
126     jumpToLastSearchResult: WebInspector.SourceView.prototype.jumpToLastSearchResult,
127     jumpToNextSearchResult: WebInspector.SourceView.prototype.jumpToNextSearchResult,
128     jumpToPreviousSearchResult: WebInspector.SourceView.prototype.jumpToPreviousSearchResult,
129     showingFirstSearchResult: WebInspector.SourceView.prototype.showingFirstSearchResult,
130     showingLastSearchResult: WebInspector.SourceView.prototype.showingLastSearchResult,
131     _jumpToSearchResult: WebInspector.SourceView.prototype._jumpToSearchResult,
132     _removeBreakpoint: WebInspector.SourceView.prototype._removeBreakpoint,
133     _editLine: WebInspector.SourceView.prototype._editLine,
134     resize: WebInspector.SourceView.prototype.resize
135 }
136
137 WebInspector.ScriptView.prototype.__proto__ = WebInspector.View.prototype;
138
139 WebInspector.didGetScriptSource = WebInspector.Callback.processCallback;