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;
22 style.position = 'absolute';
23 /* style['max-height'] = '150px';
24 style.border = '1px solid red';
25 style.background = '#cccccc';
27 style.overflow = 'auto';
28 style['z-index'] = '100000';
30 style.display = 'none';
32 this.cont = $("<div class='acpopup'></div>");
35 $("body").append(this.cont);
37 ACPopup.prototype.close = function(){
38 $(this.cont).remove();
41 ACPopup.prototype.search = function(text){
44 if (this.kp_timer) clearTimeout(this.kp_timer);
45 this.kp_timer = setTimeout( function(){that._search();}, 500);
47 ACPopup.prototype._search = function(){
48 console.log("_search");
53 search:this.searchText,
62 success:function(data){
66 $(data.items).each(function(){
67 html = "<img src='{0}' height='16px' width='16px'>{1} ({2})".format(this.photo, this.name, this.nick)
68 that.add(html, this.nick + ' - ' + this.link);
77 ACPopup.prototype.add = function(label, value){
79 var elm = $("<div class='acpopupitem' title='"+value+"'>"+label+"</div>");
80 elm.click(function(e){
81 t = $(this).attr('title').replace(new RegExp(' \- .*'),'');
83 sel = el.getSelection();
84 sel.start = sel.start- that.searchText.length;
85 el.setSelection(sel.start,sel.end).replaceSelectedText(t).collapseSelection(false);
88 $(this.cont).append(elm);
90 ACPopup.prototype.onkey = function(event){
91 if (event.keyCode == '13' && this.idsel>-1) {
92 this.cont.children()[this.idsel].click();
93 event.preventDefault();
95 if (event.keyCode == '38') { //cursor up
96 cmax = this.cont.children().size()-1;
98 if (this.idsel<0) this.idsel=cmax;
99 event.preventDefault();
101 if (event.keyCode == '40') { //cursor down
102 cmax = this.cont.children().size()-1;
104 if (this.idsel>cmax) this.idsel=0;
105 event.preventDefault();
108 if (event.keyCode == '38' || event.keyCode == '40' ) {
109 this.cont.children().removeClass('selected');
110 $(this.cont.children()[this.idsel]).addClass('selected');
113 if (event.keyCode == '27') { //ESC
118 function ContactAutocomplete(element,backend_url){
119 this.pattern=/@([^ \n]+)$/;
123 $(element).unbind('keydown');
124 $(element).unbind('keyup');
126 $(element).keydown(function(event){
127 if (that.popup!==null) that.popup.onkey(event);
130 $(element).keyup(function(event){
131 cpos = $(this).getSelection();
132 if (cpos.start==cpos.end){
133 match = $(this).val().substring(0,cpos.start).match(that.pattern);
135 if (that.popup===null){
136 that.popup = new ACPopup(this, backend_url);
138 if (that.popup.ready && match[1]!==that.popup.searchText) that.popup.search(match[1]);
139 if (!that.popup.ready) that.popup=null;
142 if (that.popup!==null) {that.popup.close(); that.popup=null;}
153 * jQuery plugin 'contact_autocomplete'
156 $.fn.contact_autocomplete = function(backend_url) {
157 this.each(function(){
158 new ContactAutocomplete(this, backend_url);