Skip to content

Commit

Permalink
After handler dragging the new state of the button depends on the pos…
Browse files Browse the repository at this point in the history
…ition on the handler
  • Loading branch information
volpino committed Sep 23, 2012
1 parent 4b861b6 commit 2e098a0
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions static/js/jquery.toggle.buttons.js
Expand Up @@ -12,7 +12,8 @@
this.each(function () {
var $spanLeft
, $spanRight
, options;
, options
, moving;

$element = $(this);
$element.addClass('toggle-button');
Expand Down Expand Up @@ -120,6 +121,8 @@
});

$('.toggle-button').find('label').on('mousedown', function (e) {
moving = false;

e.preventDefault();
e.stopImmediatePropagation();

Expand All @@ -130,6 +133,7 @@
var $element = $(this).closest('.toggle-button')
, relativeX = e.pageX - $element.offset().left
, percent = ((relativeX / (options.width * 2)) * 100);
moving = true;

if (percent < 25)
percent = 25;
Expand All @@ -141,13 +145,19 @@

$(this).on('click', function (e) {
var $target = $(e.target)
, $input = $target.siblings('input');
, $myCheckBox = $target.siblings('input');

e.stopImmediatePropagation();
e.preventDefault();
$(this).unbind('mouseleave');

$input.attr('checked', !($input.is(':checked'))).trigger('change');
if (moving)
if (parseInt($(this).parent().css('left')) < -25)
$myCheckBox.attr('checked', false);
else $myCheckBox.attr('checked', true);
else $myCheckBox.attr("checked", !$myCheckBox.is(":checked"));

$myCheckBox.trigger('change');
});

$(this).on('mouseleave', function (e) {
Expand Down Expand Up @@ -215,4 +225,4 @@
}
}
};
}($);
}($);

0 comments on commit 2e098a0

Please sign in to comment.