Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / devtools / front_end / ui / ContextMenu.js
index 76b9504..bfa696a 100644 (file)
@@ -30,7 +30,7 @@
 
 /**
  * @constructor
- * @param {!WebInspector.ContextSubMenuItem} topLevelMenu
+ * @param {!WebInspector.ContextMenu} topLevelMenu
  * @param {string} type
  * @param {string=} label
  * @param {boolean=} disabled
@@ -80,6 +80,9 @@ WebInspector.ContextMenuItem.prototype = {
         this._disabled = !enabled;
     },
 
+    /**
+     * @return {!InspectorFrontendHostAPI.ContextMenuDescriptor}
+     */
     _buildDescriptor: function()
     {
         switch (this._type) {
@@ -90,13 +93,14 @@ WebInspector.ContextMenuItem.prototype = {
         case "checkbox":
             return { type: "checkbox", id: this._id, label: this._label, checked: !!this._checked, enabled: !this._disabled };
         }
+        throw new Error("Invalid item type:"  + this._type);
     }
 }
 
 /**
  * @constructor
  * @extends {WebInspector.ContextMenuItem}
- * @param topLevelMenu
+ * @param {!WebInspector.ContextMenu} topLevelMenu
  * @param {string=} label
  * @param {boolean=} disabled
  */
@@ -125,7 +129,7 @@ WebInspector.ContextSubMenuItem.prototype = {
     /**
      * @param {string} label
      * @param {boolean=} disabled
-     * @return {!WebInspector.ContextMenuItem}
+     * @return {!WebInspector.ContextSubMenuItem}
      */
     appendSubMenuItem: function(label, disabled)
     {
@@ -135,6 +139,9 @@ WebInspector.ContextSubMenuItem.prototype = {
     },
 
     /**
+     * @param {string} label
+     * @param {function()} handler
+     * @param {boolean=} checked
      * @param {boolean=} disabled
      * @return {!WebInspector.ContextMenuItem}
      */
@@ -172,6 +179,9 @@ WebInspector.ContextSubMenuItem.prototype = {
         return !this._items.length;
     },
 
+    /**
+     * @return {!InspectorFrontendHostAPI.ContextMenuDescriptor}
+     */
     _buildDescriptor: function()
     {
         var result = { type: "subMenu", label: this._label, enabled: !this._disabled, subItems: [] };
@@ -186,20 +196,26 @@ WebInspector.ContextSubMenuItem.prototype = {
 /**
  * @constructor
  * @extends {WebInspector.ContextSubMenuItem}
+ * @param {!Event} event
  */
-WebInspector.ContextMenu = function(event) {
+WebInspector.ContextMenu = function(event)
+{
     WebInspector.ContextSubMenuItem.call(this, this, "");
     this._event = event;
     this._handlers = {};
     this._id = 0;
 }
 
-/**
- * @param {boolean} useSoftMenu
- */
-WebInspector.ContextMenu.setUseSoftMenu = function(useSoftMenu)
+WebInspector.ContextMenu.initialize = function()
 {
-    WebInspector.ContextMenu._useSoftMenu = useSoftMenu;
+    InspectorFrontendHost.events.addEventListener(InspectorFrontendHostAPI.Events.SetUseSoftMenu, setUseSoftMenu);
+    /**
+     * @param {!WebInspector.Event} event
+     */
+    function setUseSoftMenu(event)
+    {
+        WebInspector.ContextMenu._useSoftMenu = /** @type {boolean} */ (event.data);
+    }
 }
 
 WebInspector.ContextMenu.prototype = {
@@ -217,22 +233,31 @@ WebInspector.ContextMenu.prototype = {
 
         if (menuObject.length) {
             WebInspector._contextMenu = this;
-            if (WebInspector.ContextMenu._useSoftMenu) {
-                var softMenu = new WebInspector.SoftContextMenu(menuObject);
-                softMenu.show(this._event);
+            if (WebInspector.ContextMenu._useSoftMenu || InspectorFrontendHost.isHostedMode()) {
+                var softMenu = new WebInspector.SoftContextMenu(menuObject, this._itemSelected.bind(this));
+                softMenu.show(this._event.x, this._event.y);
             } else {
-                InspectorFrontendHost.showContextMenu(this._event, menuObject);
+                InspectorFrontendHost.showContextMenuAtPoint(this._event.x, this._event.y, menuObject);
+                InspectorFrontendHost.events.addEventListener(InspectorFrontendHostAPI.Events.ContextMenuCleared, this._menuCleared, this);
+                InspectorFrontendHost.events.addEventListener(InspectorFrontendHostAPI.Events.ContextMenuItemSelected, this._onItemSelected, this);
             }
             this._event.consume(true);
         }
     },
 
+    /**
+     * @param {number} id
+     * @param {function(?)} handler
+     */
     _setHandler: function(id, handler)
     {
         if (handler)
             this._handlers[id] = handler;
     },
 
+    /**
+     * @return {!Array.<!InspectorFrontendHostAPI.ContextMenuDescriptor>}
+     */
     _buildDescriptor: function()
     {
         var result = [];
@@ -241,10 +266,28 @@ WebInspector.ContextMenu.prototype = {
         return result;
     },
 
+    /**
+     * @param {!WebInspector.Event} event
+     */
+    _onItemSelected: function(event)
+    {
+        this._itemSelected(/** @type {string} */ (event.data));
+    },
+
+    /**
+     * @param {string} id
+     */
     _itemSelected: function(id)
     {
         if (this._handlers[id])
             this._handlers[id].call(this);
+        this._menuCleared();
+    },
+
+    _menuCleared: function()
+    {
+        InspectorFrontendHost.events.removeEventListener(InspectorFrontendHostAPI.Events.ContextMenuCleared, this._menuCleared, this);
+        InspectorFrontendHost.events.removeEventListener(InspectorFrontendHostAPI.Events.ContextMenuItemSelected, this._onItemSelected, this);
     },
 
     /**
@@ -252,10 +295,10 @@ WebInspector.ContextMenu.prototype = {
      */
     appendApplicableItems: function(target)
     {
-        WebInspector.moduleManager.extensions(WebInspector.ContextMenu.Provider, target).forEach(processProviders.bind(this));
+        self.runtime.extensions(WebInspector.ContextMenu.Provider, target).forEach(processProviders.bind(this));
 
         /**
-         * @param {!WebInspector.ModuleManager.Extension} extension
+         * @param {!Runtime.Extension} extension
          * @this {WebInspector.ContextMenu}
          */
         function processProviders(extension)
@@ -278,20 +321,9 @@ WebInspector.ContextMenu.Provider = function() {
 
 WebInspector.ContextMenu.Provider.prototype = {
     /**
+     * @param {!Event} event
      * @param {!WebInspector.ContextMenu} contextMenu
      * @param {!Object} target
      */
     appendApplicableItems: function(event, contextMenu, target) { }
 }
-
-WebInspector.contextMenuItemSelected = function(id)
-{
-    if (WebInspector._contextMenu)
-        WebInspector._contextMenu._itemSelected(id);
-}
-
-WebInspector.contextMenuCleared = function()
-{
-    // FIXME: Unfortunately, contextMenuCleared is invoked between show and item selected
-    // so we can't delete last menu object from WebInspector. Fix the contract.
-}