Home | History | Annotate | Download | only in sources
      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 {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.put(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._navigatorViews.keys();
     81         for (var i = 0; i < ids.length; ++i) {
     82             var id = ids[i]
     83             var navigatorView = this._navigatorViews.get(id);
     84             if (navigatorView.accept(uiSourceCode))
     85                 return id;
     86         }
     87         return null;
     88     },
     89 
     90     /**
     91      * @param {!WebInspector.UISourceCode} uiSourceCode
     92      */
     93     revealUISourceCode: function(uiSourceCode)
     94     {
     95         var id = this._navigatorViewIdForUISourceCode(uiSourceCode);
     96         if (!id)
     97             return;
     98         var navigatorView = this._navigatorViews.get(id);
     99         console.assert(navigatorView);
    100         navigatorView.revealUISourceCode(uiSourceCode, true);
    101         this._tabbedPane.selectTab(id);
    102     },
    103 
    104     /**
    105      * @param {!WebInspector.Event} event
    106      */
    107     _sourceSelected: function(event)
    108     {
    109         this.dispatchEventToListeners(WebInspector.SourcesNavigator.Events.SourceSelected, event.data);
    110     },
    111 
    112     /**
    113      * @param {!WebInspector.Event} event
    114      */
    115     _sourceRenamed: function(event)
    116     {
    117         this.dispatchEventToListeners(WebInspector.SourcesNavigator.Events.SourceRenamed, event.data);
    118     },
    119 
    120     __proto__: WebInspector.Object.prototype
    121 }
    122 
    123 /**
    124  * @constructor
    125  * @extends {WebInspector.NavigatorView}
    126  */
    127 WebInspector.SnippetsNavigatorView = function()
    128 {
    129     WebInspector.NavigatorView.call(this);
    130 }
    131 
    132 WebInspector.SnippetsNavigatorView.prototype = {
    133     /**
    134      * @override
    135      * @param {!WebInspector.UISourceCode} uiSourceCode
    136      * @return {boolean}
    137      */
    138     accept: function(uiSourceCode)
    139     {
    140         if (!WebInspector.NavigatorView.prototype.accept(uiSourceCode))
    141             return false;
    142         return uiSourceCode.project().type() === WebInspector.projectTypes.Snippets;
    143     },
    144 
    145     /**
    146      * @param {!Event} event
    147      */
    148     handleContextMenu: function(event)
    149     {
    150         var contextMenu = new WebInspector.ContextMenu(event);
    151         contextMenu.appendItem(WebInspector.UIString("New"), this._handleCreateSnippet.bind(this));
    152         contextMenu.show();
    153     },
    154 
    155     /**
    156      * @param {!Event} event
    157      * @param {!WebInspector.UISourceCode} uiSourceCode
    158      */
    159     handleFileContextMenu: function(event, uiSourceCode)
    160     {
    161         var contextMenu = new WebInspector.ContextMenu(event);
    162         contextMenu.appendItem(WebInspector.UIString("Run"), this._handleEvaluateSnippet.bind(this, uiSourceCode));
    163         contextMenu.appendItem(WebInspector.UIString("Rename"), this.rename.bind(this, uiSourceCode));
    164         contextMenu.appendItem(WebInspector.UIString("Remove"), this._handleRemoveSnippet.bind(this, uiSourceCode));
    165         contextMenu.appendSeparator();
    166         contextMenu.appendItem(WebInspector.UIString("New"), this._handleCreateSnippet.bind(this));
    167         contextMenu.show();
    168     },
    169 
    170     /**
    171      * @param {!WebInspector.UISourceCode} uiSourceCode
    172      */
    173     _handleEvaluateSnippet: function(uiSourceCode)
    174     {
    175         var executionContext = WebInspector.context.flavor(WebInspector.ExecutionContext);
    176         if (uiSourceCode.project().type() !== WebInspector.projectTypes.Snippets || !executionContext)
    177             return;
    178         WebInspector.scriptSnippetModel.evaluateScriptSnippet(executionContext, uiSourceCode);
    179     },
    180 
    181     /**
    182      * @param {!WebInspector.UISourceCode} uiSourceCode
    183      */
    184     _handleRemoveSnippet: function(uiSourceCode)
    185     {
    186         if (uiSourceCode.project().type() !== WebInspector.projectTypes.Snippets)
    187             return;
    188         uiSourceCode.remove();
    189     },
    190 
    191     _handleCreateSnippet: function()
    192     {
    193         this.create(WebInspector.scriptSnippetModel.project(), "")
    194     },
    195 
    196     /**
    197      * @override
    198      */
    199     sourceDeleted: function(uiSourceCode)
    200     {
    201         this._handleRemoveSnippet(uiSourceCode);
    202     },
    203 
    204     __proto__: WebInspector.NavigatorView.prototype
    205 }
    206