aboutsummaryrefslogtreecommitdiffstats
path: root/view/js/autocomplete.js
blob: fa293fdfd2e1c5bb8c4ddd85dc8e8256327d0618 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
 * Red people autocomplete
 *
 * require jQuery, jquery.textcomplete
 */
function mysearch(term, callback, backend_url) {
	var postdata = {
		start:0,
		count:100,
		//search:term.substring(1),
		search:term,
		type:'c',
	}
	
	$.ajax({
		type:'POST',
		url: backend_url,
		data: postdata,
		dataType: 'json',
		success:function(data){
			callback(data.items);
		},
	}).fail(function () {callback([]); }); // Callback must be invoked even if something went wrong.
}

function format(item) {
	return "<img src='{0}' height='16px' width='16px'>{1} ({2})".format(item.photo, item.name, ((item.label) ? item.nick + ' ' + item.label : item.nick) )
}

function replace(item) {
	// $2 ensures that prefix (@,@!) is preserved
	return '$1$2'+item.nick.replace(' ','') + '+' + item.id;
}

/**
 * jQuery plugin 'contact_autocomplete'
 */
(function( $ ){
	$.fn.contact_autocomplete = function(backend_url) {

	// Autocomplete contacts
	contacts = {
		match: /(^|\s)(@!?)(\w{2,})$/,
		index: 3,
		search: function(term, callback) { mysearch(term, callback, backend_url); },
		replace: replace,
		template: format,
	}

	smilies = {
		match: /(^|\s)(:[a-z]{2,})$/,
		index: 2,
		search: function(term, callback) { $.getJSON('https://caterva.eu/smilies/json').done(function(data) { callback($.map(data, function(entry) { return entry['text'].indexOf(term) === 0 ? entry : null })) }) },
		template: function(item) { return item['icon'] + item['text'] },
		replace: function(item) { return item['text'] + ' '; },
	}
	this.textcomplete([contacts,smilies],{});
  };
})( jQuery );