/* * Linkselect jQuery Plug-in * * Copyright 2008 Giva, Inc. (http://www.givainc.com/labs/) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * Date: 2009-07-30 * Rev: 1.2.08 */ ;(function($){ // set the version of the link select $.linkselect = {version: "1.2.08"}; $.fn.linkselect = function(options) { var method = typeof arguments[0] == "string" && arguments[0]; var args = method && Array.prototype.slice.call(arguments, 1) || arguments; // if a method is supplied, execute it for non-empty results if( method && this.length ){ // get a reference to the first linkselect found var self = $.data(this[0], "linkselect"); // if request a copy of the object, return it if( method.toLowerCase() == "object" ) return self; // if method is defined, run it and return either it's results or the chain else if( self[method] ){ // define a result variable to return to the jQuery chain var result; this.each(function (i){ // apply the method to the current element var r = $.data(this, "linkselect")[method].apply(self, args); // if first iteration we need to check if we're done processing or need to add it to the jquery chain if( i == 0 && r ){ // if this is a jQuery item, we need to store them in a collection if( !!r.jquery ){ result = $([]).add(r); // otherwise, just store the result and stop executing } else { result = r; // since we're a non-jQuery item, just cancel processing further items return false; } // keep adding jQuery objects to the results } else if( !!r && !!r.jquery ){ result = result.add(r); } }); // return either the results (which could be a jQuery object) or the original chain return result || this; // everything else, return the chain } else return this; // initializing request } else { // create a new linkselect for each object found return this.each(function (){ new $.LinkSelect(this, options); }); }; }; $.LinkSelect = function (el, options){ options = $.extend({}, $.LinkSelect.defaults, options); var self = this, select = el, $select = $(el), shortcuts = {}, disabled = false, current_char_pos = 0, last_char_code, is_open = false; // store the global properties this.id = $select.attr("id"); // get/set the value of the field this.val = function (value, doCallback){ // update the selected value if( arguments.length > 0 ){ setSelectedItem(value, doCallback); return $a; } else return $input.val(); }; // set the focus to the field this.focus = function (){ // place focus asynchronously to avoid click collisions setTimeout(function(){$a.focus();}, 1); return $a; }; // set the blur to the field this.blur = function (){ // place focus asynchronously to avoid click collisions setTimeout(function(){$a.blur();}, 1); return $a; }; // place focus and open the menu this.open = function (callback, bDoFocus){ // if disabled, skip processing if( disabled ) return $a; // close any open linkselects $(document).triggerHandler("click.linkselect"); // place focus if( bDoFocus !== false ) $a.trigger("focus"); // show the options asynchronously to avoid click collisions setTimeout(function(){ showOptions(callback); }, 1); return $a; } // disable the link this.disable = function (status){ disabled = status; // remove any existing disabled label $a.parent().find("span." + options.classDisabled).remove(); // show/hide the anchor depending on the disabled status $a[disabled ? "hide" : "show"](); // if disabled, add a span if( disabled ) $a.after('' + $a.html() + ''); return $a; } // replace the options this.replaceOptions = function (options, doCallback){ // remove the current options $select.children('option').remove(); // add each new item $.each(options, function (i){ // create the new option var $option = $("