REFACTOR: badge-selector component (#7683)
This commit is contained in:
parent
63e3d49508
commit
c4419b7369
|
@ -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")
|
});
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue