' : '
' + '
' + (s.headertext ? '
' : ''));
for (i = 0; i < s.wheels.length; i++) {
html += '
';
}
html += (s.display != 'inline' ? '
' + s.settext + '' + (s.button3 ? '' + s.button3text + '' : '') + '' + s.canceltext + '
' : '
') + '
';
dw = $(html);
scrolltopos();
event('onmarkupready', [dw]);
// show
if (s.display != 'inline') {
dw.appendto('body');
// remove animation class
settimeout(function () {
dw.removeclass('dw-trans').find('.dw').removeclass(manim);
}, 350);
} else if (elm.is('div')) {
elm.html(dw);
} else {
dw.insertafter(elm);
}
event('onmarkupinserted', [dw]);
visible = true;
// theme init
theme.init(dw, that);
if (s.display != 'inline') {
// init buttons
that.tap($('.dwb-s span', dw), function () {
if (that.hide(false, 'set') !== false) {
that.setvalue(false, true);
event('onselect', [that.val]);
}
});
that.tap($('.dwb-c span', dw), function () {
that.cancel();
});
if (s.button3) {
that.tap($('.dwb-n span', dw), s.button3);
}
// prevent scrolling if not specified otherwise
if (s.scrolllock) {
dw.bind('touchmove', function (e) {
if (mh <= wh && mw <= ww) {
e.preventdefault();
}
});
}
// disable inputs to prevent bleed through (android bug)
$('input,select,button').each(function () {
if (!$(this).prop('disabled')) {
$(this).addclass('dwtd').prop('disabled', true);
}
});
// set position
position();
$(window).bind('resize.dw', function () {
// sometimes scrolltop is not correctly set, so we wait a little
cleartimeout(debounce);
debounce = settimeout(function () {
position(true);
}, 100);
});
}
// events
dw.delegate('.dwwl', 'dommousescroll mousewheel', function (e) {
if (!isreadonly(this)) {
e.preventdefault();
e = e.originalevent;
var delta = e.wheeldelta ? (e.wheeldelta / 120) : (e.detail ? (-e.detail / 3) : 0),
t = $('.dw-ul', this),
p = +t.data('pos'),
val = math.round(p - delta);
setglobals(t);
calc(t, val, delta < 0 ? 1 : 2);
}
}).delegate('.dwb, .dwwb', start_event, function (e) {
// active button
$(this).addclass('dwb-a');
}).delegate('.dwwb', start_event, function (e) {
e.stoppropagation();
e.preventdefault();
var w = $(this).closest('.dwwl');
if (testtouch(e) && !isreadonly(w) && !w.hasclass('dwa')) {
click = true;
// + button
var t = w.find('.dw-ul'),
func = $(this).hasclass('dwwbp') ? plus : minus;
setglobals(t);
clearinterval(timer);
timer = setinterval(function () { func(t); }, s.delay);
func(t);
}
}).delegate('.dwwl', start_event, function (e) {
// prevent scroll
e.preventdefault();
// scroll start
if (testtouch(e) && !move && !isreadonly(this) && !click) {
move = true;
$(document).bind(move_event, onmove);
target = $('.dw-ul', this);
scrollable = s.mode != 'clickpick';
pos = +target.data('pos');
setglobals(target);
moved = iv[index] !== undefined; // don't allow tap, if still moving
start = getcoord(e, 'y');
starttime = new date();
stop = start;
that.scroll(target, index, pos, 0.001);
if (scrollable) {
target.closest('.dwwl').addclass('dwa');
}
}
});
event('onshow', [dw, v]);
};
/**
* hides the scroller instance.
*/
that.hide = function (prevanim, btn) {
// if onclose handler returns false, prevent hide
if (!visible || event('onclose', [v, btn]) === false) {
return false;
}
// re-enable temporary disabled fields
$('.dwtd').prop('disabled', false).removeclass('dwtd');
elm.blur();
// hide wheels and overlay
if (dw) {
if (s.display != 'inline' && anim && !prevanim) {
dw.addclass('dw-trans').find('.dw').addclass('dw-' + anim + ' dw-out');
settimeout(function () {
dw.remove();
dw = null;
}, 350);
} else {
dw.remove();
dw = null;
}
visible = false;
pixels = {};
// stop positioning on window resize
$(window).unbind('.dw');
}
};
/**
* cancel and hide the scroller instance.
*/
that.cancel = function () {
if (that.hide(false, 'cancel') !== false) {
event('oncancel', [that.val]);
}
};
/**
* scroller initialization.
*/
that.init = function (ss) {
// get theme defaults
theme = extend({ defaults: {}, init: empty }, ms.themes[ss.theme || s.theme]);
// get language defaults
lang = ms.i18n[ss.lang || s.lang];
extend(settings, ss); // update original user settings
extend(s, theme.defaults, lang, settings);
that.settings = s;
// unbind all events (if re-init)
elm.unbind('.dw');
var preset = ms.presets[s.preset];
if (preset) {
pres = preset.call(e, that);
extend(s, pres, settings); // load preset settings
extend(methods, pres.methods); // extend core methods
}
// set private members
m = math.floor(s.rows / 2);
hi = s.height;
anim = s.animate;
if (elm.data('dwro') !== undefined) {
e.readonly = bool(elm.data('dwro'));
}
if (visible) {
that.hide();
}
if (s.display == 'inline') {
that.show();
} else {
read();
if (input && s.showonfocus) {
// set element readonly, save original state
elm.data('dwro', e.readonly);
e.readonly = true;
// init show datewheel
elm.bind('focus.dw', function () { that.show(); });
}
}
};
that.trigger = function (name, params) {
return event(name, params);
};
that.values = null;
that.val = null;
that.temp = null;
that._selectedvalues = {}; // [];
that.init(settings);
}
function testprops(props) {
var i;
for (i in props) {
if (mod[props[i]] !== undefined) {
return true;
}
}
return false;
}
function testprefix() {
var prefixes = ['webkit', 'moz', 'o', 'ms'],
p;
for (p in prefixes) {
if (testprops([prefixes[p] + 'transform'])) {
return '-' + prefixes[p].tolowercase();
}
}
return '';
}
function getinst(e) {
return scrollers[e.id];
}
function getcoord(e, c) {
var org = e.originalevent,
ct = e.changedtouches;
return ct || (org && org.changedtouches) ? (org ? org.changedtouches[0]['page' + c] : ct[0]['page' + c]) : e['page' + c];
}
function bool(v) {
return (v === true || v == 'true');
}
function constrain(val, min, max) {
val = val > max ? max : val;
val = val < min ? min : val;
return val;
}
function calc(t, val, dir, anim, orig) {
val = constrain(val, min, max);
var cell = $('.dw-li', t).eq(val),
o = orig === undefined ? val : orig,
idx = index,
time = anim ? (val == o ? 0.1 : math.abs((val - o) * 0.1)) : 0;
// set selected scroller value
inst.temp[idx] = cell.attr('data-val');
inst.scroll(t, idx, val, time, orig);
settimeout(function () {
// validate
inst.validate(idx, dir, time, orig);
}, 10);
}
function init(that, method, args) {
if (methods[method]) {
return methods[method].apply(that, array.prototype.slice.call(args, 1));
}
if (typeof method === 'object') {
return methods.init.call(that, method);
}
return that;
}
var scrollers = {},
timer,
empty = function () { },
h,
min,
max,
inst, // current instance
date = new date(),
uuid = date.gettime(),
move,
click,
target,
index,
start,
stop,
starttime,
pos,
moved,
scrollable,
mod = document.createelement('modernizr').style,
has3d = testprops(['perspectiveproperty', 'webkitperspective', 'mozperspective', 'operspective', 'msperspective']),
prefix = testprefix(),
extend = $.extend,
tap,
touch,
start_event = 'touchstart mousedown',
move_event = 'touchmove mousemove',
end_event = 'touchend mouseup',
onmove = function (e) {
if (scrollable) {
e.preventdefault();
stop = getcoord(e, 'y');
inst.scroll(target, index, constrain(pos + (start - stop) / h, min - 1, max + 1));
}
moved = true;
},
defaults = {
// options
width: 70,
height: 40,
rows: 3,
delay: 300,
disabled: false,
readonly: false,
showonfocus: true,
showlabel: true,
wheels: [],
theme: '',
headertext: '{value}',
display: 'modal',
mode: 'scroller',
preset: '',
lang: 'en-us',
settext: 'set',
canceltext: 'cancel',
scrolllock: true,
tap: true,
formatresult: function (d) {
return d.join(' ');
},
parsevalue: function (value, inst) {
var w = inst.settings.wheels,
val = value.split(' '),
ret = [],
j = 0,
i,
l,
v;
for (i = 0; i < w.length; i++) {
for (l in w[i]) {
if (w[i][l][val[j]] !== undefined) {
ret.push(val[j]);
} else {
for (v in w[i][l]) { // select first value from wheel
ret.push(v);
break;
}
}
j++;
}
}
return ret;
}
},
methods = {
init: function (options) {
if (options === undefined) {
options = {};
}
return this.each(function () {
if (!this.id) {
uuid += 1;
this.id = 'scoller' + uuid;
}
scrollers[this.id] = new scroller(this, options);
});
},
enable: function () {
return this.each(function () {
var inst = getinst(this);
if (inst) {
inst.enable();
}
});
},
disable: function () {
return this.each(function () {
var inst = getinst(this);
if (inst) {
inst.disable();
}
});
},
isdisabled: function () {
var inst = getinst(this[0]);
if (inst) {
return inst.settings.disabled;
}
},
isvisible: function () {
var inst = getinst(this[0]);
if (inst) {
return inst.isvisible();
}
},
option: function (option, value) {
return this.each(function () {
var inst = getinst(this);
if (inst) {
var obj = {};
if (typeof option === 'object') {
obj = option;
} else {
obj[option] = value;
}
inst.init(obj);
}
});
},
setvalue: function (d, fill, time, temp) {
return this.each(function () {
var inst = getinst(this);
if (inst) {
inst.temp = d;
inst.setvalue(true, fill, time, temp);
}
});
},
getinst: function () {
return getinst(this[0]);
},
getvalue: function () {
var inst = getinst(this[0]);
if (inst) {
return inst.values;
}
},
getvalues: function () {
var inst = getinst(this[0]);
if (inst) {
return inst.getvalues();
}
},
show: function () {
var inst = getinst(this[0]);
if (inst) {
return inst.show();
}
},
hide: function () {
return this.each(function () {
var inst = getinst(this);
if (inst) {
inst.hide();
}
});
},
destroy: function () {
return this.each(function () {
var inst = getinst(this);
if (inst) {
inst.hide();
$(this).unbind('.dw');
delete scrollers[this.id];
if ($(this).is('input')) {
this.readonly = bool($(this).data('dwro'));
}
}
});
}
};
$(document).bind(end_event, function (e) {
if (move) {
var time = new date() - starttime,
val = constrain(pos + (start - stop) / h, min - 1, max + 1),
speed,
dist,
tindex,
ttop = target.offset().top;
if (time < 300) {
speed = (stop - start) / time;
dist = (speed * speed) / (2 * 0.0006);
if (stop - start < 0) {
dist = -dist;
}
} else {
dist = stop - start;
}
tindex = math.round(pos - dist / h);
if (!dist && !moved) { // this is a "tap"
var idx = math.floor((stop - ttop) / h),
li = $('.dw-li', target).eq(idx),
hl = scrollable;
if (inst.trigger('onvaluetap', [li]) !== false) {
tindex = idx;
} else {
hl = true;
}
if (hl) {
li.addclass('dw-hl'); // highlight
settimeout(function () {
li.removeclass('dw-hl');
}, 200);
}
}
if (scrollable) {
calc(target, tindex, 0, true, math.round(val));
}
move = false;
target = null;
$(document).unbind(move_event, onmove);
}
if (click) {
clearinterval(timer);
click = false;
}
$('.dwb-a').removeclass('dwb-a');
}).bind('mouseover mouseup mousedown click', function (e) { // prevent standard behaviour on body click
if (tap) {
e.stoppropagation();
e.preventdefault();
return false;
}
});
$.fn.mobiscroll = function (method) {
extend(this, $.mobiscroll.shorts);
return init(this, method, arguments);
};
$.mobiscroll = $.mobiscroll || {
/**
* set settings for all instances.
* @param {object} o - new default settings.
*/
setdefaults: function (o) {
extend(defaults, o);
},
presetshort: function (name) {
this.shorts[name] = function (method) {
return init(this, extend(method, { preset: name }), arguments);
};
},
shorts: {},
presets: {},
themes: {},
i18n: {}
};
$.scroller = $.scroller || $.mobiscroll;
$.fn.scroller = $.fn.scroller || $.fn.mobiscroll;
})(jquery);