aboutsummaryrefslogtreecommitdiffstats
path: root/view/js/autocomplete.js
blob: 63aa7a8f177d4eb98668ba62dfc1df593724499d (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/**
 * Red people autocomplete
 *
 * require jQuery, jquery.textcomplete
 */
function contact_search(term, callback, backend_url, type, extra_channels) {
	// Check if there is a cached result that contains the same information we would get with a full server-side search
	// Assume type hasn't changed
	var lterm = term.toLowerCase(); // Ignore case
	for(t in contact_search.cache) {
		if(lterm.indexOf(t) >= 0) { // A more broad search has been performed already, so use those results
			// Filter old results locally
			var matching = contact_search.cache[t].filter(function (x) { return (x.name.toLowerCase().indexOf(lterm) >= 0 || x.nick.toLowerCase().indexOf(lterm) >= 0); });
			matching.unshift({taggable:false, text: term, replace: term});
			callback(matching);
			return;
		}
	}

	var postdata = {
		start:0,
		count:100,
		search:term,
		type:type,
	}

	if(typeof extra_channels !== 'undefined' && extra_channels)
		postdata['extra_channels[]'] = extra_channels;
	
	$.ajax({
		type:'POST',
		url: backend_url,
		data: postdata,
		dataType: 'json',
		success:function(data){
			// Cache results if we got them all (more information would not improve results)
			// data.count represents the maximum number of items
			if(data.items.length -1 < data.count) {
				contact_search.cache[lterm] = data.items;
			}
			var items = data.items.slice(0);
			items.unshift({taggable:false, text: term, replace: term});
			callback(items);
		},
	}).fail(function () {callback([]); }); // Callback must be invoked even if something went wrong.
}
contact_search.cache = {};


function contact_format(item) {
	if(typeof item.text === 'undefined')
		return "<div class='{0}' title='{4}'><img src='{1}'>{2} ({3})</div>".format(item.taggable, item.photo, item.name, ((item.label) ? item.nick + ' ' + item.label : item.nick), item.link)
	else
		return "<div>"+item.text+"</div>"
}

function editor_replace(item) {
	if(typeof item.replace !== 'undefined') {
		return '$1$2'+item.replace;
	}

	// $2 ensures that prefix (@,@!) is preserved
	var id = item.id;
	 // 16 chars of hash should be enough. Full hash could be used if it can be done in a visually appealing way.
	// 16 chars is also the minimum length in the backend (otherwise it's interpreted as a local id).
	if(id.length > 16) 
		id = item.id.substring(0,16);
	return '$1$2'+item.nick.replace(' ','') + '+' + id;
}

function basic_replace(item) {
	if(typeof item.replace !== 'undefined')
		return '$1'+item.replace;

	return '$1'+item.name+' ';
}

function submit_form(e) {
	$(e).parents('form').submit();
}

/**
 * jQuery plugin 'editor_autocomplete'
 */
(function( $ ){
	$.fn.editor_autocomplete = function(backend_url, extra_channels) {
	if (typeof extra_channels === 'undefined') extra_channels = false;

	// Autocomplete contacts
	contacts = {
		match: /(^|\s)(@\!*)([^ \n]+)$/,
		index: 3,
		search: function(term, callback) { contact_search(term, callback, backend_url, 'c', extra_channels); },
		replace: editor_replace,
		template: contact_format,
	}

	smilies = {
		match: /(^|\s)(:[a-z]{2,})$/,
		index: 2,
		search: function(term, callback) { $.getJSON('/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 "$1"+item['text'] + ' '; },
	}
	this.attr('autocomplete','off');
	this.textcomplete([contacts,smilies],{className:'acpopup',zIndex:1050});
  };
})( jQuery );

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

	// Autocomplete contacts
	contacts = {
		match: /(^@)([^\n]{2,})$/,
		index: 2,
		search: function(term, callback) { contact_search(term, callback, backend_url, 'x',[]); },
		replace: basic_replace,
		template: contact_format,
	}
	this.attr('autocomplete','off');
	var a = this.textcomplete([contacts],{className:'acpopup',maxCount:100,zIndex: 1050});

	a.on('textComplete:select', function(e,value,strategy) { submit_form(this); });
	
  };
})( jQuery );

(function( $ ){
	$.fn.contact_autocomplete = function(backend_url, typ, autosubmit, onselect) {

	if(typeof typ === 'undefined') typ = '';
	if(typeof autosubmit === 'undefined') autosubmit = false;

	// Autocomplete contacts
	contacts = {
		match: /(^)([^\n]+)$/,
		index: 2,
		search: function(term, callback) { contact_search(term, callback, backend_url, typ,[]); },
		replace: basic_replace,
		template: contact_format,
	}

	this.attr('autocomplete','off');
	var a = this.textcomplete([contacts],{className:'acpopup',zIndex:1050});

	if(autosubmit)
		a.on('textComplete:select', function(e,value,strategy) { submit_form(this); });

	if(typeof onselect !== 'undefined')
		a.on('textComplete:select',function(e,value,strategy) { onselect(value); });
  };
})( jQuery );