|
11 | 11 | * Licensed under |
12 | 12 | * MIT License http://www.opensource.org/licenses/mit-license |
13 | 13 | * |
14 | | - * Date: 2025-11-04T11:10:13.179Z |
| 14 | + * Date: 2025-11-04T11:25:40.325Z |
15 | 15 | */ |
16 | 16 |
|
17 | 17 | // jscs:disable |
|
151 | 151 | // This overrides the reposition option. |
152 | 152 | hideOnSecondTrigger: false, |
153 | 153 |
|
| 154 | + // use a modal layer for closing the menu rather than a captured event on document |
| 155 | + useModal: true, |
| 156 | + |
154 | 157 | //ability to select submenu |
155 | 158 | selectableSubMenu: false, |
156 | 159 |
|
|
460 | 463 | hoveract.timer = null; |
461 | 464 | }, |
462 | 465 | // click on layer to hide contextMenu |
463 | | - layerClick: function (e) { |
| 466 | + layerClick: function (e, opt, onhide) { |
464 | 467 | var $this = $(this), |
465 | | - root = $this.data('contextMenuRoot'), |
| 468 | + root = (opt !== undefined) ? opt : $this.data('contextMenuRoot'), |
466 | 469 | button = e.button, |
467 | 470 | x = e.pageX, |
468 | 471 | y = e.pageY, |
469 | 472 | fakeClick = x === undefined, |
470 | 473 | target, |
471 | 474 | offset; |
472 | 475 |
|
| 476 | + // If the click is not real, things break: https://github.com/swisnl/jQuery-contextMenu/issues/132 |
| 477 | + if(fakeClick){ |
| 478 | + if (root !== null && typeof root !== 'undefined' && root.$menu !== null && typeof root.$menu !== 'undefined') { |
| 479 | + root.$menu.trigger('contextmenu:hide'); |
| 480 | + } |
| 481 | + return; |
| 482 | + } |
| 483 | + |
| 484 | + // if the click closing is done through windwow event listener rather than a transparent layer |
| 485 | + if (!root.$layer) { |
| 486 | + target = document.elementFromPoint(x - $win.scrollLeft(), y - $win.scrollTop()); |
| 487 | + if (root.$menu === null || typeof root.$menu === 'undefined' || !root.$menu[0].contains(target)) { |
| 488 | + |
| 489 | + root.$menu.trigger('contextmenu:hide'); |
| 490 | + if (typeof onhide !== 'undefined') |
| 491 | + onhide(); |
| 492 | + } |
| 493 | + return; |
| 494 | + } |
473 | 495 | e.preventDefault(); |
474 | 496 |
|
475 | 497 | setTimeout(function () { |
476 | | - // If the click is not real, things break: https://github.com/swisnl/jQuery-contextMenu/issues/132 |
477 | | - if(fakeClick){ |
478 | | - if (root !== null && typeof root !== 'undefined' && root.$menu !== null && typeof root.$menu !== 'undefined') { |
479 | | - root.$menu.trigger('contextmenu:hide'); |
480 | | - } |
481 | | - return; |
482 | | - } |
483 | 498 |
|
484 | 499 | var $window; |
485 | 500 | var triggerAction = ((root.trigger === 'left' && button === 0) || (root.trigger === 'right' && button === 2)); |
|
1022 | 1037 | css = {}; |
1023 | 1038 |
|
1024 | 1039 | // hide any open menus |
1025 | | - $('#context-menu-layer').trigger('mousedown'); |
| 1040 | + if ($('#context-menu-layer').length > 0) |
| 1041 | + $('#context-menu-layer').trigger('mousedown'); |
| 1042 | + else |
| 1043 | + $(document).trigger('contextmenu:hide'); |
1026 | 1044 |
|
1027 | 1045 | // backreference for callbacks |
1028 | 1046 | opt.$trigger = $trigger; |
|
1569 | 1587 | return hasVisibleItems; |
1570 | 1588 | }, |
1571 | 1589 | layer: function (opt, zIndex) { |
| 1590 | + if (!opt.useModal) { |
| 1591 | + var listener = function (ev) { |
| 1592 | + handle.layerClick(ev, opt, function() { |
| 1593 | + document.removeEventListener('mousedown', listener, true); |
| 1594 | + }); |
| 1595 | + }; |
| 1596 | + document.addEventListener('mousedown', listener, true); |
| 1597 | + return; |
| 1598 | + } |
| 1599 | + |
1572 | 1600 | // add transparent layer for click area |
1573 | 1601 | // filter and background for Internet Explorer, Issue #23 |
1574 | 1602 | var $layer = opt.$layer = $('<div id="context-menu-layer"></div>') |
|
0 commit comments