|
14 | 14 | $menu = $(menu);
|
15 | 15 | return $menu.data("ui-menu") || $menu.data("menu");
|
16 | 16 | }
|
| 17 | + |
| 18 | + var startTime, endTime; |
| 19 | + var gbMove = false; |
| 20 | + |
| 21 | + window.addEventListener('touchstart',function(event) { |
| 22 | + startTime = new Date().getTime(); |
| 23 | + gbMove = false; |
| 24 | + alert('tap hold s event'); |
| 25 | + }, false); |
| 26 | + |
| 27 | + window.addEventListener('touchmove',function(event) { |
| 28 | + gbMove = true; |
| 29 | + }, false); |
| 30 | + |
| 31 | + window.addEventListener('touchend',function(event) { |
| 32 | + endTime = new Date().getTime(); |
| 33 | + if(!gbMove && (endTime-startTime)/1000 > 2) |
| 34 | + alert('tap hold event'); |
| 35 | + }, false); |
| 36 | + |
17 | 37 | $.widget("ui.contextmenu", {
|
18 | 38 | version: "0.0.1",
|
19 | 39 | options: {
|
20 | 40 | delegate: "[data-menu]", // selector
|
21 | 41 | ignoreParentSelect: true, // Don't trigger 'select' for sub-menu parents
|
22 | 42 | menu: null, // selector or jQuery or a function returning such
|
23 |
| - taphold: 2000, // open menu after 2000 ms long touch |
| 43 | + taphold: 800, // open menu after 2000 ms long touch |
24 | 44 | // Events:
|
25 | 45 | beforeOpen: $.noop, // menu about to open; return `false` to prevent opening
|
26 | 46 | blur: $.noop, // menu option lost focus
|
|
32 | 52 | select: $.noop // menu option was selected; return `false` to prevent closing
|
33 | 53 | },
|
34 | 54 | _create: function () {
|
| 55 | + var self = this; |
35 | 56 | this.element.delegate(this.options.delegate, "contextmenu.contextmenu", $.proxy(this._openMenu, this));
|
36 | 57 | // emulate a 'taphold' event
|
37 |
| - /* |
38 |
| - this.element.delegate(this.options.delegate, "mousedown.contextmenu", $.proxy(function(event, ui){ |
39 |
| - var self = this; |
40 |
| - console.log("Event ", event.type, this.timer); |
41 |
| - if(this.timer){ |
42 |
| - console.log(" clear " + this.timer); |
43 |
| - clearTimeout(this.timer); |
44 |
| - this.timer = null; |
45 |
| - } |
46 |
| - this.timer = setTimeout(function(){ |
47 |
| - console.log("Timeout ", event.type, self.timer); |
48 |
| - self.open.call(self, $(event.target)); |
49 |
| - self.timer = null; |
| 58 | + var tapStartHandler = function(event){ |
| 59 | + console.log("Event ", event.type, this.tapTimer); |
| 60 | + tapClearHandler(event); |
| 61 | + this.tapTimer = setTimeout(function(){ |
| 62 | + console.log("Timeout ", event.type, this.tapTimer, event.target); |
| 63 | + alert("Timeout " + event.type + this.tapTimer + " " + $(event.target).text()); |
| 64 | + this.open.call(this, $(event.target)); |
| 65 | + this.tapTimer = null; |
50 | 66 | }, this.options.taphold);
|
51 |
| - console.log("Event started ", event.type, this.timer); |
52 |
| - }, this)); |
53 |
| - this.element.delegate(this.options.delegate, "mouseup.contextmenu", $.proxy(function(){ |
54 |
| - if(this.timer){ |
55 |
| - console.log("Event ", event.type, "clear" + this.timer); |
56 |
| - clearTimeout(this.timer); |
57 |
| - this.timer = null; |
| 67 | + console.log("Event started ", event.type, this.tapTimer); |
| 68 | + }; |
| 69 | + var tapClearHandler = function(event){ |
| 70 | + if(this.tapTimer){ |
| 71 | + console.log("clear " + this.tapTimer); |
| 72 | + clearTimeout(this.tapTimer); |
| 73 | + this.tapTimer = null; |
| 74 | + } |
| 75 | + }; |
| 76 | + var tapEndHandler = function(event){ |
| 77 | + if(this.tapTimer){ |
| 78 | + tapClearHandler(event); |
| 79 | + return false; |
58 | 80 | }
|
59 |
| - }, this)); |
60 |
| - */ |
| 81 | + }; |
| 82 | + this.element |
| 83 | + .delegate(this.options.delegate, "touchstart.contextmenu", $.proxy(tapStartHandler, this)) |
| 84 | + .delegate(this.options.delegate, "touchend.contextmenu", $.proxy(tapEndHandler, this)) |
| 85 | + .delegate(this.options.delegate, "touchmove.contextmenu", $.proxy(tapClearHandler, this)); |
| 86 | +// this.element.delegate(this.options.delegate, "touchstart.contextmenu", $.proxy(function(event, ui){ |
| 87 | +// var self = this; |
| 88 | +// console.log("Event ", event.type, this.tapTimer); |
| 89 | +// if(this.tapTimer){ |
| 90 | +// console.log(" clear " + this.tapTimer); |
| 91 | +// clearTimeout(this.tapTimer); |
| 92 | +// this.tapTimer = null; |
| 93 | +// } |
| 94 | +// this.tapTimer = setTimeout(function(){ |
| 95 | +// console.log("Timeout ", event.type, self.tapTimer); |
| 96 | +// self.open.call(self, $(event.target)); |
| 97 | +// self.tapTimer = null; |
| 98 | +// }, this.options.taphold); |
| 99 | +// console.log("Event started ", event.type, this.tapTimer); |
| 100 | +// }, this)); |
| 101 | +// this.element.delegate(this.options.delegate, "touchend.contextmenu", $.proxy(function(){ |
| 102 | +// if(this.tapTimer){ |
| 103 | +// console.log("Event ", event.type, "clear" + this.tapTimer); |
| 104 | +// clearTimeout(this.tapTimer); |
| 105 | +// this.tapTimer = null; |
| 106 | +// } |
| 107 | +// }, this)); |
61 | 108 | this._trigger("init");
|
62 | 109 | },
|
63 | 110 | /** Return menu jQuery object. */
|
|
140 | 187 | _closeMenu: function(){
|
141 | 188 | var self = this,
|
142 | 189 | $menu = this._getMenu();
|
143 |
| - if(this.timer){ |
144 |
| - clearTimeout(this.timer); |
145 |
| - this.timer = null; |
| 190 | + if(this.tapTimer){ |
| 191 | + clearTimeout(this.tapTimer); |
| 192 | + this.tapTimer = null; |
146 | 193 | }
|
147 | 194 | $menu.fadeOut(function() {
|
148 | 195 | self._trigger("close");
|
|
0 commit comments