ae88a7f5ae08330f73de04543b3d3977075d1abd
[WebKit-https.git] / Source / WebInspectorUI / UserInterface / Views / ScriptContentView.js
1 /*
2  * Copyright (C) 2013, 2015 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.ScriptContentView = class ScriptContentView extends WI.ContentView
27 {
28     constructor(script)
29     {
30         console.assert(script instanceof WI.Script, script);
31
32         super(script);
33
34         this.element.classList.add("script");
35
36         // Append a spinner while waiting for _contentWillPopulate.
37         var spinner = new WI.IndeterminateProgressSpinner;
38         this.element.appendChild(spinner.element);
39
40         this._script = script;
41
42         // This view is only for standalone Scripts with no corresponding Resource. All other Scripts
43         // should be handled by TextResourceContentView via the Resource.
44         console.assert(!script.resource);
45         console.assert(script.range.startLine === 0);
46         console.assert(script.range.startColumn === 0);
47
48         var toolTip = WI.UIString("Pretty print");
49         var activatedToolTip = WI.UIString("Original formatting");
50         this._prettyPrintButtonNavigationItem = new WI.ActivateButtonNavigationItem("pretty-print", toolTip, activatedToolTip, "Images/NavigationItemCurleyBraces.svg", 13, 13);
51         this._prettyPrintButtonNavigationItem.addEventListener(WI.ButtonNavigationItem.Event.Clicked, this._togglePrettyPrint, this);
52         this._prettyPrintButtonNavigationItem.enabled = false; // Enabled when the text editor is populated with content.
53         this._prettyPrintButtonNavigationItem.visibilityPriority = WI.NavigationItem.VisibilityPriority.Low;
54
55         var toolTipTypes = WI.UIString("Show type information");
56         var activatedToolTipTypes = WI.UIString("Hide type information");
57         this._showTypesButtonNavigationItem = new WI.ActivateButtonNavigationItem("show-types", toolTipTypes, activatedToolTipTypes, "Images/NavigationItemTypes.svg", 13, 14);
58         this._showTypesButtonNavigationItem.addEventListener(WI.ButtonNavigationItem.Event.Clicked, this._toggleTypeAnnotations, this);
59         this._showTypesButtonNavigationItem.enabled = false;
60         this._showTypesButtonNavigationItem.visibilityPriority = WI.NavigationItem.VisibilityPriority.Low;
61
62         WI.showJavaScriptTypeInformationSetting.addEventListener(WI.Setting.Event.Changed, this._showJavaScriptTypeInformationSettingChanged, this);
63
64         let toolTipCodeCoverage = WI.UIString("Fade unexecuted code");
65         let activatedToolTipCodeCoverage = WI.UIString("Do not fade unexecuted code");
66         this._codeCoverageButtonNavigationItem = new WI.ActivateButtonNavigationItem("code-coverage", toolTipCodeCoverage, activatedToolTipCodeCoverage, "Images/NavigationItemCodeCoverage.svg", 13, 14);
67         this._codeCoverageButtonNavigationItem.addEventListener(WI.ButtonNavigationItem.Event.Clicked, this._toggleUnexecutedCodeHighlights, this);
68         this._codeCoverageButtonNavigationItem.enabled = false;
69         this._codeCoverageButtonNavigationItem.visibilityPriority = WI.NavigationItem.VisibilityPriority.Low;
70
71         WI.enableControlFlowProfilerSetting.addEventListener(WI.Setting.Event.Changed, this._enableControlFlowProfilerSettingChanged, this);
72
73         this._textEditor = new WI.SourceCodeTextEditor(script);
74         this._textEditor.addEventListener(WI.TextEditor.Event.ExecutionLineNumberDidChange, this._executionLineNumberDidChange, this);
75         this._textEditor.addEventListener(WI.TextEditor.Event.NumberOfSearchResultsDidChange, this._numberOfSearchResultsDidChange, this);
76         this._textEditor.addEventListener(WI.TextEditor.Event.FormattingDidChange, this._textEditorFormattingDidChange, this);
77         this._textEditor.addEventListener(WI.TextEditor.Event.MIMETypeChanged, this._handleTextEditorMIMETypeChanged, this);
78         this._textEditor.addEventListener(WI.SourceCodeTextEditor.Event.ContentWillPopulate, this._contentWillPopulate, this);
79         this._textEditor.addEventListener(WI.SourceCodeTextEditor.Event.ContentDidPopulate, this._contentDidPopulate, this);
80     }
81
82     // Public
83
84     get navigationItems()
85     {
86         return [this._prettyPrintButtonNavigationItem, this._showTypesButtonNavigationItem, this._codeCoverageButtonNavigationItem];
87     }
88
89     get script()
90     {
91         return this._script;
92     }
93
94     get textEditor()
95     {
96         return this._textEditor;
97     }
98
99     get supplementalRepresentedObjects()
100     {
101         if (isNaN(this._textEditor.executionLineNumber))
102             return [];
103
104         // If the SourceCodeTextEditor has an executionLineNumber, we can assume
105         // it is always the active call frame.
106         return [WI.debuggerManager.activeCallFrame];
107     }
108
109     revealPosition(position, textRangeToSelect, forceUnformatted)
110     {
111         this._textEditor.revealPosition(position, textRangeToSelect, forceUnformatted);
112     }
113
114     shown()
115     {
116         super.shown();
117
118         this._textEditor.shown();
119     }
120
121     hidden()
122     {
123         super.hidden();
124
125         this._textEditor.hidden();
126     }
127
128     closed()
129     {
130         super.closed();
131
132         WI.showJavaScriptTypeInformationSetting.removeEventListener(null, null, this);
133         WI.enableControlFlowProfilerSetting.removeEventListener(null, null, this);
134
135         this._textEditor.close();
136     }
137
138     saveToCookie(cookie)
139     {
140         cookie.type = WI.ContentViewCookieType.Resource;
141         cookie.url = this.representedObject.url;
142     }
143
144     restoreFromCookie(cookie)
145     {
146         if ("lineNumber" in cookie && "columnNumber" in cookie)
147             this.revealPosition(new WI.SourceCodePosition(cookie.lineNumber, cookie.columnNumber));
148     }
149
150     get supportsSave()
151     {
152         return true;
153     }
154
155     get saveData()
156     {
157         var url = this._script.url || "web-inspector:///" + encodeURI(this._script.displayName) + ".js";
158         return {url, content: this._textEditor.string};
159     }
160
161     get supportsSearch()
162     {
163         return true;
164     }
165
166     get numberOfSearchResults()
167     {
168         return this._textEditor.numberOfSearchResults;
169     }
170
171     get hasPerformedSearch()
172     {
173         return this._textEditor.currentSearchQuery !== null;
174     }
175
176     set automaticallyRevealFirstSearchResult(reveal)
177     {
178         this._textEditor.automaticallyRevealFirstSearchResult = reveal;
179     }
180
181     performSearch(query)
182     {
183         this._textEditor.performSearch(query);
184     }
185
186     searchCleared()
187     {
188         this._textEditor.searchCleared();
189     }
190
191     searchQueryWithSelection()
192     {
193         return this._textEditor.searchQueryWithSelection();
194     }
195
196     revealPreviousSearchResult(changeFocus)
197     {
198         this._textEditor.revealPreviousSearchResult(changeFocus);
199     }
200
201     revealNextSearchResult(changeFocus)
202     {
203         this._textEditor.revealNextSearchResult(changeFocus);
204     }
205
206     // Private
207
208     _contentWillPopulate(event)
209     {
210         if (this._textEditor.element.parentNode === this.element)
211             return;
212
213         // Allow editing any local file since edits can be saved and reloaded right from the Inspector.
214         if (this._script.urlComponents.scheme === "file")
215             this._textEditor.readOnly = false;
216
217         this.element.removeChildren();
218         this.addSubview(this._textEditor);
219     }
220
221     _contentDidPopulate(event)
222     {
223         this._prettyPrintButtonNavigationItem.enabled = this._textEditor.canBeFormatted();
224
225         this._showTypesButtonNavigationItem.enabled = this._textEditor.canShowTypeAnnotations();
226         this._showTypesButtonNavigationItem.activated = WI.showJavaScriptTypeInformationSetting.value;
227
228         this._codeCoverageButtonNavigationItem.enabled = this._textEditor.canShowCoverageHints();
229         this._codeCoverageButtonNavigationItem.activated = WI.enableControlFlowProfilerSetting.value;
230     }
231
232     _togglePrettyPrint(event)
233     {
234         var activated = !this._prettyPrintButtonNavigationItem.activated;
235         this._textEditor.updateFormattedState(activated);
236     }
237
238     _toggleTypeAnnotations(event)
239     {
240         this._showTypesButtonNavigationItem.enabled = false;
241         this._textEditor.toggleTypeAnnotations().then(() => {
242             this._showTypesButtonNavigationItem.enabled = true;
243         });
244     }
245
246     _toggleUnexecutedCodeHighlights(event)
247     {
248         this._codeCoverageButtonNavigationItem.enabled = false;
249         this._textEditor.toggleUnexecutedCodeHighlights().then(() => {
250             this._codeCoverageButtonNavigationItem.enabled = true;
251         });
252     }
253
254     _showJavaScriptTypeInformationSettingChanged(event)
255     {
256         this._showTypesButtonNavigationItem.activated = WI.showJavaScriptTypeInformationSetting.value;
257     }
258
259     _enableControlFlowProfilerSettingChanged(event)
260     {
261         this._codeCoverageButtonNavigationItem.activated = WI.enableControlFlowProfilerSetting.value;
262     }
263
264     _textEditorFormattingDidChange(event)
265     {
266         this._prettyPrintButtonNavigationItem.activated = this._textEditor.formatted;
267     }
268
269     _handleTextEditorMIMETypeChanged(event)
270     {
271         this._prettyPrintButtonNavigationItem.enabled = this._textEditor.canBeFormatted();
272     }
273
274     _executionLineNumberDidChange(event)
275     {
276         this.dispatchEventToListeners(WI.ContentView.Event.SupplementalRepresentedObjectsDidChange);
277     }
278
279     _numberOfSearchResultsDidChange(event)
280     {
281         this.dispatchEventToListeners(WI.ContentView.Event.NumberOfSearchResultsDidChange);
282     }
283 };