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