Elgg  Version 1.9
UserPicker.js
Go to the documentation of this file.
1 
3 define(['jquery', 'elgg'], function ($, elgg) {
4 
12  function UserPicker(wrapper) {
13  this.$wrapper = $(wrapper);
14  this.$input = $('.elgg-input-user-picker', wrapper);
15  this.$ul = $('.elgg-user-picker-list', wrapper);
16 
17  var self = this,
18  data = this.$wrapper.data();
19 
20  this.name = data.name || 'members';
21  this.handler = data.handler || 'livesearch';
22  this.limit = data.limit || 0;
23  this.minLength = data.minLength || 2;
24  this.isSealed = false;
25 
26  this.$input.autocomplete({
27  source: function(request, response) {
28  // note: "this" below will not be bound to the input, but rather
29  // to an object created by the autocomplete component
30 
31  if (self.isSealed) {
32  return;
33  }
34 
35  elgg.get(self.handler, {
36  data: {
37  term: this.term,
38  "match_on[]": ($('[name=match_on]', self.$wrapper).attr('checked') ? 'friends' : 'users'),
39  name: self.name
40  },
41  dataType: 'json',
42  success: function(data) {
43  response(data);
44  }
45  });
46  },
47  minLength: self.minLength,
48  html: "html",
49  select: function(event, ui) {
50  self.addUser(event, ui.item.guid, ui.item.html);
51  },
52  // turn off experimental live help - no i18n support and a little buggy
53  messages: {
54  noResults: '',
55  results: function() {}
56  }
57  });
58 
59  $('.elgg-user-picker-remove', this.$wrapper).live('click', function(event) {
60  self.removeUser(event);
61  });
62 
63  this.enforceLimit();
64  }
65 
66  UserPicker.prototype = {
74  addUser : function(event, guid, html) {
75  // do not allow users to be added multiple times
76  if (!$('li[data-guid="' + guid + '"]', this.$ul).length) {
77  this.$ul.append(html);
78  }
79  this.$input.val('');
80 
81  this.enforceLimit();
82 
83  event.preventDefault();
84  },
85 
91  removeUser : function(event) {
92  $(event.target).closest('.elgg-user-picker-list > li').remove();
93 
94  this.enforceLimit();
95 
96  event.preventDefault();
97  },
98 
102  enforceLimit : function() {
103  if (this.limit) {
104  if ($('li[data-guid]', this.$ul).length >= this.limit) {
105  if (!this.isSealed) {
106  this.seal();
107  }
108  } else {
109  if (this.isSealed) {
110  this.unseal();
111  }
112  }
113  }
114  },
115 
119  seal : function() {
120  this.$input.prop('disabled', true);
121  this.$wrapper.addClass('elgg-state-disabled');
122  this.isSealed = true;
123  },
124 
128  unseal : function() {
129  this.$input.prop('disabled', false);
130  this.$wrapper.removeClass('elgg-state-disabled');
131  this.isSealed = false;
132  }
133  };
134 
138  UserPicker.setup = function(selector) {
139  elgg.register_hook_handler('init', 'system', function () {
140  $(selector).each(function () {
141  // we only want to wrap each picker once
142  if (!$(this).data('initialized')) {
143  new UserPicker(this);
144  $(this).data('initialized', 1);
145  }
146  });
147  });
148  };
149 
150  return UserPicker;
151 });
elgg message elgg state success
Definition: admin.php:243
elgg
Definition: install.js:23
if(!$owner||!($owner instanceof ElggUser)||!$owner->canEdit()) $input
Definition: edit.php:19
elgg page messages
Definition: admin.php:223
friends picker main wrapper
Definition: admin.php:682
html
Definition: admin.php:36