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 if(typeof elm.editorId == "undefined") {
18 style = $(elm).offset();
23 style = $(elm.container).offset();
24 w = elm.container.offsetWidth;
25 h = elm.container.offsetHeight;
28 style.top=style.top+h;
30 style.position = 'absolute';
31 /* style['max-height'] = '150px';
32 style.border = '1px solid red';
33 style.background = '#cccccc';
35 style.overflow = 'auto';
36 style['z-index'] = '100000';
38 style.display = 'none';
40 this.cont = $("<div class='acpopup'></div>");
43 $("body").append(this.cont);
45 ACPopup.prototype.close = function(){
46 $(this.cont).remove();
49 ACPopup.prototype.search = function(text){
52 if (this.kp_timer) clearTimeout(this.kp_timer);
53 this.kp_timer = setTimeout( function(){that._search();}, 500);
55 ACPopup.prototype._search = function(){
56 console.log("_search");
61 search:this.searchText,
70 success:function(data){
74 $(data.items).each(function(){
75 html = "<img src='{0}' height='16px' width='16px'>{1} ({2})".format(this.photo, this.name, this.nick)
76 that.add(html, this.nick + '+' + this.id + ' - ' + this.link);
85 ACPopup.prototype.add = function(label, value){
87 var elm = $("<div class='acpopupitem' title='"+value+"'>"+label+"</div>");
88 elm.click(function(e){
89 t = $(this).attr('title').replace(new RegExp(' \- .*'),'');
90 if(typeof(that.element.container) === "undefined") {
92 sel = el.getSelection();
93 sel.start = sel.start- that.searchText.length;
94 el.setSelection(sel.start,sel.end).replaceSelectedText(t+' ').collapseSelection(false);
98 txt = tinyMCE.activeEditor.getContent();
99 newtxt = txt.replace(that.searchText,t+' ');
100 tinyMCE.activeEditor.setContent(newtxt);
101 tinyMCE.activeEditor.focus();
105 $(this.cont).append(elm);
107 ACPopup.prototype.onkey = function(event){
108 if (event.keyCode == '13' && this.idsel>-1) {
109 this.cont.children()[this.idsel].click();
110 event.preventDefault();
112 if (event.keyCode == '38') { //cursor up
113 cmax = this.cont.children().size()-1;
115 if (this.idsel<0) this.idsel=cmax;
116 event.preventDefault();
118 if (event.keyCode == '40') { //cursor down
119 cmax = this.cont.children().size()-1;
121 if (this.idsel>cmax) this.idsel=0;
122 event.preventDefault();
125 if (event.keyCode == '38' || event.keyCode == '40' ) {
126 this.cont.children().removeClass('selected');
127 $(this.cont.children()[this.idsel]).addClass('selected');
130 if (event.keyCode == '27') { //ESC
135 function ContactAutocomplete(element,backend_url){
136 this.pattern=/@([^ \n]+)$/;
140 $(element).unbind('keydown');
141 $(element).unbind('keyup');
143 $(element).keydown(function(event){
144 if (that.popup!==null) that.popup.onkey(event);
147 $(element).keyup(function(event){
148 cpos = $(this).getSelection();
149 if (cpos.start==cpos.end){
150 match = $(this).val().substring(0,cpos.start).match(that.pattern);
152 if (that.popup===null){
153 that.popup = new ACPopup(this, backend_url);
155 if (that.popup.ready && match[1]!==that.popup.searchText) that.popup.search(match[1]);
156 if (!that.popup.ready) that.popup=null;
159 if (that.popup!==null) {that.popup.close(); that.popup=null;}
170 * jQuery plugin 'contact_autocomplete'
173 $.fn.contact_autocomplete = function(backend_url) {
174 this.each(function(){
175 new ContactAutocomplete(this, backend_url);