You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

91 lines
2.8 KiB

  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. */
  7. jQuery(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. function split(val) {
  31. return val.split(/,\s*/);
  32. }
  33. function extractLast(term) {
  34. return split(term).pop();
  35. }
  36. /**
  37. * pick one user
  38. */
  39. jQuery(".userpicker").autocomplete({
  40. source: function (request, response) {
  41. ajaxsource(request, response, function (req) {
  42. return req.term
  43. })
  44. }
  45. });
  46. /**
  47. * pick one or more users
  48. */
  49. jQuery(".userspicker")
  50. // don't navigate away from the field on tab when selecting an item
  51. .bind("keydown", function (event) {
  52. if (event.keyCode === jQuery.ui.keyCode.TAB &&
  53. jQuery(this).data("ui-autocomplete").menu.active) {
  54. event.preventDefault();
  55. }
  56. })
  57. .autocomplete({
  58. minLength: 0,
  59. source: function (request, response) {
  60. ajaxsource(request, response, function (req) {
  61. return extractLast(req.term)
  62. })
  63. },
  64. search: function () {
  65. // custom minLength
  66. var term = extractLast(this.value);
  67. return term.length >= 2;
  68. },
  69. focus: function () {
  70. // prevent value inserted on focus
  71. return false;
  72. },
  73. select: function (event, ui) {
  74. var terms = split(this.value);
  75. // remove the current input
  76. terms.pop();
  77. // add the selected item
  78. terms.push(ui.item.value);
  79. // add placeholder to get the comma-and-space at the end
  80. terms.push("");
  81. this.value = terms.join(", ");
  82. return false;
  83. }
  84. });
  85. });