Skip to content

Commit

Permalink
Fixes #70
Browse files Browse the repository at this point in the history
  • Loading branch information
istvan-ujjmeszaros committed Apr 14, 2018
1 parent bc0a607 commit d21624d
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 199 deletions.
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Bootstrap TouchSpin
v4.1.0
v4.2.0

A mobile and touch friendly input spinner component for Bootstrap 3.

Expand Down
2 changes: 1 addition & 1 deletion bootstrap-touchspin.jquery.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"spinbutton",
"spinner"
],
"version": "4.1.0",
"version": "4.2.0",
"author": {
"name": "István Ujj-Mészáros",
"url": "https://github.com/istvan-ujjmeszaros"
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bootstrap-touchspin",
"version": "4.1.0",
"version": "4.2.0",
"homepage": "http://www.virtuosoft.eu/code/bootstrap-touchspin/",
"authors": [
{
Expand Down
2 changes: 1 addition & 1 deletion dist/jquery.bootstrap-touchspin.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Bootstrap TouchSpin - v4.1.0
* Bootstrap TouchSpin - v4.2.0
* A mobile and touch friendly input spinner component for Bootstrap 3 & 4.
* http://www.virtuosoft.eu/code/bootstrap-touchspin/
*
Expand Down
123 changes: 49 additions & 74 deletions dist/jquery.bootstrap-touchspin.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Bootstrap TouchSpin - v4.1.0
* Bootstrap TouchSpin - v4.2.0
* A mobile and touch friendly input spinner component for Bootstrap 3 & 4.
* http://www.virtuosoft.eu/code/bootstrap-touchspin/
*
Expand Down Expand Up @@ -30,37 +30,11 @@

var _currentSpinnerId = 0;

function _scopedEventName(name, id) {
return name + '.touchspin_' + id;
}

function _scopeEventNames(names, id) {
return $.map(names, function(name) {
return _scopedEventName(name, id);
});
}

$.fn.TouchSpin = function(options) {

if (options === 'destroy') {
this.each(function() {
var originalinput = $(this),
originalinput_data = originalinput.data();
$(document).off(_scopeEventNames([
'mouseup',
'touchend',
'touchcancel',
'mousemove',
'touchmove',
'scroll',
'scrollstart'], originalinput_data.spinnerid).join(' '));
});
return;
}

var defaults = {
min: 0,
max: 100,
min: 0, // If null, there is no minimum enforced
max: 100, // If null, there is no maximum enforced
initval: '',
replacementval: '',
step: 1,
Expand Down Expand Up @@ -135,10 +109,8 @@
spincount = 0,
spinning = false;


init();


function init() {
if (originalinput.data('alreadyinitialized')) {
return;
Expand All @@ -148,7 +120,6 @@
_currentSpinnerId += 1;
originalinput.data('spinnerid', _currentSpinnerId);


if (!originalinput.is('input')) {
console.log('Must be an input.');
return;
Expand All @@ -162,7 +133,6 @@
_hideEmptyPrefixPostfix();
_bindEvents();
_bindEventsInterface();
elements.input.css('display', 'block');
}

function _setInitval() {
Expand Down Expand Up @@ -198,6 +168,25 @@
return data;
}

function _destroy() {
var $parent = originalinput.parent();

stopSpin();

originalinput.off('.touchspin');

if ($parent.hasClass('bootstrap-touchspin-injected')) {
originalinput.siblings().remove();
originalinput.unwrap();
}
else {
$('.bootstrap-touchspin-injected', $parent).remove();
$parent.removeClass('bootstrap-touchspin');
}

originalinput.data('alreadyinitialized', false);
}

function _updateSettings(newsettings) {
settings = $.extend({}, settings, newsettings);

Expand Down Expand Up @@ -237,24 +226,24 @@

var downhtml,
uphtml,
prefixhtml = '<span class="input-group-addon bootstrap-touchspin-prefix input-group-prepend"><span class="input-group-text">' + settings.prefix + '</span></span>',
postfixhtml = '<span class="input-group-addon bootstrap-touchspin-postfix input-group-append"><span class="input-group-text">' + settings.postfix + '</span></span>';
prefixhtml = '<span class="input-group-addon bootstrap-touchspin-prefix input-group-prepend bootstrap-touchspin-injected"><span class="input-group-text">' + settings.prefix + '</span></span>',
postfixhtml = '<span class="input-group-addon bootstrap-touchspin-postfix input-group-append bootstrap-touchspin-injected"><span class="input-group-text">' + settings.postfix + '</span></span>';

if (prev.hasClass('input-group-btn') || prev.hasClass('input-group-prepend')) {
downhtml = '<button class="' + settings.buttondown_class + ' bootstrap-touchspin-down" type="button">' + settings.buttondown_txt + '</button>';
downhtml = '<button class="' + settings.buttondown_class + ' bootstrap-touchspin-down bootstrap-touchspin-injected" type="button">' + settings.buttondown_txt + '</button>';
prev.append(downhtml);
}
else {
downhtml = '<span class="input-group-btn input-group-prepend"><button class="' + settings.buttondown_class + ' bootstrap-touchspin-down" type="button">' + settings.buttondown_txt + '</button></span>';
downhtml = '<span class="input-group-btn input-group-prepend bootstrap-touchspin-injected"><button class="' + settings.buttondown_class + ' bootstrap-touchspin-down" type="button">' + settings.buttondown_txt + '</button></span>';
$(downhtml).insertBefore(originalinput);
}

if (next.hasClass('input-group-btn') || next.hasClass('input-group-append')) {
uphtml = '<button class="' + settings.buttonup_class + ' bootstrap-touchspin-up" type="button">' + settings.buttonup_txt + '</button>';
uphtml = '<button class="' + settings.buttonup_class + ' bootstrap-touchspin-up bootstrap-touchspin-injected" type="button">' + settings.buttonup_txt + '</button>';
next.prepend(uphtml);
}
else {
uphtml = '<span class="input-group-btn input-group-append"><button class="' + settings.buttonup_class + ' bootstrap-touchspin-up" type="button">' + settings.buttonup_txt + '</button></span>';
uphtml = '<span class="input-group-btn input-group-append bootstrap-touchspin-injected"><button class="' + settings.buttonup_class + ' bootstrap-touchspin-up" type="button">' + settings.buttonup_txt + '</button></span>';
$(uphtml).insertAfter(originalinput);
}

Expand All @@ -277,10 +266,10 @@
}

if (settings.verticalbuttons) {
html = '<div class="input-group ' + inputGroupSize + ' bootstrap-touchspin"><span class="input-group-addon bootstrap-touchspin-prefix">' + settings.prefix + '</span><span class="input-group-addon bootstrap-touchspin-postfix input-group-prepend"><span class="input-group-text">' + settings.postfix + '</span></span><span class="input-group-btn-vertical"><button class="' + settings.buttondown_class + ' bootstrap-touchspin-up ' + settings.verticalupclass + '" type="button">' + settings.verticalup + '</button><button class="' + settings.buttonup_class + ' bootstrap-touchspin-down ' + settings.verticaldownclass + '" type="button">' + settings.verticaldown + '</button></span></div>';
html = '<div class="input-group ' + inputGroupSize + ' bootstrap-touchspin bootstrap-touchspin-injected"><span class="input-group-addon bootstrap-touchspin-prefix">' + settings.prefix + '</span><span class="input-group-addon bootstrap-touchspin-postfix input-group-prepend"><span class="input-group-text">' + settings.postfix + '</span></span><span class="input-group-btn-vertical"><button class="' + settings.buttondown_class + ' bootstrap-touchspin-up ' + settings.verticalupclass + '" type="button">' + settings.verticalup + '</button><button class="' + settings.buttonup_class + ' bootstrap-touchspin-down ' + settings.verticaldownclass + '" type="button">' + settings.verticaldown + '</button></span></div>';
}
else {
html = '<div class="input-group bootstrap-touchspin"><span class="input-group-btn input-group-prepend"><button class="' + settings.buttondown_class + ' bootstrap-touchspin-down" type="button">' + settings.buttondown_txt + '</button></span><span class="input-group-addon bootstrap-touchspin-prefix input-group-prepend"><span class="input-group-text">' + settings.prefix + '</span></span><span class="input-group-addon bootstrap-touchspin-postfix input-group-append"><span class="input-group-text">' + settings.postfix + '</span></span><span class="input-group-btn input-group-append"><button class="' + settings.buttonup_class + ' bootstrap-touchspin-up" type="button">' + settings.buttonup_txt + '</button></span></div>';
html = '<div class="input-group bootstrap-touchspin bootstrap-touchspin-injected"><span class="input-group-btn input-group-prepend"><button class="' + settings.buttondown_class + ' bootstrap-touchspin-down" type="button">' + settings.buttondown_txt + '</button></span><span class="input-group-addon bootstrap-touchspin-prefix input-group-prepend"><span class="input-group-text">' + settings.prefix + '</span></span><span class="input-group-addon bootstrap-touchspin-postfix input-group-append"><span class="input-group-text">' + settings.postfix + '</span></span><span class="input-group-btn input-group-append"><button class="' + settings.buttonup_class + ' bootstrap-touchspin-up" type="button">' + settings.buttonup_txt + '</button></span></div>';
}

container = $(html).insertBefore(originalinput);
Expand Down Expand Up @@ -316,7 +305,7 @@
}

function _bindEvents() {
originalinput.on('keydown', function(ev) {
originalinput.on('keydown.touchspin', function(ev) {
var code = ev.keyCode || ev.which;

if (code === 38) {
Expand All @@ -335,7 +324,7 @@
}
});

originalinput.on('keyup', function(ev) {
originalinput.on('keyup.touchspin', function(ev) {
var code = ev.keyCode || ev.which;

if (code === 38) {
Expand All @@ -346,7 +335,7 @@
}
});

originalinput.on('blur', function() {
originalinput.on('blur.touchspin', function() {
_checkValue();
originalinput.val(settings.callback_after_calculation(originalinput.val()));
});
Expand All @@ -363,15 +352,15 @@
}
});

elements.down.on('keyup', function(ev) {
elements.down.on('keyup.touchspin', function(ev) {
var code = ev.keyCode || ev.which;

if (code === 32 || code === 13) {
stopSpin();
}
});

elements.up.on('keydown', function(ev) {
elements.up.on('keydown.touchspin', function(ev) {
var code = ev.keyCode || ev.which;

if (code === 32 || code === 13) {
Expand All @@ -383,7 +372,7 @@
}
});

elements.up.on('keyup', function(ev) {
elements.up.on('keyup.touchspin', function(ev) {
var code = ev.keyCode || ev.which;

if (code === 32 || code === 13) {
Expand Down Expand Up @@ -447,7 +436,7 @@
ev.stopPropagation();
});

elements.up.on('mouseout touchleave touchend touchcancel', function(ev) {
elements.up.on('mouseout.touchspin touchleave.touchspin touchend.touchspin touchcancel.touchspin', function(ev) {
if (!spinning) {
return;
}
Expand All @@ -456,7 +445,7 @@
stopSpin();
});

elements.down.on('mouseout touchleave touchend touchcancel', function(ev) {
elements.down.on('mouseout.touchspin touchleave.touchspin touchend.touchspin touchcancel.touchspin', function(ev) {
if (!spinning) {
return;
}
Expand All @@ -465,7 +454,7 @@
stopSpin();
});

elements.down.on('mousemove touchmove', function(ev) {
elements.down.on('mousemove.touchspin touchmove.touchspin', function(ev) {
if (!spinning) {
return;
}
Expand All @@ -474,7 +463,7 @@
ev.preventDefault();
});

elements.up.on('mousemove touchmove', function(ev) {
elements.up.on('mousemove.touchspin touchmove.touchspin', function(ev) {
if (!spinning) {
return;
}
Expand All @@ -483,25 +472,7 @@
ev.preventDefault();
});

$(document).on(_scopeEventNames(['mouseup', 'touchend', 'touchcancel'], _currentSpinnerId).join(' '), function(ev) {
if (!spinning) {
return;
}

ev.preventDefault();
stopSpin();
});

$(document).on(_scopeEventNames(['mousemove', 'touchmove', 'scroll', 'scrollstart'], _currentSpinnerId).join(' '), function(ev) {
if (!spinning) {
return;
}

ev.preventDefault();
stopSpin();
});

originalinput.on('mousewheel DOMMouseScroll', function(ev) {
originalinput.on('mousewheel.touchspin DOMMouseScroll.touchspin', function(ev) {
if (!settings.mousewheel || !originalinput.is(':focus')) {
return;
}
Expand All @@ -521,6 +492,10 @@
}

function _bindEventsInterface() {
originalinput.on('touchspin.destroy', function() {
_destroy();
});

originalinput.on('touchspin.uponce', function() {
stopSpin();
upOnce();
Expand Down Expand Up @@ -595,11 +570,11 @@
returnval = parsedval;
}

if (parsedval < settings.min) {
if ((settings.min !== null) && (parsedval < settings.min)) {
returnval = settings.min;
}

if (parsedval > settings.max) {
if ((settings.max !== null) && (parsedval > settings.max)) {
returnval = settings.max;
}

Expand Down Expand Up @@ -642,7 +617,7 @@

value = value + boostedstep;

if (value > settings.max) {
if ((settings.max !== null) && (value > settings.max)) {
value = settings.max;
originalinput.trigger('touchspin.on.max');
stopSpin();
Expand All @@ -668,7 +643,7 @@

value = value - boostedstep;

if (value < settings.min) {
if ((settings.min !== null) && (value < settings.min)) {
value = settings.min;
originalinput.trigger('touchspin.on.min');
stopSpin();
Expand Down
Loading

0 comments on commit d21624d

Please sign in to comment.