UserPicker.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /** @module elgg/UserPicker */
  2. define(['jquery', 'elgg', 'jquery.ui.autocomplete.html'], function ($, elgg) {
  3. /**
  4. * @param {HTMLElement} wrapper outer div
  5. * @constructor
  6. * @alias module:elgg/UserPicker
  7. *
  8. * @todo move this to /js/classes ?
  9. */
  10. function UserPicker(wrapper) {
  11. this.$wrapper = $(wrapper);
  12. this.$input = $('.elgg-input-user-picker', wrapper);
  13. this.$ul = $('.elgg-user-picker-list', wrapper);
  14. var self = this,
  15. data = this.$wrapper.data();
  16. this.name = data.name || 'members';
  17. this.handler = data.handler || 'livesearch';
  18. this.limit = data.limit || 0;
  19. this.minLength = data.minLength || 2;
  20. this.isSealed = false;
  21. this.$input.autocomplete({
  22. source: function(request, response) {
  23. // note: "this" below will not be bound to the input, but rather
  24. // to an object created by the autocomplete component
  25. if (self.isSealed) {
  26. return;
  27. }
  28. elgg.get(self.handler, {
  29. data: {
  30. term: this.term,
  31. "match_on[]": ($('[name=match_on]', self.$wrapper).attr('checked') ? 'friends' : 'users'),
  32. name: self.name
  33. },
  34. dataType: 'json',
  35. success: function(data) {
  36. response(data);
  37. }
  38. });
  39. },
  40. minLength: self.minLength,
  41. html: "html",
  42. select: function(event, ui) {
  43. self.addUser(event, ui.item.guid, ui.item.html);
  44. },
  45. // turn off experimental live help - no i18n support and a little buggy
  46. messages: {
  47. noResults: '',
  48. results: function() {}
  49. }
  50. });
  51. $('.elgg-user-picker-remove', this.$wrapper).live('click', function(event) {
  52. self.removeUser(event);
  53. });
  54. this.enforceLimit();
  55. }
  56. UserPicker.prototype = {
  57. /**
  58. * Adds a user to the select user list
  59. *
  60. * @param {Object} event
  61. * @param {Number} guid GUID of autocomplete item selected by user
  62. * @param {String} html HTML for autocomplete item selected by user
  63. */
  64. addUser : function(event, guid, html) {
  65. // do not allow users to be added multiple times
  66. if (!$('li[data-guid="' + guid + '"]', this.$ul).length) {
  67. this.$ul.append(html);
  68. }
  69. this.$input.val('');
  70. this.enforceLimit();
  71. event.preventDefault();
  72. },
  73. /**
  74. * Removes a user from the select user list
  75. *
  76. * @param {Object} event
  77. */
  78. removeUser : function(event) {
  79. $(event.target).closest('.elgg-user-picker-list > li').remove();
  80. this.enforceLimit();
  81. event.preventDefault();
  82. },
  83. /**
  84. * Make sure user can't add more than limit
  85. */
  86. enforceLimit : function() {
  87. if (this.limit) {
  88. if ($('li[data-guid]', this.$ul).length >= this.limit) {
  89. if (!this.isSealed) {
  90. this.seal();
  91. }
  92. } else {
  93. if (this.isSealed) {
  94. this.unseal();
  95. }
  96. }
  97. }
  98. },
  99. /**
  100. * Don't allow user to add users
  101. */
  102. seal : function() {
  103. this.$input.prop('disabled', true);
  104. this.$wrapper.addClass('elgg-state-disabled');
  105. this.isSealed = true;
  106. },
  107. /**
  108. * Allow user to add users
  109. */
  110. unseal : function() {
  111. this.$input.prop('disabled', false);
  112. this.$wrapper.removeClass('elgg-state-disabled');
  113. this.isSealed = false;
  114. }
  115. };
  116. /**
  117. * @param {String} selector
  118. */
  119. UserPicker.setup = function(selector) {
  120. elgg.register_hook_handler('init', 'system', function () {
  121. $(selector).each(function () {
  122. // we only want to wrap each picker once
  123. if (!$(this).data('initialized')) {
  124. new UserPicker(this);
  125. $(this).data('initialized', 1);
  126. }
  127. });
  128. });
  129. };
  130. return UserPicker;
  131. });