aboutsummaryrefslogtreecommitdiff
path: root/js/lib/autocomplete.js
blob: 748070eaa7063da3732298103fe54619aaf64b56 (plain)
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
/**
 * 
 */
elgg.provide('elgg.autocomplete');

elgg.autocomplete.init = function() {
	$('.elgg-input-autocomplete').autocomplete({
		source: function( request, response ) {
			$.ajax({
				url: elgg.autocomplete.url, //gets set by input/autocomplete
				dataType: "json",
				data: {
					q: request.term
				},
				success: function( data ) {
					response( $.map( data, function( item ) {
						item.value = item.name;
						return item;
					}));
				}
			})
		},
		minLength: 1,
		select: function(event, ui) {
			var item = ui.item;
			item.value = item.name;
				
			if($(this).next().attr('type') == "hidden"){
				var hidden = $(this).next();
			} else {
				var hidden = $(this).after('<input type="hidden" name="'+this.name+'[]" />').next();
			}
			hidden.val(item.guid);
		}
	})
	
	.data("autocomplete")._renderItem = function(ul, item) {
		switch (item.type) {
			case 'user':
			case 'group':
				r = item.icon + item.name + ' - ' + item.desc;
				break;

			default:
				r = item.name + ' - ' + item.desc;
				break;
		}
		
		return $("<li/>")
			.data("item.autocomplete", item)
			.append('<a>'+r+'</a>')
			.appendTo(ul);
	};
};

elgg.register_hook_handler('init', 'system', elgg.autocomplete.init);