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 * @constructor 31 * @extends {WebInspector.Object} 32 * @param {!WebInspector.Workspace} workspace 33 */ 34 WebInspector.SourcesNavigator = function(workspace) 35 { 36 WebInspector.Object.call(this); 37 this._workspace = workspace; 38 39 this._tabbedPane = new WebInspector.TabbedPane(); 40 this._tabbedPane.shrinkableTabs = true; 41 this._tabbedPane.element.classList.add("navigator-tabbed-pane"); 42 this._tabbedPaneController = new WebInspector.ExtensibleTabbedPaneController(this._tabbedPane, "navigator-view", this._navigatorViewCreated.bind(this)); 43 /** @type {!StringMap.<?WebInspector.NavigatorView>} */ 44 this._navigatorViews = new StringMap(); 45 } 46 47 WebInspector.SourcesNavigator.Events = { 48 SourceSelected: "SourceSelected", 49 SourceRenamed: "SourceRenamed" 50 } 51 52 WebInspector.SourcesNavigator.prototype = { 53 /** 54 * @param {string} id 55 * @param {!WebInspector.View} view 56 */ 57 _navigatorViewCreated: function(id, view) 58 { 59 var navigatorView = /** @type {!WebInspector.NavigatorView} */ (view); 60 navigatorView.addEventListener(WebInspector.NavigatorView.Events.ItemSelected, this._sourceSelected, this); 61 navigatorView.addEventListener(WebInspector.NavigatorView.Events.ItemRenamed, this._sourceRenamed, this); 62 this._navigatorViews.set(id, navigatorView); 63 navigatorView.setWorkspace(this._workspace); 64 }, 65 66 /** 67 * @return {!WebInspector.View} 68 */ 69 get view() 70 { 71 return this._tabbedPane; 72 }, 73 74 /** 75 * @param {!WebInspector.UISourceCode} uiSourceCode 76 * @return {string|null} 77 */ 78 _navigatorViewIdForUISourceCode: function(uiSourceCode) 79 { 80 var ids = this._tabbedPaneController.viewIds(); 81 for (var i = 0; i < ids.length; ++i) { 82 var id = ids[i] 83 84 // Force navigator view creation. 85 this._tabbedPaneController.viewForId(id); 86 87 var navigatorView = this._navigatorViews.get(id); 88 if (navigatorView.accept(uiSourceCode)) 89 return id; 90 } 91 return null; 92 }, 93 94 /** 95 * @param {!WebInspector.UISourceCode} uiSourceCode 96 */ 97 revealUISourceCode: function(uiSourceCode) 98 { 99 var id = this._navigatorViewIdForUISourceCode(uiSourceCode); 100 if (!id) 101 return; 102 var navigatorView = this._navigatorViews.get(id); 103 console.assert(navigatorView); 104 navigatorView.revealUISourceCode(uiSourceCode, true); 105 this._tabbedPane.selectTab(id); 106 }, 107 108 /** 109 * @param {!WebInspector.Event} event 110 */ 111 _sourceSelected: function(event) 112 { 113 this.dispatchEventToListeners(WebInspector.SourcesNavigator.Events.SourceSelected, event.data); 114 }, 115 116 /** 117 * @param {!WebInspector.Event} event 118 */ 119 _sourceRenamed: function(event) 120 { 121 this.dispatchEventToListeners(WebInspector.SourcesNavigator.Events.SourceRenamed, event.data); 122 }, 123 124 __proto__: WebInspector.Object.prototype 125 } 126 127 /** 128 * @constructor 129 * @extends {WebInspector.NavigatorView} 130 */ 131 WebInspector.SnippetsNavigatorView = function() 132 { 133 WebInspector.NavigatorView.call(this); 134 } 135 136 WebInspector.SnippetsNavigatorView.prototype = { 137 /** 138 * @override 139 * @param {!WebInspector.UISourceCode} uiSourceCode 140 * @return {boolean} 141 */ 142 accept: function(uiSourceCode) 143 { 144 if (!WebInspector.NavigatorView.prototype.accept(uiSourceCode)) 145 return false; 146 return uiSourceCode.project().type() === WebInspector.projectTypes.Snippets; 147 }, 148 149 /** 150 * @param {!Event} event 151 */ 152 handleContextMenu: function(event) 153 { 154 var contextMenu = new WebInspector.ContextMenu(event); 155 contextMenu.appendItem(WebInspector.UIString("New"), this._handleCreateSnippet.bind(this)); 156 contextMenu.show(); 157 }, 158 159 /** 160 * @param {!Event} event 161 * @param {!WebInspector.UISourceCode} uiSourceCode 162 */ 163 handleFileContextMenu: function(event, uiSourceCode) 164 { 165 var contextMenu = new WebInspector.ContextMenu(event); 166 contextMenu.appendItem(WebInspector.UIString("Run"), this._handleEvaluateSnippet.bind(this, uiSourceCode)); 167 contextMenu.appendItem(WebInspector.UIString("Rename"), this.rename.bind(this, uiSourceCode)); 168 contextMenu.appendItem(WebInspector.UIString("Remove"), this._handleRemoveSnippet.bind(this, uiSourceCode)); 169 contextMenu.appendSeparator(); 170 contextMenu.appendItem(WebInspector.UIString("New"), this._handleCreateSnippet.bind(this)); 171 contextMenu.show(); 172 }, 173 174 /** 175 * @param {!WebInspector.UISourceCode} uiSourceCode 176 */ 177 _handleEvaluateSnippet: function(uiSourceCode) 178 { 179 var executionContext = WebInspector.context.flavor(WebInspector.ExecutionContext); 180 if (uiSourceCode.project().type() !== WebInspector.projectTypes.Snippets || !executionContext) 181 return; 182 WebInspector.scriptSnippetModel.evaluateScriptSnippet(executionContext, uiSourceCode); 183 }, 184 185 /** 186 * @param {!WebInspector.UISourceCode} uiSourceCode 187 */ 188 _handleRemoveSnippet: function(uiSourceCode) 189 { 190 if (uiSourceCode.project().type() !== WebInspector.projectTypes.Snippets) 191 return; 192 uiSourceCode.remove(); 193 }, 194 195 _handleCreateSnippet: function() 196 { 197 this.create(WebInspector.scriptSnippetModel.project(), "") 198 }, 199 200 /** 201 * @override 202 */ 203 sourceDeleted: function(uiSourceCode) 204 { 205 this._handleRemoveSnippet(uiSourceCode); 206 }, 207 208 __proto__: WebInspector.NavigatorView.prototype 209 } 210