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