37bc50381eb9fb84c6b6acec7d831975f8f44981
[WebKit-https.git] / Source / WebInspectorUI / UserInterface / Views / TextContentView.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.TextContentView = class TextContentView extends WI.ContentView
27 {
28     constructor(string, mimeType)
29     {
30         super(string);
31
32         this.element.classList.add("text");
33
34         this._textEditor = new WI.TextEditor;
35         this._textEditor.addEventListener(WI.TextEditor.Event.NumberOfSearchResultsDidChange, this._numberOfSearchResultsDidChange, this);
36         this._textEditor.addEventListener(WI.TextEditor.Event.FormattingDidChange, this._textEditorFormattingDidChange, this);
37         this._textEditor.addEventListener(WI.TextEditor.Event.MIMETypeChanged, this._handleTextEditorMIMETypeChanged, this);
38         this.addSubview(this._textEditor);
39
40         var toolTip = WI.UIString("Pretty print");
41         var activatedToolTip = WI.UIString("Original formatting");
42         this._prettyPrintButtonNavigationItem = new WI.ActivateButtonNavigationItem("pretty-print", toolTip, activatedToolTip, "Images/NavigationItemCurleyBraces.svg", 13, 13);
43         this._prettyPrintButtonNavigationItem.addEventListener(WI.ButtonNavigationItem.Event.Clicked, this._togglePrettyPrint, this);
44         this._prettyPrintButtonNavigationItem.visibilityPriority = WI.NavigationItem.VisibilityPriority.Low;
45
46         var toolTipTypes = WI.UIString("Show type information");
47         var activatedToolTipTypes = WI.UIString("Hide type information");
48         this._showTypesButtonNavigationItem = new WI.ActivateButtonNavigationItem("show-types", toolTipTypes, activatedToolTipTypes, "Images/NavigationItemTypes.svg", 13, 14);
49         this._showTypesButtonNavigationItem.enabled = false;
50         this._showTypesButtonNavigationItem.visibilityPriority = WI.NavigationItem.VisibilityPriority.Low;
51
52         let toolTipCodeCoverage = WI.UIString("Fade unexecuted code");
53         let activatedToolTipCodeCoverage = WI.UIString("Do not fade unexecuted code");
54         this._codeCoverageButtonNavigationItem = new WI.ActivateButtonNavigationItem("code-coverage", toolTipCodeCoverage, activatedToolTipCodeCoverage, "Images/NavigationItemCodeCoverage.svg", 13, 14);
55         this._codeCoverageButtonNavigationItem.enabled = false;
56         this._codeCoverageButtonNavigationItem.visibilityPriority = WI.NavigationItem.VisibilityPriority.Low;
57
58         this._textEditor.readOnly = true;
59         this._textEditor.mimeType = mimeType;
60         this._textEditor.string = string;
61         this._prettyPrintButtonNavigationItem.enabled = this._textEditor.canBeFormatted();
62     }
63
64     // Public
65
66     get textEditor()
67     {
68         return this._textEditor;
69     }
70
71     get navigationItems()
72     {
73         return [this._prettyPrintButtonNavigationItem, this._showTypesButtonNavigationItem, this._codeCoverageButtonNavigationItem];
74     }
75
76     revealPosition(position, textRangeToSelect, forceUnformatted)
77     {
78         this._textEditor.revealPosition(position, textRangeToSelect, forceUnformatted);
79     }
80
81     shown()
82     {
83         super.shown();
84
85         this._textEditor.shown();
86     }
87
88     hidden()
89     {
90         super.hidden();
91
92         this._textEditor.hidden();
93     }
94
95     get supportsSave()
96     {
97         return true;
98     }
99
100     get saveData()
101     {
102         let url = WI.FileUtilities.inspectorURLForFilename(WI.UIString("Untitled") + ".txt");
103         return {url, content: this._textEditor.string, forceSaveAs: true};
104     }
105
106     get supportsSearch()
107     {
108         return true;
109     }
110
111     get numberOfSearchResults()
112     {
113         return this._textEditor.numberOfSearchResults;
114     }
115
116     get hasPerformedSearch()
117     {
118         return this._textEditor.currentSearchQuery !== null;
119     }
120
121     set automaticallyRevealFirstSearchResult(reveal)
122     {
123         this._textEditor.automaticallyRevealFirstSearchResult = reveal;
124     }
125
126     performSearch(query)
127     {
128         this._textEditor.performSearch(query);
129     }
130
131     searchCleared()
132     {
133         this._textEditor.searchCleared();
134     }
135
136     searchQueryWithSelection()
137     {
138         return this._textEditor.searchQueryWithSelection();
139     }
140
141     revealPreviousSearchResult(changeFocus)
142     {
143         this._textEditor.revealPreviousSearchResult(changeFocus);
144     }
145
146     revealNextSearchResult(changeFocus)
147     {
148         this._textEditor.revealNextSearchResult(changeFocus);
149     }
150
151     // Private
152
153     _togglePrettyPrint(event)
154     {
155         var activated = !this._prettyPrintButtonNavigationItem.activated;
156         this._textEditor.updateFormattedState(activated);
157     }
158
159     _textEditorFormattingDidChange(event)
160     {
161         this._prettyPrintButtonNavigationItem.activated = this._textEditor.formatted;
162     }
163
164     _handleTextEditorMIMETypeChanged(event)
165     {
166         this._prettyPrintButtonNavigationItem.enabled = this._textEditor.canBeFormatted();
167     }
168
169     _numberOfSearchResultsDidChange(event)
170     {
171         this.dispatchEventToListeners(WI.ContentView.Event.NumberOfSearchResultsDidChange);
172     }
173 };