Elgg  Version 5.1
entitypicker.js
Go to the documentation of this file.
1 define(['jquery', 'elgg', 'elgg/Ajax', 'jquery-ui/widgets/autocomplete', 'jquery.ui.autocomplete.html'], function($, elgg, Ajax) {
7  function EntityPicker(wrapper) {
8  this.$wrapper = $(wrapper);
9  this.$input = $('.elgg-input-entity-picker', wrapper);
10  this.$ul = $('.elgg-entity-picker-list', wrapper);
11 
12  var EntityPicker = this,
13  data = this.$wrapper.data();
14 
15  this.name = data.name || 'entities';
16  this.matchOn = data.matchOn || 'entities';
17  this.handler = data.handler || 'livesearch';
18  this.limit = data.limit || 0;
19  this.minLength = data.minLength || 2;
20  this.isSealed = false;
21 
22  this.$input.autocomplete({
23  source: function(request, response) {
24  // note: "this" below will not be bound to the input, but rather
25  // to an object created by the autocomplete component
26  var Autocomplete = this;
27 
28  if (EntityPicker.isSealed) {
29  return;
30  }
31 
32  var ajax = new Ajax();
33  ajax.path(EntityPicker.handler, {
34  data: {
35  term: Autocomplete.term,
36  match_on: EntityPicker.getSearchType(),
37  name: EntityPicker.name
38  },
39  method: 'GET',
40  success: function(data) {
41  response(data);
42  }
43  });
44  },
45  minLength: EntityPicker.minLength,
46  html: 'html',
47  select: function(event, ui) {
48  EntityPicker.addEntity(event, ui.item.guid, ui.item.html);
49  },
50  // turn off experimental live help - no i18n support and a little buggy
51  messages: {
52  noResults: '',
53  results: function() {}
54  }
55  });
56 
57  this.$wrapper.on('click', '.elgg-autocomplete-item-remove', function(event) {
58  EntityPicker.removeEntity(event);
59  });
60 
61  this.enforceLimit();
62  }
63 
64  EntityPicker.prototype = {
72  addEntity: function(event, guid, html) {
73  if (event.isDefaultPrevented()) {
74  return;
75  }
76 
77  // do not allow entities to be added multiple times
78  if (!$('li[data-guid="' + guid + '"]', this.$ul).length) {
79  this.$ul.append(html);
80  }
81 
82  this.$input.val('');
83 
84  this.enforceLimit();
85 
86  event.preventDefault();
87  },
88 
94  removeEntity: function(event) {
95  $(event.target).closest('.elgg-entity-picker-list > li').remove();
96 
97  this.enforceLimit();
98 
99  event.preventDefault();
100  },
101 
105  enforceLimit: function() {
106  if (this.limit) {
107  if ($('li[data-guid]', this.$ul).length >= this.limit) {
108  if (!this.isSealed) {
109  this.seal();
110  }
111  } else {
112  if (this.isSealed) {
113  this.unseal();
114  }
115  }
116  }
117  },
118 
122  seal: function() {
123  this.$input.prop('disabled', true);
124  this.$wrapper.addClass('elgg-state-disabled');
125  this.isSealed = true;
126  },
127 
131  unseal: function() {
132  this.$input.prop('disabled', false);
133  this.$wrapper.removeClass('elgg-state-disabled');
134  this.isSealed = false;
135  },
136 
140  getSearchType: function() {
141  if (this.$wrapper.has('[type="checkbox"][name="match_on"]:checked').length) {
142  return $('[type="checkbox"][name=match_on]:checked', this.$wrapper).val();
143  }
144 
145  return this.matchOn;
146  }
147  };
148 
152  EntityPicker.setup = function(selector) {
153  $(selector).each(function () {
154  // we only want to wrap each picker once
155  if (!$(this).data('initialized')) {
156  new EntityPicker(this);
157  $(this).data('initialized', 1);
158  }
159  });
160  };
161 
162  return EntityPicker;
163 });
define(['jquery', 'elgg', 'elgg/Ajax', 'jquery-ui/widgets/autocomplete', 'jquery.ui.autocomplete.html'], function($, elgg, Ajax){function EntityPicker(wrapper){this.$wrapper=$(wrapper);this.$input=$('.elgg-input-entity-picker', wrapper);this.$ul=$('.elgg-entity-picker-list', wrapper);var EntityPicker=this, data=this.$wrapper.data();this.name=data.name|| 'entities';this.matchOn=data.matchOn|| 'entities';this.handler=data.handler|| 'livesearch';this.limit=data.limit||0;this.minLength=data.minLength||2;this.isSealed=false;this.$input.autocomplete({source:function(request, response){var Autocomplete=this;if(EntityPicker.isSealed){return;}var ajax=new Ajax();ajax.path(EntityPicker.handler,{data:{term:Autocomplete.term, match_on:EntityPicker.getSearchType(), name:EntityPicker.name}, method: 'GET', success:function(data){response(data);}});}, minLength:EntityPicker.minLength, html: 'html', select:function(event, ui){EntityPicker.addEntity(event, ui.item.guid, ui.item.html);}, messages:{noResults: '', results:function(){}}});this.$wrapper.on('click', '.elgg-autocomplete-item-remove', function(event){EntityPicker.removeEntity(event);});this.enforceLimit();}EntityPicker.prototype={addEntity:function(event, guid, html){if(event.isDefaultPrevented()){return;}if(!$('li[data-guid="' + guid + '"]', this.$ul).length){this.$ul.append(html);}this.$input.val('');this.enforceLimit();event.preventDefault();}, removeEntity:function(event){$(event.target).closest('.elgg-entity-picker-list > li').remove();this.enforceLimit();event.preventDefault();}, enforceLimit:function(){if(this.limit){if($('li[data-guid]', this.$ul).length >=this.limit){if(!this.isSealed){this.seal();}}else{if(this.isSealed){this.unseal();}}}}, seal:function(){this.$input.prop('disabled', true);this.$wrapper.addClass('elgg-state-disabled');this.isSealed=true;}, unseal:function(){this.$input.prop('disabled', false);this.$wrapper.removeClass('elgg-state-disabled');this.isSealed=false;}, getSearchType:function(){if(this.$wrapper.has('[type="checkbox"][name="match_on"]:checked').length){return $('[type="checkbox"][name=match_on]:checked', this.$wrapper).val();}return this.matchOn;}};EntityPicker.setup=function(selector){$(selector).each(function(){if(!$(this).data('initialized')){new EntityPicker(this);$(this).data('initialized', 1);}});};return EntityPicker;})
$input
Form field view.
Definition: field.php:13
$site name
Definition: settings.php:15
var elgg
Definition: elgglib.js:4