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