Skip to content

Commit

Permalink
Bump v0.2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
vsn4ik committed Feb 6, 2016
1 parent f3decd7 commit eef3248
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 63 deletions.
129 changes: 72 additions & 57 deletions dist/js/jquery.spinner.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*!
* jquery.spinner v0.2.0 (https://vsn4ik.github.io/jquery.spinner/)
* Copyright 2015-2016 xixilive
* jquery.spinner v0.2.1 (https://vsn4ik.github.io/jquery.spinner/)
* Copyright 2013-2016 xixilive
* Licensed under the MIT license
*/
'use strict';
Expand All @@ -20,26 +20,30 @@
}
})(function($) {
var spinningTimer;
var Spinning = function(element, options){
var Spinning = function(element, options) {
options = $.extend({}, options);
this.$el = element;
this.options = $.extend({}, Spinning.rules.defaults, Spinning.rules[options.rule] || {}, options);
this.min = parseFloat(this.options.min) || 0;
this.max = parseFloat(this.options.max) || 0;
this.min = Number(this.options.min) || 0;
this.max = Number(this.options.max) || 0;

this.$el.on({
'focus.spinner': $.proxy(function(e){
'focus.spinner': $.proxy(function(e) {
e.preventDefault();
$(document).trigger('mouseup.spinner');
this.oldValue = this.value();
}, this),
'change.spinner': $.proxy(function(e){
'change.spinner': $.proxy(function(e) {
e.preventDefault();
this.value(this.$el.val());
}, this),
'keydown.spinner': $.proxy(function(e){
var dir = {38: 'up', 40: 'down'}[e.which];
if(dir){
'keydown.spinner': $.proxy(function(e) {
var dir = {
38: 'up',
40: 'down'
}[e.which];

if (dir) {
e.preventDefault();
this.spin(dir);
}
Expand All @@ -53,19 +57,19 @@
};

Spinning.rules = {
defaults: {min: null, max: null, step: 1, precision:0},
currency: {min: 0.00, max: null, step: 0.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}
defaults: { min: null, max: null, step: 1, precision: 0 },
currency: { min: 0.00, max: null, step: 0.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 }
};

Spinning.prototype = {
spin: function(dir){
spin: function(dir) {
if (this.$el.prop('disabled')) {
return;
}
Expand All @@ -74,79 +78,85 @@
var step = $.isFunction(this.options.step) ? this.options.step.call(this, dir) : this.options.step;
var multipler = dir === 'up' ? 1 : -1;

this.value(this.oldValue + Number(step, 10) * multipler);
this.value(this.oldValue + Number(step) * multipler);
},

value: function(v){
if(v === null || v === undefined){
value: function(v) {
if (v === null || v === undefined) {
return this.numeric(this.$el.val());
}
v = this.numeric(v);

var valid = this.validate(v);
if(valid !== 0){
if (valid !== 0) {
v = (valid === -1) ? this.min : this.max;
}
this.$el.val(v.toFixed(this.options.precision));

if(this.oldValue !== this.value()){
if (this.oldValue !== this.value()) {
//changing.spinner
this.$el.trigger('changing.spinner', [this.value(), this.oldValue]);

//lazy changed.spinner
clearTimeout(spinningTimer);
spinningTimer = setTimeout($.proxy(function(){
spinningTimer = setTimeout($.proxy(function() {
this.$el.trigger('changed.spinner', [this.value(), this.oldValue]);
}, this), Spinner.delay);
}
},

numeric: function(v){
numeric: function(v) {
v = this.options.precision > 0 ? parseFloat(v, 10) : parseInt(v, 10);

// If the variable is a number
if (!isNaN(parseFloat(v)) && isFinite(v)) {
if (isFinite(v)) {
return v;
} else {
return v || this.options.min || 0;
}

return v || this.options.min || 0;
},

validate: function(val){
if(this.options.min !== null && val < this.min){
validate: function(val) {
if (this.options.min !== null && val < this.min) {
return -1;
}
if(this.options.max !== null && val > this.max){

if (this.options.max !== null && val > this.max) {
return 1;
}

return 0;
}
};

var Spinner = function(element, options){
var Spinner = function(element, options) {
options = $.extend({}, options);
this.$el = element;
this.$spinning = $('[data-spin="spinner"]', this.$el);
if (this.$spinning.length === 0){
this.$spinning = $(':input[type="text"]', this.$el);
this.$spinning = this.$el.find('[data-spin="spinner"]');

if (this.$spinning.length === 0) {
this.$spinning = this.$el.find(':input[type="text"]');
}
this.spinning = new Spinning(this.$spinning, $.extend(this.$spinning.data(), options));
this.spinning = new Spinning(this.$spinning, $.extend({}, this.$spinning.data(), options));

this.$el
.on('click.spinner', "[data-spin='up'], [data-spin='down']", $.proxy(this, 'spin'))
.on('mousedown.spinner', "[data-spin='up'], [data-spin='down']", $.proxy(this, 'spin'));

$(document).on('mouseup.spinner', $.proxy(function(){
$(document).on('mouseup.spinner', $.proxy(function() {
clearTimeout(this.spinTimeout);
clearInterval(this.spinInterval);
}, this));

if (options.delay){
if (options.delay) {
this.delay(options.delay);
}
if (options.changed){

if (options.changed) {
this.changed(options.changed);
}
if (options.changing){

if (options.changing) {
this.changing(options.changing);
}
};
Expand All @@ -156,60 +166,65 @@
Spinner.prototype = {
constructor: Spinner,

spin: function(e){
spin: function(e) {
var dir = $(e.currentTarget).data('spin');

switch (e.type) {
case 'click':
e.preventDefault();
this.spinning.spin(dir);
break;

case 'mousedown':
if(e.which === 1){
if (e.which === 1) {
this.spinTimeout = setTimeout($.proxy(this, 'beginSpin', dir), 300);
}
break;
}
},

delay: function(ms){
var delay = parseInt(ms, 10);
if(delay >= 0){
delay: function(ms) {
var delay = Number(ms);

if (delay >= 0) {
this.constructor.delay = delay + 100;
}
},

value: function(){
value: function() {
return this.spinning.value();
},

changed: function(fn){
changed: function(fn) {
this.bindHandler('changed.spinner', fn);
},

changing: function(fn){
changing: function(fn) {
this.bindHandler('changing.spinner', fn);
},

bindHandler: function(t, fn){
if($.isFunction(fn)){
bindHandler: function(t, fn) {
if ($.isFunction(fn)) {
this.$spinning.on(t, fn);
}else{
}
else {
this.$spinning.off(t);
}
},

beginSpin: function(dir){
beginSpin: function(dir) {
this.spinInterval = setInterval($.proxy(this.spinning, 'spin', dir), 100);
}
};

$.fn.spinner = function(options, value) {
return this.each(function() {
var self = $(this), data = self.data('spinner');
var self = $(this);
var data = $.data(this, 'spinner');

if (!data) {
self.data('spinner', (data = new Spinner(self, $.extend({}, self.data(), options))));
data = new Spinner(self, $.extend({}, self.data(), options));

$.data(this, 'spinner', data);
}
if (options === 'delay' || options === 'changed' || options === 'changing') {
data[options](value);
Expand Down
6 changes: 3 additions & 3 deletions dist/js/jquery.spinner.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"private": true,
"name": "jquery.spinner",
"version": "0.2.0",
"version": "0.2.1",
"description": "A Number-Spinner based-on jQuery, Support Keyboard operations and continuous changing",
"keywords": [
"spinner",
Expand Down Expand Up @@ -34,8 +34,8 @@
"grunt-contrib-concat": "~0.5.1",
"grunt-contrib-cssmin": "~0.14.0",
"grunt-contrib-jshint": "~0.6.0",
"grunt-contrib-less": "~1.1.0",
"grunt-contrib-qunit": "~0.7.0",
"grunt-contrib-less": "~1.2.0",
"grunt-contrib-qunit": "~1.0.1",
"grunt-contrib-uglify": "~0.11.0"
}
}

0 comments on commit eef3248

Please sign in to comment.