Skip to content

Commit 7c44341

Browse files
committed
version 1.2: rewrote to allow multiple instances of plugin with different settings
1 parent 7893d59 commit 7c44341

File tree

2 files changed

+55
-40
lines changed

2 files changed

+55
-40
lines changed

jquery.dragcheck.js

Lines changed: 53 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,70 +2,85 @@
22
* jQuery.DragCheck - Click-and-drag over checkboxes to change their state.
33
* Copyright (c) 2013 Seph Soliman - scarlac(at)gmail(dot)com | http://seph.dk
44
* @author Seph Soliman
5-
* @version 1.1
5+
* @version 1.2
66
*
77
* https://github.com/scarlac/jquery-drag-check
88
*/
99
(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) {
1711
// Remember the initial checkbox state
18-
window.dragCheck_state = window.dragCheck_state || null;
12+
this.dragCheck_state = this.dragCheck_state || null;
1913
// 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;
2219

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);
2623

27-
if(settings.onDragEnd) {
28-
settings.onDragEnd(window.dragCheck_items);
24+
if(this.opts.onDragEnd) {
25+
this.opts.onDragEnd(this.dragCheck_items);
2926
} 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]);
3431
else
35-
window.dragCheck_items[i].trigger('change');
32+
$(this.dragCheck_items[i]).trigger('change');
3633
}
3734
}
3835
}
3936

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 = [];
4341
}
4442
}
4543

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;
4949
})
50-
.mouseup(dragEnd)
50+
.mouseup($.proxy(this.dragEnd, this))
5151
.mouseenter(function(e) {
52+
var instance = $(this).data('dragCheck');
53+
5254
// When dragging on a new checkbox, set it's state to the state of the initial checkbox
5355
// 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);
5662

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);
5966
} 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+
}
6569
}
6670
});
6771

6872
// 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);
7079
}
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+
};
7186
})(jQuery);

jquery.dragcheck.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)