/*
* jQuery Mobile Framework : plugin to provide number spinbox.
* Copyright (c) JTSage
* CC 3.0 Attribution. May be relicensed without permission/notification.
* https://github.com/jtsage/jquery-mobile-spinbox
*/
(function($) {
$.widget( "mobile.spinbox", $.mobile.widget, {
options: {
// All widget options, including some internal runtime details
dmin: false,
dmax: false,
theme: false,
clickEvent: 'vclick'
},
_create: function() {
var w = this, tmp,
o = $.extend(this.options, this.element.jqmData('options')),
d = {
input: this.element,
wrap: this.element.wrap('
')
.buttonMarkup({icon: 'plus', theme: o.theme, iconpos: 'notext', corners:false, shadow:true, inline:true})
.addClass('ui-corner-right')
.css({'marginLeft':'0px', 'marginBottom':'0px', 'marginTop':'0px', 'paddingLeft':'.4em'})
.appendTo(w.d.wrap);
w.d.down = $('
')
.buttonMarkup({icon: 'minus', theme: o.theme, iconpos: 'notext', corners:false, shadow:true, inline:true})
.addClass('ui-corner-left')
.css({'marginRight':'0px', 'marginBottom':'0px', 'marginTop':'0px', 'paddingRight':'.4em'})
.prependTo(w.d.wrap);
w.d.up.on(o.clickEvent, function(e) {
e.preventDefault();
if ( !w.disabled ) {
tmp = parseInt(w.d.input.val(),10) + 1;
if ( tmp <= o.dmax ) { w.d.input.val(tmp); }
}
});
w.d.down.on(o.clickEvent, function(e) {
e.preventDefault();
if ( !w.disabled ) {
tmp = parseInt(w.d.input.val(),10) - 1;
if ( tmp >= o.dmin ) { w.d.input.val(tmp); }
}
});
if ( typeof $.event.special.mousewheel !== 'undefined' ) { // Mousewheel operation, if plugin is loaded
w.d.input.on('mousewheel', function(e,d) {
e.preventDefault();
if ( !w.disabled ) {
tmp = parseInt(w.d.input.val(),10) + ((d<0)?-1:1);
if ( tmp >= o.dmin && tmp <= o.dmax ) { w.d.input.val(tmp); }
}
});
}
},
disable: function(){
// Disable the element
this.d.input.attr("disabled",true);
this.d.wrap.addClass("ui-disabled").blur();
this.disabled = true;
},
enable: function(){
// Enable the element
this.d.input.attr("disabled", false);
this.d.wrap.removeClass("ui-disabled");
this.disabled = false;
},
_setOption: function() {
$.Widget.prototype._setOption.apply( this, arguments );
this.refresh();
}
});
// Degrade any inputs to text inputs, suppress standard UI functions.
$( document ).on( "pagebeforecreate", function( e ) {
$( ":jqmData(role='spinbox')", e.target ).each(function() {
$(this).prop('type', 'text');
});
});
// Automatically bind to data-role='datebox' items.
$( document ).on( "pagecreate create", function( e ){
$( ":jqmData(role='spinbox')", e.target ).each(function() {
if ( typeof($(this).data('spinbox')) === "undefined" ) {
$(this).spinbox();
}
});
});
})( jQuery );