mirror of
https://github.com/WordPress/WordPress.git
synced 2024-11-05 02:10:45 +01:00
86dd4f184c
git-svn-id: http://core.svn.wordpress.org/trunk@24372 1a063a9b-81f0-0310-95a4-ce76da25c4cd
1 line
3.0 KiB
JavaScript
1 line
3.0 KiB
JavaScript
(function(a){a.suggest=function(n,f){var c,e,m,d,p,o;c=a(n).attr("autocomplete","off");e=a("<ul/>");m=false;d=0;p=[];o=0;e.addClass(f.resultsClass).appendTo("body");i();a(window).load(i).resize(i);c.blur(function(){setTimeout(function(){e.hide()},200)});c.keydown(l);function i(){var u=c.offset();e.css({top:(u.top+n.offsetHeight)+"px",left:u.left+"px"})}function l(u){if((/27$|38$|40$/.test(u.keyCode)&&e.is(":visible"))||(/^13$|^9$/.test(u.keyCode)&&s())){if(u.preventDefault){u.preventDefault()}if(u.stopPropagation){u.stopPropagation()}u.cancelBubble=true;u.returnValue=false;switch(u.keyCode){case 38:j();break;case 40:r();break;case 9:case 13:q();break;case 27:e.hide();break}}else{if(c.val().length!=d){if(m){clearTimeout(m)}m=setTimeout(k,f.delay);d=c.val().length}}}function k(){var w=a.trim(c.val()),v,u;if(f.multiple){v=w.lastIndexOf(f.multipleSep);if(v!=-1){w=a.trim(w.substr(v+f.multipleSep.length))}}if(w.length>=f.minchars){cached=t(w);if(cached){h(cached.items)}else{a.get(f.source,{q:w},function(x){e.hide();u=b(x,w);h(u);g(w,u,x.length)})}}else{e.hide()}}function t(v){var u;for(u=0;u<p.length;u++){if(p[u]["q"]==v){p.unshift(p.splice(u,1)[0]);return p[0]}}return false}function g(x,u,v){var w;while(p.length&&(o+v>f.maxCacheSize)){w=p.pop();o-=w.size}p.push({q:x,size:v,items:u});o+=v}function h(u){var w="",v;if(!u){return}if(!u.length){e.hide();return}i();for(v=0;v<u.length;v++){w+="<li>"+u[v]+"</li>"}e.html(w).show();e.children("li").mouseover(function(){e.children("li").removeClass(f.selectClass);a(this).addClass(f.selectClass)}).click(function(x){x.preventDefault();x.stopPropagation();q()})}function b(u,y){var v=[],z=u.split(f.delimiter),x,w;for(x=0;x<z.length;x++){w=a.trim(z[x]);if(w){w=w.replace(new RegExp(y,"ig"),function(A){return'<span class="'+f.matchClass+'">'+A+"</span>"});v[v.length]=w}}return v}function s(){var u;if(!e.is(":visible")){return false}u=e.children("li."+f.selectClass);if(!u.length){u=false}return u}function q(){$currentResult=s();if($currentResult){if(f.multiple){if(c.val().indexOf(f.multipleSep)!=-1){$currentVal=c.val().substr(0,(c.val().lastIndexOf(f.multipleSep)+f.multipleSep.length))}else{$currentVal=""}c.val($currentVal+$currentResult.text()+f.multipleSep);c.focus()}else{c.val($currentResult.text())}e.hide();if(f.onSelect){f.onSelect.apply(c[0])}}}function r(){$currentResult=s();if($currentResult){$currentResult.removeClass(f.selectClass).next().addClass(f.selectClass)}else{e.children("li:first-child").addClass(f.selectClass)}}function j(){var u=s();if(u){u.removeClass(f.selectClass).prev().addClass(f.selectClass)}else{e.children("li:last-child").addClass(f.selectClass)}}};a.fn.suggest=function(c,b){if(!c){return}b=b||{};b.multiple=b.multiple||false;b.multipleSep=b.multipleSep||", ";b.source=c;b.delay=b.delay||100;b.resultsClass=b.resultsClass||"ac_results";b.selectClass=b.selectClass||"ac_over";b.matchClass=b.matchClass||"ac_match";b.minchars=b.minchars||2;b.delimiter=b.delimiter||"\n";b.onSelect=b.onSelect||false;b.maxCacheSize=b.maxCacheSize||65536;this.each(function(){new a.suggest(this,b)});return this}})(jQuery); |