]> git.mxchange.org Git - friendica.git/blob - js/fk.autocomplete.js
add contact-id to auto-complete response to resolve duplicates
[friendica.git] / js / fk.autocomplete.js
1 /**
2  * Friendika people autocomplete
3  *
4  * require jQuery, jquery.textareas
5  */
6  
7                 
8                 
9 function ACPopup(elm,backend_url){
10         this.idsel=-1;
11         this.element = elm;
12         this.searchText="";
13         this.ready=true;
14         this.kp_timer = false;
15         this.url = backend_url;
16         
17         style = $(elm).offset();
18         w = $(elm).width();
19         h = $(elm).height();
20         style.top=style.top+h;
21         style.width = w;
22         style.position = 'absolute';
23 /*      style['max-height'] = '150px';
24         style.border = '1px solid red';
25         style.background = '#cccccc';
26         
27         style.overflow = 'auto';
28         style['z-index'] = '100000';
29 */
30         style.display = 'none';
31         
32         this.cont = $("<div class='acpopup'></div>");
33         this.cont.css(style);
34         
35         $("body").append(this.cont);
36 }
37 ACPopup.prototype.close = function(){
38         $(this.cont).remove();
39         this.ready=false;
40 }
41 ACPopup.prototype.search = function(text){
42         var that = this;
43         this.searchText=text;
44         if (this.kp_timer) clearTimeout(this.kp_timer);
45         this.kp_timer = setTimeout( function(){that._search();}, 500);
46 }
47 ACPopup.prototype._search = function(){ 
48         console.log("_search");
49         var that = this;
50         var postdata = {
51                 start:0,
52                 count:100,
53                 search:this.searchText,
54                 type:'c',
55         }
56         
57         $.ajax({
58                 type:'POST',
59                 url: this.url,
60                 data: postdata,
61                 dataType: 'json',
62                 success:function(data){
63                         that.cont.html("");
64                         if (data.tot>0){
65                                 that.cont.show();
66                                 $(data.items).each(function(){
67                                         html = "<img src='{0}' height='16px' width='16px'>{1} ({2})".format(this.photo, this.name, this.nick)
68                                         that.add(html, this.nick + '+' + this.id + ' - ' + this.link);
69                                 });                     
70                         } else {
71                                 that.cont.hide();
72                         }
73                 }
74         });
75         
76 }
77 ACPopup.prototype.add = function(label, value){
78         var that=this;
79         var elm = $("<div class='acpopupitem' title='"+value+"'>"+label+"</div>");
80         elm.click(function(e){
81                         t = $(this).attr('title').replace(new RegExp(' \- .*'),'');
82                 el=$(that.element);
83                 sel = el.getSelection();
84                 sel.start = sel.start- that.searchText.length;
85                 el.setSelection(sel.start,sel.end).replaceSelectedText(t).collapseSelection(false);
86                 that.close();
87         });
88         $(this.cont).append(elm);
89 }
90 ACPopup.prototype.onkey = function(event){
91         if (event.keyCode == '13' && this.idsel>-1) {
92                 this.cont.children()[this.idsel].click();
93                 event.preventDefault();
94         }
95         if (event.keyCode == '38') { //cursor up
96                 cmax = this.cont.children().size()-1;
97                 this.idsel--;
98                 if (this.idsel<0) this.idsel=cmax;
99                 event.preventDefault();
100         }
101         if (event.keyCode == '40') { //cursor down
102                 cmax = this.cont.children().size()-1;
103                 this.idsel++;
104                 if (this.idsel>cmax) this.idsel=0;
105                 event.preventDefault();
106         }
107         
108         if (event.keyCode == '38' || event.keyCode == '40' ) {
109                 this.cont.children().removeClass('selected');
110                 $(this.cont.children()[this.idsel]).addClass('selected');
111         }
112         
113         if (event.keyCode == '27') { //ESC
114                 this.close();
115         }
116 }
117
118 function ContactAutocomplete(element,backend_url){
119         this.pattern=/@([^ \n]+)$/;
120         this.popup=null;
121         var that = this;
122         
123         $(element).unbind('keydown');
124         $(element).unbind('keyup');
125         
126         $(element).keydown(function(event){
127                 if (that.popup!==null) that.popup.onkey(event);
128         });
129         
130         $(element).keyup(function(event){
131                 cpos = $(this).getSelection();
132                 if (cpos.start==cpos.end){
133                         match = $(this).val().substring(0,cpos.start).match(that.pattern);
134                         if (match!==null){
135                                 if (that.popup===null){
136                                         that.popup = new ACPopup(this, backend_url);
137                                 }
138                                 if (that.popup.ready && match[1]!==that.popup.searchText) that.popup.search(match[1]);
139                                 if (!that.popup.ready) that.popup=null;
140                                 
141                         } else {
142                                 if (that.popup!==null) {that.popup.close(); that.popup=null;}
143                         }
144                         
145                         
146                 }
147         });             
148         
149 }
150
151
152 /**
153  * jQuery plugin 'contact_autocomplete'
154  */
155 (function( $ ){
156   $.fn.contact_autocomplete = function(backend_url) {
157     this.each(function(){
158                 new ContactAutocomplete(this, backend_url);
159         });
160   };
161 })( jQuery );
162
163
164
165