This repository has been archived on 2023-03-18. You can view files and clone it, but cannot push or open issues or pull requests.
osr-discourse-src/app/assets/javascripts/discourse/components/badge-selector.js.es6
2019-05-27 10:42:53 +02:00

56 lines
1.5 KiB
JavaScript

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