1/** 2 * Provides a list of matching user names while user inputs into a userpicker 3 * 4 * @author Adrian Lang <lang@cosmocode.de> 5 * @author Gerrit Uitslag <klapinklapin@gmail.com> 6 */ 7jQuery(function () { 8 /** 9 * Ajax request for user suggestions 10 * 11 * @param {Object} request object, with single 'term' property 12 * @param {Function} response callback, argument: the data array to suggest to the user. 13 * @param {Function} getterm callback, argument: the request Object, returns: search term 14 */ 15 function ajaxsource(request, response, getterm) { 16 jQuery.getJSON( 17 DOKU_BASE + 'lib/exe/ajax.php', { 18 call: 'bureaucracy_user_field', 19 search: getterm(request) 20 }, function (data) { 21 response(jQuery.map(data, function (name, user) { 22 return { 23 label: name + ' (' + user + ')', 24 value: user 25 } 26 })) 27 } 28 ); 29 } 30 31 function split(val) { 32 return val.split(/,\s*/); 33 } 34 35 function extractLast(term) { 36 return split(term).pop(); 37 } 38 39 40 /** 41 * pick one user 42 */ 43 jQuery(".userpicker").autocomplete({ 44 source: function (request, response) { 45 ajaxsource(request, response, function (req) { 46 return req.term 47 }) 48 } 49 }); 50 51 /** 52 * pick one or more users 53 */ 54 jQuery(".userspicker") 55 // don't navigate away from the field on tab when selecting an item 56 .bind("keydown", function (event) { 57 if (event.keyCode === jQuery.ui.keyCode.TAB && 58 jQuery(this).data("ui-autocomplete").menu.active) { 59 event.preventDefault(); 60 } 61 }) 62 .autocomplete({ 63 minLength: 0, 64 source: function (request, response) { 65 ajaxsource(request, response, function (req) { 66 return extractLast(req.term) 67 }) 68 }, 69 search: function () { 70 // custom minLength 71 var term = extractLast(this.value); 72 return term.length >= 2; 73 }, 74 focus: function () { 75 // prevent value inserted on focus 76 return false; 77 }, 78 select: function (event, ui) { 79 var terms = split(this.value); 80 // remove the current input 81 terms.pop(); 82 // add the selected item 83 terms.push(ui.item.value); 84 // add placeholder to get the comma-and-space at the end 85 terms.push(""); 86 this.value = terms.join(", "); 87 return false; 88 } 89 }); 90}); 91