1 // @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt Expat
3 * Friendica people autocomplete
5 * require jQuery, jquery.textcomplete
7 * for further documentation look at:
8 * http://yuku-t.com/jquery-textcomplete/
10 * https://github.com/yuku-t/jquery-textcomplete/blob/master/doc/how_to_use.md
14 function contact_search(term, callback, backend_url, type, mode) {
16 // Check if there is a conversation id to include the unkonwn contacts of the conversation
17 var conv_id = document.activeElement.id.match(/\d+$/);
19 // Check if there is a cached result that contains the same information we would get with a full server-side search
20 var bt = backend_url+type;
21 if(!(bt in contact_search.cache)) contact_search.cache[bt] = {};
23 var lterm = term.toLowerCase(); // Ignore case
24 for(var t in contact_search.cache[bt]) {
25 if(lterm.indexOf(t) >= 0) { // A more broad search has been performed already, so use those results
26 // Filter old results locally
27 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
28 matching.unshift({forum:false, text: term, replace: term});
29 setTimeout(function() { callback(matching); } , 1); // Use "pseudo-thread" to avoid some problems
42 postdata['conversation'] = conv_id[0];
45 postdata['smode'] = mode;
53 success: function(data){
54 // Cache results if we got them all (more information would not improve results)
55 // data.count represents the maximum number of items
56 if(data.items.length -1 < data.count) {
57 contact_search.cache[bt][lterm] = data.items;
59 var items = data.items.slice(0);
60 items.unshift({taggable:false, text: term, replace: term});
63 }).fail(function () {callback([]); }); // Callback must be invoked even if something went wrong.
65 contact_search.cache = {};
68 function contact_format(item) {
69 // Show contact information if not explicitly told to show something else
70 if(typeof item.text === 'undefined') {
71 var desc = ((item.label) ? item.nick + ' ' + item.label : item.nick);
72 var forum = ((item.forum) ? 'forum' : '');
73 if(typeof desc === 'undefined') desc = '';
74 if(desc) desc = ' ('+desc+')';
75 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);
78 return "<div>" + item.text + "</div>";
81 function tag_format(item) {
82 return "<div class='dropdown-item'>" + '#' + item.text + "</div>";
85 function editor_replace(item) {
86 if (typeof item.replace !== 'undefined') {
87 return '$1$2' + item.replace;
90 if (typeof item.addr !== 'undefined') {
91 return '$1$2' + item.addr + ' ';
94 // $2 ensures that prefix (@,@!) is preserved
97 // don't add the id if it is empty (the id empty eg. if there are unknow contacts in thread)
99 return '$1$2' + item.nick.replace(' ', '') + ' ';
101 // 16 chars of hash should be enough. Full hash could be used if it can be done in a visually appealing way.
102 // 16 chars is also the minimum length in the backend (otherwise it's interpreted as a local id).
103 if (id.length > 16) {
104 id = item.id.substring(0,16);
106 return '$1$2' + item.nick.replace(' ', '') + '+' + id + ' ';
109 function basic_replace(item) {
110 if(typeof item.replace !== 'undefined')
111 return '$1'+item.replace;
113 return '$1'+item.name+' ';
116 function webbie_replace(item) {
117 if(typeof item.replace !== 'undefined')
118 return '$1'+item.replace;
120 return '$1'+item.nick+' ';
123 function trim_replace(item) {
124 if(typeof item.replace !== 'undefined')
125 return '$1'+item.replace;
127 return '$1'+item.name;
131 function submit_form(e) {
132 $(e).parents('form').submit();
135 function getWord(text, caretPos) {
136 var index = text.indexOf(caretPos);
137 var postText = text.substring(caretPos, caretPos+8);
138 if ((postText.indexOf("[/list]") > 0) || postText.indexOf("[/ul]") > 0 || postText.indexOf("[/ol]") > 0) {
143 function getCaretPosition(ctrl) {
144 var CaretPos = 0; // IE Support
145 if (document.selection) {
147 var Sel = document.selection.createRange();
148 Sel.moveStart('character', -ctrl.value.length);
149 CaretPos = Sel.text.length;
152 else if (ctrl.selectionStart || ctrl.selectionStart == '0')
153 CaretPos = ctrl.selectionStart;
157 function setCaretPosition(ctrl, pos){
158 if(ctrl.setSelectionRange) {
160 ctrl.setSelectionRange(pos,pos);
162 else if (ctrl.createTextRange) {
163 var range = ctrl.createTextRange();
164 range.collapse(true);
165 range.moveEnd('character', pos);
166 range.moveStart('character', pos);
171 function listNewLineAutocomplete(id) {
172 var text = document.getElementById(id);
173 var caretPos = getCaretPosition(text)
174 var word = getWord(text.value, caretPos);
176 var textBefore = text.value.substring(0, caretPos);
177 var textAfter = text.value.substring(caretPos, text.length);
178 $('#' + id).val(textBefore + '\r\n[*] ' + textAfter).trigger('change');
179 setCaretPosition(text, caretPos + 5);
187 function string2bb(element) {
188 if(element == 'bold') return 'b';
189 else if(element == 'italic') return 'i';
190 else if(element == 'underline') return 'u';
191 else if(element == 'overline') return 'o';
192 else if(element == 'strike') return 's';
197 * jQuery plugin 'editor_autocomplete'
200 $.fn.editor_autocomplete = function(backend_url) {
202 // Autocomplete contacts
204 match: /(^|\s)(@\!*)([^ \n]+)$/,
206 search: function(term, callback) { contact_search(term, callback, backend_url, 'c'); },
207 replace: editor_replace,
208 template: contact_format,
211 // Autocomplete forums
213 match: /(^|\s)(!\!*)([^ \n]+)$/,
215 search: function(term, callback) { contact_search(term, callback, backend_url, 'f'); },
216 replace: editor_replace,
217 template: contact_format,
220 // Autocomplete hashtags
222 match: /(^|\s)(\#)([^ \n]{2,})$/,
224 search: function(term, callback) {
225 $.getJSON(baseurl + '/hashtag/' + '?t=' + term)
226 .done(function(data) {
227 callback($.map(data, function(entry) {
228 // .toLowerCase() enables case-insensitive search
229 return entry.text.toLowerCase().indexOf(term.toLowerCase()) === 0 ? entry : null;
233 replace: function(item) { return "$1$2" + item.text + ' '; },
237 // Autocomplete smilies e.g. ":like"
239 match: /(^|\s)(:[a-z]{2,})$/,
241 search: function(term, callback) { $.getJSON('smilies/json').done(function(data) { callback($.map(data, function(entry) { return entry.text.indexOf(term) === 0 ? entry : null; })); }); },
242 template: function(item) { return item.icon + ' ' + item.text; },
243 replace: function(item) { return "$1" + item.text + ' '; },
246 this.attr('autocomplete','off');
247 this.textcomplete([contacts, forums, smilies, tags], {className:'acpopup', zIndex:10000});
252 * jQuery plugin 'search_autocomplete'
255 $.fn.search_autocomplete = function(backend_url) {
256 // Autocomplete contacts
258 match: /(^@)([^\n]{2,})$/,
260 search: function(term, callback) { contact_search(term, callback, backend_url, 'x', 'contact'); },
261 replace: webbie_replace,
262 template: contact_format,
265 // Autocomplete forum accounts
267 match: /(^!)([^\n]{2,})$/,
269 search: function(term, callback) { contact_search(term, callback, backend_url, 'x', 'community'); },
270 replace: webbie_replace,
271 template: contact_format,
274 // Autocomplete hashtags
276 match: /(^|\s)(\#)([^ \n]{2,})$/,
278 search: function(term, callback) { $.getJSON(baseurl + '/hashtag/' + '?t=' + term).done(function(data) { callback($.map(data, function(entry) { return entry.text.indexOf(term) === 0 ? entry : null; })); }); },
279 replace: function(item) { return "$1$2" + item.text; },
283 this.attr('autocomplete', 'off');
284 var a = this.textcomplete([contacts, community, tags], {className:'acpopup', maxCount:100, zIndex: 10000, appendTo:'nav'});
285 a.on('textComplete:select', function(e, value, strategy) { submit_form(this); });
290 $.fn.name_autocomplete = function(backend_url, typ, autosubmit, onselect) {
291 if(typeof typ === 'undefined') typ = '';
292 if(typeof autosubmit === 'undefined') autosubmit = false;
294 // Autocomplete contacts
296 match: /(^)([^\n]+)$/,
298 search: function(term, callback) { contact_search(term, callback, backend_url, typ); },
299 replace: trim_replace,
300 template: contact_format,
303 this.attr('autocomplete','off');
304 var a = this.textcomplete([names], {className:'acpopup', zIndex:10000});
307 a.on('textComplete:select', function(e,value,strategy) { submit_form(this); });
309 if(typeof onselect !== 'undefined')
310 a.on('textComplete:select', function(e, value, strategy) { onselect(value); });
315 $.fn.bbco_autocomplete = function(type) {
318 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'];
319 var open_elements = ['*', 'hr'];
321 var elements = open_close_elements.concat(open_elements);
325 match: /\[(\w*\**)$/,
326 search: function (term, callback) {
327 callback($.map(elements, function (element) {
328 return element.indexOf(term) === 0 ? element : null;
332 replace: function (element) {
333 element = string2bb(element);
334 if(open_elements.indexOf(element) < 0) {
335 if(element === 'list' || element === 'ol' || element === 'ul') {
336 return ['\[' + element + '\]' + '\n\[*\] ', '\n\[/' + element + '\]'];
338 else if(element === 'table') {
339 return ['\[' + element + '\]' + '\n\[tr\]', '\[/tr\]\n\[/' + element + '\]'];
342 return ['\[' + element + '\]', '\[/' + element + '\]'];
346 return '\[' + element + '\] ';
351 this.attr('autocomplete','off');
352 var a = this.textcomplete([bbco], {className:'acpopup', zIndex:10000});
354 a.on('textComplete:select', function(e, value, strategy) { value; });
356 a.keypress(function(e){
357 if (e.keyCode == 13) {
358 var x = listNewLineAutocomplete(this.id);
360 e.stopImmediatePropagation();
369 * Friendica people autocomplete legacy code
371 * require jQuery, jquery.textareas
373 function ACPopup(elm, backend_url){
376 this.searchText = '';
378 this.kp_timer = false;
379 this.url = backend_url;
381 this.conversation_id = null;
382 var conv_id = this.element.id.match(/\d+$/);
384 this.conversation_id = conv_id[0];
387 var w = $(elm).width();
388 var h = $(elm).height();
390 var style = $(elm).offset();
391 style.top = style.top + h;
393 style.position = 'absolute';
394 style.display = 'none';
396 this.cont = $('<div class="acpopup-mce"></div>');
397 this.cont.css(style);
399 $('body').append(this.cont);
402 ACPopup.prototype.close = function(){
403 $(this.cont).remove();
406 ACPopup.prototype.search = function(text){
408 this.searchText=text;
409 if (this.kp_timer) clearTimeout(this.kp_timer);
410 this.kp_timer = setTimeout( function(){that._search();}, 500);
413 ACPopup.prototype._search = function(){
414 console.log("_search");
419 search:this.searchText,
421 conversation: this.conversation_id,
429 success:function(data){
433 $(data.items).each(function(){
434 var html = "<img src='{0}' height='16px' width='16px'>{1} ({2})".format(this.photo, this.name, this.nick);
435 var nick = this.nick.replace(' ','');
436 if (this.id!=='') nick += '+' + this.id;
437 that.add(html, nick + ' - ' + this.link);
447 ACPopup.prototype.add = function(label, value){
449 var elm = $('<div class="acpopupitem" title="' + value + '">' + label + '</div>');
450 elm.click(function(e){
451 t = $(this).attr('title').replace(new RegExp(' \- .*'), '');
452 el = $(that.element);
453 sel = el.getSelection();
454 sel.start = sel.start - that.searchText.length;
455 el.setSelection(sel.start, sel.end).replaceSelectedText(t + ' ').collapseSelection(false);
458 $(this.cont).append(elm);
461 ACPopup.prototype.onkey = function(event){
462 if (event.keyCode == '13') {
463 if(this.idsel > -1) {
464 this.cont.children()[this.idsel].click();
465 event.preventDefault();
470 if (event.keyCode == '38') { //cursor up
471 var cmax = this.cont.children().size() - 1;
473 if (this.idsel < 0) {
476 event.preventDefault();
478 if (event.keyCode == '40' || event.keyCode == '9') { //cursor down
479 var cmax = this.cont.children().size() - 1;
481 if (this.idsel > cmax) {
484 event.preventDefault();
487 if (event.keyCode == '38' || event.keyCode == '40' || event.keyCode == '9') {
488 this.cont.children().removeClass('selected');
489 $(this.cont.children()[this.idsel]).addClass('selected');
492 if (event.keyCode == '27') { //ESC