2 * Friendika people autocomplete
4 * require jQuery, jquery.textareas
9 function ACPopup(elm,backend_url){
14 this.kp_timer = false;
15 this.url = backend_url;
17 style = $(elm).offset();
20 style.top=style.top+h;
21 style['max-height'] = '150px';
23 style.border = '1px solid red';
24 style.background = '#cccccc';
25 style.position = 'absolute';
26 style.overflow = 'auto';
27 style['z-index'] = '100000';
28 style.display = 'none';
30 this.cont = $("<div class='acpopup'></div>");
33 $(elm).after(this.cont);
35 ACPopup.prototype.close = function(){
36 $(this.cont).remove();
39 ACPopup.prototype.search = function(text){
42 if (this.kp_timer) clearTimeout(this.kp_timer);
43 this.kp_timer = setTimeout( function(){that._search();}, 1000);
45 ACPopup.prototype._search = function(){
46 console.log("_search");
51 search:this.searchText,
60 success:function(data){
64 $(data.items).each(function(){
65 html = "<img src='{0}' height='16px' width='16px'>{1} ({2})".format(this.photo, this.name, this.nick)
66 that.add(html, this.nick);
75 ACPopup.prototype.add = function(label, value){
77 var elm = $("<div class='acpopupitem' title='"+value+"'>"+label+"</div>");
78 elm.click(function(e){
79 t = $(this).attr('title');
81 sel = el.getSelection();
82 sel.start = sel.start- that.searchText.length;
83 el.setSelection(sel.start,sel.end).replaceSelectedText(t).collapseSelection(false);
86 $(this.cont).append(elm);
88 ACPopup.prototype.onkey = function(event){
89 if (event.keyCode == '13' && this.idsel>-1) {
90 this.cont.children()[this.idsel].click();
91 event.preventDefault();
93 if (event.keyCode == '38') { //cursor up
94 cmax = this.cont.children().size()-1;
96 if (this.idsel<0) this.idsel=cmax;
97 event.preventDefault();
99 if (event.keyCode == '40') { //cursor down
100 cmax = this.cont.children().size()-1;
102 if (this.idsel>cmax) this.idsel=0;
103 event.preventDefault();
106 if (event.keyCode == '38' || event.keyCode == '40' ) {
107 this.cont.children().css({background:'#cccccc'}).removeClass('selected');
108 $(this.cont.children()[this.idsel]).css({background:'#ccccff'}).addClass('selected');
111 if (event.keyCode == '27') { //ESC
116 function ContactAutocomplete(element,backend_url){
117 this.pattern=/@([^ \n]+)$/;
121 $(element).keydown(function(event){
122 if (that.popup!==null) that.popup.onkey(event);
125 $(element).keyup(function(event){
126 cpos = $(this).getSelection();
127 if (cpos.start==cpos.end){
128 match = $(this).val().substring(0,cpos.start).match(that.pattern);
130 if (that.popup===null){
131 that.popup = new ACPopup(this, backend_url);
133 if (that.popup.ready && match[1]!==that.popup.searchText) that.popup.search(match[1]);
134 if (!that.popup.ready) that.popup=null;
137 if (that.popup!==null) {that.popup.close(); that.popup=null;}
147 * jQuery plugin 'contact_autocomplete'
150 $.fn.contact_autocomplete = function(backend_url) {
151 this.each(function(){
152 new ContactAutocomplete(this, backend_url);