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['smode'] = 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 tag_format(item) {
81 return "<div class='dropdown-item'>" + '#' + item.text + "</div>";
84 function editor_replace(item) {
85 if (typeof item.replace !== 'undefined') {
86 return '$1$2' + item.replace;
89 if (typeof item.addr !== 'undefined') {
90 return '$1$2' + item.addr + ' ';
93 // $2 ensures that prefix (@,@!) is preserved
96 // don't add the id if it is empty (the id empty eg. if there are unknow contacts in thread)
98 return '$1$2' + item.nick.replace(' ', '') + ' ';
100 // 16 chars of hash should be enough. Full hash could be used if it can be done in a visually appealing way.
101 // 16 chars is also the minimum length in the backend (otherwise it's interpreted as a local id).
102 if (id.length > 16) {
103 id = item.id.substring(0,16);
105 return '$1$2' + item.nick.replace(' ', '') + '+' + id + ' ';
108 function basic_replace(item) {
109 if(typeof item.replace !== 'undefined')
110 return '$1'+item.replace;
112 return '$1'+item.name+' ';
115 function webbie_replace(item) {
116 if(typeof item.replace !== 'undefined')
117 return '$1'+item.replace;
119 return '$1'+item.nick+' ';
122 function trim_replace(item) {
123 if(typeof item.replace !== 'undefined')
124 return '$1'+item.replace;
126 return '$1'+item.name;
130 function submit_form(e) {
131 $(e).parents('form').submit();
134 function getWord(text, caretPos) {
135 var index = text.indexOf(caretPos);
136 var postText = text.substring(caretPos, caretPos+8);
137 if ((postText.indexOf("[/list]") > 0) || postText.indexOf("[/ul]") > 0 || postText.indexOf("[/ol]") > 0) {
142 function getCaretPosition(ctrl) {
143 var CaretPos = 0; // IE Support
144 if (document.selection) {
146 var Sel = document.selection.createRange();
147 Sel.moveStart('character', -ctrl.value.length);
148 CaretPos = Sel.text.length;
151 else if (ctrl.selectionStart || ctrl.selectionStart == '0')
152 CaretPos = ctrl.selectionStart;
156 function setCaretPosition(ctrl, pos){
157 if(ctrl.setSelectionRange) {
159 ctrl.setSelectionRange(pos,pos);
161 else if (ctrl.createTextRange) {
162 var range = ctrl.createTextRange();
163 range.collapse(true);
164 range.moveEnd('character', pos);
165 range.moveStart('character', pos);
170 function listNewLineAutocomplete(id) {
171 var text = document.getElementById(id);
172 var caretPos = getCaretPosition(text)
173 var word = getWord(text.value, caretPos);
175 var textBefore = text.value.substring(0, caretPos);
176 var textAfter = text.value.substring(caretPos, text.length);
177 $('#' + id).val(textBefore + '\r\n[*] ' + textAfter).trigger('change');
178 setCaretPosition(text, caretPos + 5);
186 function string2bb(element) {
187 if(element == 'bold') return 'b';
188 else if(element == 'italic') return 'i';
189 else if(element == 'underline') return 'u';
190 else if(element == 'overline') return 'o';
191 else if(element == 'strike') return 's';
196 * jQuery plugin 'editor_autocomplete'
199 $.fn.editor_autocomplete = function(backend_url) {
201 // Autocomplete contacts
203 match: /(^|\s)(@\!*)([^ \n]+)$/,
205 search: function(term, callback) { contact_search(term, callback, backend_url, 'c'); },
206 replace: editor_replace,
207 template: contact_format,
210 // Autocomplete forums
212 match: /(^|\s)(!\!*)([^ \n]+)$/,
214 search: function(term, callback) { contact_search(term, callback, backend_url, 'f'); },
215 replace: editor_replace,
216 template: contact_format,
219 // Autocomplete hashtags
221 match: /(^|\s)(\#)([^ \n]{2,})$/,
223 search: function(term, callback) { $.getJSON(baseurl + '/hashtag/' + '?f=&t=' + term).done(function(data) { callback($.map(data, function(entry) { return entry.text.indexOf(term) === 0 ? entry : null; })); }); },
224 replace: function(item) { return "$1$2" + item.text + ' '; },
228 // Autocomplete smilies e.g. ":like"
230 match: /(^|\s)(:[a-z]{2,})$/,
232 search: function(term, callback) { $.getJSON('smilies/json').done(function(data) { callback($.map(data, function(entry) { return entry.text.indexOf(term) === 0 ? entry : null; })); }); },
233 template: function(item) { return item.icon + ' ' + item.text; },
234 replace: function(item) { return "$1" + item.text + ' '; },
237 this.attr('autocomplete','off');
238 this.textcomplete([contacts, forums, smilies, tags], {className:'acpopup', zIndex:10000});
243 * jQuery plugin 'search_autocomplete'
246 $.fn.search_autocomplete = function(backend_url) {
247 // Autocomplete contacts
249 match: /(^@)([^\n]{2,})$/,
251 search: function(term, callback) { contact_search(term, callback, backend_url, 'x', 'contact'); },
252 replace: webbie_replace,
253 template: contact_format,
256 // Autocomplete forum accounts
258 match: /(^!)([^\n]{2,})$/,
260 search: function(term, callback) { contact_search(term, callback, backend_url, 'x', 'community'); },
261 replace: webbie_replace,
262 template: contact_format,
265 // Autocomplete hashtags
267 match: /(^|\s)(\#)([^ \n]{2,})$/,
269 search: function(term, callback) { $.getJSON(baseurl + '/hashtag/' + '?f=&t=' + term).done(function(data) { callback($.map(data, function(entry) { return entry.text.indexOf(term) === 0 ? entry : null; })); }); },
270 replace: function(item) { return "$1$2" + item.text; },
274 this.attr('autocomplete', 'off');
275 var a = this.textcomplete([contacts, community, tags], {className:'acpopup', maxCount:100, zIndex: 10000, appendTo:'nav'});
276 a.on('textComplete:select', function(e, value, strategy) { submit_form(this); });
281 $.fn.contact_autocomplete = function(backend_url, typ, autosubmit, onselect) {
282 if(typeof typ === 'undefined') typ = '';
283 if(typeof autosubmit === 'undefined') autosubmit = false;
285 // Autocomplete contacts
287 match: /(^)([^\n]+)$/,
289 search: function(term, callback) { contact_search(term, callback, backend_url, typ); },
290 replace: basic_replace,
291 template: contact_format,
294 this.attr('autocomplete','off');
295 var a = this.textcomplete([contacts], {className:'acpopup', zIndex:10000});
298 a.on('textComplete:select', function(e,value,strategy) { submit_form(this); });
300 if(typeof onselect !== 'undefined')
301 a.on('textComplete:select', function(e, value, strategy) { onselect(value); });
307 $.fn.name_autocomplete = function(backend_url, typ, autosubmit, onselect) {
308 if(typeof typ === 'undefined') typ = '';
309 if(typeof autosubmit === 'undefined') autosubmit = false;
311 // Autocomplete contacts
313 match: /(^)([^\n]+)$/,
315 search: function(term, callback) { contact_search(term, callback, backend_url, typ); },
316 replace: trim_replace,
317 template: contact_format,
320 this.attr('autocomplete','off');
321 var a = this.textcomplete([names], {className:'acpopup', zIndex:10000});
324 a.on('textComplete:select', function(e,value,strategy) { submit_form(this); });
326 if(typeof onselect !== 'undefined')
327 a.on('textComplete:select', function(e, value, strategy) { onselect(value); });
332 $.fn.bbco_autocomplete = function(type) {
335 var open_close_elements = ['bold', 'italic', 'underline', 'overline', 'strike', 'quote', 'code', 'spoiler', 'map', 'img', 'url', 'audio', 'video', 'embed', 'youtube', 'vimeo', 'list', 'ul', 'ol', 'li', 'table', 'tr', 'th', 'td', 'center', 'color', 'font', 'size', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'nobb', 'noparse', 'pre', 'abstract'];
336 var open_elements = ['*', 'hr'];
338 var elements = open_close_elements.concat(open_elements);
342 match: /\[(\w*\**)$/,
343 search: function (term, callback) {
344 callback($.map(elements, function (element) {
345 return element.indexOf(term) === 0 ? element : null;
349 replace: function (element) {
350 element = string2bb(element);
351 if(open_elements.indexOf(element) < 0) {
352 if(element === 'list' || element === 'ol' || element === 'ul') {
353 return ['\[' + element + '\]' + '\n\[*\] ', '\n\[/' + element + '\]'];
355 else if(element === 'table') {
356 return ['\[' + element + '\]' + '\n\[tr\]', '\[/tr\]\n\[/' + element + '\]'];
359 return ['\[' + element + '\]', '\[/' + element + '\]'];
363 return '\[' + element + '\] ';
368 this.attr('autocomplete','off');
369 var a = this.textcomplete([bbco], {className:'acpopup', zIndex:10000});
371 a.on('textComplete:select', function(e, value, strategy) { value; });
373 a.keypress(function(e){
374 if (e.keyCode == 13) {
375 var x = listNewLineAutocomplete(this.id);
377 e.stopImmediatePropagation();
386 * Friendica people autocomplete legacy code
388 * require jQuery, jquery.textareas
390 function ACPopup(elm, backend_url){
393 this.searchText = '';
395 this.kp_timer = false;
396 this.url = backend_url;
398 this.conversation_id = null;
399 var conv_id = this.element.id.match(/\d+$/);
401 this.conversation_id = conv_id[0];
404 var w = $(elm).width();
405 var h = $(elm).height();
407 var style = $(elm).offset();
408 style.top = style.top + h;
410 style.position = 'absolute';
411 style.display = 'none';
413 this.cont = $('<div class="acpopup-mce"></div>');
414 this.cont.css(style);
416 $('body').append(this.cont);
419 ACPopup.prototype.close = function(){
420 $(this.cont).remove();
423 ACPopup.prototype.search = function(text){
425 this.searchText=text;
426 if (this.kp_timer) clearTimeout(this.kp_timer);
427 this.kp_timer = setTimeout( function(){that._search();}, 500);
430 ACPopup.prototype._search = function(){
431 console.log("_search");
436 search:this.searchText,
438 conversation: this.conversation_id,
446 success:function(data){
450 $(data.items).each(function(){
451 var html = "<img src='{0}' height='16px' width='16px'>{1} ({2})".format(this.photo, this.name, this.nick);
452 var nick = this.nick.replace(' ','');
453 if (this.id!=='') nick += '+' + this.id;
454 that.add(html, nick + ' - ' + this.link);
464 ACPopup.prototype.add = function(label, value){
466 var elm = $('<div class="acpopupitem" title="' + value + '">' + label + '</div>');
467 elm.click(function(e){
468 t = $(this).attr('title').replace(new RegExp(' \- .*'), '');
469 el = $(that.element);
470 sel = el.getSelection();
471 sel.start = sel.start - that.searchText.length;
472 el.setSelection(sel.start, sel.end).replaceSelectedText(t + ' ').collapseSelection(false);
475 $(this.cont).append(elm);
478 ACPopup.prototype.onkey = function(event){
479 if (event.keyCode == '13') {
480 if(this.idsel > -1) {
481 this.cont.children()[this.idsel].click();
482 event.preventDefault();
487 if (event.keyCode == '38') { //cursor up
488 var cmax = this.cont.children().size() - 1;
490 if (this.idsel < 0) {
493 event.preventDefault();
495 if (event.keyCode == '40' || event.keyCode == '9') { //cursor down
496 var cmax = this.cont.children().size() - 1;
498 if (this.idsel > cmax) {
501 event.preventDefault();
504 if (event.keyCode == '38' || event.keyCode == '40' || event.keyCode == '9') {
505 this.cont.children().removeClass('selected');
506 $(this.cont.children()[this.idsel]).addClass('selected');
509 if (event.keyCode == '27') { //ESC