2016-09-26 20:01:40 -04:00
|
|
|
import { on, observes, default as computed } from 'ember-addons/ember-computed-decorators';
|
2016-12-16 10:29:30 -05:00
|
|
|
import { getOwner } from 'discourse-common/lib/get-owner';
|
2016-12-13 10:53:08 -05:00
|
|
|
|
2014-12-17 15:09:06 -05:00
|
|
|
export default Ember.Component.extend({
|
2016-06-15 19:49:57 +02:00
|
|
|
@computed('placeholderKey')
|
|
|
|
placeholder(placeholderKey) {
|
|
|
|
return placeholderKey ? I18n.t(placeholderKey) : '';
|
|
|
|
},
|
2014-05-09 18:22:15 +10:00
|
|
|
|
2016-09-26 20:01:40 -04:00
|
|
|
@observes('groupNames')
|
|
|
|
_update() {
|
|
|
|
if (this.get('canReceiveUpdates') === 'true')
|
|
|
|
this._initializeAutocomplete({updateData: true});
|
|
|
|
},
|
|
|
|
|
2016-06-15 19:49:57 +02:00
|
|
|
@on('didInsertElement')
|
2016-09-26 20:01:40 -04:00
|
|
|
_initializeAutocomplete(opts) {
|
2014-05-09 18:22:15 +10:00
|
|
|
var self = this;
|
|
|
|
var selectedGroups;
|
2016-09-26 20:01:40 -04:00
|
|
|
var groupNames = this.get('groupNames');
|
2014-05-09 18:22:15 +10:00
|
|
|
|
2016-12-16 10:29:30 -05:00
|
|
|
var template = getOwner(this).lookup('template:group-selector-autocomplete.raw');
|
2014-05-09 18:22:15 +10:00
|
|
|
self.$('input').autocomplete({
|
|
|
|
allowAny: false,
|
2016-09-26 20:01:40 -04:00
|
|
|
items: _.isArray(groupNames) ? groupNames : (Ember.isEmpty(groupNames)) ? [] : [groupNames],
|
|
|
|
single: this.get('single'),
|
|
|
|
updateData: (opts && opts.updateData) ? opts.updateData : false,
|
2014-05-09 18:22:15 +10:00
|
|
|
onChangeItems: function(items){
|
|
|
|
selectedGroups = items;
|
|
|
|
self.set("groupNames", items.join(","));
|
|
|
|
},
|
|
|
|
transformComplete: function(g) {
|
|
|
|
return g.name;
|
|
|
|
},
|
|
|
|
dataSource: function(term) {
|
2014-07-22 20:11:21 -04:00
|
|
|
return self.get("groupFinder")(term).then(function(groups){
|
|
|
|
|
2014-05-09 18:22:15 +10:00
|
|
|
if(!selectedGroups){
|
|
|
|
return groups;
|
|
|
|
}
|
|
|
|
|
|
|
|
return groups.filter(function(group){
|
2015-09-15 17:08:50 -04:00
|
|
|
return !selectedGroups.any(function(s){return s === group.name;});
|
2014-05-09 18:22:15 +10:00
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
2016-12-16 10:29:30 -05:00
|
|
|
template: template
|
2014-05-09 18:22:15 +10:00
|
|
|
});
|
2016-06-15 19:49:57 +02:00
|
|
|
}
|
2014-05-09 18:22:15 +10:00
|
|
|
});
|