1/* 2 * Copyright (C) 2013 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 26WebInspector.TextResourceContentView = function(resource) 27{ 28 WebInspector.ResourceContentView.call(this, resource, WebInspector.TextResourceContentView.StyleClassName); 29 30 resource.addEventListener(WebInspector.SourceCode.Event.ContentDidChange, this._sourceCodeContentDidChange, this); 31 32 this._textEditor = new WebInspector.SourceCodeTextEditor(resource); 33 this._textEditor.addEventListener(WebInspector.TextEditor.Event.ExecutionLineNumberDidChange, this._executionLineNumberDidChange, this); 34 this._textEditor.addEventListener(WebInspector.TextEditor.Event.NumberOfSearchResultsDidChange, this._numberOfSearchResultsDidChange, this); 35 this._textEditor.addEventListener(WebInspector.TextEditor.Event.ContentDidChange, this._textEditorContentDidChange, this); 36 this._textEditor.addEventListener(WebInspector.TextEditor.Event.FormattingDidChange, this._textEditorFormattingDidChange, this); 37 this._textEditor.addEventListener(WebInspector.SourceCodeTextEditor.Event.ContentWillPopulate, this._contentWillPopulate, this); 38 this._textEditor.addEventListener(WebInspector.SourceCodeTextEditor.Event.ContentDidPopulate, this._contentDidPopulate, this); 39 40 WebInspector.probeManager.addEventListener(WebInspector.ProbeManager.Event.ProbeSetAdded, this._probeSetsChanged, this); 41 WebInspector.probeManager.addEventListener(WebInspector.ProbeManager.Event.ProbeSetRemoved, this._probeSetsChanged, this); 42 43 var curleyBracesImage; 44 if (WebInspector.Platform.isLegacyMacOS) 45 curleyBracesImage = {src: "Images/Legacy/NavigationItemCurleyBraces.svg", width: 16, height: 16}; 46 else 47 curleyBracesImage = {src: "Images/NavigationItemCurleyBraces.svg", width: 13, height: 13}; 48 49 var toolTip = WebInspector.UIString("Pretty print"); 50 var activatedToolTip = WebInspector.UIString("Original formatting"); 51 this._prettyPrintButtonNavigationItem = new WebInspector.ActivateButtonNavigationItem("pretty-print", toolTip, activatedToolTip, curleyBracesImage.src, curleyBracesImage.width, curleyBracesImage.height); 52 this._prettyPrintButtonNavigationItem.addEventListener(WebInspector.ButtonNavigationItem.Event.Clicked, this._togglePrettyPrint, this); 53 this._prettyPrintButtonNavigationItem.enabled = false; // Enabled when the text editor is populated with content. 54}; 55 56WebInspector.TextResourceContentView.StyleClassName = "text"; 57 58WebInspector.TextResourceContentView.prototype = { 59 constructor: WebInspector.TextResourceContentView, 60 61 // Public 62 63 get navigationItems() 64 { 65 return [this._prettyPrintButtonNavigationItem]; 66 }, 67 68 get managesOwnIssues() 69 { 70 // SourceCodeTextEditor manages the issues, we don't need ResourceContentView doing it. 71 return true; 72 }, 73 74 get textEditor() 75 { 76 return this._textEditor; 77 }, 78 79 get supplementalRepresentedObjects() 80 { 81 var objects = WebInspector.probeManager.probeSets.filter(function(probeSet) { 82 return this._resource.url === probeSet.breakpoint.url; 83 }.bind(this)); 84 85 // If the SourceCodeTextEditor has an executionLineNumber, we can assume 86 // it is always the active call frame. 87 if (!isNaN(this._textEditor.executionLineNumber)) 88 objects.push(WebInspector.debuggerManager.activeCallFrame); 89 90 return objects; 91 }, 92 93 revealPosition: function(position, textRangeToSelect, forceUnformatted) 94 { 95 this._textEditor.revealPosition(position, textRangeToSelect, forceUnformatted); 96 }, 97 98 shown: function() 99 { 100 WebInspector.ResourceContentView.prototype.shown.call(this); 101 102 this._textEditor.shown(); 103 }, 104 105 hidden: function() 106 { 107 WebInspector.ResourceContentView.prototype.hidden.call(this); 108 109 this._textEditor.hidden(); 110 }, 111 112 closed: function() 113 { 114 WebInspector.ResourceContentView.prototype.closed.call(this); 115 116 this.resource.removeEventListener(WebInspector.SourceCode.Event.ContentDidChange, this._sourceCodeContentDidChange, this); 117 118 this._textEditor.close(); 119 }, 120 121 get supportsSave() 122 { 123 return true; 124 }, 125 126 get saveData() 127 { 128 return {url: this.resource.url, content: this._textEditor.string}; 129 }, 130 131 get supportsSearch() 132 { 133 return true; 134 }, 135 136 get numberOfSearchResults() 137 { 138 return this._textEditor.numberOfSearchResults; 139 }, 140 141 get hasPerformedSearch() 142 { 143 return this._textEditor.currentSearchQuery !== null; 144 }, 145 146 set automaticallyRevealFirstSearchResult(reveal) 147 { 148 this._textEditor.automaticallyRevealFirstSearchResult = reveal; 149 }, 150 151 performSearch: function(query) 152 { 153 this._textEditor.performSearch(query); 154 }, 155 156 searchCleared: function() 157 { 158 this._textEditor.searchCleared(); 159 }, 160 161 searchQueryWithSelection: function() 162 { 163 return this._textEditor.searchQueryWithSelection(); 164 }, 165 166 revealPreviousSearchResult: function(changeFocus) 167 { 168 this._textEditor.revealPreviousSearchResult(changeFocus); 169 }, 170 171 revealNextSearchResult: function(changeFocus) 172 { 173 this._textEditor.revealNextSearchResult(changeFocus); 174 }, 175 176 updateLayout: function() 177 { 178 this._textEditor.updateLayout(); 179 }, 180 181 // Private 182 183 _contentWillPopulate: function(event) 184 { 185 if (this._textEditor.element.parentNode === this.element) 186 return; 187 188 // Check the MIME-type for CSS since Resource.Type.Stylesheet also includes XSL, which we can't edit yet. 189 if (this.resource.type === WebInspector.Resource.Type.Stylesheet && this.resource.syntheticMIMEType === "text/css") 190 this._textEditor.readOnly = false; 191 192 // Allow editing any local file since edits can be saved and reloaded right from the Inspector. 193 if (this.resource.urlComponents.scheme === "file") 194 this._textEditor.readOnly = false; 195 196 this.element.removeChildren(); 197 this.element.appendChild(this._textEditor.element); 198 }, 199 200 _contentDidPopulate: function(event) 201 { 202 this._prettyPrintButtonNavigationItem.enabled = this._textEditor.canBeFormatted(); 203 }, 204 205 _togglePrettyPrint: function(event) 206 { 207 var activated = !this._prettyPrintButtonNavigationItem.activated; 208 this._textEditor.formatted = activated; 209 }, 210 211 _textEditorFormattingDidChange: function(event) 212 { 213 this._prettyPrintButtonNavigationItem.activated = this._textEditor.formatted; 214 }, 215 216 _sourceCodeContentDidChange: function(event) 217 { 218 if (this._ignoreSourceCodeContentDidChangeEvent) 219 return; 220 221 this._textEditor.string = this.resource.currentRevision.content; 222 }, 223 224 _textEditorContentDidChange: function(event) 225 { 226 this._ignoreSourceCodeContentDidChangeEvent = true; 227 WebInspector.branchManager.currentBranch.revisionForRepresentedObject(this.resource).content = this._textEditor.string; 228 delete this._ignoreSourceCodeContentDidChangeEvent; 229 }, 230 231 _executionLineNumberDidChange: function(event) 232 { 233 this.dispatchEventToListeners(WebInspector.ContentView.Event.SupplementalRepresentedObjectsDidChange); 234 }, 235 236 _numberOfSearchResultsDidChange: function(event) 237 { 238 this.dispatchEventToListeners(WebInspector.ContentView.Event.NumberOfSearchResultsDidChange); 239 }, 240 241 _probeSetsChanged: function(event) 242 { 243 var breakpoint = event.data.probeSet.breakpoint; 244 if (breakpoint.sourceCodeLocation.sourceCode === this.resource) 245 this.dispatchEventToListeners(WebInspector.ContentView.Event.SupplementalRepresentedObjectsDidChange); 246 } 247}; 248 249WebInspector.TextResourceContentView.prototype.__proto__ = WebInspector.ResourceContentView.prototype; 250