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
87 // 16 chars of hash should be enough. Full hash could be used if it can be done in a visually appealing way.
88 // 16 chars is also the minimum length in the backend (otherwise it's interpreted as a local id).
90 id = item.id.substring(0,16);
92 return '$1$2' + item.nick.replace(' ', '') + '+' + id + ' ';
95 function basic_replace(item) {
96 if(typeof item.replace !== 'undefined')
97 return '$1'+item.replace;
99 return '$1'+item.name+' ';
102 function trim_replace(item) {
103 if(typeof item.replace !== 'undefined')
104 return '$1'+item.replace;
106 return '$1'+item.name;
110 function submit_form(e) {
111 $(e).parents('form').submit();
115 * jQuery plugin 'editor_autocomplete'
118 $.fn.editor_autocomplete = function(backend_url) {
120 // list of supported bbtags
121 var bbelements = ['b', 'u', 'i', 'img', 'url', 'quote', 'code', 'spoiler', 'audio', 'video', 'youtube', 'map', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 's', 'o', 'list', 'center', 'nosmile', 'vimeo' ];
123 // Autocomplete contacts
125 match: /(^|\s)(@\!*)([^ \n]+)$/,
127 search: function(term, callback) { contact_search(term, callback, backend_url, 'c'); },
128 replace: editor_replace,
129 template: contact_format,
132 // Autocomplete smilies e.g. ":like"
134 match: /(^|\s)(:[a-z]{2,})$/,
136 search: function(term, callback) { $.getJSON('smilies/json').done(function(data) { callback($.map(data, function(entry) { return entry.text.indexOf(term) === 0 ? entry : null; })); }); },
137 template: function(item) { return item.icon + ' ' + item.text; },
138 replace: function(item) { return "$1" + item.text + ' '; },
141 // Autocomplete BBTags
145 search: function (term, callback) { callback($.map(bbelements, function (element) { return element.indexOf(term) === 0 ? element : null; })); },
146 replace: function (element) { return ['[' + element + ']', '[/' + element + ']']; },
148 this.attr('autocomplete','off');
149 this.textcomplete([contacts,smilies, bbtags], {className:'acpopup', zIndex:1020});
154 * jQuery plugin 'search_autocomplete'
157 $.fn.search_autocomplete = function(backend_url) {
158 // Autocomplete contacts
160 match: /(^@)([^\n]{2,})$/,
162 search: function(term, callback) { contact_search(term, callback, backend_url, 'x', 'contact'); },
163 replace: basic_replace,
164 template: contact_format,
167 // Autocomplete forum accounts
169 match: /(^!)([^\n]{2,})$/,
171 search: function(term, callback) { contact_search(term, callback, backend_url, 'x', 'community'); },
172 replace: basic_replace,
173 template: contact_format,
175 this.attr('autocomplete', 'off');
176 var a = this.textcomplete([contacts, community], {className:'acpopup', maxCount:100, zIndex: 1020, appendTo:'#nav-search-box'});
177 a.on('textComplete:select', function(e, value, strategy) { submit_form(this); });
182 $.fn.contact_autocomplete = function(backend_url, typ, autosubmit, onselect) {
183 if(typeof typ === 'undefined') typ = '';
184 if(typeof autosubmit === 'undefined') autosubmit = false;
186 // Autocomplete contacts
188 match: /(^)([^\n]+)$/,
190 search: function(term, callback) { contact_search(term, callback, backend_url, typ); },
191 replace: basic_replace,
192 template: contact_format,
195 this.attr('autocomplete','off');
196 var a = this.textcomplete([contacts], {className:'acpopup', zIndex:1020});
199 a.on('textComplete:select', function(e,value,strategy) { submit_form(this); });
201 if(typeof onselect !== 'undefined')
202 a.on('textComplete:select', function(e, value, strategy) { onselect(value); });
208 $.fn.name_autocomplete = function(backend_url, typ, autosubmit, onselect) {
209 if(typeof typ === 'undefined') typ = '';
210 if(typeof autosubmit === 'undefined') autosubmit = false;
212 // Autocomplete contacts
214 match: /(^)([^\n]+)$/,
216 search: function(term, callback) { contact_search(term, callback, backend_url, typ); },
217 replace: trim_replace,
218 template: contact_format,
221 this.attr('autocomplete','off');
222 var a = this.textcomplete([names], {className:'acpopup', zIndex:1020});
225 a.on('textComplete:select', function(e,value,strategy) { submit_form(this); });
227 if(typeof onselect !== 'undefined')
228 a.on('textComplete:select', function(e, value, strategy) { onselect(value); });
234 * Friendica people autocomplete legacy
235 * code which is needed for tinymce
237 * require jQuery, jquery.textareas
240 function ACPopup(elm,backend_url){
245 this.kp_timer = false;
246 this.url = backend_url;
248 this.conversation_id = null;
249 var conv_id = this.element.id.match(/\d+$/);
250 if (conv_id) this.conversation_id = conv_id[0];
251 console.log("ACPopup elm id",this.element.id,"conversation",this.conversation_id);
257 if(tinyMCE.activeEditor == null) {
258 style = $(elm).offset();
263 // I can't find an "official" way to get the element who get all
264 // this fraking thing that is tinyMCE.
265 // This code will broke again at some point...
266 var container = $(tinyMCE.activeEditor.getContainer()).find("table");
267 style = $(container).offset();
268 w = $(container).width();
269 h = $(container).height();
272 style.top=style.top+h;
274 style.position = 'absolute';
275 /* style['max-height'] = '150px';
276 style.border = '1px solid red';
277 style.background = '#cccccc';
279 style.overflow = 'auto';
280 style['z-index'] = '100000';
282 style.display = 'none';
284 this.cont = $("<div class='acpopup-mce'></div>");
285 this.cont.css(style);
287 $("body").append(this.cont);
290 ACPopup.prototype.close = function(){
291 $(this.cont).remove();
294 ACPopup.prototype.search = function(text){
296 this.searchText=text;
297 if (this.kp_timer) clearTimeout(this.kp_timer);
298 this.kp_timer = setTimeout( function(){that._search();}, 500);
301 ACPopup.prototype._search = function(){
302 console.log("_search");
307 search:this.searchText,
309 conversation: this.conversation_id,
317 success:function(data){
321 $(data.items).each(function(){
322 var html = "<img src='{0}' height='16px' width='16px'>{1} ({2})".format(this.photo, this.name, this.nick);
323 var nick = this.nick.replace(' ','');
324 if (this.id!=='') nick += '+' + this.id;
325 that.add(html, nick + ' - ' + this.link);
335 ACPopup.prototype.add = function(label, value){
337 var elm = $("<div class='acpopupitem' title='"+value+"'>"+label+"</div>");
338 elm.click(function(e){
339 t = $(this).attr('title').replace(new RegExp(' \- .*'),'');
340 if(typeof(that.element.container) === "undefined") {
342 sel = el.getSelection();
343 sel.start = sel.start- that.searchText.length;
344 el.setSelection(sel.start,sel.end).replaceSelectedText(t+' ').collapseSelection(false);
348 txt = tinyMCE.activeEditor.getContent();
349 // alert(that.searchText + ':' + t);
350 newtxt = txt.replace('@' + that.searchText,'@' + t +' ');
351 tinyMCE.activeEditor.setContent(newtxt);
352 tinyMCE.activeEditor.focus();
356 $(this.cont).append(elm);
359 ACPopup.prototype.onkey = function(event){
360 if (event.keyCode == '13') {
362 this.cont.children()[this.idsel].click();
363 event.preventDefault();
368 if (event.keyCode == '38') { //cursor up
369 cmax = this.cont.children().size()-1;
371 if (this.idsel<0) this.idsel=cmax;
372 event.preventDefault();
374 if (event.keyCode == '40' || event.keyCode == '9') { //cursor down
375 cmax = this.cont.children().size()-1;
377 if (this.idsel>cmax) this.idsel=0;
378 event.preventDefault();
381 if (event.keyCode == '38' || event.keyCode == '40' || event.keyCode == '9') {
382 this.cont.children().removeClass('selected');
383 $(this.cont.children()[this.idsel]).addClass('selected');
386 if (event.keyCode == '27') { //ESC