Home | History | Annotate | Download | only in ui
      1 // Copyright 2014 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 /**
      6  * @constructor
      7  */
      8 WebInspector.ActionRegistry = function()
      9 {
     10     /** @type {!StringMap.<!Runtime.Extension>} */
     11     this._actionsById = new StringMap();
     12     this._registerActions();
     13 }
     14 
     15 WebInspector.ActionRegistry.prototype = {
     16     _registerActions: function()
     17     {
     18         self.runtime.extensions(WebInspector.ActionDelegate).forEach(registerExtension, this);
     19 
     20         /**
     21          * @param {!Runtime.Extension} extension
     22          * @this {WebInspector.ActionRegistry}
     23          */
     24         function registerExtension(extension)
     25         {
     26             var actionId = extension.descriptor()["actionId"];
     27             console.assert(actionId);
     28             console.assert(!this._actionsById.get(actionId));
     29             this._actionsById.set(actionId, extension);
     30         }
     31     },
     32 
     33     /**
     34      * @param {!Array.<string>} actionIds
     35      * @param {!WebInspector.Context} context
     36      * @return {!Array.<string>}
     37      */
     38     applicableActions: function(actionIds, context)
     39     {
     40         var extensions = [];
     41         actionIds.forEach(function(actionId) {
     42            var extension = this._actionsById.get(actionId);
     43            if (extension)
     44                extensions.push(extension);
     45         }, this);
     46         return context.applicableExtensions(extensions).values().map(function(extension) {
     47             return extension.descriptor()["actionId"];
     48         });
     49     },
     50 
     51     /**
     52      * @param {string} actionId
     53      * @return {boolean}
     54      */
     55     execute: function(actionId)
     56     {
     57         var extension = this._actionsById.get(actionId);
     58         console.assert(extension, "No action found for actionId '" + actionId + "'");
     59         return extension.instance().handleAction(WebInspector.context);
     60     }
     61 }
     62 
     63 /**
     64  * @interface
     65  */
     66 WebInspector.ActionDelegate = function()
     67 {
     68 }
     69 
     70 WebInspector.ActionDelegate.prototype = {
     71     /**
     72      * @param {!WebInspector.Context} context
     73      * @return {boolean}
     74      */
     75     handleAction: function(context) {}
     76 }
     77 
     78 /** @type {!WebInspector.ActionRegistry} */
     79 WebInspector.actionRegistry;
     80