2
2
* jQuery.DragCheck - Click-and-drag over checkboxes to change their state.
3
3
* Copyright (c) 2013 Seph Soliman - scarlac(at)gmail(dot)com | http://seph.dk
4
4
* @author Seph Soliman
5
- * @version 1.1
5
+ * @version 1.2
6
6
*
7
7
* https://github.com/scarlac/jquery-drag-check
8
8
*/
9
9
( function ( $ ) {
10
- $ . fn . dragCheck = function ( options ) {
11
- var settings = $ . extend ( {
12
- 'deferChangeTrigger' : false , // Should "change" be triggered WHILE user is drag-checking, or wait until user has released the mouse?
13
- 'onDragEnd' : undefined , // type: function. Instead of triggering change, what should happend when user releases cursor?
14
- 'onChange' : undefined // type: function. instead of calling onChange on each checkbox, what callback should we use?
15
- } , options ) ;
16
-
10
+ var dragCheckClass = function ( el , opts ) {
17
11
// Remember the initial checkbox state
18
- window . dragCheck_state = window . dragCheck_state || null ;
12
+ this . dragCheck_state = this . dragCheck_state || null ;
19
13
// Remember the initial checkbox (since click-drag outside-release action will not check initial checkbox)
20
- window . dragCheck_origin = window . dragCheck_origin || null ;
21
- window . dragCheck_items = window . dragCheck_items || [ ] ;
14
+ this . dragCheck_origin = this . dragCheck_origin || null ;
15
+ this . dragCheck_items = this . dragCheck_items || [ ] ;
16
+ // Boolean indicating if onDragEnd should be called when user stops dragging
17
+ this . dragCheck_triggerOnDragEnd = this . dragCheck_triggerOnDragEnd || false ;
18
+ this . opts = opts ;
22
19
23
- var dragEnd = function ( ) {
24
- if ( window . dragCheck_state !== null ) {
25
- window . dragCheck_items . push ( dragCheck_origin ) ;
20
+ this . dragEnd = function ( ) {
21
+ if ( this . dragCheck_state !== null && this . dragCheck_triggerOnDragEnd ) {
22
+ this . dragCheck_items . push ( this . dragCheck_origin ) ;
26
23
27
- if ( settings . onDragEnd ) {
28
- settings . onDragEnd ( window . dragCheck_items ) ;
24
+ if ( this . opts . onDragEnd ) {
25
+ this . opts . onDragEnd ( this . dragCheck_items ) ;
29
26
} else {
30
- if ( window . dragCheck_items . length > 0 ) {
31
- for ( var i in window . dragCheck_items ) {
32
- if ( settings . onChange )
33
- settings . onChange . call ( window . dragCheck_items [ i ] ) ;
27
+ if ( this . dragCheck_items . length > 0 ) {
28
+ for ( var i in this . dragCheck_items ) {
29
+ if ( this . opts . onChange )
30
+ this . opts . onChange . call ( this . dragCheck_items [ i ] ) ;
34
31
else
35
- window . dragCheck_items [ i ] . trigger ( 'change' ) ;
32
+ $ ( this . dragCheck_items [ i ] ) . trigger ( 'change' ) ;
36
33
}
37
34
}
38
35
}
39
36
40
- window . dragCheck_state = null ;
41
- window . dragCheck_origin = null ;
42
- window . dragCheck_items = [ ] ;
37
+ this . dragCheck_state = null ;
38
+ this . dragCheck_origin = null ;
39
+ this . dragCheck_triggerOnDragEnd = false ;
40
+ this . dragCheck_items = [ ] ;
43
41
}
44
42
}
45
43
46
- this . mousedown ( function ( ) {
47
- window . dragCheck_state = ! this . checked ;
48
- window . dragCheck_origin = this ;
44
+ el . data ( 'dragCheck' , this )
45
+ . mousedown ( function ( ) {
46
+ var instance = $ ( this ) . data ( 'dragCheck' ) ;
47
+ instance . dragCheck_state = ! this . checked ;
48
+ instance . dragCheck_origin = this ;
49
49
} )
50
- . mouseup ( dragEnd )
50
+ . mouseup ( $ . proxy ( this . dragEnd , this ) )
51
51
. mouseenter ( function ( e ) {
52
+ var instance = $ ( this ) . data ( 'dragCheck' ) ;
53
+
52
54
// When dragging on a new checkbox, set it's state to the state of the initial checkbox
53
55
// Also set the initial checkbox' state to ensure it's checked when user finishes dragging
54
- if ( window . dragCheck_state !== null ) {
55
- var item = $ ( this ) . add ( window . dragCheck_origin ) . prop ( 'checked' , window . dragCheck_state ) ;
56
+ if ( instance . dragCheck_state !== null ) {
57
+ // onDragEnd should only trigger after 2nd checkbox has been changed
58
+ instance . dragCheck_triggerOnDragEnd = true ;
59
+
60
+ instance . dragCheck_items . push ( this ) ;
61
+ var item = $ ( this ) . add ( instance . dragCheck_origin ) . prop ( 'checked' , instance . dragCheck_state ) ;
56
62
57
- if ( settings . deferChangeTrigger ) {
58
- window . dragCheck_items . push ( this ) ;
63
+ if ( instance . opts . onChange ) {
64
+ instance . opts . onChange . call ( instance . dragCheck_origin , e ) ; // TODO: Only trigger change for origin box once
65
+ instance . opts . onChange . call ( this , e ) ;
59
66
} else {
60
- if ( settings . onChange )
61
- settings . onChange . call ( item , e ) ;
62
- else
63
- item . trigger ( 'change' , e ) ; // Force the event to trigger "change", so live feedback can be given
64
- }
67
+ item . trigger ( 'change' , e ) ; // Force the event to trigger "change", so live feedback can be given
68
+ }
65
69
}
66
70
} ) ;
67
71
68
72
// Mouse release should disable the functionality
69
- $ ( document . body ) . mouseup ( dragEnd ) ;
73
+ $ ( document . body ) . mouseup ( $ . proxy ( this . dragEnd , this ) ) ;
74
+ } ;
75
+
76
+ $ . fn . dragCheck = function ( options ) {
77
+ var opts = $ . extend ( { } , $ . fn . dragCheck . defaults , options ) ;
78
+ return new dragCheckClass ( $ ( this ) , opts ) ;
70
79
}
80
+
81
+ $ . fn . dragCheck . defaults = {
82
+ 'deferChangeTrigger' : false , // Should "change" be triggered WHILE user is drag-checking, or wait until user has released the mouse?
83
+ 'onDragEnd' : undefined , // type: function. Instead of triggering change, what should happend when user releases cursor?
84
+ 'onChange' : undefined // type: function. instead of calling onChange on each checkbox, what callback should we use?
85
+ } ;
71
86
} ) ( jQuery ) ;
0 commit comments