﻿/*
*
* Copyright (c) 2006-2008 Sam Collett (http://www.texotela.co.uk)
* Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
* and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
*
* Version 2.2.3
* Demo: http://www.texotela.co.uk/code/jquery/select/
*
* $LastChangedDate: 2008-06-17 17:27:25 +0100 (Tue, 17 Jun 2008) $
* $Rev: 5727 $
*
*/
; (function($) { $.fn.addOption = function() { var e = function(a, v, t, b) { var c = document.createElement("option"); c.value = v, c.text = t; var o = a.options; var d = o.length; if (!a.cache) { a.cache = {}; for (var i = 0; i < d; i++) { a.cache[o[i].value] = i } } if (typeof a.cache[v] == "undefined") a.cache[v] = d; a.options[a.cache[v]] = c; if (b) { c.selected = true } }; var a = arguments; if (a.length == 0) return this; var f = true; var m = false; var g, v, t; if (typeof (a[0]) == "object") { m = true; g = a[0] } if (a.length >= 2) { if (typeof (a[1]) == "boolean") f = a[1]; else if (typeof (a[2]) == "boolean") f = a[2]; if (!m) { v = a[0]; t = a[1] } } this.each(function() { if (this.nodeName.toLowerCase() != "select") return; if (m) { for (var a in g) { e(this, a, g[a], f) } } else { e(this, v, t, f) } }); return this }; $.fn.ajaxAddOption = function(b, c, d, e, f) { if (typeof (b) != "string") return this; if (typeof (c) != "object") c = {}; if (typeof (d) != "boolean") d = true; this.each(function() { var a = this; $.getJSON(b, c, function(r) { $(a).addOption(r, d); if (typeof e == "function") { if (typeof f == "object") { e.apply(a, f) } else { e.call(a) } } }) }); return this }; $.fn.removeOption = function() { var a = arguments; if (a.length == 0) return this; var d = typeof (a[0]); var v, index; if (d == "string" || d == "object" || d == "function") { v = a[0]; if (v.constructor == Array) { var l = v.length; for (var i = 0; i < l; i++) { this.removeOption(v[i], a[1]) } return this } } else if (d == "number") index = a[0]; else return this; this.each(function() { if (this.nodeName.toLowerCase() != "select") return; if (this.cache) this.cache = null; var b = false; var o = this.options; if (!!v) { var c = o.length; for (var i = c - 1; i >= 0; i--) { if (v.constructor == RegExp) { if (o[i].value.match(v)) { b = true } } else if (o[i].value == v) { b = true } if (b && a[1] === true) b = o[i].selected; if (b) { o[i] = null } b = false } } else { if (a[1] === true) { b = o[index].selected } else { b = true } if (b) { this.remove(index) } } }); return this }; $.fn.sortOptions = function(f) { var a = typeof (f) == "undefined" ? true : !!f; this.each(function() { if (this.nodeName.toLowerCase() != "select") return; var o = this.options; var d = o.length; var e = []; for (var i = 0; i < d; i++) { e[i] = { v: o[i].value, t: o[i].text} } e.sort(function(b, c) { o1t = b.t.toLowerCase(), o2t = c.t.toLowerCase(); if (o1t == o2t) return 0; if (a) { return o1t < o2t ? -1 : 1 } else { return o1t > o2t ? -1 : 1 } }); for (var i = 0; i < d; i++) { o[i].text = e[i].t; o[i].value = e[i].v } }); return this }; $.fn.selectOptions = function(b, d) { var v = b; var e = typeof (b); var c = d || false; if (e != "string" && e != "function" && e != "object") return this; this.each(function() { if (this.nodeName.toLowerCase() != "select") return this; var o = this.options; var a = o.length; for (var i = 0; i < a; i++) { if (v.constructor == RegExp) { if (o[i].value.match(v)) { o[i].selected = true } else if (c) { o[i].selected = false } } else { if (o[i].value == v) { o[i].selected = true } else if (c) { o[i].selected = false } } } }); return this }; $.fn.copyOptions = function(b, c) { var w = c || "selected"; if ($(b).size() == 0) return this; this.each(function() { if (this.nodeName.toLowerCase() != "select") return this; var o = this.options; var a = o.length; for (var i = 0; i < a; i++) { if (w == "all" || (w == "selected" && o[i].selected)) { $(b).addOption(o[i].value, o[i].text) } } }); return this }; $.fn.containsOption = function(b, c) { var d = false; var v = b; var e = typeof (v); var f = typeof (c); if (e != "string" && e != "function" && e != "object") return f == "function" ? this : d; this.each(function() { if (this.nodeName.toLowerCase() != "select") return this; if (d && f != "function") return false; var o = this.options; var a = o.length; for (var i = 0; i < a; i++) { if (v.constructor == RegExp) { if (o[i].value.match(v)) { d = true; if (f == "function") c.call(o[i], i) } } else { if (o[i].value == v) { d = true; if (f == "function") c.call(o[i], i) } } } }); return f == "function" ? this : d }; $.fn.selectedValues = function() { var v = []; this.find("option:selected").each(function() { v[v.length] = this.value }); return v }; $.fn.selectedOptions = function() { return this.find("option:selected") } })(jQuery);
