ui-contextmenu
Version:
Turn a jQuery UI Menu widget into a contextmenu.
212 lines (184 loc) • 7 kB
HTML
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
<title>jquery.ui-contextmenu.js - Demo</title>
<!--
<link href="../node_modules/jquery-ui/themes/base/jquery.ui.all.css" rel="stylesheet">
<script src="../lib/jquery.js"></script>
<script src="../lib/jquery-ui.custom.js"></script>
-->
<link href="../node_modules/jquery-ui-dist/jquery-ui.css" rel="stylesheet">
<script src="../node_modules/jquery/dist/jquery.js"></script>
<script src="../node_modules/jquery-ui-dist/jquery-ui.js"></script>
<!-- Some custom library to enable 'taphold' events -->
<script src="../lib/jquery-taphold/taphold.js"></script>
<script src="../jquery.ui-contextmenu.js"></script>
<style type="text/css">
.hasmenu, .hasmenu2 {
border: 1px solid #008;
margin: 3px;
padding: 5px;
width: 30px;
}
.ui-widget{
font-size: .8em;
}
.ui-menu {
width: 150px;
}
/* Define a custom icon */
.ui-icon.custom-icon-firefox {
background-image: url(application_firefox.gif);
background-position: 0 0;
}
</style>
<script type="text/javascript">
var CLIPBOARD = "";
$(function(){
/* Menu 1: init by passing an array of entries. */
$(document).contextmenu({
autoFocus: true,
delegate: ".hasmenu",
preventContextMenuForPopup: true,
preventSelect: true,
taphold: true,
menu: [
{title: "Cut", cmd: "cut", uiIcon: "ui-icon-scissors" },
{title: "Copy", cmd: "copy", uiIcon: "ui-icon-copy"},
{title: "Paste", cmd: "paste", uiIcon: "ui-icon-clipboard", disabled: true },
{title: "----"},
{title: "More", children: [
{title: "Sub 1 (using callback)", cmd: "sub1", action: function(event, ui) { alert("action callback sub1");} },
{title: "Sub 2", cmd: "sub2", disabled: function(){ return true; } },
{title: "Sub 3", cmd: "sub3", disabled: function(){ return "hide"; } },
{cmd: "sub4", title: function(){ return "Sub 4" + Date.now(); } }
]}
],
// Handle menu selection to implement a fake-clipboard
select: function(event, ui) {
var $target = ui.target;
switch(ui.cmd){
case "copy":
CLIPBOARD = $target.text();
break
case "paste":
CLIPBOARD = "";
break
}
alert("select " + ui.cmd + " on " + $target.text());
// Optionally return false, to prevent closing the menu now
},
// Implement the beforeOpen callback to dynamically change the entries
beforeOpen: function(event, ui) {
var $menu = ui.menu,
$target = ui.target,
extraData = ui.extraData; // passed when menu was opened by call to open()
// console.log("beforeOpen", event, ui, event.originalEvent.type);
console.log("beforeOpen", event, ui);
// ui.menu.zIndex( $(event.target).zIndex() + 1);
$(document)
// .contextmenu("replaceMenu", [{title: "aaa"}, {title: "bbb"}])
// .contextmenu("replaceMenu", "#options2")
// .contextmenu("updateEntry", "cut", {title: "Cuty", uiIcon: "ui-icon-heart", disabled: true})
.contextmenu("setTitle", "copy", "Copy '" + $target.text() + "'")
.contextmenu("setTitle", "paste", "Paste" + (CLIPBOARD ? " '" + CLIPBOARD + "'" : ""))
.contextmenu("enableEntry", "paste", (CLIPBOARD !== ""));
// Optionally return false, to prevent opening the menu now
}
});
/* Menu 2: init menu by passing an <ul> element. */
$("#container").contextmenu({
delegate: ".hasmenu2",
menu: "#options",
// position: {my: "left top", at: "left bottom"},
position: function(event, ui){
return {my: "left top", at: "left bottom", of: ui.target};
},
preventSelect: true,
taphold: true,
show: { effect: "fold", duration: "slow"},
hide: { effect: "explode", duration: "slow" },
focus: function(event, ui) {
var menuId = ui.item.find(">a").attr("href");
$("#info").text("focus " + menuId);
console.log("focus", ui.item);
},
blur: function(event, ui) {
$("#info").text("");
console.log("blur", ui.item);
},
beforeOpen: function(event, ui) {
// $("#container").contextmenu("replaceMenu", "#options2");
// $("#container").contextmenu("replaceMenu", [{title: "aaa"}, {title: "bbb"}]);
},
open: function(event, ui) {
// alert("open on " + ui.target.text());
},
select: function(event, ui) {
alert("select " + ui.cmd + " on " + ui.target.text());
}
});
/* Open and close an existing menu without programatically. */
$("#triggerPopup").click(function(){
// Trigger popup menu on the first target element
$(document).contextmenu("open", $(".hasmenu:first"), {foo: "bar"});
setTimeout(function(){
$(document).contextmenu("close");
}, 2000);
});
});
</script>
</head>
<body class="example">
<h1>jquery.ui-contextmenu.js</h1>
<p><a href="https://github.com/mar10/jquery-ui-contextmenu">View project on GitHub</a></p>
<h3>Sample 1</h3>
<ul>
<li>Initialized using a command-array.
<li>Entry 'More - Sub1' uses the callback syntax.
<li>The menu is modified in the `beforeOpen` event (disabling an renaming entries).
<li>`preventSelect: true` prevents accidential selection of the menu
targets (i.e. 'AAA') when double-clicking or dragging the mouse.
<li>`taphold: true` enables long-press to open the menu (useful on
tablet computers).
<li>Ctrl+Click or two-finger-click on the touchpad also works (MacBook).
</ul>
<p>Right-click in an element to open the context menu:</p>
<div>
<span class="hasmenu">AAA</span>
<span class="hasmenu">BBB</span>
<span class="hasmenu">CCC</span>
</div>
<h3>Sample 2</h3>
<ul>
<li>Initialized by hidden <ul> element.
<li>Use custom show/hide effects.
<li>Define custom position.
</ul>
<div id="container">
<span class="hasmenu2">AAA</span>
<span class="hasmenu2">BBB</span>
<span class="hasmenu2">CCC</span>
</div>
<ul id="options" style="display: none;">
<li><a href="#action1"><span class="ui-icon custom-icon-firefox"></span>Action 1</a>
<li><a href="#action2"><span class="ui-icon ui-icon-heart"></span>Action 2</a>
<li class="ui-state-disabled"><a href="#action3">Action 3</a>
<li>----
<li><a>Extra</a>
<ul>
<li><a href="#action4">sub4</a>
<li><a href="#action5">sub5</a>
</ul>
</ul>
<ul id="options2" class="ui-helper-hidden">
<li><a href="#action2"><span class="ui-icon ui-icon-heart"></span>Action 2</a>
<li class="ui-state-disabled"><a href="#action3">Action 3</a>
</ul>
<h3>Sample 3</h3>
<p>Open context menu using <code>$("#container").contextmenu("open", $(".hasmenu:first"))</code> and close after 2 sec.:</p>
<button id="triggerPopup">Trigger popup</button>
<a href="https://github.com/mar10/jquery-ui-contextmenu/"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://github-camo.global.ssl.fastly.net/652c5b9acfaddf3a9c326fa6bde407b87f7be0f4/68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f6f72616e67655f6666373630302e706e67" alt="Fork me on GitHub" data-canonical-src="https://s3.amazonaws.com/github/ribbons/forkmerightorange_ff7600.png"></a>
</body>
</html>