Upstream version 5.34.104.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     this.addShortcut(WebInspector.KeyboardShortcut.makeKey("s", WebInspector.KeyboardShortcut.Modifiers.CtrlOrMeta), this._commitEditing.bind(this));
46 }
47
48 WebInspector.UISourceCodeFrame.prototype = {
49     _commitEditing: function()
50     {
51         this.commitEditing();
52         return true;
53     },
54
55     /**
56      * @return {!WebInspector.UISourceCode}
57      */
58     uiSourceCode: function()
59     {
60         return this._uiSourceCode;
61     },
62
63     _enableAutocompletionIfNeeded: function()
64     {
65         this.textEditor.setCompletionDictionary(WebInspector.settings.textEditorAutocompletion.get() ? new WebInspector.SampleCompletionDictionary() : null);
66     },
67
68     wasShown: function()
69     {
70         WebInspector.SourceFrame.prototype.wasShown.call(this);
71         this._boundWindowFocused = this._windowFocused.bind(this);
72         window.addEventListener("focus", this._boundWindowFocused, false);
73         this._checkContentUpdated();
74     },
75
76     willHide: function()
77     {
78         WebInspector.SourceFrame.prototype.willHide.call(this);
79         window.removeEventListener("focus", this._boundWindowFocused, false);
80         delete this._boundWindowFocused;
81         this._uiSourceCode.removeWorkingCopyGetter();
82     },
83
84     /**
85      * @return {boolean}
86      */
87     canEditSource: function()
88     {
89         return this._uiSourceCode.isEditable();
90     },
91
92     _windowFocused: function(event)
93     {
94         this._checkContentUpdated();
95     },
96
97     _checkContentUpdated: function()
98     {
99         if (!this.loaded || !this.isShowing())
100             return;
101         this._uiSourceCode.checkContentUpdated();
102     },
103
104     commitEditing: function()
105     {
106         if (!this._uiSourceCode.isDirty())
107             return;
108
109         this._muteSourceCodeEvents = true;
110         this._uiSourceCode.commitWorkingCopy(this._didEditContent.bind(this));
111         delete this._muteSourceCodeEvents;
112     },
113
114     onTextChanged: function(oldRange, newRange)
115     {
116         WebInspector.SourceFrame.prototype.onTextChanged.call(this, oldRange, newRange);
117         if (this._isSettingContent)
118             return;
119         this._muteSourceCodeEvents = true;
120         if (this._textEditor.isClean())
121             this._uiSourceCode.resetWorkingCopy();
122         else
123             this._uiSourceCode.setWorkingCopyGetter(this._textEditor.text.bind(this._textEditor));
124         delete this._muteSourceCodeEvents;
125     },
126
127     _didEditContent: function(error)
128     {
129         if (error) {
130             WebInspector.log(error, WebInspector.ConsoleMessage.MessageLevel.Error, true);
131             return;
132         }
133     },
134
135     beforeFormattedChange: function() { },
136
137     /**
138      * @param {!WebInspector.Event} event
139      */
140     _onFormattedChanged: function(event)
141     {
142         this.beforeFormattedChange();
143         var content = /** @type {string} */ (event.data.content);
144         this._textEditor.setReadOnly(this._uiSourceCode.formatted());
145         var selection = this._textEditor.selection();
146         this._innerSetContent(content);
147         var start = null;
148         var end = null;
149         if (this._uiSourceCode.formatted()) {
150             start = event.data.newFormatter.originalToFormatted(selection.startLine, selection.startColumn);
151             end = event.data.newFormatter.originalToFormatted(selection.endLine, selection.endColumn);
152         } else {
153             start = event.data.oldFormatter.formattedToOriginal(selection.startLine, selection.startColumn);
154             end = event.data.oldFormatter.formattedToOriginal(selection.endLine, selection.endColumn);
155         }
156         this.textEditor.setSelection(new WebInspector.TextRange(start[0], start[1],
157             end[0], end[1]));
158         this.textEditor.revealLine(start[0]);
159     },
160
161     /**
162      * @param {!WebInspector.Event} event
163      */
164     _onWorkingCopyChanged: function(event)
165     {
166         if (this._muteSourceCodeEvents)
167             return;
168         this._innerSetContent(this._uiSourceCode.workingCopy());
169         this.onUISourceCodeContentChanged();
170     },
171
172     /**
173      * @param {!WebInspector.Event} event
174      */
175     _onWorkingCopyCommitted: function(event)
176     {
177         if (!this._muteSourceCodeEvents) {
178             this._innerSetContent(this._uiSourceCode.workingCopy());
179             this.onUISourceCodeContentChanged();
180         }
181         this._textEditor.markClean();
182         this._updateStyle();
183     },
184
185     _updateStyle: function()
186     {
187         this.element.enableStyleClass("source-frame-unsaved-committed-changes", this._uiSourceCode.hasUnsavedCommittedChanges());
188     },
189
190     onUISourceCodeContentChanged: function()
191     {
192     },
193
194     /**
195      * @param {string} content
196      */
197     _innerSetContent: function(content)
198     {
199         this._isSettingContent = true;
200         this.setContent(content);
201         delete this._isSettingContent;
202     },
203
204     populateTextAreaContextMenu: function(contextMenu, lineNumber)
205     {
206         WebInspector.SourceFrame.prototype.populateTextAreaContextMenu.call(this, contextMenu, lineNumber);
207         contextMenu.appendApplicableItems(this._uiSourceCode);
208         contextMenu.appendSeparator();
209     },
210
211     dispose: function()
212     {
213         this.detach();
214     },
215
216     __proto__: WebInspector.SourceFrame.prototype
217 }