Upstream version 7.35.144.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / devtools / front_end / SourcesNavigator.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  * @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     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 {!WebInspector.View} view
55      */
56     _navigatorViewCreated: function(id, view)
57     {
58         var navigatorView = /** @type {!WebInspector.NavigatorView} */ (view);
59         navigatorView.addEventListener(WebInspector.NavigatorView.Events.ItemSelected, this._sourceSelected, this);
60         navigatorView.addEventListener(WebInspector.NavigatorView.Events.ItemRenamed, this._sourceRenamed, this);
61         this._navigatorViews.put(id, navigatorView);
62         navigatorView.setWorkspace(this._workspace);
63     },
64
65     /**
66      * @return {!WebInspector.View}
67      */
68     get view()
69     {
70         return this._tabbedPane;
71     },
72
73     /**
74      * @param {!WebInspector.UISourceCode} uiSourceCode
75      * @return {string|null}
76      */
77     _navigatorViewIdForUISourceCode: function(uiSourceCode)
78     {
79         var ids = this._navigatorViews.keys();
80         for (var i = 0; i < ids.length; ++i) {
81             var id = ids[i]
82             var navigatorView = this._navigatorViews.get(id);
83             if (navigatorView.accept(uiSourceCode))
84                 return id;
85         }
86         return null;
87     },
88
89     /**
90      * @param {!WebInspector.UISourceCode} uiSourceCode
91      */
92     revealUISourceCode: function(uiSourceCode)
93     {
94         var id = this._navigatorViewIdForUISourceCode(uiSourceCode);
95         if (!id)
96             return;
97         var navigatorView = this._navigatorViews.get(id);
98         console.assert(navigatorView);
99         navigatorView.revealUISourceCode(uiSourceCode, true);
100         this._tabbedPane.selectTab(id);
101     },
102
103     /**
104      * @param {!WebInspector.Event} event
105      */
106     _sourceSelected: function(event)
107     {
108         this.dispatchEventToListeners(WebInspector.SourcesNavigator.Events.SourceSelected, event.data);
109     },
110
111     /**
112      * @param {!WebInspector.Event} event
113      */
114     _sourceRenamed: function(event)
115     {
116         this.dispatchEventToListeners(WebInspector.SourcesNavigator.Events.SourceRenamed, event.data);
117     },
118
119     __proto__: WebInspector.Object.prototype
120 }
121
122 /**
123  * @constructor
124  * @extends {WebInspector.NavigatorView}
125  */
126 WebInspector.SnippetsNavigatorView = function()
127 {
128     WebInspector.NavigatorView.call(this);
129 }
130
131 WebInspector.SnippetsNavigatorView.prototype = {
132     /**
133      * @override
134      * @param {!WebInspector.UISourceCode} uiSourceCode
135      * @return {boolean}
136      */
137     accept: function(uiSourceCode)
138     {
139         if (!WebInspector.NavigatorView.prototype.accept(uiSourceCode))
140             return false;
141         return uiSourceCode.project().type() === WebInspector.projectTypes.Snippets;
142     },
143
144     /**
145      * @param {!Event} event
146      */
147     handleContextMenu: function(event)
148     {
149         var contextMenu = new WebInspector.ContextMenu(event);
150         contextMenu.appendItem(WebInspector.UIString("New"), this._handleCreateSnippet.bind(this));
151         contextMenu.show();
152     },
153
154     /**
155      * @param {!Event} event
156      * @param {!WebInspector.UISourceCode} uiSourceCode
157      */
158     handleFileContextMenu: function(event, uiSourceCode)
159     {
160         var contextMenu = new WebInspector.ContextMenu(event);
161         contextMenu.appendItem(WebInspector.UIString("Run"), this._handleEvaluateSnippet.bind(this, uiSourceCode));
162         contextMenu.appendItem(WebInspector.UIString("Rename"), this.rename.bind(this, uiSourceCode));
163         contextMenu.appendItem(WebInspector.UIString("Remove"), this._handleRemoveSnippet.bind(this, uiSourceCode));
164         contextMenu.appendSeparator();
165         contextMenu.appendItem(WebInspector.UIString("New"), this._handleCreateSnippet.bind(this));
166         contextMenu.show();
167     },
168
169     /**
170      * @param {!WebInspector.UISourceCode} uiSourceCode
171      */
172     _handleEvaluateSnippet: function(uiSourceCode)
173     {
174         if (uiSourceCode.project().type() !== WebInspector.projectTypes.Snippets)
175             return;
176         WebInspector.scriptSnippetModel.evaluateScriptSnippet(uiSourceCode);
177     },
178
179     /**
180      * @param {!WebInspector.UISourceCode} uiSourceCode
181      */
182     _handleRemoveSnippet: function(uiSourceCode)
183     {
184         if (uiSourceCode.project().type() !== WebInspector.projectTypes.Snippets)
185             return;
186         uiSourceCode.remove();
187     },
188
189     _handleCreateSnippet: function()
190     {
191         this.create(WebInspector.scriptSnippetModel.project(), "")
192     },
193
194     /**
195      * @override
196      */
197     sourceDeleted: function(uiSourceCode)
198     {
199         this._handleRemoveSnippet(uiSourceCode);
200     },
201
202     __proto__: WebInspector.NavigatorView.prototype
203 }