jquery.spinner.min.js 3.9 KB

123456
  1. /*!
  2. * jquery.spinner v0.2.1 (https://vsn4ik.github.io/jquery.spinner/)
  3. * Copyright 2013-2016 xixilive
  4. * Licensed under the MIT license
  5. */
  6. "use strict";!function(a){"function"==typeof define&&define.amd?define(["jquery"],a):"object"==typeof exports?module.exports=a(require("jquery")):a(jQuery)}(function(a){var b,c,d=function(b,c){return this.$el=b,this.options=a.extend({},d.rules.defaults,d.rules[c.rule]||{},c),this.min=Number(this.options.min)||0,this.max=Number(this.options.max)||0,this.$el.on({"focus.spinner":a.proxy(function(b){b.preventDefault(),a(document).trigger("mouseup.spinner"),this.oldValue=this.value()},this),"change.spinner":a.proxy(function(a){a.preventDefault(),this.value(this.$el.val())},this),"keydown.spinner":a.proxy(function(a){var b={38:"up",40:"down"}[a.which];b&&(a.preventDefault(),this.spin(b))},this)}),this.oldValue=this.value(),this.value(this.$el.val()),this};d.rules={defaults:{min:null,max:null,step:1,precision:0},currency:{min:0,max:null,step:.01,precision:2},quantity:{min:1,max:999,step:1,precision:0},percent:{min:1,max:100,step:1,precision:0},month:{min:1,max:12,step:1,precision:0},day:{min:1,max:31,step:1,precision:0},hour:{min:0,max:23,step:1,precision:0},minute:{min:1,max:59,step:1,precision:0},second:{min:1,max:59,step:1,precision:0}},d.prototype={spin:function(b){if(!this.$el.prop("disabled")){this.oldValue=this.value();var c=a.isFunction(this.options.step)?this.options.step.call(this,b):this.options.step,d="up"===b?1:-1;this.value(this.oldValue+Number(c)*d)}},value:function(d){if(null===d||void 0===d)return this.numeric(this.$el.val());d=this.numeric(d);var e=this.validate(d);0!==e&&(d=e===-1?this.min:this.max),this.$el.val(d.toFixed(this.options.precision)),this.oldValue!==this.value()&&(this.$el.trigger("changing.spinner",[this.value(),this.oldValue]),clearTimeout(b),b=setTimeout(a.proxy(function(){this.$el.trigger("changed.spinner",[this.value(),this.oldValue])},this),c.delay))},numeric:function(a){return a=this.options.precision>0?parseFloat(a,10):parseInt(a,10),isFinite(a)?a:a||this.options.min||0},validate:function(a){return null!==this.options.min&&a<this.min?-1:null!==this.options.max&&a>this.max?1:0}},c=function(b,c){this.$el=a(b),this.$spinning=this.$el.find('[data-spin="spinner"]'),0===this.$spinning.length&&(this.$spinning=this.$el.find(':input[type="text"]')),c=a.extend({},c,this.$spinning.data()),this.spinning=new d(this.$spinning,c),this.$el.on("click.spinner",'[data-spin="up"], [data-spin="down"]',a.proxy(this,"spin")).on("mousedown.spinner",'[data-spin="up"], [data-spin="down"]',a.proxy(this,"spin")),a(document).on("mouseup.spinner",a.proxy(function(){clearTimeout(this.spinTimeout),clearInterval(this.spinInterval)},this)),c.delay&&this.delay(c.delay),c.changed&&this.changed(c.changed),c.changing&&this.changing(c.changing)},c.delay=500,c.prototype={constructor:c,spin:function(b){var c=a(b.currentTarget).data("spin");switch(b.type){case"click":b.preventDefault(),this.spinning.spin(c);break;case"mousedown":1===b.which&&(this.spinTimeout=setTimeout(a.proxy(this,"beginSpin",c),300))}},delay:function(a){var b=Number(a);b>=0&&(this.constructor.delay=b+100)},value:function(){return this.spinning.value()},changed:function(a){this.bindHandler("changed.spinner",a)},changing:function(a){this.bindHandler("changing.spinner",a)},bindHandler:function(b,c){a.isFunction(c)?this.$spinning.on(b,c):this.$spinning.off(b)},beginSpin:function(b){this.spinInterval=setInterval(a.proxy(this.spinning,"spin",b),100)}};var e=a.fn.spinner;return a.fn.spinner=function(b,d){return this.each(function(){var e=a.data(this,"spinner");e||(e=new c(this,b),a.data(this,"spinner",e)),"delay"===b||"changed"===b||"changing"===b?e[b](d):"step"===b&&d?e.spinning.step=d:"spin"===b&&d&&e.spinning.spin(d)})},a.fn.spinner.Constructor=c,a.fn.spinner.noConflict=function(){return a.fn.spinner=e,this},a(function(){a('[data-trigger="spinner"]').spinner()}),a.fn.spinner});