Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / devtools / front_end / UISourceCodeFrame.js
1 /*
2  * Copyright (C) 2012 Google 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 are
6  * met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above
12  * copyright notice, this list of conditions and the following disclaimer
13  * in the documentation and/or other materials provided with the
14  * distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY GOOGLE INC. AND ITS CONTRIBUTORS
17  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GOOGLE INC.
20  * OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 /**
30  * @constructor
31  * @extends {WebInspector.SourceFrame}
32  * @param {!WebInspector.UISourceCode} uiSourceCode
33  */
34 WebInspector.UISourceCodeFrame = function(uiSourceCode)
35 {
36     this._uiSourceCode = uiSourceCode;
37     WebInspector.SourceFrame.call(this, this._uiSourceCode);
38     WebInspector.settings.textEditorAutocompletion.addChangeListener(this._enableAutocompletionIfNeeded, this);
39     this._enableAutocompletionIfNeeded();
40
41     this._uiSourceCode.addEventListener(WebInspector.UISourceCode.Events.FormattedChanged, this._onFormattedChanged, this);
42     this._uiSourceCode.addEventListener(WebInspector.UISourceCode.Events.WorkingCopyChanged, this._onWorkingCopyChanged, this);
43     this._uiSourceCode.addEventListener(WebInspector.UISourceCode.Events.WorkingCopyCommitted, this._onWorkingCopyCommitted, this);
44     this._updateStyle();
45 }
46
47 WebInspector.UISourceCodeFrame.prototype = {
48     /**
49      * @return {!WebInspector.UISourceCode}
50      */
51     uiSourceCode: function()
52     {
53         return this._uiSourceCode;
54     },
55
56     _enableAutocompletionIfNeeded: function()
57     {
58         this.textEditor.setCompletionDictionary(WebInspector.settings.textEditorAutocompletion.get() ? new WebInspector.SampleCompletionDictionary() : null);
59     },
60
61     wasShown: function()
62     {
63         WebInspector.SourceFrame.prototype.wasShown.call(this);
64         this._boundWindowFocused = this._windowFocused.bind(this);
65         window.addEventListener("focus", this._boundWindowFocused, false);
66         this._checkContentUpdated();
67     },
68
69     willHide: function()
70     {
71         WebInspector.SourceFrame.prototype.willHide.call(this);
72         window.removeEventListener("focus", this._boundWindowFocused, false);
73         delete this._boundWindowFocused;
74         this._uiSourceCode.removeWorkingCopyGetter();
75     },
76
77     /**
78      * @return {boolean}
79      */
80     canEditSource: function()
81     {
82         return this._uiSourceCode.isEditable();
83     },
84
85     _windowFocused: function(event)
86     {
87         this._checkContentUpdated();
88     },
89
90     _checkContentUpdated: function()
91     {
92         if (!this.loaded || !this.isShowing())
93             return;
94         this._uiSourceCode.checkContentUpdated();
95     },
96
97     /**
98      * @param {string} text
99      */
100     commitEditing: function(text)
101     {
102         if (!this._uiSourceCode.isDirty())
103             return;
104
105         this._muteSourceCodeEvents = true;
106         this._uiSourceCode.commitWorkingCopy(this._didEditContent.bind(this));
107         delete this._muteSourceCodeEvents;
108     },
109
110     onTextChanged: function(oldRange, newRange)
111     {
112         WebInspector.SourceFrame.prototype.onTextChanged.call(this, oldRange, newRange);
113         if (this._isSettingContent)
114             return;
115         this._muteSourceCodeEvents = true;
116         if (this._textEditor.isClean())
117             this._uiSourceCode.resetWorkingCopy();
118         else
119             this._uiSourceCode.setWorkingCopyGetter(this._textEditor.text.bind(this._textEditor));
120         delete this._muteSourceCodeEvents;
121     },
122
123     _didEditContent: function(error)
124     {
125         if (error) {
126             WebInspector.log(error, WebInspector.ConsoleMessage.MessageLevel.Error, true);
127             return;
128         }
129     },
130
131     beforeFormattedChange: function() { },
132
133     /**
134      * @param {!WebInspector.Event} event
135      */
136     _onFormattedChanged: function(event)
137     {
138         this.beforeFormattedChange();
139         var content = /** @type {string} */ (event.data.content);
140         this._textEditor.setReadOnly(this._uiSourceCode.formatted());
141         var selection = this._textEditor.selection();
142         this._innerSetContent(content);
143         var start = null;
144         var end = null;
145         if (this._uiSourceCode.formatted()) {
146             start = event.data.newFormatter.originalToFormatted(selection.startLine, selection.startColumn);
147             end = event.data.newFormatter.originalToFormatted(selection.endLine, selection.endColumn);
148         } else {
149             start = event.data.oldFormatter.formattedToOriginal(selection.startLine, selection.startColumn);
150             end = event.data.oldFormatter.formattedToOriginal(selection.endLine, selection.endColumn);
151         }
152         this.textEditor.setSelection(new WebInspector.TextRange(start[0], start[1],
153             end[0], end[1]));
154         this.textEditor.revealLine(start[0]);
155     },
156
157     /**
158      * @param {!WebInspector.Event} event
159      */
160     _onWorkingCopyChanged: function(event)
161     {
162         if (this._muteSourceCodeEvents)
163             return;
164         this._innerSetContent(this._uiSourceCode.workingCopy());
165         this.onUISourceCodeContentChanged();
166     },
167
168     /**
169      * @param {!WebInspector.Event} event
170      */
171     _onWorkingCopyCommitted: function(event)
172     {
173         if (!this._muteSourceCodeEvents) {
174             this._innerSetContent(this._uiSourceCode.workingCopy());
175             this.onUISourceCodeContentChanged();
176         }
177         this._textEditor.markClean();
178         this._updateStyle();
179     },
180
181     _updateStyle: function()
182     {
183         this.element.enableStyleClass("source-frame-unsaved-committed-changes", this._uiSourceCode.hasUnsavedCommittedChanges());
184     },
185
186     onUISourceCodeContentChanged: function()
187     {
188     },
189
190     /**
191      * @param {string} content
192      */
193     _innerSetContent: function(content)
194     {
195         this._isSettingContent = true;
196         this.setContent(content);
197         delete this._isSettingContent;
198     },
199
200     populateTextAreaContextMenu: function(contextMenu, lineNumber)
201     {
202         WebInspector.SourceFrame.prototype.populateTextAreaContextMenu.call(this, contextMenu, lineNumber);
203         contextMenu.appendApplicableItems(this._uiSourceCode);
204         contextMenu.appendSeparator();
205     },
206
207     dispose: function()
208     {
209         this.detach();
210     },
211
212     __proto__: WebInspector.SourceFrame.prototype
213 }