2 * Red people autocomplete
4 * require jQuery, jquery.textcomplete
6 function contact_search(term, callback, backend_url, type) {
8 // Check if there is a cached result that contains the same information we would get with a full server-side search
9 var bt = backend_url+type;
10 if(!(bt in contact_search.cache)) contact_search.cache[bt] = {};
12 var lterm = term.toLowerCase(); // Ignore case
13 for(var t in contact_search.cache[bt]) {
14 if(lterm.indexOf(t) >= 0) { // A more broad search has been performed already, so use those results
15 // Filter old results locally
16 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
17 matching.unshift({taggable:false, text: term, replace: term});
18 setTimeout(function() { callback(matching); } , 1); // Use "pseudo-thread" to avoid some problems
36 success: function(data){
37 // Cache results if we got them all (more information would not improve results)
38 // data.count represents the maximum number of items
39 if(data.items.length -1 < data.count) {
40 contact_search.cache[bt][lterm] = data.items;
42 var items = data.items.slice(0);
43 items.unshift({taggable:false, text: term, replace: term});
46 }).fail(function () {callback([]); }); // Callback must be invoked even if something went wrong.
48 contact_search.cache = {};
51 function contact_format(item) {
52 // Show contact information if not explicitly told to show something else
53 if(typeof item.text === 'undefined') {
54 var desc = ((item.label) ? item.nick + ' ' + item.label : item.nick);
55 if(typeof desc === 'undefined') desc = '';
56 if(desc) desc = ' ('+desc+')';
57 return "<div class='{0}' title='{4}'><img class='dropdown-menu-img-sm' src='{1}'><span class='contactname'>{2}</span><span class='dropdown-sub-text'>{3}</span><div class='clear'></div></div>".format(item.taggable, item.photo, item.name, desc, item.link);
60 return "<div>" + item.text + "</div>";
63 function editor_replace(item) {
64 if(typeof item.replace !== 'undefined') {
65 return '$1$2' + item.replace;
68 // $2 ensures that prefix (@,@!) is preserved
70 // 16 chars of hash should be enough. Full hash could be used if it can be done in a visually appealing way.
71 // 16 chars is also the minimum length in the backend (otherwise it's interpreted as a local id).
73 id = item.id.substring(0,16);
75 return '$1$2' + item.nick.replace(' ', '') + '+' + id + ' ';
78 function basic_replace(item) {
79 if(typeof item.replace !== 'undefined')
80 return '$1'+item.replace;
82 return '$1'+item.name+' ';
85 function trim_replace(item) {
86 if(typeof item.replace !== 'undefined')
87 return '$1'+item.replace;
89 return '$1'+item.name;
93 function submit_form(e) {
94 $(e).parents('form').submit();
98 * jQuery plugin 'editor_autocomplete'
101 $.fn.editor_autocomplete = function(backend_url) {
103 // Autocomplete contacts
105 match: /(^|\s)(@\!*)([^ \n]+)$/,
107 search: function(term, callback) { contact_search(term, callback, backend_url, 'c'); },
108 replace: editor_replace,
109 template: contact_format,
113 match: /(^|\s)(:[a-z]{2,})$/,
115 search: function(term, callback) { $.getJSON('/smilies/json').done(function(data) { callback($.map(data, function(entry) { return entry.text.indexOf(term) === 0 ? entry : null; })); }); },
116 template: function(item) { return item.icon + item.text; },
117 replace: function(item) { return "$1" + item.text + ' '; },
119 this.attr('autocomplete','off');
120 this.textcomplete([contacts,smilies], {className:'acpopup', zIndex:1020});
125 * jQuery plugin 'search_autocomplete'
128 $.fn.search_autocomplete = function(backend_url) {
129 // Autocomplete contacts
131 match: /(^@)([^\n]{2,})$/,
133 search: function(term, callback) { contact_search(term, callback, backend_url, 'x'); },
134 replace: basic_replace,
135 template: contact_format,
137 this.attr('autocomplete', 'off');
138 var a = this.textcomplete([contacts], {className:'acpopup', maxCount:100, zIndex: 1020, appendTo:'nav'});
139 a.on('textComplete:select', function(e, value, strategy) { submit_form(this); });
144 $.fn.contact_autocomplete = function(backend_url, typ, autosubmit, onselect) {
145 if(typeof typ === 'undefined') typ = '';
146 if(typeof autosubmit === 'undefined') autosubmit = false;
148 // Autocomplete contacts
150 match: /(^)([^\n]+)$/,
152 search: function(term, callback) { contact_search(term, callback, backend_url, typ); },
153 replace: basic_replace,
154 template: contact_format,
157 this.attr('autocomplete','off');
158 var a = this.textcomplete([contacts], {className:'acpopup', zIndex:1020});
161 a.on('textComplete:select', function(e,value,strategy) { submit_form(this); });
163 if(typeof onselect !== 'undefined')
164 a.on('textComplete:select', function(e, value, strategy) { onselect(value); });
170 $.fn.name_autocomplete = function(backend_url, typ, autosubmit, onselect) {
171 if(typeof typ === 'undefined') typ = '';
172 if(typeof autosubmit === 'undefined') autosubmit = false;
174 // Autocomplete contacts
176 match: /(^)([^\n]+)$/,
178 search: function(term, callback) { contact_search(term, callback, backend_url, typ); },
179 replace: trim_replace,
180 template: contact_format,
183 this.attr('autocomplete','off');
184 var a = this.textcomplete([names], {className:'acpopup', zIndex:1020});
187 a.on('textComplete:select', function(e,value,strategy) { submit_form(this); });
189 if(typeof onselect !== 'undefined')
190 a.on('textComplete:select', function(e, value, strategy) { onselect(value); });