﻿jQuery.fn.combobox = function (o) { var g = { comboboxContainerClass: null, comboboxValueContainerClass: null, comboboxValueContentClass: null, comboboxDropDownButtonClass: null, comboboxDropDownClass: null, comboboxDropDownItemClass: null, comboboxDropDownItemHoverClass: null, comboboxDropDownGroupItemHeaderClass: null, comboboxDropDownGroupItemContainerClass: null, animationType: "slide", width: "120px" }; o && jQuery.extend(g, o); this.onChange = function () { }; return this.each(function () { function o(a, b) { b.each(function () { var b = jQuery(this), d = "", d = null, c = "", e = null, f = "", h = "option"; b.is("option") ? (c = b.text(), e = b.val(), g.comboboxDropDownItemClass ? (d = g.comboboxDropDownItemClass, f = v) : f = v + G, d = d ? String.format("<li style='{0}' class='{1}'>{2}</li>", f, d, c) : String.format("<li style='{0}'>{1}</li>", f, c)) : (c = b.attr("label"), e = b.attr("class"), h = "optgroup", g.comboboxDropDownGroupItemHeaderClass ? (d = g.comboboxDropDownGroupItemHeaderClass, f = x) : f = x + H, d = d ? String.format("<li><span style='{0}' class='{1}'>{2}</span></li>", f, d, c) : String.format("<li><span style='{0}'>{1}</span></li>", f, c)); d = jQuery(d); d.css("display", "inline"); d[0].dataValue = e; d[0].dataType = h; d[0].title = c; a.append(d); c = d.offset().left; c -= y.left; c < 0 && (c = 0); c += d.outerWidth(); c > p && (p = c); z(d, v); b.is("optgroup") && (c = "", c = g.comboboxDropDownGroupItemContainerClass ? String.format("<ul style='{0}' class='{1}'></ul>", A, g.comboboxDropDownGroupItemContainerClass) : String.format("<ul style='{0}'></ul>", A + I), c = jQuery(c), d.append(c), o(c, b.children())) }) } function B() { var a = f.children(); q = i = null; if (c) c.empty(); else { var b = "", b = g.comboboxDropDownClass ? String.format("<ul class='{0}' style='{1}'></ul>", g.comboboxDropDownClass, C) : String.format("<ul style='{0}'></ul>", C + J); c = jQuery(b); c.appendTo(e); c.attr("tabIndex", 0) } a.length > 0 && (p = 0, y = c.offset(), o(c, a)) } function z(a, b) { var c = b.split(";"); if (c.length > 0) for (var d = 0; d < c.length; d++) { var e = c[d].split(":"); a.css(e[0], e[1]) } } function w(a) { if (g.comboboxDropDownButtonClass) { var b = j.width(), a = String.format("-{0}px 0px", a * b); j.css("background-position", a) } else b = K, a == 1 && (b = L), z(j, b) } function D() { var a = e.outerWidth(); a < p && (a = p); c.width(a) } function E() { jQuery("*", c).not("ul").not("span").not("[@dataType='optgroup']").each(function () { var a = jQuery(this); a.click(function (b) { b.stopPropagation(); f.val(a[0].dataValue); r(); k(!1) }); a.mouseover(function () { m(i, !1); m(a, !0) }); a.mouseout(function () { m(a, !1) }) }) } function M() { c.blur(function (a) { a.stopPropagation(); c.is(":visible") && k(!1) }) } function F() { e.click(function () { c.is(":hidden") ? k(!0) : k(!1) }) } function r() { var a = !1, b = f[0]; if (b.length > 0) { b = b[b.selectedIndex].text; l.text(b); l.attr("title", b); b = l.outerHeight(); b = (h.height() - b) / 2; b < 0 && (b = 0); l.css("top", b); q && q != f.val() && (a = !0); q = f.val(); if (a && f.combobox.onChange) f.combobox.onChange(); i && m(i, !1); i = jQuery("li[@dataValue='" + q + "']", c); m(i, !0) } } function m(a, b) { a && (g.comboboxDropDownItemHoverClass ? b ? a.addClass(g.comboboxDropDownItemHoverClass) : a.removeClass(g.comboboxDropDownItemHoverClass) : b ? (a.css("background", "#000"), a.css("color", "#fff")) : (a.css("background", ""), a.css("color", ""))) } function s() { c.focus() } function t() { e.focus(); F() } function k(a) { if (a) { if (c.is(":hidden")) { e.unbind("click"); m(i, !0); w(1); var a = e.position().top, b = c.outerHeight(), f = a + e.outerHeight(), d = jQuery(window).scrollTop(), h = jQuery(window).height() - (f - d); u = !1; h < b && a - d > b && (f = a - b, u = !0); a = f; c.css("top", a); c.css("left", e.offset().left); switch (g.animationType) { case "slide": u ? (b = e.position().top, d = e.outerHeight(), c.css("top", b - d), c.animate({ height: "toggle", top: a }, "fast", s)) : c.slideDown("fast", s); break; case "fade": c.fadeIn("fast", s); break; default: c.show(), s() } } } else if (c.is(":visible")) switch (w(0), g.animationType) { case "slide": u ? (b = e.position().top, dropdownListHeight = c.height(), a = b - e.outerHeight(), c.animate({ height: "toggle", top: a }, "fast", t)) : c.slideUp("fast", t); break; case "fade": c.fadeOut("fast", t); break; default: c.hide(), t() } } function n(a) { var b = f[0], c = b.selectedIndex, d = -1, e = b.length - 1; switch (a) { case ":next": d = c + 1; d > e && (d = e); break; case ":previous": d = c - 1; d < 0 && (d = 0); break; case ":first": d = 0; break; case ":last": d = e } b.selectedIndex = d; r() } var f = jQuery(this), e = null, c = null, C = "list-style-type:none;min-height:15px;padding-top:0;margin:0;", J = "cursor:default;padding:2px;background:#fff;border-right:solid 1px #000;border-bottom:solid 1px #000;border-left:solid 1px #aaa;border-top:solid 1px #aaa;overflow:auto", v = "display:block;", G = "cursor:default;padding-left:2px;font-weight:normal;font-style:normal;", A = "list-style-type:none;", I = "padding-left:10px;margin-left:0;", x = "", H = "font-style:italic;font-weight:bold;", h = null, l = null, j = null, K = "padding-left:0px;padding-top:1px;width:12px;height:13px;border-right:solid 2px #404040;border-bottom:solid 2px #404040;border-left:solid 2px #f0f0f0;border-top:solid 2px #f0f0f0", L = "padding-left:1px;padding-top:3px;width:12px;height:13px;border:solid 1px #808080", i = null, q = null, u = !1, p = 0, y = null; String.format = function () { var a = null; if (arguments.length != 0) for (var a = arguments[0], b = 1; b < arguments.length; b++) a = a.replace(RegExp("\\{" + (b - 1) + "\\}", "gm"), arguments[b]); return a }; f.combobox.updateSelection = function () { r() }; f.combobox.update = function () { B(); D(); E(); r() }; (function () { var a = "", a = g.comboboxContainerClass ? String.format("<div class='{0}' style='{1}'></div>", g.comboboxContainerClass, "padding:0;") : String.format("<div style='{0}' style='{1}'></div>", "border-left: solid 2px #777;border-top: solid 2px #777;border-right: solid 1px #ccc;border-bottom: solid 1px #ccc;", "padding:0;"); e = jQuery(a); f.before(e); e.append(f); f.hide(); e.attr("tabIndex", 0) })(); (function () { var a = "", a = g.comboboxValueContainerClass ? String.format("<div class='{0}' style='{1}'></div>", g.comboboxValueContainerClass, "position:relative;overflow:hidden;") : String.format("<div style='{0}'></div>", "position:relative;overflow:hidden;"), b = "", b = g.comboboxValueContentClass ? String.format("<div class='{0}' style='{1}'></div>", g.comboboxValueContentClass, "float:left;position:absolute;cursor:default;overflow:hidden;") : String.format("<div style='{0}'></div>", "float:left;position:absolute;cursor:default;overflow:hidden;"), c = "", c = g.comboboxDropDownButtonClass ? String.format("<div class='{1}' style='{0}'></div>", "background-repeat:no-repeat;float:right;", g.comboboxDropDownButtonClass) : String.format("<div style='{0}'>{1}</div>", "background-repeat:no-repeat;float:right;overflow:hidden;width: 16px;height: 18px;color:#000;background: #D6D3CE;,font-family: verdana;font-size: 10px;cursor: default;text-align: center;vertical-align:middle;", "&#9660;"); l = jQuery(b); j = jQuery(c); h = jQuery(a); h.appendTo(e); l.appendTo(h); j.appendTo(h); w(0) })(); B(); (function () { e.width(g.width); var a = e.width(), b = h, f = b.outerWidth() - b.width(); b.width(a - f); a = h.width() - j.outerWidth(); b = l; f = b.outerWidth() - b.width(); b.width(a - f); a = j.outerHeight(); b = h; f = b.outerHeight() - b.height(); b.height(a - f); c.css("position", "absolute"); c.css("z-index", "20000"); D(); a = c.offset().left - (e.outerWidth() - e.width()); c.css("left", a + 1); c.hide() })(); (function () { e.keydown(function (a) { a.preventDefault(); switch (a.which) { case 33: case 36: n(":first"); break; case 34: case 35: n(":last"); break; case 37: n(":previous"); break; case 38: a.altKey ? k(!c.is(":visible")) : n(":previous"); break; case 39: n(":next"); break; case 40: a.altKey ? k(!c.is(":visible")) : n(":next"); break; case 27: case 13: k(!1); break; case 9: c.blur(), $(window)[0].focus() } a.cancelBubble = !0 }); F(); M(); E() })(); r() }) };
