mirror of
https://github.com/friendica/friendica
synced 2024-11-16 10:53:55 +00:00
10 lines
8.2 KiB
JavaScript
10 lines
8.2 KiB
JavaScript
|
/**
|
||
|
* Ajax Autocomplete for jQuery, version 1.1.3
|
||
|
* (c) 2010 Tomas Kirda
|
||
|
*
|
||
|
* Ajax Autocomplete for jQuery is freely distributable under the terms of an MIT-style license.
|
||
|
* For details, see the web site: http://www.devbridge.com/projects/autocomplete/jquery/
|
||
|
*
|
||
|
* Last Review: 04/19/2010
|
||
|
* Heavily modified for contact completion in Friendica (add photos, hover tips. etc.) 11-May-2012 mike@macgirvin.com
|
||
|
*//*jslint onevar: true, evil: true, nomen: true, eqeqeq: true, bitwise: true, regexp: true, newcap: true, immed: true *//*global window: true, document: true, clearInterval: true, setInterval: true, jQuery: true */(function($){function fnFormatResult(value,data,currentValue){var pattern="("+currentValue.replace(reEscape,"\\$1")+")";return value.replace(new RegExp(pattern,"gi"),"<strong>$1</strong>")}function Autocomplete(el,options){this.el=$(el),this.el.attr("autocomplete","off"),this.suggestions=[],this.data=[],this.badQueries=[],this.selectedIndex=-1,this.currentValue=this.el.val(),this.intervalId=0,this.cachedResponse=[],this.onChangeInterval=null,this.ignoreValueChange=!1,this.serviceUrl=options.serviceUrl,this.isLocal=!1,this.options={autoSubmit:!1,minChars:1,maxHeight:300,deferRequestBy:0,width:0,highlight:!0,params:{},fnFormatResult:fnFormatResult,delimiter:null,zIndex:9999},this.initialize(),this.setOptions(options)}var reEscape=new RegExp("(\\"+["/",".","*","+","?","|","(",")","[","]","{","}","\\"].join("|\\")+")","g");$.fn.autocomplete=function(options){return new Autocomplete(this.get(0)||$("<input />"),options)},Autocomplete.prototype={killerFn:null,initialize:function(){var me,uid,autocompleteElId;me=this,uid=Math.floor(Math.random()*1048576).toString(16),autocompleteElId="Autocomplete_"+uid,this.killerFn=function(e){$(e.target).parents(".autocomplete").size()===0&&(me.killSuggestions(),me.disableKillerFn())},this.options.width||(this.options.width=this.el.width()),this.mainContainerId="AutocompleteContainter_"+uid,$('<div id="'+this.mainContainerId+'" style="position:absolute;z-index:9999;"><div class="autocomplete-w1"><div class="autocomplete" id="'+autocompleteElId+'" style="display:none; width:300px;"></div></div></div>').appendTo("body"),this.container=$("#"+autocompleteElId),this.fixPosition(),window.opera?this.el.keypress(function(e){me.onKeyPress(e)}):this.el.keydown(function(e){me.onKeyPress(e)}),this.el.keyup(function(e){me.onKeyUp(e)}),this.el.blur(function(){me.enableKillerFn()}),this.el.focus(function(){me.fixPosition()})},setOptions:function(options){var o=this.options;$.extend(o,options),o.lookup&&(this.isLocal=!0,$.isArray(o.lookup)&&(o.lookup={suggestions:o.lookup,data:[]})),$("#"+this.mainContainerId).css({zIndex:o.zIndex}),this.container.css({maxHeight:o.maxHeight+"px",width:o.width})},clearCache:function(){this.cachedResponse=[],this.badQueries=[]},disable:function(){this.disabled=!0},enable:function(){this.disabled=!1},fixPosition:function(){var offset=this.el.offset();$("#"+this.mainContainerId).css({top:offset.top+this.el.innerHeight()+"px",left:offset.left+"px"})},enableKillerFn:function(){var me=this;$(document).bind("click",me.killerFn)},disableKillerFn:function(){var me=this;$(document).unbind("click",me.killerFn)},killSuggestions:function(){var me=this;this.stopKillSuggestions(),this.intervalId=window.setInterval(function(){me.hide(),me.stopKillSuggestions()},300)},stopKillSuggestions:function(){window.clearInterval(this.intervalId)},onKeyPress:function(e){if(this.disabled||!this.enabled)return;switch(e.keyCode){case 27:this.el.val(this.currentValue),this.hide();break;case 9:case 13:if(this.selectedIndex===-1){this.hide();return}this.select(this.selectedIndex);if(e.keyCode===9)return;break;case 38:this.moveUp();break;case 40:this.moveDown();break;default:return}e.stopImmediatePropagation(),e.preventDefault()},onKeyUp:function(e){if(this.disabled)return;switch(e.keyCode){case 38:case 40:return}clearInterval(this.onChangeInterval);if(this.currentValue!==this.el.val())if(this.options.deferRequestBy>0){var me=this;this.onChangeInterval=setInterval(function(){me.onValueChange()},this.options.deferRequestBy)}else this.onValueChange()},onValueChange:function(){clearInterval(this.onChangeInterval),this.currentValue=this.el.val();var q=this.getQuery(this.currentValue);this.selectedIndex=-1;if(this.ignoreValueChange){this.ignoreValueChange=!1;return}q===""||q.length<this.options.minChars?this.hide():this.getSuggestions(q)},getQuery:function(val){var d,arr;return d=this.options.delimiter,
|