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(typeof elm.editorId == "undefined") {
27 style = $(elm).offset();
32 var container = elm.getContainer();
33 if(typeof container != "undefined") {
34 style = $(container).offset();
35 w = $(container).width();
36 h = $(container).height();
40 style.top=style.top+h;
42 style.position = 'absolute';
43 /* style['max-height'] = '150px';
44 style.border = '1px solid red';
45 style.background = '#cccccc';
47 style.overflow = 'auto';
48 style['z-index'] = '100000';
50 style.display = 'none';
52 this.cont = $("<div class='acpopup'></div>");
55 $("body").append(this.cont);
57 ACPopup.prototype.close = function(){
58 $(this.cont).remove();
61 ACPopup.prototype.search = function(text){
64 if (this.kp_timer) clearTimeout(this.kp_timer);
65 this.kp_timer = setTimeout( function(){that._search();}, 500);
67 ACPopup.prototype._search = function(){
68 console.log("_search");
73 search:this.searchText,
75 conversation: this.conversation_id,
83 success:function(data){
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);
100 ACPopup.prototype.add = function(label, value){
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") {
107 sel = el.getSelection();
108 sel.start = sel.start- that.searchText.length;
109 el.setSelection(sel.start,sel.end).replaceSelectedText(t+' ').collapseSelection(false);
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();
121 $(this.cont).append(elm);
123 ACPopup.prototype.onkey = function(event){
124 if (event.keyCode == '13') {
126 this.cont.children()[this.idsel].click();
127 event.preventDefault();
132 if (event.keyCode == '38') { //cursor up
133 cmax = this.cont.children().size()-1;
135 if (this.idsel<0) this.idsel=cmax;
136 event.preventDefault();
138 if (event.keyCode == '40' || event.keyCode == '9') { //cursor down
139 cmax = this.cont.children().size()-1;
141 if (this.idsel>cmax) this.idsel=0;
142 event.preventDefault();
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');
150 if (event.keyCode == '27') { //ESC
155 function ContactAutocomplete(element,backend_url){
156 this.pattern=/@([^ \n]+)$/;
160 $(element).unbind('keydown');
161 $(element).unbind('keyup');
163 $(element).keydown(function(event){
164 if (that.popup!==null) that.popup.onkey(event);
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);
172 if (that.popup===null){
173 that.popup = new ACPopup(this, backend_url);
175 if (that.popup.ready && match[1]!==that.popup.searchText) that.popup.search(match[1]);
176 if (!that.popup.ready) that.popup=null;
179 if (that.popup!==null) {that.popup.close(); that.popup=null;}
190 * jQuery plugin 'contact_autocomplete'
193 $.fn.contact_autocomplete = function(backend_url) {
194 this.each(function(){
195 new ContactAutocomplete(this, backend_url);