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 editor_replace(item) {
81 if (typeof item.replace !== 'undefined') {
82 return '$1$2' + item.replace;
85 if (typeof item.addr !== 'undefined') {
86 return '$1$2' + item.addr + ' ';
89 // $2 ensures that prefix (@,@!) is preserved
92 // don't add the id if it is empty (the id empty eg. if there are unknow contacts in thread)
94 return '$1$2' + item.nick.replace(' ', '') + ' ';
96 // 16 chars of hash should be enough. Full hash could be used if it can be done in a visually appealing way.
97 // 16 chars is also the minimum length in the backend (otherwise it's interpreted as a local id).
99 id = item.id.substring(0,16);
101 return '$1$2' + item.nick.replace(' ', '') + '+' + id + ' ';
104 function basic_replace(item) {
105 if(typeof item.replace !== 'undefined')
106 return '$1'+item.replace;
108 return '$1'+item.name+' ';
111 function webbie_replace(item) {
112 if(typeof item.replace !== 'undefined')
113 return '$1'+item.replace;
115 return '$1'+item.nick+' ';
118 function trim_replace(item) {
119 if(typeof item.replace !== 'undefined')
120 return '$1'+item.replace;
122 return '$1'+item.name;
126 function submit_form(e) {
127 $(e).parents('form').submit();
130 function getWord(text, caretPos) {
131 var index = text.indexOf(caretPos);
132 var postText = text.substring(caretPos, caretPos+8);
133 if ((postText.indexOf("[/list]") > 0) || postText.indexOf("[/ul]") > 0 || postText.indexOf("[/ol]") > 0) {
138 function getCaretPosition(ctrl) {
139 var CaretPos = 0; // IE Support
140 if (document.selection) {
142 var Sel = document.selection.createRange();
143 Sel.moveStart('character', -ctrl.value.length);
144 CaretPos = Sel.text.length;
147 else if (ctrl.selectionStart || ctrl.selectionStart == '0')
148 CaretPos = ctrl.selectionStart;
152 function setCaretPosition(ctrl, pos){
153 if(ctrl.setSelectionRange) {
155 ctrl.setSelectionRange(pos,pos);
157 else if (ctrl.createTextRange) {
158 var range = ctrl.createTextRange();
159 range.collapse(true);
160 range.moveEnd('character', pos);
161 range.moveStart('character', pos);
166 function listNewLineAutocomplete(id) {
167 var text = document.getElementById(id);
168 var caretPos = getCaretPosition(text)
169 var word = getWord(text.value, caretPos);
171 var textBefore = text.value.substring(0, caretPos);
172 var textAfter = text.value.substring(caretPos, text.length);
173 $('#' + id).val(textBefore + '\r\n[*] ' + textAfter).trigger('change');
174 setCaretPosition(text, caretPos + 5);
182 function string2bb(element) {
183 if(element == 'bold') return 'b';
184 else if(element == 'italic') return 'i';
185 else if(element == 'underline') return 'u';
186 else if(element == 'overline') return 'o';
187 else if(element == 'strike') return 's';
192 * jQuery plugin 'editor_autocomplete'
195 $.fn.editor_autocomplete = function(backend_url) {
197 // Autocomplete contacts
199 match: /(^|\s)(@\!*)([^ \n]+)$/,
201 search: function(term, callback) { contact_search(term, callback, backend_url, 'c'); },
202 replace: editor_replace,
203 template: contact_format,
206 // Autocomplete forums
208 match: /(^|\s)(!\!*)([^ \n]+)$/,
210 search: function(term, callback) { contact_search(term, callback, backend_url, 'f'); },
211 replace: editor_replace,
212 template: contact_format,
215 // Autocomplete smilies e.g. ":like"
217 match: /(^|\s)(:[a-z]{2,})$/,
219 search: function(term, callback) { $.getJSON('smilies/json').done(function(data) { callback($.map(data, function(entry) { return entry.text.indexOf(term) === 0 ? entry : null; })); }); },
220 template: function(item) { return item.icon + ' ' + item.text; },
221 replace: function(item) { return "$1" + item.text + ' '; },
224 this.attr('autocomplete','off');
225 this.textcomplete([contacts, forums, smilies], {className:'acpopup', zIndex:10000});
230 * jQuery plugin 'search_autocomplete'
233 $.fn.search_autocomplete = function(backend_url) {
234 // Autocomplete contacts
236 match: /(^@)([^\n]{2,})$/,
238 search: function(term, callback) { contact_search(term, callback, backend_url, 'x', 'contact'); },
239 replace: webbie_replace,
240 template: contact_format,
243 // Autocomplete forum accounts
245 match: /(^!)([^\n]{2,})$/,
247 search: function(term, callback) { contact_search(term, callback, backend_url, 'x', 'community'); },
248 replace: webbie_replace,
249 template: contact_format,
251 this.attr('autocomplete', 'off');
252 var a = this.textcomplete([contacts, community], {className:'acpopup', maxCount:100, zIndex: 10000, appendTo:'nav'});
253 a.on('textComplete:select', function(e, value, strategy) { submit_form(this); });
258 $.fn.contact_autocomplete = function(backend_url, typ, autosubmit, onselect) {
259 if(typeof typ === 'undefined') typ = '';
260 if(typeof autosubmit === 'undefined') autosubmit = false;
262 // Autocomplete contacts
264 match: /(^)([^\n]+)$/,
266 search: function(term, callback) { contact_search(term, callback, backend_url, typ); },
267 replace: basic_replace,
268 template: contact_format,
271 this.attr('autocomplete','off');
272 var a = this.textcomplete([contacts], {className:'acpopup', zIndex:10000});
275 a.on('textComplete:select', function(e,value,strategy) { submit_form(this); });
277 if(typeof onselect !== 'undefined')
278 a.on('textComplete:select', function(e, value, strategy) { onselect(value); });
284 $.fn.name_autocomplete = function(backend_url, typ, autosubmit, onselect) {
285 if(typeof typ === 'undefined') typ = '';
286 if(typeof autosubmit === 'undefined') autosubmit = false;
288 // Autocomplete contacts
290 match: /(^)([^\n]+)$/,
292 search: function(term, callback) { contact_search(term, callback, backend_url, typ); },
293 replace: trim_replace,
294 template: contact_format,
297 this.attr('autocomplete','off');
298 var a = this.textcomplete([names], {className:'acpopup', zIndex:10000});
301 a.on('textComplete:select', function(e,value,strategy) { submit_form(this); });
303 if(typeof onselect !== 'undefined')
304 a.on('textComplete:select', function(e, value, strategy) { onselect(value); });
309 $.fn.bbco_autocomplete = function(type) {
312 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'];
313 var open_elements = ['*', 'hr'];
315 var elements = open_close_elements.concat(open_elements);
319 match: /\[(\w*\**)$/,
320 search: function (term, callback) {
321 callback($.map(elements, function (element) {
322 return element.indexOf(term) === 0 ? element : null;
326 replace: function (element) {
327 element = string2bb(element);
328 if(open_elements.indexOf(element) < 0) {
329 if(element === 'list' || element === 'ol' || element === 'ul') {
330 return ['\[' + element + '\]' + '\n\[*\] ', '\n\[/' + element + '\]'];
332 else if(element === 'table') {
333 return ['\[' + element + '\]' + '\n\[tr\]', '\[/tr\]\n\[/' + element + '\]'];
336 return ['\[' + element + '\]', '\[/' + element + '\]'];
340 return '\[' + element + '\] ';
345 this.attr('autocomplete','off');
346 var a = this.textcomplete([bbco], {className:'acpopup', zIndex:10000});
348 a.on('textComplete:select', function(e, value, strategy) { value; });
350 a.keypress(function(e){
351 if (e.keyCode == 13) {
352 var x = listNewLineAutocomplete(this.id);
354 e.stopImmediatePropagation();
363 * Friendica people autocomplete legacy code
365 * require jQuery, jquery.textareas
367 function ACPopup(elm, backend_url){
370 this.searchText = '';
372 this.kp_timer = false;
373 this.url = backend_url;
375 this.conversation_id = null;
376 var conv_id = this.element.id.match(/\d+$/);
378 this.conversation_id = conv_id[0];
381 var w = $(elm).width();
382 var h = $(elm).height();
384 var style = $(elm).offset();
385 style.top = style.top + h;
387 style.position = 'absolute';
388 style.display = 'none';
390 this.cont = $('<div class="acpopup-mce"></div>');
391 this.cont.css(style);
393 $('body').append(this.cont);
396 ACPopup.prototype.close = function(){
397 $(this.cont).remove();
400 ACPopup.prototype.search = function(text){
402 this.searchText=text;
403 if (this.kp_timer) clearTimeout(this.kp_timer);
404 this.kp_timer = setTimeout( function(){that._search();}, 500);
407 ACPopup.prototype._search = function(){
408 console.log("_search");
413 search:this.searchText,
415 conversation: this.conversation_id,
423 success:function(data){
427 $(data.items).each(function(){
428 var html = "<img src='{0}' height='16px' width='16px'>{1} ({2})".format(this.photo, this.name, this.nick);
429 var nick = this.nick.replace(' ','');
430 if (this.id!=='') nick += '+' + this.id;
431 that.add(html, nick + ' - ' + this.link);
441 ACPopup.prototype.add = function(label, value){
443 var elm = $('<div class="acpopupitem" title="' + value + '">' + label + '</div>');
444 elm.click(function(e){
445 t = $(this).attr('title').replace(new RegExp(' \- .*'), '');
446 el = $(that.element);
447 sel = el.getSelection();
448 sel.start = sel.start - that.searchText.length;
449 el.setSelection(sel.start, sel.end).replaceSelectedText(t + ' ').collapseSelection(false);
452 $(this.cont).append(elm);
455 ACPopup.prototype.onkey = function(event){
456 if (event.keyCode == '13') {
457 if(this.idsel > -1) {
458 this.cont.children()[this.idsel].click();
459 event.preventDefault();
464 if (event.keyCode == '38') { //cursor up
465 var cmax = this.cont.children().size() - 1;
467 if (this.idsel < 0) {
470 event.preventDefault();
472 if (event.keyCode == '40' || event.keyCode == '9') { //cursor down
473 var cmax = this.cont.children().size() - 1;
475 if (this.idsel > cmax) {
478 event.preventDefault();
481 if (event.keyCode == '38' || event.keyCode == '40' || event.keyCode == '9') {
482 this.cont.children().removeClass('selected');
483 $(this.cont.children()[this.idsel]).addClass('selected');
486 if (event.keyCode == '27') { //ESC