2017-11-21 18:53:09 +08:00
|
|
|
import MultiSelectComponent from "select-kit/components/multi-select";
|
|
|
|
const { makeArray } = Ember;
|
|
|
|
|
|
|
|
export default MultiSelectComponent.extend({
|
|
|
|
pluginApiIdentifiers: ["admin-group-selector"],
|
|
|
|
classNames: "admin-group-selector",
|
|
|
|
selected: null,
|
|
|
|
available: null,
|
|
|
|
allowAny: false,
|
|
|
|
|
|
|
|
computeValues() {
|
2018-06-15 23:03:24 +08:00
|
|
|
return makeArray(this.get("selected")).map(s =>
|
|
|
|
this.valueForContentItem(s)
|
|
|
|
);
|
2017-11-21 18:53:09 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
computeContent() {
|
|
|
|
return makeArray(this.get("available"));
|
|
|
|
},
|
|
|
|
|
|
|
|
computeContentItem(contentItem, name) {
|
2018-03-22 18:29:55 +08:00
|
|
|
let computedContentItem = this._super(contentItem, name);
|
|
|
|
computedContentItem.locked = contentItem.automatic;
|
|
|
|
return computedContentItem;
|
2017-11-21 18:53:09 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
mutateValues(values) {
|
|
|
|
if (values.length > this.get("selected").length) {
|
2018-06-15 23:03:24 +08:00
|
|
|
const newValues = values.filter(
|
|
|
|
v =>
|
|
|
|
!this.get("selected")
|
|
|
|
.map(s => this.valueForContentItem(s))
|
|
|
|
.includes(v)
|
|
|
|
);
|
2017-11-21 18:53:09 +08:00
|
|
|
|
|
|
|
newValues.forEach(value => {
|
2018-06-15 23:03:24 +08:00
|
|
|
const actionContext = this.get("available").findBy(
|
|
|
|
this.get("valueAttribute"),
|
|
|
|
parseInt(value, 10)
|
|
|
|
);
|
2017-11-21 18:53:09 +08:00
|
|
|
|
|
|
|
this.triggerAction({ action: "groupAdded", actionContext });
|
|
|
|
});
|
|
|
|
} else if (values.length < this.get("selected").length) {
|
2018-06-15 23:03:24 +08:00
|
|
|
const selected = this.get("selected").filter(
|
|
|
|
s => !values.includes(this.valueForContentItem(s))
|
|
|
|
);
|
2017-11-21 18:53:09 +08:00
|
|
|
|
|
|
|
selected.forEach(s => {
|
|
|
|
this.triggerAction({
|
|
|
|
action: "groupRemoved",
|
|
|
|
actionContext: this.valueForContentItem(s)
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|