ContextMenu.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. * Copyright (C) 2009 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. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above
  11. * copyright notice, this list of conditions and the following disclaimer
  12. * in the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Google Inc. nor the names of its
  15. * contributors may be used to endorse or promote products derived from
  16. * this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. /**
  31. * @constructor
  32. * @param {WebInspector.ContextSubMenuItem} topLevelMenu
  33. * @param {string} type
  34. * @param {string=} label
  35. * @param {boolean=} disabled
  36. * @param {boolean=} checked
  37. */
  38. WebInspector.ContextMenuItem = function(topLevelMenu, type, label, disabled, checked)
  39. {
  40. this._type = type;
  41. this._label = label;
  42. this._disabled = disabled;
  43. this._checked = checked;
  44. this._contextMenu = topLevelMenu;
  45. if (type === "item" || type === "checkbox")
  46. this._id = topLevelMenu.nextId();
  47. }
  48. WebInspector.ContextMenuItem.prototype = {
  49. id: function()
  50. {
  51. return this._id;
  52. },
  53. type: function()
  54. {
  55. return this._type;
  56. },
  57. /**
  58. * @return {boolean}
  59. */
  60. isEnabled: function()
  61. {
  62. return !this._disabled;
  63. },
  64. /**
  65. * @param {boolean} enabled
  66. */
  67. setEnabled: function(enabled)
  68. {
  69. this._disabled = !enabled;
  70. },
  71. _buildDescriptor: function()
  72. {
  73. switch (this._type) {
  74. case "item":
  75. return { type: "item", id: this._id, label: this._label, enabled: !this._disabled };
  76. case "separator":
  77. return { type: "separator" };
  78. case "checkbox":
  79. return { type: "checkbox", id: this._id, label: this._label, checked: !!this._checked, enabled: !this._disabled };
  80. }
  81. }
  82. }
  83. /**
  84. * @constructor
  85. * @extends {WebInspector.ContextMenuItem}
  86. * @param topLevelMenu
  87. * @param {string=} label
  88. * @param {boolean=} disabled
  89. */
  90. WebInspector.ContextSubMenuItem = function(topLevelMenu, label, disabled)
  91. {
  92. WebInspector.ContextMenuItem.call(this, topLevelMenu, "subMenu", label, disabled);
  93. /** @type {!Array.<!WebInspector.ContextMenuItem>} */
  94. this._items = [];
  95. }
  96. WebInspector.ContextSubMenuItem.prototype = {
  97. /**
  98. * @param {string} label
  99. * @param {function(?)} handler
  100. * @param {boolean=} disabled
  101. * @return {WebInspector.ContextMenuItem}
  102. */
  103. appendItem: function(label, handler, disabled)
  104. {
  105. var item = new WebInspector.ContextMenuItem(this._contextMenu, "item", label, disabled);
  106. this._pushItem(item);
  107. this._contextMenu._setHandler(item.id(), handler);
  108. return item;
  109. },
  110. /**
  111. * @param {string} label
  112. * @param {boolean=} disabled
  113. * @return {WebInspector.ContextMenuItem}
  114. */
  115. appendSubMenuItem: function(label, disabled)
  116. {
  117. var item = new WebInspector.ContextSubMenuItem(this._contextMenu, label, disabled);
  118. this._pushItem(item);
  119. return item;
  120. },
  121. /**
  122. * @param {boolean=} disabled
  123. */
  124. appendCheckboxItem: function(label, handler, checked, disabled)
  125. {
  126. var item = new WebInspector.ContextMenuItem(this._contextMenu, "checkbox", label, disabled, checked);
  127. this._pushItem(item);
  128. this._contextMenu._setHandler(item.id(), handler);
  129. return item;
  130. },
  131. appendSeparator: function()
  132. {
  133. if (this._items.length)
  134. this._pendingSeparator = true;
  135. },
  136. /**
  137. * @param {!WebInspector.ContextMenuItem} item
  138. */
  139. _pushItem: function(item)
  140. {
  141. if (this._pendingSeparator) {
  142. this._items.push(new WebInspector.ContextMenuItem(this._contextMenu, "separator"));
  143. delete this._pendingSeparator;
  144. }
  145. this._items.push(item);
  146. },
  147. /**
  148. * @return {boolean}
  149. */
  150. isEmpty: function()
  151. {
  152. return !this._items.length;
  153. },
  154. _buildDescriptor: function()
  155. {
  156. var result = { type: "subMenu", label: this._label, enabled: !this._disabled, subItems: [] };
  157. for (var i = 0; i < this._items.length; ++i)
  158. result.subItems.push(this._items[i]._buildDescriptor());
  159. return result;
  160. },
  161. __proto__: WebInspector.ContextMenuItem.prototype
  162. }
  163. /**
  164. * @constructor
  165. * @extends {WebInspector.ContextSubMenuItem}
  166. */
  167. WebInspector.ContextMenu = function(event) {
  168. WebInspector.ContextSubMenuItem.call(this, this, "");
  169. this._event = event;
  170. this._handlers = {};
  171. this._id = 0;
  172. }
  173. WebInspector.ContextMenu.prototype = {
  174. nextId: function()
  175. {
  176. return this._id++;
  177. },
  178. show: function()
  179. {
  180. var menuObject = this._buildDescriptor();
  181. if (menuObject.length) {
  182. WebInspector._contextMenu = this;
  183. InspectorFrontendHost.showContextMenu(this._event, menuObject);
  184. this._event.consume();
  185. }
  186. },
  187. showSoftMenu: function()
  188. {
  189. var menuObject = this._buildDescriptor();
  190. if (menuObject.length) {
  191. WebInspector._contextMenu = this;
  192. var softMenu = new WebInspector.SoftContextMenu(menuObject);
  193. softMenu.show(this._event, true);
  194. }
  195. this._event.consume();
  196. },
  197. _setHandler: function(id, handler)
  198. {
  199. if (handler)
  200. this._handlers[id] = handler;
  201. },
  202. _buildDescriptor: function()
  203. {
  204. var result = [];
  205. for (var i = 0; i < this._items.length; ++i)
  206. result.push(this._items[i]._buildDescriptor());
  207. return result;
  208. },
  209. _itemSelected: function(id)
  210. {
  211. if (this._handlers[id])
  212. this._handlers[id].call(this);
  213. },
  214. /**
  215. * @param {Object} target
  216. */
  217. appendApplicableItems: function(target)
  218. {
  219. for (var i = 0; i < WebInspector.ContextMenu._providers.length; ++i) {
  220. var provider = WebInspector.ContextMenu._providers[i];
  221. this.appendSeparator();
  222. provider.appendApplicableItems(this._event, this, target);
  223. this.appendSeparator();
  224. }
  225. },
  226. __proto__: WebInspector.ContextSubMenuItem.prototype
  227. }
  228. /**
  229. * @interface
  230. */
  231. WebInspector.ContextMenu.Provider = function() {
  232. }
  233. WebInspector.ContextMenu.Provider.prototype = {
  234. /**
  235. * @param {WebInspector.ContextMenu} contextMenu
  236. * @param {Object} target
  237. */
  238. appendApplicableItems: function(event, contextMenu, target) { }
  239. }
  240. /**
  241. * @param {WebInspector.ContextMenu.Provider} provider
  242. */
  243. WebInspector.ContextMenu.registerProvider = function(provider)
  244. {
  245. WebInspector.ContextMenu._providers.push(provider);
  246. }
  247. WebInspector.ContextMenu._providers = [];
  248. WebInspector.contextMenuItemSelected = function(id)
  249. {
  250. if (WebInspector._contextMenu)
  251. WebInspector._contextMenu._itemSelected(id);
  252. }
  253. WebInspector.contextMenuCleared = function()
  254. {
  255. // FIXME: Unfortunately, contextMenuCleared is invoked between show and item selected
  256. // so we can't delete last menu object from WebInspector. Fix the contract.
  257. }