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;
26 // Quick fix for chrome until I get a tool to inspect the dom
35 style.top=style.top+h;
37 style.position = 'absolute';
38 /* style['max-height'] = '150px';
39 style.border = '1px solid red';
40 style.background = '#cccccc';
42 style.overflow = 'auto';
43 style['z-index'] = '100000';
45 style.display = 'none';
47 this.cont = $("<div class='acpopup'></div>");
50 $("body").append(this.cont);
52 ACPopup.prototype.close = function(){
53 $(this.cont).remove();
56 ACPopup.prototype.search = function(text){
59 if (this.kp_timer) clearTimeout(this.kp_timer);
60 this.kp_timer = setTimeout( function(){that._search();}, 500);
62 ACPopup.prototype._search = function(){
63 console.log("_search");
68 search:this.searchText,
77 success:function(data){
81 $(data.items).each(function(){
82 html = "<img src='{0}' height='16px' width='16px'>{1} ({2})".format(this.photo, this.name, this.nick)
83 that.add(html, this.nick.replace(' ','') + '+' + this.id + ' - ' + this.link);
92 ACPopup.prototype.add = function(label, value){
94 var elm = $("<div class='acpopupitem' title='"+value+"'>"+label+"</div>");
95 elm.click(function(e){
96 t = $(this).attr('title').replace(new RegExp(' \- .*'),'');
97 if(typeof(that.element.container) === "undefined") {
99 sel = el.getSelection();
100 sel.start = sel.start- that.searchText.length;
101 el.setSelection(sel.start,sel.end).replaceSelectedText(t+' ').collapseSelection(false);
105 txt = tinyMCE.activeEditor.getContent();
106 newtxt = txt.replace(that.searchText,t+' ');
107 tinyMCE.activeEditor.setContent(newtxt);
108 tinyMCE.activeEditor.focus();
112 $(this.cont).append(elm);
114 ACPopup.prototype.onkey = function(event){
115 if (event.keyCode == '13') {
117 this.cont.children()[this.idsel].click();
118 event.preventDefault();
123 if (event.keyCode == '38') { //cursor up
124 cmax = this.cont.children().size()-1;
126 if (this.idsel<0) this.idsel=cmax;
127 event.preventDefault();
129 if (event.keyCode == '40' || event.keyCode == '9') { //cursor down
130 cmax = this.cont.children().size()-1;
132 if (this.idsel>cmax) this.idsel=0;
133 event.preventDefault();
136 if (event.keyCode == '38' || event.keyCode == '40' || event.keyCode == '9') {
137 this.cont.children().removeClass('selected');
138 $(this.cont.children()[this.idsel]).addClass('selected');
141 if (event.keyCode == '27') { //ESC
146 function ContactAutocomplete(element,backend_url){
147 this.pattern=/@([^ \n]+)$/;
151 $(element).unbind('keydown');
152 $(element).unbind('keyup');
154 $(element).keydown(function(event){
155 if (that.popup!==null) that.popup.onkey(event);
158 $(element).keyup(function(event){
159 cpos = $(this).getSelection();
160 if (cpos.start==cpos.end){
161 match = $(this).val().substring(0,cpos.start).match(that.pattern);
163 if (that.popup===null){
164 that.popup = new ACPopup(this, backend_url);
166 if (that.popup.ready && match[1]!==that.popup.searchText) that.popup.search(match[1]);
167 if (!that.popup.ready) that.popup=null;
170 if (that.popup!==null) {that.popup.close(); that.popup=null;}
181 * jQuery plugin 'contact_autocomplete'
184 $.fn.contact_autocomplete = function(backend_url) {
185 this.each(function(){
186 new ContactAutocomplete(this, backend_url);