aboutsummaryrefslogtreecommitdiff
path: root/js/lib/userpicker.js
blob: 780fa47c66bead812907a191caca9c014b625cd2 (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
57
58
59
elgg.provide('elgg.userpicker');

elgg.userpicker.init = function() {
	
	// binding autocomplete.
	// doing this as an each so we can pass this to functions.
	$('.elgg-input-user-picker').each(function() {

		$(this).autocomplete({
			source: function(request, response) {

				var params = elgg.userpicker.getSearchParams(this);
				
				elgg.get('livesearch', {
					data: params,
					dataType: 'json',
					success: function(data) {
						response(data);
					}
				});
			},
			minLength: 2,
			html: "html",
			select: elgg.userpicker.addUser
		})
	});
};

/**
 * elgg.userpicker.userList is defined in the input/userpicker view
 */
elgg.userpicker.addUser = function(event, ui) {
	var info = ui.item;

	// do not allow users to be added multiple times
	if (!(info.guid in elgg.userpicker.userList)) {
		elgg.userpicker.userList[info.guid] = true;
		var users = $(this).siblings('.elgg-user-picker-entries');
		var li = '<input type="hidden" name="members[]" value="' + info.guid + '" />';
		$('<li>').html(li).appendTo(users);
	}

	$(this).val('');
	event.preventDefault();
};

elgg.userpicker.removeUser = function(link, guid) {
	$(link).closest('.elgg-user-picker-entries > li').remove();
};

elgg.userpicker.getSearchParams = function(e) {
	if (e.element.siblings('[name=match_on]').attr('checked')) {
		return {'match_on[]': 'friends', 'term' : e.term};
	} else {
		return {'match_on[]': 'users', 'term' : e.term};
	}
}

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