Skip to content

Commit 8631fa2

Browse files
author
Eric Prud'hommeaux
committed
Add demo for promise-based context menu in text input
1 parent 2ae293c commit 8631fa2

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed

documentation/demo/menu-promise.md

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
---
2+
currentMenu: menu-promise
3+
---
4+
5+
# Demo: Submenu through promise (asynchronous)
6+
7+
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
8+
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
9+
10+
11+
- [Example code](#example-code)
12+
- [Example HTML](#example-html)
13+
14+
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
15+
16+
<h3>right-clickable context menus</h3>
17+
<ul>
18+
<li><label for="immediate">immediate:</label><input id="immediate"/></li>
19+
<li><label for="async">async:</label><input id="async"/></li>
20+
</ul>
21+
22+
## Example code
23+
24+
<script type="text/javascript" class="showcase">
25+
$(function() {
26+
// normally, contextMenu sets up a handler for right click
27+
$.contextMenu({
28+
selector: '#immediate',
29+
callback: menuCallback,
30+
items: buildMenuItems()
31+
});
32+
33+
// for async menus, we set up a handler and a menu for it to call
34+
$('#async').on('contextmenu', rightClickHandler)
35+
$.contextMenu({
36+
selector: '#async',
37+
trigger: 'none',
38+
build: function($trigger, e) {
39+
// return callback set by the mouseup handler
40+
return $trigger.data('runCallbackThingie')();
41+
}
42+
});
43+
44+
function buildMenuItems () {
45+
return {
46+
"GTA": {name: "Lysine", icon: "edit"},
47+
"TGG": {name: "Tryptophan", icon: "cut"},
48+
"TTC": {name: "Phenylalanine", icon: "copy"}
49+
}
50+
}
51+
52+
// asynchronous menu item construction takes one second
53+
function buildMenuItemsPromise () {
54+
return new Promise((accept, reject) => {
55+
setTimeout(() => accept(buildMenuItems()), 1000)
56+
})
57+
}
58+
59+
// right-click handler enables mouseup handler
60+
function rightClickHandler (e) {
61+
e.preventDefault();
62+
const $this = $(this)
63+
$this.on('mouseup', mouseUpHandler);
64+
$this.off('contextmenu', rightClickHandler)
65+
}
66+
67+
// on mouseup, wait for the menu items and paint a context menu
68+
function mouseUpHandler (e) {
69+
e.preventDefault();
70+
const $this = $(this);
71+
$this.off('mouseup', mouseUpHandler);
72+
73+
// when the items are ready,
74+
buildMenuItemsPromise().then(items => {
75+
76+
// store a callback on the trigger
77+
$this.data('runCallbackThingie', function () {
78+
return {
79+
callback: menuCallback,
80+
items: items
81+
};
82+
});
83+
const _offset = $this.offset(),
84+
position = {
85+
x: _offset.left + 10,
86+
y: _offset.top + 10
87+
}
88+
$this.contextMenu(position);
89+
90+
$this.on('contextmenu', rightClickHandler)
91+
});
92+
return false
93+
}
94+
95+
// callback insert letters over the selection
96+
function menuCallback (key, options) {
97+
const contents = $(this).val(),
98+
start = this.get(0).selectionStart,
99+
end = this.get(0).selectionEnd
100+
$(this).val(contents.substr(0, start)
101+
+ key
102+
+ contents.substr(end)
103+
)
104+
}
105+
});
106+
</script>
107+
108+
## Example HTML
109+
<div style="display:none;" class="showcase" data-showcase-import=".context-menu-one"></div>

0 commit comments

Comments
 (0)