;(function($, window, document, undefined) { 'use strict'; var output = function(msg) { window.console && console.log(msg); }; var wanspinner = function(element, options) { this.defaults = { maxvalue: 9999, minvalue: 1, step: 1, start: 1, inputwidth: 40, plusclick: function(element, val) { return true; }, minusclick: function(element, val) { return true; }, exceptionfun: function(exp) { return true; }, valuechanged: function(element, val) { return true; } }; this.options = $.extend({}, this.defaults, options); this.options.steplength = ((+this.options.step).tostring().split('.')[1] || '').length; this.options.stepfloat = parseint(1 * math.pow(10, this.options.steplength) / this.options.step) || 1; this.element = $(element); this.element.each(function(index, dt) { var input = $(dt).children('input'); var initvalue = input.val() || this.options.start; input.val(initvalue); }); this.element.children('input').css('width', this.options.inputwidth); this.exception = { too_large: 1, normal: 0, too_small: -1 }; }; wanspinner.prototype.bind = function() { var self = this; $(self.element).delegate('.minus', 'click', function() { var val; var input = $(this).siblings('input'); if (self.options.stepfloat > 1) { val = (+input.val() || 0) * self.options.stepfloat - self.options.step * self.options.stepfloat; val = math.round(val) / self.options.stepfloat; } else { val = (+input.val() || 0) - self.options.step; } val = val.tofixed(self.options.steplength); if (val < self.options.minvalue) { typeof(self.options.exceptionfun) === 'function' && self.options.exceptionfun(self.exception.too_small); } else { input.val(val); typeof(self.options.minusclick) === 'function' && self.options.minusclick($(this).parent(), val); typeof(self.options.valuechanged) === 'function' && self.options.valuechanged($(this).parent(), val); } return false; }).delegate('.plus', 'click', function() { var val; var input = $(this).siblings('input'); if (self.options.stepfloat > 1) { val = (+input.val() || 0) * self.options.stepfloat + self.options.step * self.options.stepfloat; val = math.round(val) / self.options.stepfloat; } else { val = (+input.val() || 0) + self.options.step; } val = val.tofixed(self.options.steplength); if (val > self.options.maxvalue) { typeof(self.options.exceptionfun) === 'function' && self.options.exceptionfun(self.exception.too_large); } else { input.val(val); typeof(self.options.plusclick) === 'function' && self.options.plusclick($(this).parent(), val); typeof(self.options.valuechanged) === 'function' && self.options.valuechanged($(this).parent(), val); } return false; }).delegate('input', 'change', function() { var val = +$(this).val() || 0; if (val > self.options.maxvalue) { val = self.options.maxvalue; typeof(self.options.exceptionfun) === 'function' && self.options.exceptionfun(self.exception.too_large); } else if (val < self.options.minvalue) { val = self.options.minvalue; typeof(self.options.exceptionfun) === 'function' && self.options.exceptionfun(self.exception.too_small); } $(this).val(val); typeof(self.options.valuechanged) === 'function' && self.options.valuechanged($(this).parent(), val); }); } $.fn.wanspinner = function(options) { var wanspinner = new wanspinner(this, options); wanspinner.bind(); return this; }; })(jquery, window, document);