2 // Use LongPress() to set a function callback for a long-press on a particular DOM element. |
2 // Use LongPress() to set a function callback for a long-press on a particular DOM element. |
3 // Parameters: |
3 // Parameters: |
4 // elementId: id string of the element |
4 // elementId: id string of the element |
5 // callback: the function to be called when the long-press fires |
5 // callback: the function to be called when the long-press fires |
6 // mouseDownCallback: the function to be called on mouse-down |
6 // mouseDownCallback: the function to be called on mouse-down |
|
7 // timerValue : timeout value for long press |
7 // Example: |
8 // Example: |
8 // <javascript ...> |
9 // <javascript ...> |
9 // new LongPress("btnId", function(el) { alert("hello"); }); |
10 // new LongPress("btnId", function(el) { alert("hello"); }); |
10 // </javascript> |
11 // </javascript> |
11 // ... |
12 // ... |
12 // <img id="btnId" ... /> |
13 // <img id="btnId" ... /> |
13 // |
14 // |
14 var gInitialX = 0; |
15 var gInitialX = 0; |
15 var gInitialY = 0; |
16 var gInitialY = 0; |
16 |
17 |
17 function LongPress(elementId, callback, mouseDownCallback) { |
18 function LongPress(elementId, callback, mouseDownCallback, timerValue) { |
18 this.el = document.getElementById(elementId); |
19 this.el = document.getElementById(elementId); |
19 this.callback = callback; |
20 this.callback = callback; |
20 this.initialX = 0; |
21 this.initialX = 0; |
21 this.initialY = 0; |
22 this.initialY = 0; |
22 this.mouseDownCallback = mouseDownCallback; |
23 this.mouseDownCallback = mouseDownCallback; |
23 |
24 this.timerValue = timerValue; |
|
25 |
24 if (!this.el) { |
26 if (!this.el) { |
25 //window.app.debug("LongPress: element " + elementId + " not found"); |
27 //window.app.debug("LongPress: element " + elementId + " not found"); |
26 return; |
28 return; |
27 } |
29 } |
28 |
30 |
33 this.unregisterMouseMove(); |
35 this.unregisterMouseMove(); |
34 } |
36 } |
35 |
37 |
36 this.startTimer = function() { |
38 this.startTimer = function() { |
37 this.cancelTimer(); |
39 this.cancelTimer(); |
38 this.timer = window.setTimeout(createDelegate(this, this.onTimerFired), 250); |
40 this.timer = window.setTimeout(createDelegate(this, this.onTimerFired), timerValue); |
39 } |
41 } |
40 |
42 |
41 this.cancelTimer = function() { |
43 this.cancelTimer = function() { |
42 if (this.timer) { |
44 if (this.timer) { |
43 window.clearTimeout(this.timer); |
45 window.clearTimeout(this.timer); |