2 * Friendica people autocomplete
4 * require jQuery, jquery.textareas
9 function ACPopup(elm,backend_url){
14 this.kp_timer = false;
15 this.url = backend_url;
21 if(typeof elm.editorId == "undefined") {
22 style = $(elm).offset();
27 var container = elm.getContainer();
28 if(typeof container != "undefined") {
29 style = $(container).offset();
30 w = $(container).width();
31 h = $(container).height();
35 style.top=style.top+h;
37 style.position = 'absolute';
38 /* style['max-height'] = '150px';
39 style.border = '1px solid red';
40 style.background = '#cccccc';
42 style.overflow = 'auto';
43 style['z-index'] = '100000';
45 style.display = 'none';
47 this.cont = $("<div class='acpopup'></div>");
50 $("body").append(this.cont);
52 ACPopup.prototype.close = function(){
53 $(this.cont).remove();
56 ACPopup.prototype.search = function(text){
59 if (this.kp_timer) clearTimeout(this.kp_timer);
60 this.kp_timer = setTimeout( function(){that._search();}, 500);
62 ACPopup.prototype._search = function(){
63 console.log("_search");
68 search:this.searchText,
77 success:function(data){
81 $(data.items).each(function(){
82 html = "<img src='{0}' height='16px' width='16px'>{1} ({2})".format(this.photo, this.name, this.nick)
83 that.add(html, this.nick.replace(' ','') + '+' + this.id + ' - ' + this.link);
92 ACPopup.prototype.add = function(label, value){
94 var elm = $("<div class='acpopupitem' title='"+value+"'>"+label+"</div>");
95 elm.click(function(e){
96 t = $(this).attr('title').replace(new RegExp(' \- .*'),'');
97 if(typeof(that.element.container) === "undefined") {
99 sel = el.getSelection();
100 sel.start = sel.start- that.searchText.length;
101 el.setSelection(sel.start,sel.end).replaceSelectedText(t+' ').collapseSelection(false);
105 txt = tinyMCE.activeEditor.getContent();
106 // alert(that.searchText + ':' + t);
107 newtxt = txt.replace(that.searchText,t+' ');
108 tinyMCE.activeEditor.setContent(newtxt);
109 tinyMCE.activeEditor.focus();
113 $(this.cont).append(elm);
115 ACPopup.prototype.onkey = function(event){
116 if (event.keyCode == '13') {
118 this.cont.children()[this.idsel].click();
119 event.preventDefault();
124 if (event.keyCode == '38') { //cursor up
125 cmax = this.cont.children().size()-1;
127 if (this.idsel<0) this.idsel=cmax;
128 event.preventDefault();
130 if (event.keyCode == '40' || event.keyCode == '9') { //cursor down
131 cmax = this.cont.children().size()-1;
133 if (this.idsel>cmax) this.idsel=0;
134 event.preventDefault();
137 if (event.keyCode == '38' || event.keyCode == '40' || event.keyCode == '9') {
138 this.cont.children().removeClass('selected');
139 $(this.cont.children()[this.idsel]).addClass('selected');
142 if (event.keyCode == '27') { //ESC
147 function ContactAutocomplete(element,backend_url){
148 this.pattern=/@([^ \n]+)$/;
152 $(element).unbind('keydown');
153 $(element).unbind('keyup');
155 $(element).keydown(function(event){
156 if (that.popup!==null) that.popup.onkey(event);
159 $(element).keyup(function(event){
160 cpos = $(this).getSelection();
161 if (cpos.start==cpos.end){
162 match = $(this).val().substring(0,cpos.start).match(that.pattern);
164 if (that.popup===null){
165 that.popup = new ACPopup(this, backend_url);
167 if (that.popup.ready && match[1]!==that.popup.searchText) that.popup.search(match[1]);
168 if (!that.popup.ready) that.popup=null;
171 if (that.popup!==null) {that.popup.close(); that.popup=null;}
182 * jQuery plugin 'contact_autocomplete'
185 $.fn.contact_autocomplete = function(backend_url) {
186 this.each(function(){
187 new ContactAutocomplete(this, backend_url);