/* * Copyright 2012, Google Inc. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are * met: * * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above * copyright notice, this list of conditions and the following disclaimer * in the documentation and/or other materials provided with the * distribution. * * Neither the name of Google Inc. nor the names of its * contributors may be used to endorse or promote products derived from * this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * @author Dae Park (daepark@google.com) */ (function($, undefined){ if (!("console" in window)) { var c = window.console = {}; c.log = c.warn = c.error = c.debug = function(){}; } /** * jQuery UI provides a way to be notified when an element is removed from the DOM. * suggest would like to use this facility to properly teardown it's elements from the DOM (suggest list, flyout, etc.). * The following logic tries to determine if "remove" event is already present, else * tries to mimic what jQuery UI does (as of 1.8.5) by adding a hook to $.cleanData or $.fn.remove. */ $(function() { var div = $("
"); $(document.body).append(div); var t = setTimeout(function() { // copied from jquery-ui // for remove event if ( $.cleanData ) { var _cleanData = $.cleanData; $.cleanData = function( elems ) { for ( var i = 0, elem; (elem = elems[i]) != null; i++ ) { $( elem ).triggerHandler( "remove" ); } _cleanData( elems ); }; } else { var _remove = $.fn.remove; $.fn.remove = function( selector, keepData ) { return this.each(function() { if ( !keepData ) { if ( !selector || $.filter( selector, [ this ] ).length ) { $( "*", this ).add( [ this ] ).each(function() { $( this ).triggerHandler( "remove" ); }); } } return _remove.call( $(this), selector, keepData ); }); }; } }, 1); div.bind("remove", function() { clearTimeout(t); }); div.remove(); }); /** * These are the search parameters that are transparently passed * to the search service as specified by service_url + service_path */ var SEARCH_PARAMS = { key:1, filter:1, spell:1, exact:1, lang:1, scoring:1, prefixed:1, stemmed:1, format:1, mql_output:1, output:1, type:1 }; $.suggest = function(name, prototype) { $.fn[name] = function(options) { if (!this.length) { console.warn('Suggest: invoked on empty element set'); } return this .each(function() { if (this.nodeName) { if (this.nodeName.toUpperCase() === 'INPUT') { if (this.type && this.type.toUpperCase() !== 'TEXT') { console.warn('Suggest: unsupported INPUT type: '+this.type); } } else { console.warn('Suggest: unsupported DOM element: '+this.nodeName); } } var instance = $.data(this, name); if (instance) { instance._destroy(); } $.data(this, name, new $.suggest[name](this, options))._init(); }); }; $.suggest[name] = function(input, options) { var self = this, o = this.options = $.extend(true, {}, $.suggest.defaults, $.suggest[name].defaults, options), pfx = o.css_prefix = o.css_prefix || "", css = o.css; this.name = name; $.each(css, function(k, v) { css[k] = pfx + css[k]; }); // suggest parameters o.ac_param = {}; $.each(SEARCH_PARAMS, function(k) { var v = o[k]; if (v === null || v === "") { return; } o.ac_param[k] = v; }); // flyout service lang is the first specified lang o.flyout_lang = null; if (o.ac_param.lang) { var lang = o.ac_param.lang; if ($.isArray(lang) && lang.length) { lang = lang.join(','); } if (lang) { o.flyout_lang = lang; } } // status texts this._status = { START: "", LOADING: "", SELECT: "", ERROR: "" }; if (o.status && o.status instanceof Array && o.status.length >= 3) { this._status.START = o.status[0] || ""; this._status.LOADING = o.status[1] || ""; this._status.SELECT = o.status[2] || ""; if (o.status.length === 4) { this._status.ERROR = o.status[3] || ""; } } // create the container for the drop down list var s = this.status = $('
').addClass(css.status), l = this.list = $("