diff --git a/LICENSE.md b/LICENSE.md index 32ae3be..755b2b2 100644 --- a/LICENSE.md +++ b/LICENSE.md @@ -1,5 +1,5 @@ Bootstrap TouchSpin -v4.1.0 +v4.2.0 A mobile and touch friendly input spinner component for Bootstrap 3. diff --git a/bootstrap-touchspin.jquery.json b/bootstrap-touchspin.jquery.json index 0a5fe48..b267907 100644 --- a/bootstrap-touchspin.jquery.json +++ b/bootstrap-touchspin.jquery.json @@ -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" diff --git a/bower.json b/bower.json index 1925a00..046c8ae 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "bootstrap-touchspin", - "version": "4.1.0", + "version": "4.2.0", "homepage": "http://www.virtuosoft.eu/code/bootstrap-touchspin/", "authors": [ { diff --git a/dist/jquery.bootstrap-touchspin.css b/dist/jquery.bootstrap-touchspin.css index 588c965..5fa3990 100644 --- a/dist/jquery.bootstrap-touchspin.css +++ b/dist/jquery.bootstrap-touchspin.css @@ -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/ * diff --git a/dist/jquery.bootstrap-touchspin.js b/dist/jquery.bootstrap-touchspin.js index 10a3f78..ad01c13 100644 --- a/dist/jquery.bootstrap-touchspin.js +++ b/dist/jquery.bootstrap-touchspin.js @@ -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/ * @@ -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, @@ -135,10 +109,8 @@ spincount = 0, spinning = false; - init(); - function init() { if (originalinput.data('alreadyinitialized')) { return; @@ -148,7 +120,6 @@ _currentSpinnerId += 1; originalinput.data('spinnerid', _currentSpinnerId); - if (!originalinput.is('input')) { console.log('Must be an input.'); return; @@ -162,7 +133,6 @@ _hideEmptyPrefixPostfix(); _bindEvents(); _bindEventsInterface(); - elements.input.css('display', 'block'); } function _setInitval() { @@ -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); @@ -237,24 +226,24 @@ var downhtml, uphtml, - prefixhtml = '' + settings.prefix + '', - postfixhtml = '' + settings.postfix + ''; + prefixhtml = '' + settings.prefix + '', + postfixhtml = '' + settings.postfix + ''; if (prev.hasClass('input-group-btn') || prev.hasClass('input-group-prepend')) { - downhtml = ''; + downhtml = ''; prev.append(downhtml); } else { - downhtml = ''; + downhtml = ''; $(downhtml).insertBefore(originalinput); } if (next.hasClass('input-group-btn') || next.hasClass('input-group-append')) { - uphtml = ''; + uphtml = ''; next.prepend(uphtml); } else { - uphtml = ''; + uphtml = ''; $(uphtml).insertAfter(originalinput); } @@ -277,10 +266,10 @@ } if (settings.verticalbuttons) { - html = '
' + settings.prefix + '' + settings.postfix + '
'; + html = '
' + settings.prefix + '' + settings.postfix + '
'; } else { - html = '
' + settings.prefix + '' + settings.postfix + '
'; + html = '
' + settings.prefix + '' + settings.postfix + '
'; } container = $(html).insertBefore(originalinput); @@ -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) { @@ -335,7 +324,7 @@ } }); - originalinput.on('keyup', function(ev) { + originalinput.on('keyup.touchspin', function(ev) { var code = ev.keyCode || ev.which; if (code === 38) { @@ -346,7 +335,7 @@ } }); - originalinput.on('blur', function() { + originalinput.on('blur.touchspin', function() { _checkValue(); originalinput.val(settings.callback_after_calculation(originalinput.val())); }); @@ -363,7 +352,7 @@ } }); - elements.down.on('keyup', function(ev) { + elements.down.on('keyup.touchspin', function(ev) { var code = ev.keyCode || ev.which; if (code === 32 || code === 13) { @@ -371,7 +360,7 @@ } }); - elements.up.on('keydown', function(ev) { + elements.up.on('keydown.touchspin', function(ev) { var code = ev.keyCode || ev.which; if (code === 32 || code === 13) { @@ -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) { @@ -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; } @@ -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; } @@ -465,7 +454,7 @@ stopSpin(); }); - elements.down.on('mousemove touchmove', function(ev) { + elements.down.on('mousemove.touchspin touchmove.touchspin', function(ev) { if (!spinning) { return; } @@ -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; } @@ -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; } @@ -521,6 +492,10 @@ } function _bindEventsInterface() { + originalinput.on('touchspin.destroy', function() { + _destroy(); + }); + originalinput.on('touchspin.uponce', function() { stopSpin(); upOnce(); @@ -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; } @@ -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(); @@ -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(); diff --git a/dist/jquery.bootstrap-touchspin.min.js b/dist/jquery.bootstrap-touchspin.min.js index fe61801..4e7411c 100644 --- a/dist/jquery.bootstrap-touchspin.min.js +++ b/dist/jquery.bootstrap-touchspin.min.js @@ -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/ * @@ -7,4 +7,4 @@ * Under Apache License v2.0 License */ -!function(o){"function"==typeof define&&define.amd?define(["jquery"],o):"object"==typeof module&&module.exports?module.exports=function(t,n){return void 0===n&&(n="undefined"!=typeof window?require("jquery"):require("jquery")(t)),o(n),n}:o(jQuery)}(function(y){"use strict";var k=0;function C(t,n){return y.map(t,function(t){return t+".touchspin_"+n})}y.fn.TouchSpin=function(m){if("destroy"!==m){var w={min:0,max:100,initval:"",replacementval:"",step:1,decimals:0,stepinterval:100,forcestepdivisibility:"round",stepintervaldelay:500,verticalbuttons:!1,verticalup:"+",verticaldown:"-",verticalupclass:"",verticaldownclass:"",prefix:"",postfix:"",prefix_extraclass:"",postfix_extraclass:"",booster:!0,boostat:10,maxboostedstep:!1,mousewheel:!0,buttondown_class:"btn btn-primary",buttonup_class:"btn btn-primary",buttondown_txt:"-",buttonup_txt:"+",callback_before_calculation:function(t){return t},callback_after_calculation:function(t){return t}},_={min:"min",max:"max",initval:"init-val",replacementval:"replacement-val",step:"step",decimals:"decimals",stepinterval:"step-interval",verticalbuttons:"vertical-buttons",verticalupclass:"vertical-up-class",verticaldownclass:"vertical-down-class",forcestepdivisibility:"force-step-divisibility",stepintervaldelay:"step-interval-delay",prefix:"prefix",postfix:"postfix",prefix_extraclass:"prefix-extra-class",postfix_extraclass:"postfix-extra-class",booster:"booster",boostat:"boostat",maxboostedstep:"max-boosted-step",mousewheel:"mouse-wheel",buttondown_class:"button-down-class",buttonup_class:"button-up-class",buttondown_txt:"button-down-txt",buttonup_txt:"button-up-txt"};return this.each(function(){var i,u,s,o,t,n,a,e,r=y(this),p=r.data(),c=0,l=!1;function d(){var t,n,o;""!==(t=i.callback_before_calculation(r.val()))?0i.max&&(o=i.max),o=function(t){switch(i.forcestepdivisibility){case"round":return(Math.round(t/i.step)*i.step).toFixed(i.decimals);case"floor":return(Math.floor(t/i.step)*i.step).toFixed(i.decimals);case"ceil":return(Math.ceil(t/i.step)*i.step).toFixed(i.decimals);default:return t}}(o),Number(t).toString()!==o.toString()&&(r.val(o),r.trigger("change"))):""!==i.replacementval&&(r.val(i.replacementval),r.trigger("change"))}function f(){if(i.booster){var t=Math.pow(2,Math.floor(c/i.boostat))*i.step;return i.maxboostedstep&&t>i.maxboostedstep&&(t=i.maxboostedstep,o=Math.round(o/t)*t),Math.max(i.step,t)}return i.step}function b(){d(),o=parseFloat(i.callback_before_calculation(s.input.val())),isNaN(o)&&(o=0);var t=o,n=f();(o+=n)>i.max&&(o=i.max,r.trigger("touchspin.on.max"),g()),s.input.val(i.callback_after_calculation(Number(o).toFixed(i.decimals))),t!==o&&r.trigger("change")}function v(){d(),o=parseFloat(i.callback_before_calculation(s.input.val())),isNaN(o)&&(o=0);var t=o,n=f();(o-=n)'+i.prefix+"",p=''+i.postfix+"";a.hasClass("input-group-btn")||a.hasClass("input-group-prepend")?(n='",a.append(n)):(n='",y(n).insertBefore(r));s.hasClass("input-group-btn")||s.hasClass("input-group-append")?(o='",s.prepend(o)):(o='",y(o).insertAfter(r));y(e).insertBefore(r),y(p).insertAfter(r),u=t}(n):function(){var t,n="";r.hasClass("input-sm")&&(n="input-group-sm");r.hasClass("input-lg")&&(n="input-group-lg");t=i.verticalbuttons?'
'+i.prefix+''+i.postfix+'
":'
'+i.prefix+''+i.postfix+'
";u=y(t).insertBefore(r),y(".bootstrap-touchspin-prefix",u).after(r),r.hasClass("input-sm")?u.addClass("input-group-sm"):r.hasClass("input-lg")&&u.addClass("input-group-lg")}()}(),s={down:y(".bootstrap-touchspin-down",u),up:y(".bootstrap-touchspin-up",u),input:y("input",u),prefix:y(".bootstrap-touchspin-prefix",u).addClass(i.prefix_extraclass),postfix:y(".bootstrap-touchspin-postfix",u).addClass(i.postfix_extraclass)},function(){""===i.prefix&&s.prefix.hide();""===i.postfix&&s.postfix.hide()}(),r.on("keydown",function(t){var n=t.keyCode||t.which;38===n?("up"!==l&&(b(),x()),t.preventDefault()):40===n&&("down"!==l&&(v(),h()),t.preventDefault())}),r.on("keyup",function(t){var n=t.keyCode||t.which;38===n?g():40===n&&g()}),r.on("blur",function(){d(),r.val(i.callback_after_calculation(r.val()))}),s.down.on("keydown",function(t){var n=t.keyCode||t.which;32!==n&&13!==n||("down"!==l&&(v(),h()),t.preventDefault())}),s.down.on("keyup",function(t){var n=t.keyCode||t.which;32!==n&&13!==n||g()}),s.up.on("keydown",function(t){var n=t.keyCode||t.which;32!==n&&13!==n||("up"!==l&&(b(),x()),t.preventDefault())}),s.up.on("keyup",function(t){var n=t.keyCode||t.which;32!==n&&13!==n||g()}),s.down.on("mousedown.touchspin",function(t){s.down.off("touchstart.touchspin"),r.is(":disabled")||(v(),h(),t.preventDefault(),t.stopPropagation())}),s.down.on("touchstart.touchspin",function(t){s.down.off("mousedown.touchspin"),r.is(":disabled")||(v(),h(),t.preventDefault(),t.stopPropagation())}),s.up.on("mousedown.touchspin",function(t){s.up.off("touchstart.touchspin"),r.is(":disabled")||(b(),x(),t.preventDefault(),t.stopPropagation())}),s.up.on("touchstart.touchspin",function(t){s.up.off("mousedown.touchspin"),r.is(":disabled")||(b(),x(),t.preventDefault(),t.stopPropagation())}),s.up.on("mouseout touchleave touchend touchcancel",function(t){l&&(t.stopPropagation(),g())}),s.down.on("mouseout touchleave touchend touchcancel",function(t){l&&(t.stopPropagation(),g())}),s.down.on("mousemove touchmove",function(t){l&&(t.stopPropagation(),t.preventDefault())}),s.up.on("mousemove touchmove",function(t){l&&(t.stopPropagation(),t.preventDefault())}),y(document).on(C(["mouseup","touchend","touchcancel"],k).join(" "),function(t){l&&(t.preventDefault(),g())}),y(document).on(C(["mousemove","touchmove","scroll","scrollstart"],k).join(" "),function(t){l&&(t.preventDefault(),g())}),r.on("mousewheel DOMMouseScroll",function(t){if(i.mousewheel&&r.is(":focus")){var n=t.originalEvent.wheelDelta||-t.originalEvent.deltaY||-t.originalEvent.detail;t.stopPropagation(),t.preventDefault(),n<0?v():b()}}),r.on("touchspin.uponce",function(){g(),b()}),r.on("touchspin.downonce",function(){g(),v()}),r.on("touchspin.startupspin",function(){x()}),r.on("touchspin.startdownspin",function(){h()}),r.on("touchspin.stopspin",function(){g()}),r.on("touchspin.updatesettings",function(t,n){!function(t){var n;n=t,i=y.extend({},i,n),n.postfix&&r.parent().find(".bootstrap-touchspin-postfix").text(n.postfix),n.prefix&&r.parent().find(".bootstrap-touchspin-prefix").text(n.prefix),d();var o=s.input.val();""!==o&&(o=Number(i.callback_before_calculation(s.input.val())),s.input.val(i.callback_after_calculation(o.toFixed(i.decimals))))}(n)}),s.input.css("display","block");var a}()})}this.each(function(){var t=y(this).data();y(document).off(C(["mouseup","touchend","touchcancel","mousemove","touchmove","scroll","scrollstart"],t.spinnerid).join(" "))})}}); \ No newline at end of file +!function(o){"function"==typeof define&&define.amd?define(["jquery"],o):"object"==typeof module&&module.exports?module.exports=function(t,n){return void 0===n&&(n="undefined"!=typeof window?require("jquery"):require("jquery")(t)),o(n),n}:o(jQuery)}(function(y){"use strict";var k=0;y.fn.TouchSpin=function(m){var w={min:0,max:100,initval:"",replacementval:"",step:1,decimals:0,stepinterval:100,forcestepdivisibility:"round",stepintervaldelay:500,verticalbuttons:!1,verticalup:"+",verticaldown:"-",verticalupclass:"",verticaldownclass:"",prefix:"",postfix:"",prefix_extraclass:"",postfix_extraclass:"",booster:!0,boostat:10,maxboostedstep:!1,mousewheel:!0,buttondown_class:"btn btn-primary",buttonup_class:"btn btn-primary",buttondown_txt:"-",buttonup_txt:"+",callback_before_calculation:function(t){return t},callback_after_calculation:function(t){return t}},_={min:"min",max:"max",initval:"init-val",replacementval:"replacement-val",step:"step",decimals:"decimals",stepinterval:"step-interval",verticalbuttons:"vertical-buttons",verticalupclass:"vertical-up-class",verticaldownclass:"vertical-down-class",forcestepdivisibility:"force-step-divisibility",stepintervaldelay:"step-interval-delay",prefix:"prefix",postfix:"postfix",prefix_extraclass:"prefix-extra-class",postfix_extraclass:"postfix-extra-class",booster:"booster",boostat:"boostat",maxboostedstep:"max-boosted-step",mousewheel:"mouse-wheel",buttondown_class:"button-down-class",buttonup_class:"button-up-class",buttondown_txt:"button-down-txt",buttonup_txt:"button-up-txt"};return this.each(function(){var i,u,a,o,t,n,s,p,r=y(this),e=r.data(),c=0,l=!1;function d(){var t,n,o;""!==(t=i.callback_before_calculation(r.val()))?0i.max&&(o=i.max),o=function(t){switch(i.forcestepdivisibility){case"round":return(Math.round(t/i.step)*i.step).toFixed(i.decimals);case"floor":return(Math.floor(t/i.step)*i.step).toFixed(i.decimals);case"ceil":return(Math.ceil(t/i.step)*i.step).toFixed(i.decimals);default:return t}}(o),Number(t).toString()!==o.toString()&&(r.val(o),r.trigger("change"))):""!==i.replacementval&&(r.val(i.replacementval),r.trigger("change"))}function f(){if(i.booster){var t=Math.pow(2,Math.floor(c/i.boostat))*i.step;return i.maxboostedstep&&t>i.maxboostedstep&&(t=i.maxboostedstep,o=Math.round(o/t)*t),Math.max(i.step,t)}return i.step}function b(){d(),o=parseFloat(i.callback_before_calculation(a.input.val())),isNaN(o)&&(o=0);var t=o,n=f();o+=n,null!==i.max&&o>i.max&&(o=i.max,r.trigger("touchspin.on.max"),g()),a.input.val(i.callback_after_calculation(Number(o).toFixed(i.decimals))),t!==o&&r.trigger("change")}function h(){d(),o=parseFloat(i.callback_before_calculation(a.input.val())),isNaN(o)&&(o=0);var t=o,n=f();o-=n,null!==i.min&&o'+i.prefix+"",e=''+i.postfix+"";s.hasClass("input-group-btn")||s.hasClass("input-group-prepend")?(n='",s.append(n)):(n='",y(n).insertBefore(r));a.hasClass("input-group-btn")||a.hasClass("input-group-append")?(o='",a.prepend(o)):(o='",y(o).insertAfter(r));y(p).insertBefore(r),y(e).insertAfter(r),u=t}(n):function(){var t,n="";r.hasClass("input-sm")&&(n="input-group-sm");r.hasClass("input-lg")&&(n="input-group-lg");t=i.verticalbuttons?'
'+i.prefix+''+i.postfix+'
":'
'+i.prefix+''+i.postfix+'
";u=y(t).insertBefore(r),y(".bootstrap-touchspin-prefix",u).after(r),r.hasClass("input-sm")?u.addClass("input-group-sm"):r.hasClass("input-lg")&&u.addClass("input-group-lg")}()}(),a={down:y(".bootstrap-touchspin-down",u),up:y(".bootstrap-touchspin-up",u),input:y("input",u),prefix:y(".bootstrap-touchspin-prefix",u).addClass(i.prefix_extraclass),postfix:y(".bootstrap-touchspin-postfix",u).addClass(i.postfix_extraclass)},function(){""===i.prefix&&a.prefix.hide();""===i.postfix&&a.postfix.hide()}(),r.on("keydown.touchspin",function(t){var n=t.keyCode||t.which;38===n?("up"!==l&&(b(),x()),t.preventDefault()):40===n&&("down"!==l&&(h(),v()),t.preventDefault())}),r.on("keyup.touchspin",function(t){var n=t.keyCode||t.which;38===n?g():40===n&&g()}),r.on("blur.touchspin",function(){d(),r.val(i.callback_after_calculation(r.val()))}),a.down.on("keydown",function(t){var n=t.keyCode||t.which;32!==n&&13!==n||("down"!==l&&(h(),v()),t.preventDefault())}),a.down.on("keyup.touchspin",function(t){var n=t.keyCode||t.which;32!==n&&13!==n||g()}),a.up.on("keydown.touchspin",function(t){var n=t.keyCode||t.which;32!==n&&13!==n||("up"!==l&&(b(),x()),t.preventDefault())}),a.up.on("keyup.touchspin",function(t){var n=t.keyCode||t.which;32!==n&&13!==n||g()}),a.down.on("mousedown.touchspin",function(t){a.down.off("touchstart.touchspin"),r.is(":disabled")||(h(),v(),t.preventDefault(),t.stopPropagation())}),a.down.on("touchstart.touchspin",function(t){a.down.off("mousedown.touchspin"),r.is(":disabled")||(h(),v(),t.preventDefault(),t.stopPropagation())}),a.up.on("mousedown.touchspin",function(t){a.up.off("touchstart.touchspin"),r.is(":disabled")||(b(),x(),t.preventDefault(),t.stopPropagation())}),a.up.on("touchstart.touchspin",function(t){a.up.off("mousedown.touchspin"),r.is(":disabled")||(b(),x(),t.preventDefault(),t.stopPropagation())}),a.up.on("mouseout.touchspin touchleave.touchspin touchend.touchspin touchcancel.touchspin",function(t){l&&(t.stopPropagation(),g())}),a.down.on("mouseout.touchspin touchleave.touchspin touchend.touchspin touchcancel.touchspin",function(t){l&&(t.stopPropagation(),g())}),a.down.on("mousemove.touchspin touchmove.touchspin",function(t){l&&(t.stopPropagation(),t.preventDefault())}),a.up.on("mousemove.touchspin touchmove.touchspin",function(t){l&&(t.stopPropagation(),t.preventDefault())}),r.on("mousewheel.touchspin DOMMouseScroll.touchspin",function(t){if(i.mousewheel&&r.is(":focus")){var n=t.originalEvent.wheelDelta||-t.originalEvent.deltaY||-t.originalEvent.detail;t.stopPropagation(),t.preventDefault(),n<0?h():b()}}),r.on("touchspin.destroy",function(){var t;t=r.parent(),g(),r.off(".touchspin"),t.hasClass("bootstrap-touchspin-injected")?(r.siblings().remove(),r.unwrap()):(y(".bootstrap-touchspin-injected",t).remove(),t.removeClass("bootstrap-touchspin")),r.data("alreadyinitialized",!1)}),r.on("touchspin.uponce",function(){g(),b()}),r.on("touchspin.downonce",function(){g(),h()}),r.on("touchspin.startupspin",function(){x()}),r.on("touchspin.startdownspin",function(){v()}),r.on("touchspin.stopspin",function(){g()}),r.on("touchspin.updatesettings",function(t,n){!function(t){var n;n=t,i=y.extend({},i,n),n.postfix&&r.parent().find(".bootstrap-touchspin-postfix").text(n.postfix),n.prefix&&r.parent().find(".bootstrap-touchspin-prefix").text(n.prefix),d();var o=a.input.val();""!==o&&(o=Number(i.callback_before_calculation(a.input.val())),a.input.val(i.callback_after_calculation(o.toFixed(i.decimals))))}(n)});var s}()})}}); \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index c2c2b90..4530d7a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "bootstrap-touchspin", - "version": "4.1.0", + "version": "4.2.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 3c18e01..f7de939 100644 --- a/package.json +++ b/package.json @@ -8,62 +8,13 @@ }, "main": "dist/jquery.bootstrap-touchspin.js", "license": "Apache-2.0", - "contributors": [ - { - "name": "István Ujj-Mészáros", - "url": "https://github.com/istvan-ujjmeszaros" - }, - { - "name": "Phil H", - "url": "https://github.com/pennstatephil" - }, - { - "name": "lukchojnicki", - "url": "https://github.com/lukchojnicki" - }, - { - "name": "Stefan Bauer", - "url": "https://github.com/sba" - }, - { - "name": "Serhan Apaydın", - "url": "https://github.com/monoblaine" - }, - { - "name": "Dewey Nguyen", - "url": "https://github.com/aduyng" - }, - { - "name": "Emanuele Rampichini", - "url": "https://github.com/lele85" - }, - { - "name": "Greg Walker", - "url": "https://github.com/mgwalker" - }, - { - "name": "amid2887", - "url": "https://github.com/amid2887" - }, - { - "name": "Eliak", - "url": "https://github.com/Eliak" - }, - { - "name": "Benjamin Cook", - "url": "https://github.com/blcook223" - }, - { - "name": "Hendy Irawan", - "url": "https://github.com/ceefour" - } - ], + "contributors": [], "repository": { "type": "git", "url": "https://github.com/istvan-ujjmeszaros/bootstrap-touchspin.git" }, "homepage": "httsp://www.virtuosoft.eu/code/bootstrap-touchspin/", - "version": "4.1.0", + "version": "4.2.0", "devDependencies": { "grunt": "^1.0.2", "grunt-cli": "^1.2.0", diff --git a/src/jquery.bootstrap-touchspin.js b/src/jquery.bootstrap-touchspin.js index 1321c55..614b1ee 100644 --- a/src/jquery.bootstrap-touchspin.js +++ b/src/jquery.bootstrap-touchspin.js @@ -22,34 +22,8 @@ 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, // If null, there is no minimum enforced max: 100, // If null, there is no maximum enforced @@ -127,10 +101,8 @@ spincount = 0, spinning = false; - init(); - function init() { if (originalinput.data('alreadyinitialized')) { return; @@ -140,7 +112,6 @@ _currentSpinnerId += 1; originalinput.data('spinnerid', _currentSpinnerId); - if (!originalinput.is('input')) { console.log('Must be an input.'); return; @@ -154,7 +125,6 @@ _hideEmptyPrefixPostfix(); _bindEvents(); _bindEventsInterface(); - elements.input.css('display', 'block'); } function _setInitval() { @@ -190,6 +160,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); @@ -229,24 +218,24 @@ var downhtml, uphtml, - prefixhtml = '' + settings.prefix + '', - postfixhtml = '' + settings.postfix + ''; + prefixhtml = '' + settings.prefix + '', + postfixhtml = '' + settings.postfix + ''; if (prev.hasClass('input-group-btn') || prev.hasClass('input-group-prepend')) { - downhtml = ''; + downhtml = ''; prev.append(downhtml); } else { - downhtml = ''; + downhtml = ''; $(downhtml).insertBefore(originalinput); } if (next.hasClass('input-group-btn') || next.hasClass('input-group-append')) { - uphtml = ''; + uphtml = ''; next.prepend(uphtml); } else { - uphtml = ''; + uphtml = ''; $(uphtml).insertAfter(originalinput); } @@ -269,10 +258,10 @@ } if (settings.verticalbuttons) { - html = '
' + settings.prefix + '' + settings.postfix + '
'; + html = '
' + settings.prefix + '' + settings.postfix + '
'; } else { - html = '
' + settings.prefix + '' + settings.postfix + '
'; + html = '
' + settings.prefix + '' + settings.postfix + '
'; } container = $(html).insertBefore(originalinput); @@ -308,7 +297,7 @@ } function _bindEvents() { - originalinput.on('keydown', function(ev) { + originalinput.on('keydown.touchspin', function(ev) { var code = ev.keyCode || ev.which; if (code === 38) { @@ -327,7 +316,7 @@ } }); - originalinput.on('keyup', function(ev) { + originalinput.on('keyup.touchspin', function(ev) { var code = ev.keyCode || ev.which; if (code === 38) { @@ -338,7 +327,7 @@ } }); - originalinput.on('blur', function() { + originalinput.on('blur.touchspin', function() { _checkValue(); originalinput.val(settings.callback_after_calculation(originalinput.val())); }); @@ -355,7 +344,7 @@ } }); - elements.down.on('keyup', function(ev) { + elements.down.on('keyup.touchspin', function(ev) { var code = ev.keyCode || ev.which; if (code === 32 || code === 13) { @@ -363,7 +352,7 @@ } }); - elements.up.on('keydown', function(ev) { + elements.up.on('keydown.touchspin', function(ev) { var code = ev.keyCode || ev.which; if (code === 32 || code === 13) { @@ -375,7 +364,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) { @@ -439,7 +428,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; } @@ -448,7 +437,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; } @@ -457,7 +446,7 @@ stopSpin(); }); - elements.down.on('mousemove touchmove', function(ev) { + elements.down.on('mousemove.touchspin touchmove.touchspin', function(ev) { if (!spinning) { return; } @@ -466,7 +455,7 @@ ev.preventDefault(); }); - elements.up.on('mousemove touchmove', function(ev) { + elements.up.on('mousemove.touchspin touchmove.touchspin', function(ev) { if (!spinning) { return; } @@ -475,25 +464,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; } @@ -513,6 +484,10 @@ } function _bindEventsInterface() { + originalinput.on('touchspin.destroy', function() { + _destroy(); + }); + originalinput.on('touchspin.uponce', function() { stopSpin(); upOnce();