group_invite_autocomplete.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. * special autocomplete input
  4. */
  5. $name = elgg_extract("name", $vars); // input name of the selected user
  6. $id = elgg_extract("id", $vars);
  7. $relationship = elgg_extract("relationship", $vars);
  8. $destination = $id . "_autocomplete_results";
  9. $minChars = (int) elgg_extract("minChars", $vars, 3);
  10. if ($minChars < 1) {
  11. $minChars = 3;
  12. }
  13. echo elgg_view('input/text', [
  14. 'id' => $id . '_autocomplete',
  15. 'class' => 'elgg-input-autocomplete'
  16. ]);
  17. echo elgg_format_element('div', ['id' => $destination, 'class' => 'mtm clearfloat']);
  18. ?>
  19. <script type="text/javascript">
  20. $(document).ready(function() {
  21. $("#<?php echo $id; ?>_autocomplete").each(function() {
  22. $(this)
  23. // don't navigate away from the field on tab when selecting an item
  24. .bind( "keydown", function(event) {
  25. if (event.keyCode === $.ui.keyCode.TAB &&
  26. $(this).data("autocomplete").menu.active) {
  27. event.preventDefault();
  28. }
  29. })
  30. .autocomplete({
  31. source: function(request, response) {
  32. $.getJSON(elgg.get_site_url() + "groups/group_invite_autocomplete", {
  33. q: request.term,
  34. 'user_guids': function() {
  35. var result = "";
  36. $("#<?php echo $destination; ?> input[name='<?php echo $name; ?>[]']").each(function(index, elem) {
  37. if (result == "") {
  38. result = $(this).val();
  39. } else {
  40. result += "," + $(this).val();
  41. }
  42. });
  43. return result;
  44. },
  45. 'group_guid' : <?php echo $vars["group_guid"]; ?>
  46. <?php
  47. if (!empty($relationship)) {
  48. echo ", 'relationship' : '" . $relationship . "'";
  49. }
  50. ?>
  51. }, response );
  52. },
  53. search: function() {
  54. // custom minLength
  55. var term = this.value;
  56. if (term.length < <?php echo $minChars; ?>) {
  57. return false;
  58. }
  59. },
  60. focus: function(event) {
  61. // prevent value inserted on focus
  62. return false;
  63. },
  64. select: function(event, ui) {
  65. this.value = "";
  66. var result = "";
  67. result += "<div class='group_tools_group_invite_autocomplete_autocomplete_result elgg-discover_result elgg-discover'>";
  68. if (ui.item.type == "user") {
  69. result += "<input type='hidden' value='" + ui.item.value + "' name='<?php echo $name; ?>[]' />";
  70. } else if (ui.item.type == "email") {
  71. result += "<input type='hidden' value='" + ui.item.value + "' name='<?php echo $name; ?>_email[]' />";
  72. }
  73. result += ui.item.content;
  74. result += "<?php echo addslashes(elgg_view_icon("delete-alt", "elgg-discoverable float-alt")); ?>";
  75. // some filter is breaking the following line, so we do this
  76. // to bypass the filter :P
  77. var divThis = "</d";
  78. divThis += "iv>";
  79. result += divThis;
  80. $('#<?php echo $destination; ?>').append(result);
  81. return false;
  82. },
  83. autoFocus: true,
  84. messages: {
  85. noResults: '',
  86. results: function() {}
  87. }
  88. }).data("ui-autocomplete")._renderItem = function(ul, item) {
  89. var list_body = "";
  90. list_body = item.content;
  91. return $("<li></li>")
  92. .data("item.autocomplete", item)
  93. .append("<a>" + list_body + "</a>")
  94. .appendTo(ul);
  95. };
  96. });
  97. $('#<?php echo $destination; ?> .elgg-icon-delete-alt').live("click", function() {
  98. $(this).parent('div').remove();
  99. });
  100. });
  101. </script>