REFACTOR: badge-selector component (#7683)

This commit is contained in:
Joffrey JAFFEUX 2019-06-04 10:30:56 +02:00 committed by GitHub
parent 63e3d49508
commit c4419b7369
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 21 additions and 23 deletions

View File

@ -4,6 +4,7 @@ import {
default as computed default as computed
} from "ember-addons/ember-computed-decorators"; } from "ember-addons/ember-computed-decorators";
import { findRawTemplate } from "discourse/lib/raw-templates"; import { findRawTemplate } from "discourse/lib/raw-templates";
const { makeArray } = Ember;
export default Ember.Component.extend({ export default Ember.Component.extend({
@computed("placeholderKey") @computed("placeholderKey")
@ -13,43 +14,40 @@ export default Ember.Component.extend({
@observes("badgeNames") @observes("badgeNames")
_update() { _update() {
if (this.canReceiveUpdates === "true") if (this.canReceiveUpdates === "true") {
this._initializeAutocomplete({ updateData: true }); this._initializeAutocomplete({ updateData: true });
}
}, },
@on("didInsertElement") @on("didInsertElement")
_initializeAutocomplete(opts) { _initializeAutocomplete(opts) {
var self = this; let selectedBadges;
var selectedBadges;
self.$("input").autocomplete({ $(this.element.querySelector("input")).autocomplete({
allowAny: false, allowAny: false,
items: _.isArray(this.badgeNames) ? this.badgeNames : [this.badgeNames], items: makeArray(this.badgeNames),
single: this.single, single: this.single,
updateData: opts && opts.updateData ? opts.updateData : false, updateData: opts && opts.updateData ? opts.updateData : false,
onChangeItems: function(items) { template: findRawTemplate("badge-selector-autocomplete"),
onChangeItems(items) {
selectedBadges = items; selectedBadges = items;
self.set("badgeNames", items.join(",")); this.set("badgeNames", items.join(","));
}, },
transformComplete: function(g) {
transformComplete(g) {
return g.name; return g.name;
}, },
dataSource: function(term) {
return self
.get("badgeFinder")(term)
.then(function(badges) {
if (!selectedBadges) {
return badges;
}
return badges.filter(function(badge) { dataSource(term) {
return !selectedBadges.any(function(s) { return this.badgeFinder(term).then(badges => {
return s === badge.name; if (!selectedBadges) return badges;
});
}); return badges.filter(
}); badge => !selectedBadges.any(s => s === badge.name)
}, );
template: findRawTemplate("badge-selector-autocomplete") });
}
}); });
} }
}); });