dir/js/fk.autocomplete.min.js

5 lines
3 KiB
JavaScript
Raw Normal View History

/**
* Friendica people autocomplete
*
* require jQuery, jquery.textareas
*/function ACPopup(e,t){this.idsel=-1,this.element=e,this.searchText="",this.ready=!0,this.kp_timer=!1,this.url=t;var n=530,r=130;if(typeof e.editorId=="undefined")style=$(e).offset(),n=$(e).width(),r=$(e).height();else{var i=e.getContainer();typeof i!="undefined"&&(style=$(i).offset(),n=$(i).width(),r=$(i).height())}style.top=style.top+r,style.width=n,style.position="absolute",style.display="none",this.cont=$("<div class='acpopup'></div>"),this.cont.css(style),$("body").append(this.cont)}function ContactAutocomplete(e,t){this.pattern=/@([^ \n]+)$/,this.popup=null;var n=this;$(e).unbind("keydown"),$(e).unbind("keyup"),$(e).keydown(function(e){n.popup!==null&&n.popup.onkey(e)}),$(e).keyup(function(e){cpos=$(this).getSelection(),cpos.start==cpos.end&&(match=$(this).val().substring(0,cpos.start).match(n.pattern),match!==null?(n.popup===null&&(n.popup=new ACPopup(this,t)),n.popup.ready&&match[1]!==n.popup.searchText&&n.popup.search(match[1]),n.popup.ready||(n.popup=null)):n.popup!==null&&(n.popup.close(),n.popup=null))})}ACPopup.prototype.close=function(){$(this.cont).remove(),this.ready=!1},ACPopup.prototype.search=function(e){var t=this;this.searchText=e,this.kp_timer&&clearTimeout(this.kp_timer),this.kp_timer=setTimeout(function(){t._search()},500)},ACPopup.prototype._search=function(){console.log("_search");var e=this,t={start:0,count:100,search:this.searchText,type:"c"};$.ajax({type:"POST",url:this.url,data:t,dataType:"json",success:function(t){e.cont.html(""),t.tot>0?(e.cont.show(),$(t.items).each(function(){html="<img src='{0}' height='16px' width='16px'>{1} ({2})".format(this.photo,this.name,this.nick),e.add(html,this.nick.replace(" ","")+"+"+this.id+" - "+this.link)})):e.cont.hide()}})},ACPopup.prototype.add=function(e,n){var r=this,i=$("<div class='acpopupitem' title='"+n+"'>"+e+"</div>");i.click(function(e){t=$(this).attr("title").replace(new RegExp(" - .*"),""),typeof r.element.container=="undefined"?(el=$(r.element),sel=el.getSelection(),sel.start=sel.start-r.searchText.length,el.setSelection(sel.start,sel.end).replaceSelectedText(t+" ").collapseSelection(!1),r.close()):(txt=tinyMCE.activeEditor.getContent(),newtxt=txt.replace(r.searchText,t+" "),tinyMCE.activeEditor.setContent(newtxt),tinyMCE.activeEditor.focus(),r.close())}),$(this.cont).append(i)},ACPopup.prototype.onkey=function(e){e.keyCode=="13"&&(this.idsel>-1?(this.cont.children()[this.idsel].click(),e.preventDefault()):this.close()),e.keyCode=="38"&&(cmax=this.cont.children().size()-1,this.idsel--,this.idsel<0&&(this.idsel=cmax),e.preventDefault());if(e.keyCode=="40"||e.keyCode=="9")cmax=this.cont.children().size()-1,this.idsel++,this.idsel>cmax&&(this.idsel=0),e.preventDefault();if(e.keyCode=="38"||e.keyCode=="40"||e.keyCode=="9")this.cont.children().removeClass("selected"),$(this.cont.children()[this.idsel]).addClass("selected");e.keyCode=="27"&&this.close()},function(e){e.fn.contact_autocomplete=function(e){this.each(function(){new ContactAutocomplete(this,e)})}}(jQuery);