Skip to content

Commit df14a7b

Browse files
authored
Merge pull request #734 from ericprud/master
Add demo for promise-based context menu in text input
2 parents 1708487 + 59fec3a commit df14a7b

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed

documentation/demo/menu-promise.md

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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.off('contextmenu', rightClickHandler);
64+
65+
// when the items are ready,
66+
buildMenuItemsPromise().then(items => {
67+
68+
// store a callback on the trigger
69+
$this.data('runCallbackThingie', function () {
70+
return {
71+
callback: menuCallback,
72+
items: items
73+
};
74+
});
75+
const _offset = $this.offset(),
76+
position = {
77+
x: _offset.left + 10,
78+
y: _offset.top + 10
79+
}
80+
$this.contextMenu(position);
81+
82+
$this.on('contextmenu', rightClickHandler)
83+
});
84+
}
85+
86+
// callback insert letters over the selection
87+
function menuCallback (key, options) {
88+
const contents = $(this).val(),
89+
start = this.get(0).selectionStart,
90+
end = this.get(0).selectionEnd
91+
$(this).val(contents.substr(0, start)
92+
+ key
93+
+ contents.substr(end)
94+
)
95+
}
96+
});
97+
</script>
98+
99+
## Example HTML
100+
<div style="display:none;" class="showcase" data-showcase-import=".context-menu-one"></div>

0 commit comments

Comments
 (0)