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