2 * Friendica people autocomplete
4 * require jQuery, jquery.textareas
9 function ACPopup(elm,backend_url){
14 this.kp_timer = false;
15 this.url = backend_url;
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);
26 if(tinyMCE.activeEditor == null) {
27 style = $(elm).offset();
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();
41 style.top=style.top+h;
43 style.position = 'absolute';
44 /* style['max-height'] = '150px';
45 style.border = '1px solid red';
46 style.background = '#cccccc';
48 style.overflow = 'auto';
49 style['z-index'] = '100000';
51 style.display = 'none';
53 this.cont = $("<div class='acpopup'></div>");
56 $("body").append(this.cont);
58 ACPopup.prototype.close = function(){
59 $(this.cont).remove();
62 ACPopup.prototype.search = function(text){
65 if (this.kp_timer) clearTimeout(this.kp_timer);
66 this.kp_timer = setTimeout( function(){that._search();}, 500);
68 ACPopup.prototype._search = function(){
69 console.log("_search");
74 search:this.searchText,
76 conversation: this.conversation_id,
84 success:function(data){
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);
101 ACPopup.prototype.add = function(label, value){
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") {
108 sel = el.getSelection();
109 sel.start = sel.start- that.searchText.length;
110 el.setSelection(sel.start,sel.end).replaceSelectedText(t+' ').collapseSelection(false);
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();
122 $(this.cont).append(elm);
124 ACPopup.prototype.onkey = function(event){
125 if (event.keyCode == '13') {
127 this.cont.children()[this.idsel].click();
128 event.preventDefault();
133 if (event.keyCode == '38') { //cursor up
134 cmax = this.cont.children().size()-1;
136 if (this.idsel<0) this.idsel=cmax;
137 event.preventDefault();
139 if (event.keyCode == '40' || event.keyCode == '9') { //cursor down
140 cmax = this.cont.children().size()-1;
142 if (this.idsel>cmax) this.idsel=0;
143 event.preventDefault();
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');
151 if (event.keyCode == '27') { //ESC
156 function ContactAutocomplete(element,backend_url){
157 this.pattern=/@([^ \n]+)$/;
161 $(element).unbind('keydown');
162 $(element).unbind('keyup');
164 $(element).keydown(function(event){
165 if (that.popup!==null) that.popup.onkey(event);
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);
173 if (that.popup===null){
174 that.popup = new ACPopup(this, backend_url);
176 if (that.popup.ready && match[1]!==that.popup.searchText) that.popup.search(match[1]);
177 if (!that.popup.ready) that.popup=null;
180 if (that.popup!==null) {that.popup.close(); that.popup=null;}
191 * jQuery plugin 'contact_autocomplete'
194 $.fn.contact_autocomplete = function(backend_url) {
195 this.each(function(){
196 new ContactAutocomplete(this, backend_url);