2 * @brief Friendica people autocomplete
4 * require jQuery, jquery.textcomplete
6 * for further documentation look at:
7 * http://yuku-t.com/jquery-textcomplete/
9 * https://github.com/yuku-t/jquery-textcomplete/blob/master/doc/how_to_use.md
13 function contact_search(term, callback, backend_url, type, mode) {
15 // Check if there is a conversation id to include the unkonwn contacts of the conversation
16 var conv_id = document.activeElement.id.match(/\d+$/);
18 // Check if there is a cached result that contains the same information we would get with a full server-side search
19 var bt = backend_url+type;
20 if(!(bt in contact_search.cache)) contact_search.cache[bt] = {};
22 var lterm = term.toLowerCase(); // Ignore case
23 for(var t in contact_search.cache[bt]) {
24 if(lterm.indexOf(t) >= 0) { // A more broad search has been performed already, so use those results
25 // Filter old results locally
26 var matching = contact_search.cache[bt][t].filter(function (x) { return (x.name.toLowerCase().indexOf(lterm) >= 0 || (typeof x.nick !== 'undefined' && x.nick.toLowerCase().indexOf(lterm) >= 0)); }); // Need to check that nick exists because groups don't have one
27 matching.unshift({forum:false, text: term, replace: term});
28 setTimeout(function() { callback(matching); } , 1); // Use "pseudo-thread" to avoid some problems
41 postdata['conversation'] = conv_id[0];
44 postdata['mode'] = mode;
52 success: function(data){
53 // Cache results if we got them all (more information would not improve results)
54 // data.count represents the maximum number of items
55 if(data.items.length -1 < data.count) {
56 contact_search.cache[bt][lterm] = data.items;
58 var items = data.items.slice(0);
59 items.unshift({taggable:false, text: term, replace: term});
62 }).fail(function () {callback([]); }); // Callback must be invoked even if something went wrong.
64 contact_search.cache = {};
67 function contact_format(item) {
68 // Show contact information if not explicitly told to show something else
69 if(typeof item.text === 'undefined') {
70 var desc = ((item.label) ? item.nick + ' ' + item.label : item.nick);
71 var forum = ((item.forum) ? 'forum' : '');
72 if(typeof desc === 'undefined') desc = '';
73 if(desc) desc = ' ('+desc+')';
74 return "<div class='{0}' title='{4}'><img class='acpopup-img' src='{1}'><span class='acpopup-contactname'>{2}</span><span class='acpopup-sub-text'>{3}</span><div class='clear'></div></div>".format(forum, item.photo, item.name, desc, item.link);
77 return "<div>" + item.text + "</div>";
80 function editor_replace(item) {
81 if(typeof item.replace !== 'undefined') {
82 return '$1$2' + item.replace;
85 // $2 ensures that prefix (@,@!) is preserved
88 // don't add the id if it is empty (the id empty eg. if there are unknow contacts in thread)
90 return '$1$2' + item.nick;
92 // 16 chars of hash should be enough. Full hash could be used if it can be done in a visually appealing way.
93 // 16 chars is also the minimum length in the backend (otherwise it's interpreted as a local id).
95 id = item.id.substring(0,16);
97 return '$1$2' + item.nick.replace(' ', '') + '+' + id + ' ';
100 function basic_replace(item) {
101 if(typeof item.replace !== 'undefined')
102 return '$1'+item.replace;
104 return '$1'+item.name+' ';
107 function trim_replace(item) {
108 if(typeof item.replace !== 'undefined')
109 return '$1'+item.replace;
111 return '$1'+item.name;
115 function submit_form(e) {
116 $(e).parents('form').submit();
119 function getWord(text, caretPos) {
120 var index = text.indexOf(caretPos);
121 var postText = text.substring(caretPos, caretPos+8);
122 if ((postText.indexOf("[/list]") > 0) || postText.indexOf("[/ul]") > 0 || postText.indexOf("[/ol]") > 0) {
127 function getCaretPosition(ctrl) {
128 var CaretPos = 0; // IE Support
129 if (document.selection) {
131 var Sel = document.selection.createRange();
132 Sel.moveStart('character', -ctrl.value.length);
133 CaretPos = Sel.text.length;
136 else if (ctrl.selectionStart || ctrl.selectionStart == '0')
137 CaretPos = ctrl.selectionStart;
141 function setCaretPosition(ctrl, pos){
142 if(ctrl.setSelectionRange) {
144 ctrl.setSelectionRange(pos,pos);
146 else if (ctrl.createTextRange) {
147 var range = ctrl.createTextRange();
148 range.collapse(true);
149 range.moveEnd('character', pos);
150 range.moveStart('character', pos);
155 function listNewLineAutocomplete(id) {
156 var text = document.getElementById(id);
157 var caretPos = getCaretPosition(text)
158 var word = getWord(text.value, caretPos);
160 var textBefore = text.value.substring(0, caretPos);
161 var textAfter = text.value.substring(caretPos, text.length);
162 $('#' + id).val(textBefore + '\r\n[*] ' + textAfter);
163 setCaretPosition(text, caretPos + 5);
168 function string2bb(element) {
169 if(element == 'bold') return 'b';
170 else if(element == 'italic') return 'i';
171 else if(element == 'underline') return 'u';
172 else if(element == 'overline') return 'o';
173 else if(element == 'strike') return 's';
178 * jQuery plugin 'editor_autocomplete'
181 $.fn.editor_autocomplete = function(backend_url) {
183 // Autocomplete contacts
185 match: /(^|\s)(@\!*)([^ \n]+)$/,
187 search: function(term, callback) { contact_search(term, callback, backend_url, 'c'); },
188 replace: editor_replace,
189 template: contact_format,
192 // Autocomplete smilies e.g. ":like"
194 match: /(^|\s)(:[a-z]{2,})$/,
196 search: function(term, callback) { $.getJSON('smilies/json').done(function(data) { callback($.map(data, function(entry) { return entry.text.indexOf(term) === 0 ? entry : null; })); }); },
197 template: function(item) { return item.icon + ' ' + item.text; },
198 replace: function(item) { return "$1" + item.text + ' '; },
201 this.attr('autocomplete','off');
202 this.textcomplete([contacts,smilies], {className:'acpopup', zIndex:10000});
207 * jQuery plugin 'search_autocomplete'
210 $.fn.search_autocomplete = function(backend_url) {
211 // Autocomplete contacts
213 match: /(^@)([^\n]{2,})$/,
215 search: function(term, callback) { contact_search(term, callback, backend_url, 'x', 'contact'); },
216 replace: basic_replace,
217 template: contact_format,
220 // Autocomplete forum accounts
222 match: /(^!)([^\n]{2,})$/,
224 search: function(term, callback) { contact_search(term, callback, backend_url, 'x', 'community'); },
225 replace: basic_replace,
226 template: contact_format,
228 this.attr('autocomplete', 'off');
229 var a = this.textcomplete([contacts, community], {className:'acpopup', maxCount:100, zIndex: 10000, appendTo:'nav'});
230 a.on('textComplete:select', function(e, value, strategy) { submit_form(this); });
235 $.fn.contact_autocomplete = function(backend_url, typ, autosubmit, onselect) {
236 if(typeof typ === 'undefined') typ = '';
237 if(typeof autosubmit === 'undefined') autosubmit = false;
239 // Autocomplete contacts
241 match: /(^)([^\n]+)$/,
243 search: function(term, callback) { contact_search(term, callback, backend_url, typ); },
244 replace: basic_replace,
245 template: contact_format,
248 this.attr('autocomplete','off');
249 var a = this.textcomplete([contacts], {className:'acpopup', zIndex:10000});
252 a.on('textComplete:select', function(e,value,strategy) { submit_form(this); });
254 if(typeof onselect !== 'undefined')
255 a.on('textComplete:select', function(e, value, strategy) { onselect(value); });
261 $.fn.name_autocomplete = function(backend_url, typ, autosubmit, onselect) {
262 if(typeof typ === 'undefined') typ = '';
263 if(typeof autosubmit === 'undefined') autosubmit = false;
265 // Autocomplete contacts
267 match: /(^)([^\n]+)$/,
269 search: function(term, callback) { contact_search(term, callback, backend_url, typ); },
270 replace: trim_replace,
271 template: contact_format,
274 this.attr('autocomplete','off');
275 var a = this.textcomplete([names], {className:'acpopup', zIndex:10000});
278 a.on('textComplete:select', function(e,value,strategy) { submit_form(this); });
280 if(typeof onselect !== 'undefined')
281 a.on('textComplete:select', function(e, value, strategy) { onselect(value); });
286 $.fn.bbco_autocomplete = function(type) {
289 var open_close_elements = ['bold', 'italic', 'underline', 'overline', 'strike', 'quote', 'code', 'spoiler', 'map', 'nobb', 'list', 'ul', 'ol', 'li', 'table', 'tr', 'th', 'td', 'center', 'color', 'font', 'size'];
290 var open_elements = ['*', 'hr'];
292 var elements = open_close_elements.concat(open_elements);
296 match: /\[(\w*\**)$/,
297 search: function (term, callback) {
298 callback($.map(elements, function (element) {
299 return element.indexOf(term) === 0 ? element : null;
303 replace: function (element) {
304 element = string2bb(element);
305 if(open_elements.indexOf(element) < 0) {
306 if(element === 'list' || element === 'ol' || element === 'ul') {
307 return ['\[' + element + '\]' + '\n\[*\] ', '\n\[/' + element + '\]'];
309 else if(element === 'table') {
310 return ['\[' + element + '\]' + '\n\[tr\]', '\[/tr\]\n\[/' + element + '\]'];
313 return ['\[' + element + '\]', '\[/' + element + '\]'];
317 return '\[' + element + '\] ';
322 this.attr('autocomplete','off');
323 var a = this.textcomplete([bbco], {className:'acpopup', zIndex:10000});
325 a.on('textComplete:select', function(e, value, strategy) { value; });
327 a.keypress(function(e){
328 e.stopImmediatePropagation();
329 if (e.keyCode == 13) {
330 var x = listNewLineAutocomplete(this.id);
339 * Friendica people autocomplete legacy
340 * code which is needed for tinymce
342 * require jQuery, jquery.textareas
345 function ACPopup(elm,backend_url){
350 this.kp_timer = false;
351 this.url = backend_url;
353 this.conversation_id = null;
354 var conv_id = this.element.id.match(/\d+$/);
355 if (conv_id) this.conversation_id = conv_id[0];
356 console.log("ACPopup elm id",this.element.id,"conversation",this.conversation_id);
362 if(tinyMCE.activeEditor == null) {
363 style = $(elm).offset();
368 // I can't find an "official" way to get the element who get all
369 // this fraking thing that is tinyMCE.
370 // This code will broke again at some point...
371 var container = $(tinyMCE.activeEditor.getContainer()).find("table");
372 style = $(container).offset();
373 w = $(container).width();
374 h = $(container).height();
377 style.top=style.top+h;
379 style.position = 'absolute';
380 /* style['max-height'] = '150px';
381 style.border = '1px solid red';
382 style.background = '#cccccc';
384 style.overflow = 'auto';
385 style['z-index'] = '100000';
387 style.display = 'none';
389 this.cont = $("<div class='acpopup-mce'></div>");
390 this.cont.css(style);
392 $("body").append(this.cont);
395 ACPopup.prototype.close = function(){
396 $(this.cont).remove();
399 ACPopup.prototype.search = function(text){
401 this.searchText=text;
402 if (this.kp_timer) clearTimeout(this.kp_timer);
403 this.kp_timer = setTimeout( function(){that._search();}, 500);
406 ACPopup.prototype._search = function(){
407 console.log("_search");
412 search:this.searchText,
414 conversation: this.conversation_id,
422 success:function(data){
426 $(data.items).each(function(){
427 var html = "<img src='{0}' height='16px' width='16px'>{1} ({2})".format(this.photo, this.name, this.nick);
428 var nick = this.nick.replace(' ','');
429 if (this.id!=='') nick += '+' + this.id;
430 that.add(html, nick + ' - ' + this.link);
440 ACPopup.prototype.add = function(label, value){
442 var elm = $("<div class='acpopupitem' title='"+value+"'>"+label+"</div>");
443 elm.click(function(e){
444 t = $(this).attr('title').replace(new RegExp(' \- .*'),'');
445 if(typeof(that.element.container) === "undefined") {
447 sel = el.getSelection();
448 sel.start = sel.start- that.searchText.length;
449 el.setSelection(sel.start,sel.end).replaceSelectedText(t+' ').collapseSelection(false);
453 txt = tinyMCE.activeEditor.getContent();
454 // alert(that.searchText + ':' + t);
455 newtxt = txt.replace('@' + that.searchText,'@' + t +' ');
456 tinyMCE.activeEditor.setContent(newtxt);
457 tinyMCE.activeEditor.focus();
461 $(this.cont).append(elm);
464 ACPopup.prototype.onkey = function(event){
465 if (event.keyCode == '13') {
467 this.cont.children()[this.idsel].click();
468 event.preventDefault();
473 if (event.keyCode == '38') { //cursor up
474 cmax = this.cont.children().size()-1;
476 if (this.idsel<0) this.idsel=cmax;
477 event.preventDefault();
479 if (event.keyCode == '40' || event.keyCode == '9') { //cursor down
480 cmax = this.cont.children().size()-1;
482 if (this.idsel>cmax) this.idsel=0;
483 event.preventDefault();
486 if (event.keyCode == '38' || event.keyCode == '40' || event.keyCode == '9') {
487 this.cont.children().removeClass('selected');
488 $(this.cont.children()[this.idsel]).addClass('selected');
491 if (event.keyCode == '27') { //ESC