jquery.scroller-1.0.2.min.js
17.6 KB
(function (b) { function t(D, y, z) { var B = this; var D = D; var y = y; var C; var m; var A; var h = false; this.settings = z; this.values = null; this.val = null; this.temp = null; this.setDefaults = function (E) { b.extend(f, E) }; this.formatDate = function (N, F, G) { if (!F) { return null } var O = b.extend({}, this.settings, G); var L = function (P) { var Q = 0; while (J + 1 < N.length && N.charAt(J + 1) == P) { Q++; J++ } return Q }; var I = function (Q, R, P) { var S = "" + R; if (L(Q)) { while (S.length < P) { S = "0" + S } } return S }; var H = function (P, S, R, Q) { return (L(P) ? Q[S] : R[S]) }; var E = ""; var M = false; for (var J = 0; J < N.length; J++) { if (M) { if (N.charAt(J) == "'" && !L("'")) { M = false } else { E += N.charAt(J) } } else { switch (N.charAt(J)) { case "d": E += I("d", F.getDate(), 2); break; case "D": E += H("D", F.getDay(), O.dayNamesShort, O.dayNames); break; case "o": E += I("o", (F.getTime() - new Date(F.getFullYear(), 0, 0).getTime()) / 86400000, 3); break; case "m": E += I("m", F.getMonth() + 1, 2); break; case "M": E += H("M", F.getMonth(), O.monthNamesShort, O.monthNames); break; case "y": E += (L("y") ? F.getFullYear() : (F.getYear() % 100 < 10 ? "0" : "") + F.getYear() % 100); break; case "h": var K = F.getHours(); E += I("h", (K > 12 ? (K - 12) : (K == 0 ? 12 : K)), 2); break; case "H": E += I("H", F.getHours(), 2); break; case "i": E += I("i", F.getMinutes(), 2); break; case "s": E += I("s", F.getSeconds(), 2); break; case "a": E += F.getHours() > 11 ? "pm" : "am"; break; case "A": E += F.getHours() > 11 ? "PM" : "AM"; break; case "'": if (L("'")) { E += "'" } else { M = true } break; default: E += N.charAt(J) } } } return E }; this.parseDate = function (U, N, W) { var I = new Date(); if (!U || !N) { return I } N = (typeof N == "object" ? N.toString() : N + ""); var K = b.extend({}, this.settings, W); var F = I.getFullYear(); var Y = I.getMonth(); var S = I.getDate(); var H = -1; var V = I.getHours(); var O = I.getMinutes(); var G = I.getSeconds(); var L = 0; var R = false; var M = function (aa) { var ab = (E + 1 < U.length && U.charAt(E + 1) == aa); if (ab) { E++ } return ab }; var Z = function (ab) { M(ab); var ac = (ab == "@" ? 14 : (ab == "!" ? 20 : (ab == "y" ? 4 : (ab == "o" ? 3 : 2)))); var ad = new RegExp("^\\d{1," + ac + "}"); var aa = N.substr(T).match(ad); if (!aa) { throw "Missing number at position " + T } T += aa[0].length; return parseInt(aa[0], 10) }; var J = function (ab, ad, aa) { var ae = (M(ab) ? aa : ad); for (var ac = 0; ac < ae.length; ac++) { if (N.substr(T, ae[ac].length).toLowerCase() == ae[ac].toLowerCase()) { T += ae[ac].length; return ac + 1 } } throw "Unknown name at position " + T }; var Q = function () { if (N.charAt(T) != U.charAt(E)) { throw "Unexpected literal at position " + T } T++ }; var T = 0; for (var E = 0; E < U.length; E++) { if (R) { if (U.charAt(E) == "'" && !M("'")) { R = false } else { Q() } } else { switch (U.charAt(E)) { case "d": S = Z("d"); break; case "D": J("D", K.dayNamesShort, K.dayNames); break; case "o": H = Z("o"); break; case "m": Y = Z("m"); break; case "M": Y = J("M", K.monthNamesShort, K.monthNames); break; case "y": F = Z("y"); break; case "H": V = Z("H"); break; case "h": V = Z("h"); break; case "i": O = Z("i"); break; case "s": G = Z("s"); break; case "a": L = J("a", ["am", "pm"], ["am", "pm"]) - 1; break; case "A": L = J("A", ["am", "pm"], ["am", "pm"]) - 1; break; case "'": if (M("'")) { Q() } else { R = true } break; default: Q() } } } if (F < 100) { F += new Date().getFullYear() - new Date().getFullYear() % 100 + (F <= K.shortYearCutoff ? 0 : -100) } if (H > -1) { Y = 1; S = H; do { var P = 32 - new Date(F, Y - 1, 32).getDate(); if (S <= P) { break } Y++; S -= P } while (true) } if (L && V < 12) { V += 12 } var X = new Date(F, Y - 1, S, V, O, G); if (X.getFullYear() != F || X.getMonth() + 1 != Y || X.getDate() != S) { throw "Invalid date" } return X }; this.setValue = function (F) { if (F == undefined) { F = true } var E = this.formatResult(); this.val = E; this.values = this.temp.slice(0); if (F && b(D).is("input")) { b(D).val(E).change() } }; this.getDate = function () { var G = this.values; var F = this.settings; if (F.preset == "date") { return new Date(G[C], G[m], G[A]) } if (F.preset == "time") { var E = (F.ampm && G[F.seconds ? 3 : 2] == "PM" && (G[0] - 0) < 12) ? (G[0] - 0 + 12) : G[0]; return new Date(1970, 0, 1, E, G[1], F.seconds ? G[2] : null) } if (F.preset == "datetime") { var E = (F.ampm && G[F.seconds ? 6 : 5] == "PM" && (G[3] - 0) < 12) ? (G[3] - 0 + 12) : G[3]; return new Date(G[C], G[m], G[A], E, G[4], F.seconds ? G[5] : null) } }; this.setDate = function (H, F) { var G = this.settings; if (G.preset.match(/date/i)) { this.temp[C] = H.getFullYear(); this.temp[m] = H.getMonth(); this.temp[A] = H.getDate() } if (G.preset == "time") { var E = H.getHours(); this.temp[0] = (G.ampm) ? (E > 12 ? (E - 12) : (E == 0 ? 12 : E)) : E; this.temp[1] = H.getMinutes(); if (G.seconds) { this.temp[2] = H.getSeconds() } if (G.ampm) { this.temp[G.seconds ? 3 : 2] = E > 11 ? "PM" : "AM" } } if (G.preset == "datetime") { var E = H.getHours(); this.temp[3] = (G.ampm) ? (E > 12 ? (E - 12) : (E == 0 ? 12 : E)) : E; this.temp[4] = H.getMinutes(); if (G.seconds) { this.temp[2] = H.getSeconds() } if (G.ampm) { this.temp[G.seconds ? 6 : 5] = E > 11 ? "PM" : "AM" } } this.setValue(F) }; this.parseValue = function (J) { var G = this.settings; if (this.preset) { var E = []; if (G.preset == "date") { try { var I = this.parseDate(G.dateFormat, J, G) } catch (H) { var I = new Date() } E[C] = I.getFullYear(); E[m] = I.getMonth(); E[A] = I.getDate() } else { if (G.preset == "time") { try { var I = this.parseDate(G.timeFormat, J, G) } catch (H) { var I = new Date() } var F = I.getHours(); E[0] = (G.ampm) ? (F > 12 ? (F - 12) : (F == 0 ? 12 : F)) : F; E[1] = I.getMinutes(); if (G.seconds) { E[2] = I.getSeconds() } if (G.ampm) { E[G.seconds ? 3 : 2] = F > 11 ? "PM" : "AM" } } else { if (G.preset == "datetime") { try { var I = this.parseDate(G.dateFormat + " " + G.timeFormat, J, G) } catch (H) { var I = new Date() } var F = I.getHours(); E[C] = I.getFullYear(); E[m] = I.getMonth(); E[A] = I.getDate(); E[3] = (G.ampm) ? (F > 12 ? (F - 12) : (F == 0 ? 12 : F)) : F; E[4] = I.getMinutes(); if (G.seconds) { E[5] = I.getSeconds() } if (G.ampm) { E[G.seconds ? 6 : 5] = F > 11 ? "PM" : "AM" } } } } return E } return G.parseValue(J) }; this.formatResult = function () { var F = this.settings; var G = this.temp; if (this.preset) { if (F.preset == "date") { return this.formatDate(F.dateFormat, new Date(G[C], G[m], G[A]), F) } else { if (F.preset == "datetime") { var E = (F.ampm) ? ((G[F.seconds ? 6 : 5] == "PM" && (G[3] - 0) < 12) ? (G[3] - 0 + 12) : (G[F.seconds ? 6 : 5] == "AM" && (G[3] == 12) ? 0 : G[3])) : G[3]; return this.formatDate(F.dateFormat + " " + F.timeFormat, new Date(G[C], G[m], G[A], E, G[4], F.seconds ? G[5] : null), F) } else { if (F.preset == "time") { var E = (F.ampm) ? ((G[F.seconds ? 3 : 2] == "PM" && (G[0] - 0) < 12) ? (G[0] - 0 + 12) : (G[F.seconds ? 3 : 2] == "AM" && (G[0] == 12) ? 0 : G[0])) : G[0]; return this.formatDate(F.timeFormat, new Date(1970, 0, 1, E, G[1], F.seconds ? G[2] : null), F) } } } } return F.formatResult(G) }; this.validate = function (F) { var G = this.settings; if (this.preset && G.preset.match(/date/i) && ((F == C) || (F == m))) { var H = 32 - new Date(this.temp[C], this.temp[m], 32).getDate() - 1; var E = b("ul:eq(" + A + ")", y); b("li", E).show(); b("li:gt(" + H + ")", E).hide(); if (this.temp[A] > H) { if (G.fx) { E.animate({ top: (s * (o - H - 1)) + "px" }, "fast") } else { E.css({ top: (s * (o - H - 1)) }) } this.temp[A] = b("li:eq(" + H + ")", E).data("val") } } else { u.validate(F) } }; this.hide = function () { this.settings.onClose(this.val, this); b(":input:not(.dwtd)").attr("disabled", false).removeClass("dwtd"); b(D).blur(); y.hide(); i.hide(); h = false; if (this.preset) { this.settings.wheels = null } b(window).unbind("resize.dw") }; this.show = function () { var L = this.settings; L.beforeShow(D, this); s = L.height; o = Math.round(L.rows / 2); a = this; this.init(); if (this.preset) { L.wheels = new Array(); if (L.preset.match(/date/i)) { var E = {}; for (var G = 0; G < 3; G++) { if (G == C) { E[L.yearText] = {}; for (var J = L.startYear; J <= L.endYear; J++) { E[L.yearText][J] = L.dateOrder.search(/yy/i) < 0 ? J.toString().substr(2, 2) : J.toString() } } else { if (G == m) { E[L.monthText] = {}; for (var J = 0; J < 12; J++) { E[L.monthText][J] = (L.dateOrder.search(/MM/) < 0 ? (L.dateOrder.search(/M/) < 0 ? (L.dateOrder.search(/mm/) < 0 ? (J + 1) : (J < 9) ? ("0" + (J + 1)) : (J + 1)) : L.monthNamesShort[J]) : L.monthNames[J]) } } else { if (G == A) { E[L.dayText] = {}; for (var J = 1; J < 32; J++) { E[L.dayText][J] = L.dateOrder.search(/dd/i) < 0 ? J : (J < 10) ? ("0" + J) : J } } } } } L.wheels.push(E) } if (L.preset.match(/time/i)) { L.stepHour = (L.stepHour < 1) ? 1 : parseInt(L.stepHour); L.stepMinute = (L.stepMinute < 1) ? 1 : parseInt(L.stepMinute); L.stepSecond = (L.stepSecond < 1) ? 1 : parseInt(L.stepSecond); var E = {}; E[L.hourText] = {}; for (var J = 0; J < (L.ampm ? 13 : 24); J += L.stepHour) { E[L.hourText][J] = (J < 10) ? ("0" + J) : J } E[L.minuteText] = {}; for (var J = 0; J < 60; J += L.stepMinute) { E[L.minuteText][J] = (J < 10) ? ("0" + J) : J } if (L.seconds) { E[L.secText] = {}; for (var J = 0; J < 60; J += L.stepSecond) { E[L.secText][J] = (J < 10) ? ("0" + J) : J } } if (L.ampm) { E[L.ampmText] = {}; E[L.ampmText]["AM"] = "AM"; E[L.ampmText]["PM"] = "PM" } L.wheels.push(E) } } b(".dwc", y).remove(); for (var J = 0; J < L.wheels.length; J++) { var F = b('<div class="dwc"><div class="dwwc"><div class="clear" style="clear:both;"></div></div>').insertBefore(b(".dwbc", y)); for (var I in L.wheels[J]) { var K = b(".dwwc .clear", F); var E = b('<div class="dwwl"><div class="dwl">' + I + '</div><div class="dww"><ul></ul><div class="dwwo"></div></div><div class="dwwol"></div></div>').insertBefore(K); for (var H in L.wheels[J][I]) { b('<li class="val_' + H + '">' + L.wheels[J][I][H] + "</li>").data("val", H).appendTo(b("ul", E)) } } } b(".dww ul", y).each(function (N) { var M = b("li", this).index(b("li.val_" + B.temp[N], this)); while ((M < 0) && (--B.temp[N] >= 0)) { M = b("li", this).index(b("li.val_" + B.temp[N], this)) } var O = s * (o - (M < 0 ? 0 : M) - 1); b(this).css("top", O) }); b(".dwv", y).html(this.formatResult()); b("#dw_set", y).text(L.setText).unbind().bind("click tap", function (M) { B.setValue(); L.onSelect(B.val, a); B.hide(); k = true; setTimeout(function () { k = false }, 300); return false }); b("#dw_cancel", y).text(L.cancelText).unbind().bind("click tap", function (M) { B.hide(); k = true; setTimeout(function () { k = false }, 300); return false }); b(":input:disabled").addClass("dwtd"); b(":input").attr("disabled", true); i.show(); y.attr("class", "dw " + L.theme).show(); h = true; b(".dww, .dwwl", y).height(L.rows * s); b(".dww", y).each(function () { b(this).width(b(this).parent().width() < L.width ? L.width : b(this).parent().width()) }); b(".dwbc a", y).attr("class", L.btnClass); b(".dww li", y).css({ height: s, lineHeight: s + "px" }); b(".dwwc", y).each(function () { var M = 0; b(".dwwl", this).each(function () { M += b(this).outerWidth(true) }); b(this).width(M) }); b(".dwc", y).each(function () { b(this).width(b(".dwwc", this).outerWidth(true)) }); this.pos(); b(window).bind("resize.dw", function () { B.pos() }) }; this.pos = function () { var E = 0; var H = 0; var K = b(window).width(); var G = b(window).height(); var I = b(window).scrollTop(); var F; var J; b(".dwc", y).each(function () { F = b(this).outerWidth(true); E += F; H = (F > H) ? F : H }); F = E > K ? H : E; y.width(F); F = y.outerWidth(); J = y.outerHeight(); y.css({ left: (K - F) / 2, top: I + (G - J) / 2 }); i.height(0); i.height(b(document).height()) }; this.init = function () { var G = this.settings; var E = G.dateOrder.search(/y/i); var F = G.dateOrder.search(/m/i); var H = G.dateOrder.search(/d/i); C = (E < F) ? (E < H ? 0 : 1) : (E < H ? 1 : 2); m = (F < E) ? (F < H ? 0 : 1) : (F < H ? 1 : 2); A = (H < E) ? (H < F ? 0 : 1) : (H < F ? 1 : 2); this.preset = (G.wheels === null); if (this.values !== null) { this.temp = this.values.slice(0) } else { this.temp = this.parseValue(b(D).val() ? b(D).val() : ""); this.setValue(false) } }; this.init(); b(D).is("input") ? b(D).attr("readonly", "readonly").data("readonly", b(D).attr("readonly")) : false; b(D).addClass("scroller").unbind("focus.dw").bind("focus.dw", function (E) { if (k) { b(D).blur(); setTimeout(function () { b(D).removeClass("ui-focus") }, 50); return false } else { if (!B.settings.disabled && B.settings.showOnFocus && !h) { B.show() } } }) } var j; var i; var s; var o; var a; var g = {}; var w = new Date(); var r = w.getTime(); var n = false; var x = null; var c; var p; var d; var e = ("ontouchstart" in window); var q = e ? "touchstart" : "mousedown"; var v = e ? "touchmove" : "mousemove"; var l = e ? "touchend" : "mouseup"; var k = false; var f = { width: 80, height: 40, rows: 3, fx: false, disabled: false, showOnFocus: true, showOnTap: true, wheels: null, theme: "", preset: "date", dateFormat: "yy-mm-dd", dateOrder: "yymmdd", ampm: true, seconds: false, timeFormat: "hh:ii A", startYear: w.getFullYear() - 100, endYear: w.getFullYear() + 10, monthNames: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"], monthNamesShort: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"], dayNames: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"], dayNamesShort: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"], shortYearCutoff: "+10", monthText: "月", dayText: "日", yearText: "年", hourText: "Hours", minuteText: "Minutes", secText: "Seconds", ampmText: " ", setText: "确定", cancelText: "取消", btnClass: "dwb", stepHour: 1, stepMinute: 1, stepSecond: 1, beforeShow: function () { }, onClose: function () { }, onSelect: function () { }, formatResult: function (y) { var h = ""; for (var m = 0; m < y.length; m++) { h += (m > 0 ? " " : "") + y[m] } return h }, parseValue: function (h) { return h.split(" ") } }; var u = { init: function (m) { var y = b.extend({}, f, m); if (b(".dwo").length) { i = b(".dwo"); j = b(".dw") } else { i = b('<div class="dwo"></div>').hide().appendTo("body"); j = b('<div class="dw"><div class="dwv"> </div><div class="dwbc" style="clear:both;"><a id="dw_set" href="#"></a><a id="dw_cancel" href="#"></a></div></div>'); j.hide().appendTo("body"); b(document).bind(v, function (z) { if (n) { p = e ? z.originalEvent.changedTouches[0].pageY : z.pageY; x.css("top", (d + p - c) + "px"); z.preventDefault(); z.stopPropagation(); return false } }); function h(A, B) { B = B > ((o - 1) * s) ? ((o - 1) * s) : B; B = B < (o * s - b("li:visible", A).length * s) ? (o * s - b("li:visible", A).length * s) : B; if (a.settings.fx) { A.stop(true, true).animate({ top: B + "px" }, "fast") } else { A.css("top", B) } var z = b("ul", j).index(A); a.temp[z] = b("li:eq(" + (o - 1 - B / s) + ")", A).data("val"); a.validate(z); b(".dwv", j).html(a.formatResult()) } b(document).bind(l, function (z) { if (n) { var A = Math.round((d + p - c) / s) * s; A = A > ((o - 1) * s) ? ((o - 1) * s) : A; A = A < (o * s - b("li:visible", x).length * s) ? (o * s - b("li:visible", x).length * s) : A; h(x, A); n = false; x = null; z.preventDefault(); z.stopPropagation() } }); b(".dwwl").live("DOMMouseScroll mousewheel", function (B) { var D = 0; if (B.wheelDelta) { D = B.wheelDelta / 120 } if (B.detail) { D = -B.detail / 3 } var z = b("ul", this); var A = z.css("top").replace(/px/i, "") - 0; var C = Math.round((A + D * s) / s) * s; h(z, C); B.preventDefault(); B.stopPropagation() }); b(".dwwl").live(q, function (B) { if (!n) { var A = e ? B.originalEvent.changedTouches[0].pageX : B.pageX; var z = b(this).offset().left; n = true; x = b("ul", this); d = x.css("top").replace(/px/i, "") - 0; c = e ? B.originalEvent.changedTouches[0].pageY : B.pageY; p = c; B.preventDefault(); B.stopPropagation() } }) } return this.each(function () { if (!this.id) { r += 1; this.id = "scoller" + r } g[this.id] = new t(this, j, y) }) }, validate: function () { }, enable: function () { return this.each(function () { if (g[this.id]) { g[this.id].settings.disabled = false } }) }, disable: function () { return this.each(function () { if (g[this.id]) { g[this.id].settings.disabled = true } }) }, isDisabled: function () { if (g[this[0].id]) { return g[this[0].id].settings.disabled } }, option: function (h, m) { return this.each(function () { if (g[this.id]) { if (typeof h === "object") { b.extend(g[this.id].settings, h) } else { g[this.id].settings[h] = m } g[this.id].init() } }) }, setValue: function (m, h) { if (h == undefined) { h = false } return this.each(function () { if (g[this.id]) { g[this.id].temp = m; g[this.id].setValue(m, h) } }) }, getValue: function () { if (g[this[0].id]) { return g[this[0].id].values } }, setDate: function (m, h) { if (h == undefined) { h = false } return this.each(function () { if (g[this.id]) { g[this.id].setDate(m, h) } }) }, getDate: function () { if (g[this[0].id]) { return g[this[0].id].getDate() } }, show: function () { if (g[this[0].id]) { return g[this[0].id].show() } }, hide: function () { return this.each(function () { if (g[this.id]) { g[this.id].hide() } }) }, destroy: function () { return this.each(function () { if (g[this.id]) { b(this).unbind("focus.dw").removeClass("scroller"); b(this).is("input") ? b(this).attr("readonly", b(this).data("readonly")) : false; delete g[this.id] } }) } }; b.fn.scroller = function (h) { if (u[h]) { return u[h].apply(this, Array.prototype.slice.call(arguments, 1)) } else { if (typeof h === "object" || !h) { return u.init.apply(this, arguments) } else { b.error("Unknown method") } } }; b.scroller = new t(null, null, f) })(jQuery);