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