2 * Copyright (C) 2012 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
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
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.
31 * @extends {WebInspector.SourceFrame}
32 * @param {!WebInspector.UISourceCode} uiSourceCode
34 WebInspector.UISourceCodeFrame = function(uiSourceCode)
36 this._uiSourceCode = uiSourceCode;
37 WebInspector.SourceFrame.call(this, this._uiSourceCode);
38 WebInspector.settings.textEditorAutocompletion.addChangeListener(this._enableAutocompletionIfNeeded, this);
39 this._enableAutocompletionIfNeeded();
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);
47 WebInspector.UISourceCodeFrame.prototype = {
49 * @return {!WebInspector.UISourceCode}
51 uiSourceCode: function()
53 return this._uiSourceCode;
56 _enableAutocompletionIfNeeded: function()
58 this.textEditor.setCompletionDictionary(WebInspector.settings.textEditorAutocompletion.get() ? new WebInspector.SampleCompletionDictionary() : null);
63 WebInspector.SourceFrame.prototype.wasShown.call(this);
64 this._boundWindowFocused = this._windowFocused.bind(this);
65 window.addEventListener("focus", this._boundWindowFocused, false);
66 this._checkContentUpdated();
71 WebInspector.SourceFrame.prototype.willHide.call(this);
72 window.removeEventListener("focus", this._boundWindowFocused, false);
73 delete this._boundWindowFocused;
74 this._uiSourceCode.removeWorkingCopyGetter();
80 canEditSource: function()
82 return this._uiSourceCode.isEditable();
85 _windowFocused: function(event)
87 this._checkContentUpdated();
90 _checkContentUpdated: function()
92 if (!this.loaded || !this.isShowing())
94 this._uiSourceCode.checkContentUpdated();
98 * @param {string} text
100 commitEditing: function(text)
102 if (!this._uiSourceCode.isDirty())
105 this._muteSourceCodeEvents = true;
106 this._uiSourceCode.commitWorkingCopy(this._didEditContent.bind(this));
107 delete this._muteSourceCodeEvents;
110 onTextChanged: function(oldRange, newRange)
112 WebInspector.SourceFrame.prototype.onTextChanged.call(this, oldRange, newRange);
113 if (this._isSettingContent)
115 this._muteSourceCodeEvents = true;
116 if (this._textEditor.isClean())
117 this._uiSourceCode.resetWorkingCopy();
119 this._uiSourceCode.setWorkingCopyGetter(this._textEditor.text.bind(this._textEditor));
120 delete this._muteSourceCodeEvents;
123 _didEditContent: function(error)
126 WebInspector.log(error, WebInspector.ConsoleMessage.MessageLevel.Error, true);
131 beforeFormattedChange: function() { },
134 * @param {!WebInspector.Event} event
136 _onFormattedChanged: function(event)
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);
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);
149 start = event.data.oldFormatter.formattedToOriginal(selection.startLine, selection.startColumn);
150 end = event.data.oldFormatter.formattedToOriginal(selection.endLine, selection.endColumn);
152 this.textEditor.setSelection(new WebInspector.TextRange(start[0], start[1],
154 this.textEditor.revealLine(start[0]);
158 * @param {!WebInspector.Event} event
160 _onWorkingCopyChanged: function(event)
162 if (this._muteSourceCodeEvents)
164 this._innerSetContent(this._uiSourceCode.workingCopy());
165 this.onUISourceCodeContentChanged();
169 * @param {!WebInspector.Event} event
171 _onWorkingCopyCommitted: function(event)
173 if (!this._muteSourceCodeEvents) {
174 this._innerSetContent(this._uiSourceCode.workingCopy());
175 this.onUISourceCodeContentChanged();
177 this._textEditor.markClean();
181 _updateStyle: function()
183 this.element.enableStyleClass("source-frame-unsaved-committed-changes", this._uiSourceCode.hasUnsavedCommittedChanges());
186 onUISourceCodeContentChanged: function()
191 * @param {string} content
193 _innerSetContent: function(content)
195 this._isSettingContent = true;
196 this.setContent(content);
197 delete this._isSettingContent;
200 populateTextAreaContextMenu: function(contextMenu, lineNumber)
202 WebInspector.SourceFrame.prototype.populateTextAreaContextMenu.call(this, contextMenu, lineNumber);
203 contextMenu.appendApplicableItems(this._uiSourceCode);
204 contextMenu.appendSeparator();
212 __proto__: WebInspector.SourceFrame.prototype