1 /* 2 * Copyright (C) 2011 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 * @extends {WebInspector.Object} 31 * @constructor 32 */ 33 WebInspector.SourcesNavigator = function() 34 { 35 WebInspector.Object.call(this); 36 37 this._tabbedPane = new WebInspector.TabbedPane(); 38 this._tabbedPane.shrinkableTabs = true; 39 this._tabbedPane.element.classList.add("navigator-tabbed-pane"); 40 41 this._sourcesView = new WebInspector.NavigatorView(); 42 this._sourcesView.addEventListener(WebInspector.NavigatorView.Events.ItemSelected, this._sourceSelected, this); 43 this._sourcesView.addEventListener(WebInspector.NavigatorView.Events.ItemSearchStarted, this._itemSearchStarted, this); 44 this._sourcesView.addEventListener(WebInspector.NavigatorView.Events.ItemRenamingRequested, this._itemRenamingRequested, this); 45 this._sourcesView.addEventListener(WebInspector.NavigatorView.Events.ItemCreationRequested, this._itemCreationRequested, this); 46 47 this._contentScriptsView = new WebInspector.NavigatorView(); 48 this._contentScriptsView.addEventListener(WebInspector.NavigatorView.Events.ItemSelected, this._sourceSelected, this); 49 this._contentScriptsView.addEventListener(WebInspector.NavigatorView.Events.ItemSearchStarted, this._itemSearchStarted, this); 50 this._contentScriptsView.addEventListener(WebInspector.NavigatorView.Events.ItemRenamingRequested, this._itemRenamingRequested, this); 51 this._contentScriptsView.addEventListener(WebInspector.NavigatorView.Events.ItemCreationRequested, this._itemCreationRequested, this); 52 53 this._snippetsView = new WebInspector.SnippetsNavigatorView(); 54 this._snippetsView.addEventListener(WebInspector.NavigatorView.Events.ItemSelected, this._sourceSelected, this); 55 this._snippetsView.addEventListener(WebInspector.NavigatorView.Events.ItemSearchStarted, this._itemSearchStarted, this); 56 this._snippetsView.addEventListener(WebInspector.NavigatorView.Events.ItemRenamingRequested, this._itemRenamingRequested, this); 57 this._snippetsView.addEventListener(WebInspector.NavigatorView.Events.ItemCreationRequested, this._itemCreationRequested, this); 58 59 this._tabbedPane.appendTab(WebInspector.SourcesNavigator.SourcesTab, WebInspector.UIString("Sources"), this._sourcesView); 60 this._tabbedPane.selectTab(WebInspector.SourcesNavigator.SourcesTab); 61 this._tabbedPane.appendTab(WebInspector.SourcesNavigator.ContentScriptsTab, WebInspector.UIString("Content scripts"), this._contentScriptsView); 62 this._tabbedPane.appendTab(WebInspector.SourcesNavigator.SnippetsTab, WebInspector.UIString("Snippets"), this._snippetsView); 63 } 64 65 WebInspector.SourcesNavigator.Events = { 66 SourceSelected: "SourceSelected", 67 ItemCreationRequested: "ItemCreationRequested", 68 ItemRenamingRequested: "ItemRenamingRequested", 69 ItemSearchStarted: "ItemSearchStarted", 70 } 71 72 WebInspector.SourcesNavigator.SourcesTab = "sources"; 73 WebInspector.SourcesNavigator.ContentScriptsTab = "contentScripts"; 74 WebInspector.SourcesNavigator.SnippetsTab = "snippets"; 75 76 WebInspector.SourcesNavigator.prototype = { 77 /** 78 * @return {!WebInspector.View} 79 */ 80 get view() 81 { 82 return this._tabbedPane; 83 }, 84 85 /** 86 * @param {!WebInspector.UISourceCode} uiSourceCode 87 */ 88 _navigatorViewForUISourceCode: function(uiSourceCode) 89 { 90 if (uiSourceCode.isContentScript) 91 return this._contentScriptsView; 92 else if (uiSourceCode.project().type() === WebInspector.projectTypes.Snippets) 93 return this._snippetsView; 94 else 95 return this._sourcesView; 96 }, 97 98 /** 99 * @param {!WebInspector.UISourceCode} uiSourceCode 100 */ 101 addUISourceCode: function(uiSourceCode) 102 { 103 this._navigatorViewForUISourceCode(uiSourceCode).addUISourceCode(uiSourceCode); 104 }, 105 106 /** 107 * @param {!WebInspector.UISourceCode} uiSourceCode 108 */ 109 removeUISourceCode: function(uiSourceCode) 110 { 111 this._navigatorViewForUISourceCode(uiSourceCode).removeUISourceCode(uiSourceCode); 112 }, 113 114 /** 115 * @param {!WebInspector.UISourceCode} uiSourceCode 116 * @param {boolean=} select 117 */ 118 revealUISourceCode: function(uiSourceCode, select) 119 { 120 this._navigatorViewForUISourceCode(uiSourceCode).revealUISourceCode(uiSourceCode, select); 121 if (uiSourceCode.isContentScript) 122 this._tabbedPane.selectTab(WebInspector.SourcesNavigator.ContentScriptsTab); 123 else if (uiSourceCode.project().type() !== WebInspector.projectTypes.Snippets) 124 this._tabbedPane.selectTab(WebInspector.SourcesNavigator.SourcesTab); 125 }, 126 127 /** 128 * @param {!WebInspector.UISourceCode} uiSourceCode 129 */ 130 updateIcon: function(uiSourceCode) 131 { 132 this._navigatorViewForUISourceCode(uiSourceCode).updateIcon(uiSourceCode); 133 }, 134 135 /** 136 * @param {!WebInspector.UISourceCode} uiSourceCode 137 * @param {function(boolean)=} callback 138 */ 139 rename: function(uiSourceCode, callback) 140 { 141 this._navigatorViewForUISourceCode(uiSourceCode).rename(uiSourceCode, callback); 142 }, 143 144 /** 145 * @param {!WebInspector.Event} event 146 */ 147 _sourceSelected: function(event) 148 { 149 this.dispatchEventToListeners(WebInspector.SourcesNavigator.Events.SourceSelected, event.data); 150 }, 151 152 /** 153 * @param {!WebInspector.Event} event 154 */ 155 _itemSearchStarted: function(event) 156 { 157 this.dispatchEventToListeners(WebInspector.SourcesNavigator.Events.ItemSearchStarted, event.data); 158 }, 159 160 /** 161 * @param {!WebInspector.Event} event 162 */ 163 _itemRenamingRequested: function(event) 164 { 165 this.dispatchEventToListeners(WebInspector.SourcesNavigator.Events.ItemRenamingRequested, event.data); 166 }, 167 168 /** 169 * @param {!WebInspector.Event} event 170 */ 171 _itemCreationRequested: function(event) 172 { 173 this.dispatchEventToListeners(WebInspector.SourcesNavigator.Events.ItemCreationRequested, event.data); 174 }, 175 176 __proto__: WebInspector.Object.prototype 177 } 178 179 /** 180 * @constructor 181 * @extends {WebInspector.NavigatorView} 182 */ 183 WebInspector.SnippetsNavigatorView = function() 184 { 185 WebInspector.NavigatorView.call(this); 186 } 187 188 WebInspector.SnippetsNavigatorView.prototype = { 189 /** 190 * @param {!Event} event 191 */ 192 handleContextMenu: function(event) 193 { 194 var contextMenu = new WebInspector.ContextMenu(event); 195 contextMenu.appendItem(WebInspector.UIString("New"), this._handleCreateSnippet.bind(this)); 196 contextMenu.show(); 197 }, 198 199 /** 200 * @param {!Event} event 201 * @param {!WebInspector.UISourceCode} uiSourceCode 202 */ 203 handleFileContextMenu: function(event, uiSourceCode) 204 { 205 var contextMenu = new WebInspector.ContextMenu(event); 206 contextMenu.appendItem(WebInspector.UIString("Run"), this._handleEvaluateSnippet.bind(this, uiSourceCode)); 207 contextMenu.appendItem(WebInspector.UIString("Rename"), this.requestRename.bind(this, uiSourceCode)); 208 contextMenu.appendItem(WebInspector.UIString("Remove"), this._handleRemoveSnippet.bind(this, uiSourceCode)); 209 contextMenu.appendSeparator(); 210 contextMenu.appendItem(WebInspector.UIString("New"), this._handleCreateSnippet.bind(this)); 211 contextMenu.show(); 212 }, 213 214 /** 215 * @param {!WebInspector.UISourceCode} uiSourceCode 216 */ 217 _handleEvaluateSnippet: function(uiSourceCode) 218 { 219 if (uiSourceCode.project().type() !== WebInspector.projectTypes.Snippets) 220 return; 221 WebInspector.scriptSnippetModel.evaluateScriptSnippet(uiSourceCode); 222 }, 223 224 /** 225 * @param {!WebInspector.UISourceCode} uiSourceCode 226 */ 227 _handleRemoveSnippet: function(uiSourceCode) 228 { 229 if (uiSourceCode.project().type() !== WebInspector.projectTypes.Snippets) 230 return; 231 uiSourceCode.project().deleteFile(uiSourceCode.path()); 232 }, 233 234 _handleCreateSnippet: function() 235 { 236 var data = {}; 237 data.project = WebInspector.scriptSnippetModel.project(); 238 data.path = ""; 239 this.dispatchEventToListeners(WebInspector.NavigatorView.Events.ItemCreationRequested, data); 240 }, 241 242 /** 243 * @override 244 */ 245 sourceDeleted: function(uiSourceCode) 246 { 247 this._handleRemoveSnippet(uiSourceCode); 248 }, 249 250 __proto__: WebInspector.NavigatorView.prototype 251 } 252