Skip to content

Commit 7fc904c

Browse files
committed
prepared taphold
1 parent 6f2ced1 commit 7fc904c

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

jquery.contextmenu.js

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@
1515
return $menu.data("ui-menu") || $menu.data("menu");
1616
}
1717
$.widget("ui.contextmenu", {
18-
version: "0.1.0",
18+
version: "0.0.1",
1919
options: {
2020
delegate: "[data-menu]", // selector
2121
menu: null, // selector or jQuery or a function returning such
22+
taphold: 2000, // open menu after 2000 ms long touch
2223
// Events:
2324
beforeopen: $.noop, // menu about to open; return `false` to prevent opening
2425
blur: $.noop, // menu option lost focus
@@ -30,7 +31,32 @@
3031
select: $.noop // menu option was selected; return `false` to prevent closing
3132
},
3233
_create: function () {
33-
this.element.delegate(this.options.delegate, "contextmenu.contextmenu", $.proxy(this._openMenu, this));
34+
this.element.delegate(this.options.delegate, "contextmenu.contextmenu", $.proxy(this._openMenu, this));
35+
// emulate a 'taphold' event
36+
/*
37+
this.element.delegate(this.options.delegate, "mousedown.contextmenu", $.proxy(function(event, ui){
38+
var self = this;
39+
console.log("Event ", event.type, this.timer);
40+
if(this.timer){
41+
console.log(" clear " + this.timer);
42+
clearTimeout(this.timer);
43+
this.timer = null;
44+
}
45+
this.timer = setTimeout(function(){
46+
console.log("Timeout ", event.type, self.timer);
47+
self.open.call(self, $(event.target));
48+
self.timer = null;
49+
}, this.options.taphold);
50+
console.log("Event started ", event.type, this.timer);
51+
}, this));
52+
this.element.delegate(this.options.delegate, "mouseup.contextmenu", $.proxy(function(){
53+
if(this.timer){
54+
console.log("Event ", event.type, "clear" + this.timer);
55+
clearTimeout(this.timer);
56+
this.timer = null;
57+
}
58+
}, this));
59+
*/
3460
this._trigger("init");
3561
},
3662
/** Return menu jQuery object. */
@@ -108,6 +134,10 @@
108134
_closeMenu: function(){
109135
var self = this,
110136
$menu = this._getMenu();
137+
if(this.timer){
138+
clearTimeout(this.timer);
139+
this.timer = null;
140+
}
111141
$menu.fadeOut(function() {
112142
self._trigger("close");
113143
});

sample-widget.html

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@
1414
font-family: "Trebuchet MS", "Helvetica", "Arial", "Verdana", "sans-serif";
1515
font-size: 62.5%;
1616
}
17+
.hasmenu{
18+
border: 1px solid #008;
19+
margin: 3px;
20+
padding: 2px;
21+
width: 30px;
22+
}
1723
</style>
1824

1925
<!-- Add code to initialize the tree when the document is loaded: -->
@@ -33,7 +39,7 @@
3339
// alert("beforeopen on " + $(event.relatedTarget).text());
3440
},
3541
open: function(event) {
36-
alert("open on " + $(event.relatedTarget).text());
42+
// alert("open on " + $(event.relatedTarget).text());
3743
},
3844
select: function(event, ui) {
3945
var menuId = ui.item.find(">a").attr("href");
@@ -62,7 +68,7 @@ <h1>jquery.contextmenu.js</h1>
6268

6369
<ul id="options" class="ui-helper-hidden">
6470
<li><a href="#action1">Action 1</a>
65-
<li><a href="#action2">Action 2</a>
71+
<li><a href="#action2"><span class="ui-icon ui-icon-heart"></span>Action 2</a>
6672
<li class="ui-state-disabled"><a href="#action3">Action 3</a>
6773
<li>----
6874
<li><a>Extra</a>

0 commit comments

Comments
 (0)