SoftContextMenu.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
  17. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  18. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  20. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  21. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. /**
  26. * @constructor
  27. * @param {WebInspector.SoftContextMenu=} parentMenu
  28. */
  29. WebInspector.SoftContextMenu = function(items, parentMenu)
  30. {
  31. this._items = items;
  32. this._parentMenu = parentMenu;
  33. }
  34. WebInspector.SoftContextMenu.prototype = {
  35. /**
  36. * @param {boolean=} alignToCurrentTarget
  37. */
  38. show: function(event, alignToCurrentTarget)
  39. {
  40. this._x = event.x;
  41. this._y = event.y;
  42. this._time = new Date().getTime();
  43. // Absolutely position menu for iframes.
  44. var absoluteX = event.pageX;
  45. var absoluteY = event.pageY;
  46. var targetElement = event.target;
  47. while (targetElement && window !== targetElement.ownerDocument.defaultView) {
  48. var frameElement = targetElement.ownerDocument.defaultView.frameElement;
  49. absoluteY += frameElement.totalOffsetTop();
  50. absoluteX += frameElement.totalOffsetLeft();
  51. targetElement = frameElement;
  52. }
  53. // Create context menu.
  54. var targetRect;
  55. this._contextMenuElement = document.createElement("div");
  56. this._contextMenuElement.className = "soft-context-menu";
  57. this._contextMenuElement.tabIndex = 0;
  58. if (alignToCurrentTarget) {
  59. targetRect = event.currentTarget.getBoundingClientRect();
  60. // Align with bottom left of currentTarget by default.
  61. absoluteX = targetRect.left;
  62. absoluteY = targetRect.bottom;
  63. }
  64. this._contextMenuElement.style.top = absoluteY + "px";
  65. this._contextMenuElement.style.left = absoluteX + "px";
  66. this._contextMenuElement.addEventListener("mouseup", consumeEvent, false);
  67. this._contextMenuElement.addEventListener("keydown", this._menuKeyDown.bind(this), false);
  68. for (var i = 0; i < this._items.length; ++i)
  69. this._contextMenuElement.appendChild(this._createMenuItem(this._items[i]));
  70. // Install glass pane capturing events.
  71. if (!this._parentMenu) {
  72. this._glassPaneElement = document.createElement("div");
  73. this._glassPaneElement.className = "soft-context-menu-glass-pane";
  74. this._glassPaneElement.tabIndex = 0;
  75. this._glassPaneElement.addEventListener("mouseup", this._glassPaneMouseUp.bind(this), false);
  76. this._glassPaneElement.appendChild(this._contextMenuElement);
  77. document.body.appendChild(this._glassPaneElement);
  78. this._focus();
  79. } else
  80. this._parentMenu._parentGlassPaneElement().appendChild(this._contextMenuElement);
  81. // Re-position menu in case it does not fit.
  82. if (document.body.offsetWidth < this._contextMenuElement.offsetLeft + this._contextMenuElement.offsetWidth) {
  83. if (alignToCurrentTarget)
  84. this._contextMenuElement.style.left = Math.max(0, targetRect.right - this._contextMenuElement.offsetWidth) + "px";
  85. else
  86. this._contextMenuElement.style.left = (absoluteX - this._contextMenuElement.offsetWidth) + "px";
  87. }
  88. if (document.body.offsetHeight < this._contextMenuElement.offsetTop + this._contextMenuElement.offsetHeight) {
  89. if (alignToCurrentTarget)
  90. this._contextMenuElement.style.top = Math.max(0, targetRect.top - this._contextMenuElement.offsetHeight) + "px";
  91. else
  92. this._contextMenuElement.style.top = (document.body.offsetHeight - this._contextMenuElement.offsetHeight) + "px";
  93. }
  94. event.consume(true);
  95. },
  96. _parentGlassPaneElement: function()
  97. {
  98. if (this._glassPaneElement)
  99. return this._glassPaneElement;
  100. if (this._parentMenu)
  101. return this._parentMenu._parentGlassPaneElement();
  102. return null;
  103. },
  104. _createMenuItem: function(item)
  105. {
  106. if (item.type === "separator")
  107. return this._createSeparator();
  108. if (item.type === "subMenu")
  109. return this._createSubMenu(item);
  110. var menuItemElement = document.createElement("div");
  111. menuItemElement.className = "soft-context-menu-item";
  112. var checkMarkElement = document.createElement("span");
  113. checkMarkElement.textContent = "\u2713 "; // Checkmark Unicode symbol
  114. checkMarkElement.className = "soft-context-menu-item-checkmark";
  115. if (!item.checked)
  116. checkMarkElement.style.opacity = "0";
  117. menuItemElement.appendChild(checkMarkElement);
  118. menuItemElement.appendChild(document.createTextNode(item.label));
  119. menuItemElement.addEventListener("mousedown", this._menuItemMouseDown.bind(this), false);
  120. menuItemElement.addEventListener("mouseup", this._menuItemMouseUp.bind(this), false);
  121. // Manually manage hover highlight since :hover does not work in case of click-and-hold menu invocation.
  122. menuItemElement.addEventListener("mouseover", this._menuItemMouseOver.bind(this), false);
  123. menuItemElement.addEventListener("mouseout", this._menuItemMouseOut.bind(this), false);
  124. menuItemElement._actionId = item.id;
  125. return menuItemElement;
  126. },
  127. _createSubMenu: function(item)
  128. {
  129. var menuItemElement = document.createElement("div");
  130. menuItemElement.className = "soft-context-menu-item";
  131. menuItemElement._subItems = item.subItems;
  132. // Occupy the same space on the left in all items.
  133. var checkMarkElement = document.createElement("span");
  134. checkMarkElement.textContent = "\u2713 "; // Checkmark Unicode symbol
  135. checkMarkElement.className = "soft-context-menu-item-checkmark";
  136. checkMarkElement.style.opacity = "0";
  137. menuItemElement.appendChild(checkMarkElement);
  138. var subMenuArrowElement = document.createElement("span");
  139. subMenuArrowElement.textContent = "\u25B6"; // BLACK RIGHT-POINTING TRIANGLE
  140. subMenuArrowElement.className = "soft-context-menu-item-submenu-arrow";
  141. menuItemElement.appendChild(document.createTextNode(item.label));
  142. menuItemElement.appendChild(subMenuArrowElement);
  143. menuItemElement.addEventListener("mousedown", this._menuItemMouseDown.bind(this), false);
  144. menuItemElement.addEventListener("mouseup", this._menuItemMouseUp.bind(this), false);
  145. // Manually manage hover highlight since :hover does not work in case of click-and-hold menu invocation.
  146. menuItemElement.addEventListener("mouseover", this._menuItemMouseOver.bind(this), false);
  147. menuItemElement.addEventListener("mouseout", this._menuItemMouseOut.bind(this), false);
  148. return menuItemElement;
  149. },
  150. _createSeparator: function()
  151. {
  152. var separatorElement = document.createElement("div");
  153. separatorElement.className = "soft-context-menu-separator";
  154. separatorElement._isSeparator = true;
  155. separatorElement.addEventListener("mouseover", this._hideSubMenu.bind(this), false);
  156. separatorElement.createChild("div", "separator-line");
  157. return separatorElement;
  158. },
  159. _menuItemMouseDown: function(event)
  160. {
  161. // Do not let separator's mouse down hit menu's handler - we need to receive mouse up!
  162. event.consume(true);
  163. },
  164. _menuItemMouseUp: function(event)
  165. {
  166. this._triggerAction(event.target, event);
  167. event.consume();
  168. },
  169. _focus: function()
  170. {
  171. this._contextMenuElement.focus();
  172. },
  173. _triggerAction: function(menuItemElement, event)
  174. {
  175. if (!menuItemElement._subItems) {
  176. this._discardMenu(true, event);
  177. if (typeof menuItemElement._actionId !== "undefined") {
  178. WebInspector.contextMenuItemSelected(menuItemElement._actionId);
  179. delete menuItemElement._actionId;
  180. }
  181. return;
  182. }
  183. this._showSubMenu(menuItemElement, event);
  184. event.consume();
  185. },
  186. _showSubMenu: function(menuItemElement, event)
  187. {
  188. if (menuItemElement._subMenuTimer) {
  189. clearTimeout(menuItemElement._subMenuTimer);
  190. delete menuItemElement._subMenuTimer;
  191. }
  192. if (this._subMenu)
  193. return;
  194. this._subMenu = new WebInspector.SoftContextMenu(menuItemElement._subItems, this);
  195. this._subMenu.show(this._buildMouseEventForSubMenu(menuItemElement));
  196. },
  197. _buildMouseEventForSubMenu: function(subMenuItemElement)
  198. {
  199. var subMenuOffset = { x: subMenuItemElement.offsetWidth - 3, y: subMenuItemElement.offsetTop - 1 };
  200. var targetX = this._x + subMenuOffset.x;
  201. var targetY = this._y + subMenuOffset.y;
  202. var targetPageX = parseInt(this._contextMenuElement.style.left, 10) + subMenuOffset.x;
  203. var targetPageY = parseInt(this._contextMenuElement.style.top, 10) + subMenuOffset.y;
  204. return { x: targetX, y: targetY, pageX: targetPageX, pageY: targetPageY, consume: function() {} };
  205. },
  206. _hideSubMenu: function()
  207. {
  208. if (!this._subMenu)
  209. return;
  210. this._subMenu._discardSubMenus();
  211. this._focus();
  212. },
  213. _menuItemMouseOver: function(event)
  214. {
  215. this._highlightMenuItem(event.target);
  216. },
  217. _menuItemMouseOut: function(event)
  218. {
  219. if (!this._subMenu || !event.relatedTarget) {
  220. this._highlightMenuItem(null);
  221. return;
  222. }
  223. var relatedTarget = event.relatedTarget;
  224. if (this._contextMenuElement.isSelfOrAncestor(relatedTarget) || relatedTarget.hasStyleClass("soft-context-menu-glass-pane"))
  225. this._highlightMenuItem(null);
  226. },
  227. _highlightMenuItem: function(menuItemElement)
  228. {
  229. if (this._highlightedMenuItemElement === menuItemElement)
  230. return;
  231. this._hideSubMenu();
  232. if (this._highlightedMenuItemElement) {
  233. this._highlightedMenuItemElement.removeStyleClass("soft-context-menu-item-mouse-over");
  234. if (this._highlightedMenuItemElement._subItems && this._highlightedMenuItemElement._subMenuTimer) {
  235. clearTimeout(this._highlightedMenuItemElement._subMenuTimer);
  236. delete this._highlightedMenuItemElement._subMenuTimer;
  237. }
  238. }
  239. this._highlightedMenuItemElement = menuItemElement;
  240. if (this._highlightedMenuItemElement) {
  241. this._highlightedMenuItemElement.addStyleClass("soft-context-menu-item-mouse-over");
  242. this._contextMenuElement.focus();
  243. if (this._highlightedMenuItemElement._subItems && !this._highlightedMenuItemElement._subMenuTimer)
  244. this._highlightedMenuItemElement._subMenuTimer = setTimeout(this._showSubMenu.bind(this, this._highlightedMenuItemElement, this._buildMouseEventForSubMenu(this._highlightedMenuItemElement)), 150);
  245. }
  246. },
  247. _highlightPrevious: function()
  248. {
  249. var menuItemElement = this._highlightedMenuItemElement ? this._highlightedMenuItemElement.previousSibling : this._contextMenuElement.lastChild;
  250. while (menuItemElement && menuItemElement._isSeparator)
  251. menuItemElement = menuItemElement.previousSibling;
  252. if (menuItemElement)
  253. this._highlightMenuItem(menuItemElement);
  254. },
  255. _highlightNext: function()
  256. {
  257. var menuItemElement = this._highlightedMenuItemElement ? this._highlightedMenuItemElement.nextSibling : this._contextMenuElement.firstChild;
  258. while (menuItemElement && menuItemElement._isSeparator)
  259. menuItemElement = menuItemElement.nextSibling;
  260. if (menuItemElement)
  261. this._highlightMenuItem(menuItemElement);
  262. },
  263. _menuKeyDown: function(event)
  264. {
  265. switch (event.keyIdentifier) {
  266. case "Up":
  267. this._highlightPrevious(); break;
  268. case "Down":
  269. this._highlightNext(); break;
  270. case "Left":
  271. if (this._parentMenu) {
  272. this._highlightMenuItem(null);
  273. this._parentMenu._focus();
  274. }
  275. break;
  276. case "Right":
  277. if (!this._highlightedMenuItemElement)
  278. break;
  279. if (this._highlightedMenuItemElement._subItems) {
  280. this._showSubMenu(this._highlightedMenuItemElement, this._buildMouseEventForSubMenu(this._highlightedMenuItemElement));
  281. this._subMenu._focus();
  282. this._subMenu._highlightNext();
  283. }
  284. break;
  285. case "U+001B": // Escape
  286. this._discardMenu(true, event); break;
  287. case "Enter":
  288. if (!isEnterKey(event))
  289. break;
  290. // Fall through
  291. case "U+0020": // Space
  292. if (this._highlightedMenuItemElement)
  293. this._triggerAction(this._highlightedMenuItemElement, event);
  294. break;
  295. }
  296. event.consume(true);
  297. },
  298. _glassPaneMouseUp: function(event)
  299. {
  300. // Return if this is simple 'click', since dispatched on glass pane, can't use 'click' event.
  301. if (event.x === this._x && event.y === this._y && new Date().getTime() - this._time < 300)
  302. return;
  303. this._discardMenu(true, event);
  304. event.consume();
  305. },
  306. /**
  307. * @param {boolean} closeParentMenus
  308. * @param {Event=} event
  309. */
  310. _discardMenu: function(closeParentMenus, event)
  311. {
  312. if (this._subMenu && !closeParentMenus)
  313. return;
  314. if (this._glassPaneElement) {
  315. var glassPane = this._glassPaneElement;
  316. delete this._glassPaneElement;
  317. // This can re-enter discardMenu due to blur.
  318. document.body.removeChild(glassPane);
  319. if (this._parentMenu) {
  320. delete this._parentMenu._subMenu;
  321. if (closeParentMenus)
  322. this._parentMenu._discardMenu(closeParentMenus, event);
  323. }
  324. if (event)
  325. event.consume(true);
  326. } else if (this._parentMenu && this._contextMenuElement.parentElement) {
  327. this._discardSubMenus();
  328. if (closeParentMenus)
  329. this._parentMenu._discardMenu(closeParentMenus, event);
  330. if (event)
  331. event.consume(true);
  332. }
  333. },
  334. _discardSubMenus: function()
  335. {
  336. if (this._subMenu)
  337. this._subMenu._discardSubMenus();
  338. this._contextMenuElement.remove();
  339. if (this._parentMenu)
  340. delete this._parentMenu._subMenu;
  341. }
  342. }
  343. if (!InspectorFrontendHost.showContextMenu) {
  344. InspectorFrontendHost.showContextMenu = function(event, items)
  345. {
  346. new WebInspector.SoftContextMenu(items).show(event);
  347. }
  348. }