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);
45 this.addShortcut(WebInspector.KeyboardShortcut.makeKey("s", WebInspector.KeyboardShortcut.Modifiers.CtrlOrMeta), this._commitEditing.bind(this));
48 WebInspector.UISourceCodeFrame.prototype = {
49 _commitEditing: function()
56 * @return {!WebInspector.UISourceCode}
58 uiSourceCode: function()
60 return this._uiSourceCode;
63 _enableAutocompletionIfNeeded: function()
65 this.textEditor.setCompletionDictionary(WebInspector.settings.textEditorAutocompletion.get() ? new WebInspector.SampleCompletionDictionary() : null);
70 WebInspector.SourceFrame.prototype.wasShown.call(this);
71 this._boundWindowFocused = this._windowFocused.bind(this);
72 window.addEventListener("focus", this._boundWindowFocused, false);
73 this._checkContentUpdated();
78 WebInspector.SourceFrame.prototype.willHide.call(this);
79 window.removeEventListener("focus", this._boundWindowFocused, false);
80 delete this._boundWindowFocused;
81 this._uiSourceCode.removeWorkingCopyGetter();
87 canEditSource: function()
89 return this._uiSourceCode.isEditable();
92 _windowFocused: function(event)
94 this._checkContentUpdated();
97 _checkContentUpdated: function()
99 if (!this.loaded || !this.isShowing())
101 this._uiSourceCode.checkContentUpdated();
104 commitEditing: function()
106 if (!this._uiSourceCode.isDirty())
109 this._muteSourceCodeEvents = true;
110 this._uiSourceCode.commitWorkingCopy(this._didEditContent.bind(this));
111 delete this._muteSourceCodeEvents;
114 onTextChanged: function(oldRange, newRange)
116 WebInspector.SourceFrame.prototype.onTextChanged.call(this, oldRange, newRange);
117 if (this._isSettingContent)
119 this._muteSourceCodeEvents = true;
120 if (this._textEditor.isClean())
121 this._uiSourceCode.resetWorkingCopy();
123 this._uiSourceCode.setWorkingCopyGetter(this._textEditor.text.bind(this._textEditor));
124 delete this._muteSourceCodeEvents;
127 _didEditContent: function(error)
130 WebInspector.log(error, WebInspector.ConsoleMessage.MessageLevel.Error, true);
135 beforeFormattedChange: function() { },
138 * @param {!WebInspector.Event} event
140 _onFormattedChanged: function(event)
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);
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);
153 start = event.data.oldFormatter.formattedToOriginal(selection.startLine, selection.startColumn);
154 end = event.data.oldFormatter.formattedToOriginal(selection.endLine, selection.endColumn);
156 this.textEditor.setSelection(new WebInspector.TextRange(start[0], start[1],
158 this.textEditor.revealLine(start[0]);
162 * @param {!WebInspector.Event} event
164 _onWorkingCopyChanged: function(event)
166 if (this._muteSourceCodeEvents)
168 this._innerSetContent(this._uiSourceCode.workingCopy());
169 this.onUISourceCodeContentChanged();
173 * @param {!WebInspector.Event} event
175 _onWorkingCopyCommitted: function(event)
177 if (!this._muteSourceCodeEvents) {
178 this._innerSetContent(this._uiSourceCode.workingCopy());
179 this.onUISourceCodeContentChanged();
181 this._textEditor.markClean();
185 _updateStyle: function()
187 this.element.enableStyleClass("source-frame-unsaved-committed-changes", this._uiSourceCode.hasUnsavedCommittedChanges());
190 onUISourceCodeContentChanged: function()
195 * @param {string} content
197 _innerSetContent: function(content)
199 this._isSettingContent = true;
200 this.setContent(content);
201 delete this._isSettingContent;
204 populateTextAreaContextMenu: function(contextMenu, lineNumber)
206 WebInspector.SourceFrame.prototype.populateTextAreaContextMenu.call(this, contextMenu, lineNumber);
207 contextMenu.appendApplicableItems(this._uiSourceCode);
208 contextMenu.appendSeparator();
216 __proto__: WebInspector.SourceFrame.prototype