Skip to content

Commit

Permalink
Mainly optional duration argument
Browse files Browse the repository at this point in the history
* `longclick` method now accepts optional duration argument which overrides the default duration.
* `jQuery.longclick.delay` has been renamed to `jQuery.longclick.duration`.
* Invalid flag store key fixed.
* Documentation comments & README updates.
  • Loading branch information
pisi committed May 31, 2010
1 parent 8a8ac7a commit 1645aa8
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 35 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.markdown
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
Changelog
=========

0.2
---
* `longclick` method now accepts an optional duration argument which overrides the default duration.
* `jQuery.longclick.delay` has been renamed to `jQuery.longclick.duration`.
* Invalid flag store key fixed.
* Documentation comments & README updates.

0.1
---
* Initial release.
23 changes: 15 additions & 8 deletions README.markdown
Original file line number Diff line number Diff line change
@@ -1,29 +1,33 @@
jQuery Longclick
================
jQuery Longclick Event
======================

*Press & hold mouse button special event for [jQuery 1.4.x][jquery]*
*Press & hold mouse button "long click" special event for [jQuery 1.4.x][jquery]*

The event is triggered when the mouse button stays pressed for half a second (configurable) while the pointer is stationery.

**WARNING! UNDER INTENSIVE DEVELOPMENT THUS NOT QUITE STABLE YET**

`longclick` method:
-------------------
.longclick( handler(eventObject) )
.longclick( [ duration ], [ handler ] )
.longclick()

This method is a shortcut for `.bind('longclick', handler)` in the first variation,
and `.trigger('longclick')` in the second.

If supplied, custom duration (in milliseconds) is used for target element(s).

Returns *jQuery*.

`longclick` event:
------------------

Works with standard `bind`, `unbind`, `trigger` and `live` event methods as usual.
Works with standard [`live`][jquery-live], [`bind`][jquery-bind], [`unbind`][jquery-unbind] and [`trigger`][jquery-trigger] event methods as usual.

Configuration:
--------------

* *`jQuery.longclick.delay`* (default 500) - duration of mouse button press (in milliseconds) after which the `longclick` event occurs and is triggered.
* `jQuery.longclick.`**`duration`** - for how long (in milliseconds) mouse button must be pressed for `longclick` event to be fired. Defaults to 500.

Requirements:
-------------
Expand All @@ -33,8 +37,7 @@ Requirements:
License:
--------

It's free for use in all personal or commercial projects under both
[MIT][license-mit] and [GPL][license-gpl] licenses.
Free for use in all personal or commercial projects under both [MIT][license-mit] and [GPL][license-gpl] licenses.

---
Copyright (c) 2010 [Petr Vostřel][pisi]
Expand All @@ -43,5 +46,9 @@ Copyright (c) 2010 [Petr Vostřel][pisi]
[license-mit]: /pisi/Longclick/raw/master/MIT-LICENSE.txt
[license-gpl]: /pisi/Longclick/raw/master/GPL-LICENSE.txt
[jquery]: http://www.jquery.com/
[jquery-live]: http://api.jquery.com/live/
[jquery-bind]: http://api.jquery.com/bind/
[jquery-unbind]: http://api.jquery.com/unbind/
[jquery-trigger]: http://api.jquery.com/trigger/
[pisi]: http://petr.vostrel.cz/
[reel]: http://jquery.vostrel.cz/reel
7 changes: 4 additions & 3 deletions jquery.longclick-min.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
/*
Longclick Event
Copyright (c) 2010 Petr Vostrel (http://petr.vostrel.cz/)
Dual licensed under the MIT (MIT-LICENSE.txt)
and GPL (GPL-LICENSE.txt) licenses.
Version: 0.1
Version: 0.2
Updated: 2010-05-31
*/
(function(a){function h(c){function i(){a(f).data(e,true);c.type=d;jQuery.event.handle.apply(f,j)}var f=this,j=arguments;a(this).data(e,false).data(g,setTimeout(i,a.longclick.delay))}function k(){clearTimeout(a(this).data(g))}function l(){if(a(this).data(e))return false}a.fn.longclick=function(c){return c?a(this).bind(d,c):a(this).trigger(d)};a.longclick={delay:500};a.event.special.longclick={setup:function(){a(this).bind(m,h).bind(n+" "+o,k).bind(p,l)},teardown:function(){a(this).unbind(b)}};var d=
"longclick",b="."+d,m="mousedown"+b,p="click"+b,n="mousemove"+b,o="mouseup"+b,g="timer"+b,e="fired"+b})(jQuery);
(function(a){function j(b){function d(){a(g).data(f,true);b.type=e;jQuery.event.handle.apply(g,k)}var g=this,k=arguments;a(this).data(f,false).data(h,setTimeout(d,a(this).data(i)||a.longclick.duration))}function l(){clearTimeout(a(this).data(h))}function m(){if(a(this).data(f))return false}a.fn.longclick=function(){var b=[].splice.call(arguments,0),d=b.pop();b=b.pop();b=a(this).data(i,b||null);return d?b.bind(e,d):b.trigger(e)};a.longclick={duration:500};a.event.special.longclick={setup:function(){a(this).bind(n,
j).bind(o+" "+p,l).bind(q,m)},teardown:function(){a(this).unbind(c)}};var e="longclick",c="."+e,n="mousedown"+c,q="click"+c,o="mousemove"+c,p="mouseup"+c,i="duration"+c,h="timer"+c,f="fired"+c})(jQuery);
55 changes: 31 additions & 24 deletions jquery.longclick.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,37 @@
/**
* jQuery Longclick
* ================
* Press & hold mouse button special event for jQuery 1.4.x
* jQuery Longclick Event
* ======================
* Press & hold mouse button "long click" special event for jQuery 1.4.x
*
* @license Copyright (c) 2010 Petr Vostrel (http://petr.vostrel.cz/)
* @license Longclick Event
* Copyright (c) 2010 Petr Vostrel (http://petr.vostrel.cz/)
* Dual licensed under the MIT (MIT-LICENSE.txt)
* and GPL (GPL-LICENSE.txt) licenses.
*
* Version: 0.1
* Version: 0.2
* Updated: 2010-05-31
*/

(function($){

/*
.longclick( handler(eventObject) )
.longclick( [ duration ], [ handler ] )
.longclick()
This method is a shortcut for .bind('longclick', handler) in the first variation,
and .trigger('longclick') in the second.
If supplied, custom duration (in milliseconds) is used for target element(s).
returns jQuery
*/
$.fn.longclick= function longclick(handler){
return handler ? $(this).bind(type, handler) : $(this).trigger(type)
$.fn.longclick= function longclick(){
var
args= [].splice.call(arguments, 0),
handler= args.pop(),
duration= args.pop(),
$this= $(this).data(_duration_, duration || null)
return handler ? $this.bind(type, handler) : $this.trigger(type)
}

/*
Expand All @@ -32,11 +40,12 @@
$.longclick= {
/*
Duration of mouse button press (in milliseconds) after which `longclick` event occours.
Example: `jQuery.longclick.delay= 1000` sets the delay to one second
Defaults to 500 (half a second)
Example: `jQuery.longclick.duration= 2000` sets 1 second duration
*/
delay: 500
duration: 500
}

/*
Bindings
*/
Expand All @@ -58,27 +67,27 @@
function schedule(event){
/* Catch in closure the `this` reference and `arguments` for later */
var
that= this,
element= this,
args= arguments
/* Flag as "not triggered" and schedule the trigger */
/* Flag as "not fired" and schedule the trigger */
$(this)
.data(_triggered_, false)
.data(_timer_, setTimeout(scheduled, $.longclick.delay))
.data(_fired_, false)
.data(_timer_, setTimeout(scheduled, $(this).data(_duration_) || $.longclick.duration))

function scheduled(){
/* Flag as "triggered" and rejoin the default event flow */
$(that).data(_triggered_, true)
/* Flag as "fired" and rejoin the default event flow */
$(element).data(_fired_, true)
event.type= type
jQuery.event.handle.apply(that, args)
jQuery.event.handle.apply(element, args)
}
}
function annul(event){
/* Annul the scheduled triggering */
/* Annul the scheduled trigger */
clearTimeout($(this).data(_timer_))
}
function click(event){
/* Prevent `click` event to be fired after button release once `longclick` was fired */
if ($(this).data(_triggered_)) return false
if ($(this).data(_fired_)) return false
}

/*
Expand All @@ -87,14 +96,12 @@
var
type= 'longclick',
namespace= '.' + type,
self= $.event.special[type],

/* Event strings */
_mousedown_= 'mousedown'+namespace, _click_= 'click'+namespace,
_mousemove_= 'mousemove'+namespace, _mouseup_= 'mouseup'+namespace,

/* Storage keys */
_timer_= 'timer'+namespace,
_triggered_= 'fired'+namespace
_duration_= 'duration'+namespace, _timer_= 'timer'+namespace, _fired_= 'fired'+namespace

})(jQuery);

0 comments on commit 1645aa8

Please sign in to comment.