(function (a) { a.widget("ui.selectmenu", { _init: function () { var b = this, c = this.options; this.ids = [this.element.attr("id") + "-" + "button", this.element.attr("id") + "-" + "menu"]; this._safemouseup = true; this.newelement = a('<a class="' + this.widgetBaseClass + ' ui-widget ui-state-default ui-corner-all" id="' + this.ids[0] + '" role="button" href="#" aria-haspopup="true" aria-owns="' + this.ids[1] + '"></a>').insertAfter(this.element); var d = this.element.attr("tabindex"); if (d) { this.newelement.attr("tabindex", d) } this.newelement.data("selectelement", this.element); this.selectmenuIcon = a('<span class="' + this.widgetBaseClass + '-icon ui-icon"></span>').prependTo(this.newelement).addClass(c.style == "popup" ? "ui-icon-triangle-2-n-s" : "ui-icon-triangle-1-s"); a("label[for=" + this.element.attr("id") + "]").attr("for", this.ids[0]).bind("click", function () { b.newelement[0].focus(); return false }); this.newelement.bind("mousedown", function (a) { b._toggle(a); if (c.style == "popup") { b._safemouseup = false; setTimeout(function () { b._safemouseup = true }, 300) } return false }).bind("click", function () { return false }).keydown(function (c) { var d = true; switch (c.keyCode) { case a.ui.keyCode.ENTER: d = true; break; case a.ui.keyCode.SPACE: d = false; b._toggle(c); break; case a.ui.keyCode.UP: case a.ui.keyCode.LEFT: d = false; b._moveSelection(-1); break; case a.ui.keyCode.DOWN: case a.ui.keyCode.RIGHT: d = false; b._moveSelection(1); break; case a.ui.keyCode.TAB: d = true; break; default: d = false; b._typeAhead(c.keyCode, "mouseup"); break } return d }).bind("mouseover focus", function () { a(this).addClass(b.widgetBaseClass + "-focus ui-state-hover") }).bind("mouseout blur", function () { a(this).removeClass(b.widgetBaseClass + "-focus ui-state-hover") }); a(document).mousedown(function (a) { b.close(a) }); this.element.click(function () { this._refreshValue() }).focus(function () { this.newelement[0].focus() }); var e = c.style == "dropdown" ? " ui-corner-bottom" : " ui-corner-all"; this.list = a('<ul class="' + b.widgetBaseClass + "-menu ui-widget ui-widget-content" + e + '" aria-hidden="true" role="listbox" aria-labelledby="' + this.ids[0] + '" id="' + this.ids[1] + '"></ul>').appendTo("body"); var f = []; this.element.find("option").each(function () { f.push({ value: a(this).attr("value"), text: b._formatText(jQuery(this).text()), selected: a(this).attr("selected"), classes: a(this).attr("class"), parentOptGroup: a(this).parent("optgroup").attr("label") }) }); var g = b.options.style == "popup" ? " ui-state-active" : ""; for (var h in f) { if (f[h].text != null) var i = a('<li role="presentation"><a href="#" tabindex="-1" role="option" aria-selected="false">' + f[h].text + "</a></li>").data("index", h).addClass(f[h].classes).data("optionClasses", f[h].classes || "").mouseup(function (c) { if (b._safemouseup) { var d = a(this).data("index") != b._selectedIndex(); b.value(a(this).data("index")); b.select(c); if (d) { b.change(c) } b.close(c, true) } return false }).click(function () { return false }).bind("mouseover focus", function () { b._selectedOptionLi().addClass(g); b._focusedOptionLi().removeClass(b.widgetBaseClass + "-item-focus ui-state-hover"); a(this).removeClass("ui-state-active").addClass(b.widgetBaseClass + "-item-focus ui-state-hover") }).bind("mouseout blur", function () { if (a(this).is(b._selectedOptionLi())) { a(this).addClass(g) } a(this).removeClass(b.widgetBaseClass + "-item-focus ui-state-hover") }); if (f[h].parentOptGroup) { var j = b.widgetBaseClass + "-group-" + f[h].parentOptGroup; if (this.list.find("li." + j).size()) { this.list.find("li." + j + ":last ul").append(i) } else { a('<li role="presentation" class="' + b.widgetBaseClass + "-group " + j + '"><span class="' + b.widgetBaseClass + '-group-label">' + f[h].parentOptGroup + "</span><ul></ul></li>").appendTo(this.list).find("ul").append(i) } } else { i.appendTo(this.list) } this.list.bind("mousedown mouseup", function () { return false }); if (c.icons) { for (var k in c.icons) { if (i.is(c.icons[k].find)) { i.data("optionClasses", f[h].classes + " " + b.widgetBaseClass + "-hasIcon").addClass(b.widgetBaseClass + "-hasIcon"); var l = c.icons[k].icon || ""; i.find("a:eq(0)").prepend('<span class="' + b.widgetBaseClass + "-item-icon ui-icon " + l + '"></span>') } } } } this.list.find("li:last").addClass("ui-corner-bottom"); if (c.style == "popup") { this.list.find("li:first").addClass("ui-corner-top") } if (c.transferClasses) { var m = this.element.attr("class") || ""; this.newelement.add(this.list).addClass(m) } var n = this.element.width(); this.newelement.width(c.width ? c.width : n); if (c.style == "dropdown") { this.list.width(c.menuWidth ? c.menuWidth : c.width ? c.width : n) } else { this.list.width(c.menuWidth ? c.menuWidth : c.width ? c.width - c.handleWidth : n - c.handleWidth) } if (c.maxHeight && c.maxHeight < this.list.height()) { this.list.height(c.maxHeight) } this._optionLis = this.list.find("li:not(." + b.widgetBaseClass + "-group)"); this.list.keydown(function (c) { var d = true; switch (c.keyCode) { case a.ui.keyCode.UP: case a.ui.keyCode.LEFT: d = false; b._moveFocus(-1); break; case a.ui.keyCode.DOWN: case a.ui.keyCode.RIGHT: d = false; b._moveFocus(1); break; case a.ui.keyCode.HOME: d = false; b._moveFocus(":first"); break; case a.ui.keyCode.PAGE_UP: d = false; b._scrollPage("up"); break; case a.ui.keyCode.PAGE_DOWN: d = false; b._scrollPage("down"); break; case a.ui.keyCode.END: d = false; b._moveFocus(":last"); break; case a.ui.keyCode.ENTER: case a.ui.keyCode.SPACE: d = false; b.close(c, true); a(c.target).parents("li:eq(0)").trigger("mouseup"); break; case a.ui.keyCode.TAB: d = true; b.close(c, true); break; case a.ui.keyCode.ESCAPE: d = false; b.close(c, true); break; default: d = false; b._typeAhead(c.keyCode, "focus"); break } return d }); if (c.style == "dropdown") { this.newelement.addClass(b.widgetBaseClass + "-dropdown"); this.list.addClass(b.widgetBaseClass + "-menu-dropdown") } else { this.newelement.addClass(b.widgetBaseClass + "-popup"); this.list.addClass(b.widgetBaseClass + "-menu-popup") } this.newelement.prepend('<span class="' + b.widgetBaseClass + '-status">' + f[this._selectedIndex()].text + "</span>"); this.element.hide(); if (this.element.attr("disabled") == true) { this.disable() } this.value(this._selectedIndex()) }, destroy: function () { this.element.removeData(this.widgetName).removeClass(this.widgetBaseClass + "-disabled" + " " + this.namespace + "-state-disabled").removeAttr("aria-disabled"); a("label[for=" + this.newelement.attr("id") + "]").attr("for", this.element.attr("id")).unbind("click"); this.newelement.remove(); this.list.remove(); this.element.show() }, _typeAhead: function (b, d) { function h(b, c) { g = true; a(b).trigger(d); e._prevChar[1] = c } var e = this; if (!e._prevChar) { e._prevChar = ["", 0] } var f = String.fromCharCode(b); c = f.toLowerCase(); var g = false; this.list.find("li a").each(function (b) { if (!g) { var d = a(this).text(); if (d.indexOf(f) == 0 || d.indexOf(c) == 0) { if (e._prevChar[0] == f) { if (e._prevChar[1] < b) { h(this, b) } } else { h(this, b) } } } }); this._prevChar[0] = f }, _uiHash: function () { return { value: this.value()} }, open: function (a) { var b = this; var c = this.newelement.attr("aria-disabled"); if (c != "true") { this._refreshPosition(); this._closeOthers(a); this.newelement.addClass("ui-state-active"); this.list.appendTo("body").addClass(b.widgetBaseClass + "-open").attr("aria-hidden", false).find("li:not(." + b.widgetBaseClass + "-group):eq(" + this._selectedIndex() + ") a")[0].focus(); if (this.options.style == "dropdown") { this.newelement.removeClass("ui-corner-all").addClass("ui-corner-top") } this._refreshPosition(); this._trigger("open", a, this._uiHash()) } }, close: function (a, b) { if (this.newelement.is(".ui-state-active")) { this.newelement.removeClass("ui-state-active"); this.list.attr("aria-hidden", true).removeClass(this.widgetBaseClass + "-open"); if (this.options.style == "dropdown") { this.newelement.removeClass("ui-corner-top").addClass("ui-corner-all") } if (b) { this.newelement[0].focus() } this._trigger("close", a, this._uiHash()) } }, change: function (a) { this.element.trigger("change"); this._trigger("change", a, this._uiHash()) }, select: function (a) { this._trigger("select", a, this._uiHash()) }, _closeOthers: function (b) { a("." + this.widgetBaseClass + ".ui-state-active").not(this.newelement).each(function () { a(this).data("selectelement").selectmenu("close", b) }); a("." + this.widgetBaseClass + ".ui-state-hover").trigger("mouseout") }, _toggle: function (a, b) { if (this.list.is("." + this.widgetBaseClass + "-open")) { this.close(a, b) } else { this.open(a) } }, _formatText: function (a) { return this.options.format ? this.options.format(a) : a }, _selectedIndex: function () { return this.element[0].selectedIndex }, _selectedOptionLi: function () { return this._optionLis.eq(this._selectedIndex()) }, _focusedOptionLi: function () { return this.list.find("." + this.widgetBaseClass + "-item-focus") }, _moveSelection: function (a) { var b = parseInt(this._selectedOptionLi().data("index"), 10); var c = b + a; return this._optionLis.eq(c).trigger("mouseup") }, _moveFocus: function (a) { if (!isNaN(a)) { var b = parseInt(this._focusedOptionLi().data("index"), 10); var c = b + a } else { var c = parseInt(this._optionLis.filter(a).data("index"), 10) } if (c < 0) { c = 0 } if (c > this._optionLis.size() - 1) { c = this._optionLis.size() - 1 } var d = this.widgetBaseClass + "-item-" + Math.round(Math.random() * 1e3); this._focusedOptionLi().find("a:eq(0)").attr("id", ""); this._optionLis.eq(c).find("a:eq(0)").attr("id", d)[0].focus(); this.list.attr("aria-activedescendant", d) }, _scrollPage: function (a) { var b = Math.floor(this.list.outerHeight() / this.list.find("li:first").outerHeight()); b = a == "up" ? -b : b; this._moveFocus(b) }, _setData: function (a, b) { this.options[a] = b; if (a == "disabled") { this.close(); this.element.add(this.newelement).add(this.list)[b ? "addClass" : "removeClass"](this.widgetBaseClass + "-disabled" + " " + this.namespace + "-state-disabled").attr("aria-disabled", b) } }, value: function (a) { if (arguments.length) { this.element[0].selectedIndex = a; this._refreshValue(); this._refreshPosition() } return this.element[0].selectedIndex }, _refreshValue: function () { var a = this.options.style == "popup" ? " ui-state-active" : ""; var b = this.widgetBaseClass + "-item-" + Math.round(Math.random() * 1e3); this.list.find("." + this.widgetBaseClass + "-item-selected").removeClass(this.widgetBaseClass + "-item-selected" + a).find("a").attr("aria-selected", "false").attr("id", ""); this._selectedOptionLi().addClass(this.widgetBaseClass + "-item-selected" + a).find("a").attr("aria-selected", "true").attr("id", b); var c = this.newelement.data("optionClasses") ? this.newelement.data("optionClasses") : ""; var d = this._selectedOptionLi().data("optionClasses") ? this._selectedOptionLi().data("optionClasses") : ""; this.newelement.removeClass(c).data("optionClasses", d).addClass(d).find("." + this.widgetBaseClass + "-status").html(this._selectedOptionLi().find("a:eq(0)").html()); this.list.attr("aria-activedescendant", b) }, _refreshPosition: function () { this.list.css("left", this.newelement.offset().left); var b = this.newelement.offset().top; var c = this.list[0].scrollTop; this.list.find("li:lt(" + this._selectedIndex() + ")").each(function () { c -= a(this).outerHeight() }); if (this.newelement.is("." + this.widgetBaseClass + "-popup")) { b += c; this.list.css("top", b) } else { b += this.newelement.height(); this.list.css("top", b) } } }); a.extend(a.ui.selectmenu, { getter: "value", version: "@VERSION", eventPrefix: "selectmenu", defaults: { transferClasses: true, style: "popup", width: null, menuWidth: null, handleWidth: 26, maxHeight: null, icons: null, format: null} }) })(jQuery)
