From 340c6c39ee29a9c14662342ac10faabe6953606d Mon Sep 17 00:00:00 2001 From: bjorn Date: Wed, 29 Jul 2026 16:25:40 +0200 Subject: [PATCH] feat: make the trigger reachable from item-level events handlers Item `events` handlers are bound with their menu's options object as jQuery event data, so `e.data.$trigger` already exposed the trigger for items in the root menu. Items in a sub-menu got the sub-menu's own options object, which never had `$trigger` set, leaving no supported way to reach the element that opened the menu. op.show() now stamps the trigger onto the root options object and every already-created sub-menu's options object, and op.create() inherits it from the root for sub-menus built later (a resolving promise, for instance). The `events` documentation now spells out what `this` and `e.data` contain, and its example was fixed: it nested the item under `events` instead of `items`. Added a demo showing a text input renaming the button that opened the menu. Closes #729 --- documentation/_data/nav.js | 1 + documentation/demo/input-rename-trigger.md | 63 +++++++ documentation/docs/items.md | 32 +++- src/jquery.contextMenu.js | 31 +++- .../issue-729-trigger-in-item-events.test.js | 154 ++++++++++++++++++ 5 files changed, 279 insertions(+), 2 deletions(-) create mode 100644 documentation/demo/input-rename-trigger.md create mode 100644 test/unit/issue-729-trigger-in-item-events.test.js diff --git a/documentation/_data/nav.js b/documentation/_data/nav.js index 8d7db221..07ee3024 100644 --- a/documentation/_data/nav.js +++ b/documentation/_data/nav.js @@ -47,6 +47,7 @@ module.exports = [ { id: 'html5-polyfill', text: 'HTML5 polyfill', url: '/demo/html5-polyfill.html' }, { id: 'html5-polyfill-firefox8', text: 'HTML5 polyfill (Firefox)', url: '/demo/html5-polyfill-firefox8.html' }, { id: 'input', text: 'Input Commands', url: '/demo/input.html' }, + { id: 'input-rename-trigger', text: 'Input Commands (accessing the trigger)', url: '/demo/input-rename-trigger.html' }, { id: 'keeping-contextmenu-open', text: 'Keeping the context menu open', url: '/demo/keeping-contextmenu-open.html' }, { id: 'menu-title', text: 'Menus with titles', url: '/demo/menu-title.html' }, { id: 'menu-promise', text: 'Menu with promise', url: '/demo/menu-promise.html' }, diff --git a/documentation/demo/input-rename-trigger.md b/documentation/demo/input-rename-trigger.md new file mode 100644 index 00000000..1db4babb --- /dev/null +++ b/documentation/demo/input-rename-trigger.md @@ -0,0 +1,63 @@ +--- +currentMenu: input-rename-trigger +--- + +# Demo: Renaming the trigger from an input command + + + + + +- [Example code](#example-code) +- [Example HTML](#example-html) + + + +Item level [`events`](../docs/items.html#events) handlers are bound with the menu's +options object as jQuery event data, so `e.data.$trigger` is the element that opened +the menu. Inside the handler `this` is the `` itself, exactly like any other +jQuery event handler. + +Both buttons below share a single menu definition. Left-click one, type a new label +and click somewhere else so the input loses focus. The button you clicked is renamed, +the other one is not. + + + + +## Example code + + + +## Example HTML + diff --git a/documentation/docs/items.md b/documentation/docs/items.md index 2b49cca8..7f903a93 100644 --- a/documentation/docs/items.md +++ b/documentation/docs/items.md @@ -316,12 +316,20 @@ __Only used with [types](#type) `text`, `textarea`, `radio`, `checkbox` and `sel `events`: `object` +Inside a handler: + +* `this` is the ``, `