]> git.mxchange.org Git - friendica.git/blob - js/fk.autocomplete.js
for people whose DB's are not updating
[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         if(typeof elm.editorId == "undefined") {        
18                 style = $(elm).offset();
19                 w = $(elm).width();
20                 h = $(elm).height();
21         }
22         else {
23                 style = $(elm.container).offset();
24                 w = elm.container.offsetWidth;
25                 h = elm.container.offsetHeight;
26                 // Quick fix for chrome until I get a tool to inspect the dom
27                 // Chrome returns 0x0
28                 if(! w)
29                         w = 530;
30                 if(! h)
31                         h = 130;
32
33         }
34
35         style.top=style.top+h;
36         style.width = w;
37         style.position = 'absolute';
38         /*      style['max-height'] = '150px';
39                 style.border = '1px solid red';
40                 style.background = '#cccccc';
41         
42                 style.overflow = 'auto';
43                 style['z-index'] = '100000';
44         */
45         style.display = 'none';
46         
47         this.cont = $("<div class='acpopup'></div>");
48         this.cont.css(style);
49         
50         $("body").append(this.cont);
51 }
52 ACPopup.prototype.close = function(){
53         $(this.cont).remove();
54         this.ready=false;
55 }
56 ACPopup.prototype.search = function(text){
57         var that = this;
58         this.searchText=text;
59         if (this.kp_timer) clearTimeout(this.kp_timer);
60         this.kp_timer = setTimeout( function(){that._search();}, 500);
61 }
62 ACPopup.prototype._search = function(){ 
63         console.log("_search");
64         var that = this;
65         var postdata = {
66                 start:0,
67                 count:100,
68                 search:this.searchText,
69                 type:'c',
70         }
71         
72         $.ajax({
73                 type:'POST',
74                 url: this.url,
75                 data: postdata,
76                 dataType: 'json',
77                 success:function(data){
78                         that.cont.html("");
79                         if (data.tot>0){
80                                 that.cont.show();
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);
84                                 });                     
85                         } else {
86                                 that.cont.hide();
87                         }
88                 }
89         });
90         
91 }
92         ACPopup.prototype.add = function(label, value){
93         var that=this;
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") {
98                         el=$(that.element);
99                         sel = el.getSelection();
100                         sel.start = sel.start- that.searchText.length;
101                         el.setSelection(sel.start,sel.end).replaceSelectedText(t+' ').collapseSelection(false);
102                         that.close();
103                 }
104                 else {
105                         txt = tinyMCE.activeEditor.getContent();
106                         newtxt = txt.replace(that.searchText,t+' ');
107                         tinyMCE.activeEditor.setContent(newtxt);
108                         tinyMCE.activeEditor.focus();
109                         that.close();
110                 }
111         });
112         $(this.cont).append(elm);
113 }
114 ACPopup.prototype.onkey = function(event){
115         if (event.keyCode == '13') {
116                 if(this.idsel>-1) {
117                         this.cont.children()[this.idsel].click();
118                         event.preventDefault();
119                 }
120                 else
121                         this.close();
122         }
123         if (event.keyCode == '38') { //cursor up
124                 cmax = this.cont.children().size()-1;
125                 this.idsel--;
126                 if (this.idsel<0) this.idsel=cmax;
127                 event.preventDefault();
128         }
129         if (event.keyCode == '40' || event.keyCode == '9') { //cursor down
130                 cmax = this.cont.children().size()-1;
131                 this.idsel++;
132                 if (this.idsel>cmax) this.idsel=0;
133                 event.preventDefault();
134         }
135         
136         if (event.keyCode == '38' || event.keyCode == '40' || event.keyCode == '9') {
137                 this.cont.children().removeClass('selected');
138                 $(this.cont.children()[this.idsel]).addClass('selected');
139         }
140         
141         if (event.keyCode == '27') { //ESC
142                 this.close();
143         }
144 }
145
146 function ContactAutocomplete(element,backend_url){
147         this.pattern=/@([^ \n]+)$/;
148         this.popup=null;
149         var that = this;
150         
151         $(element).unbind('keydown');
152         $(element).unbind('keyup');
153         
154         $(element).keydown(function(event){
155                 if (that.popup!==null) that.popup.onkey(event);
156         });
157         
158         $(element).keyup(function(event){
159                 cpos = $(this).getSelection();
160                 if (cpos.start==cpos.end){
161                         match = $(this).val().substring(0,cpos.start).match(that.pattern);
162                         if (match!==null){
163                                 if (that.popup===null){
164                                         that.popup = new ACPopup(this, backend_url);
165                                 }
166                                 if (that.popup.ready && match[1]!==that.popup.searchText) that.popup.search(match[1]);
167                                 if (!that.popup.ready) that.popup=null;
168                                 
169                         } else {
170                                 if (that.popup!==null) {that.popup.close(); that.popup=null;}
171                         }
172                         
173                         
174                 }
175         });             
176         
177 }
178
179
180 /**
181  * jQuery plugin 'contact_autocomplete'
182  */
183 (function( $ ){
184   $.fn.contact_autocomplete = function(backend_url) {
185     this.each(function(){
186                 new ContactAutocomplete(this, backend_url);
187         });
188   };
189 })( jQuery );
190
191
192
193