2019-11-08 05:38:28 +08:00
|
|
|
import discourseComputed from "discourse-common/utils/decorators";
|
2019-11-02 01:06:50 +08:00
|
|
|
import { makeArray } from "discourse-common/lib/helpers";
|
2020-02-03 21:22:14 +08:00
|
|
|
import { empty, reads } from "@ember/object/computed";
|
2019-10-24 00:30:52 +08:00
|
|
|
import Component from "@ember/component";
|
2019-11-08 05:38:28 +08:00
|
|
|
import { on } from "discourse-common/utils/decorators";
|
2018-08-04 04:41:37 +08:00
|
|
|
|
2019-10-24 00:30:52 +08:00
|
|
|
export default Component.extend({
|
2015-06-10 00:19:41 +08:00
|
|
|
classNameBindings: [":value-list"],
|
2019-10-31 04:28:29 +08:00
|
|
|
inputInvalid: empty("newValue"),
|
2018-08-04 04:41:37 +08:00
|
|
|
inputDelimiter: null,
|
|
|
|
inputType: null,
|
|
|
|
newValue: "",
|
|
|
|
collection: null,
|
|
|
|
values: null,
|
2020-02-03 21:22:14 +08:00
|
|
|
noneKey: reads("addKey"),
|
2018-08-04 04:41:37 +08:00
|
|
|
|
|
|
|
@on("didReceiveAttrs")
|
|
|
|
_setupCollection() {
|
2019-05-27 16:15:39 +08:00
|
|
|
const values = this.values;
|
|
|
|
if (this.inputType === "array") {
|
2015-07-29 00:29:40 +08:00
|
|
|
this.set("collection", values || []);
|
2018-08-04 04:41:37 +08:00
|
|
|
return;
|
2015-07-29 00:29:40 +08:00
|
|
|
}
|
2015-06-10 00:19:41 +08:00
|
|
|
|
2018-08-04 04:41:37 +08:00
|
|
|
this.set(
|
|
|
|
"collection",
|
2019-05-27 16:15:39 +08:00
|
|
|
this._splitValues(values, this.inputDelimiter || "\n")
|
2018-08-04 04:41:37 +08:00
|
|
|
);
|
2015-07-29 03:58:49 +08:00
|
|
|
},
|
2015-06-10 00:19:41 +08:00
|
|
|
|
2019-11-08 05:38:28 +08:00
|
|
|
@discourseComputed("choices.[]", "collection.[]")
|
2018-08-04 04:41:37 +08:00
|
|
|
filteredChoices(choices, collection) {
|
2019-11-01 01:55:01 +08:00
|
|
|
return makeArray(choices).filter(i => collection.indexOf(i) < 0);
|
2018-08-04 04:41:37 +08:00
|
|
|
},
|
2015-06-10 00:19:41 +08:00
|
|
|
|
2018-08-04 04:41:37 +08:00
|
|
|
keyDown(event) {
|
2019-05-27 16:15:39 +08:00
|
|
|
if (event.keyCode === 13) this.send("addValue", this.newValue);
|
2015-06-10 00:19:41 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
actions: {
|
2018-08-04 04:41:37 +08:00
|
|
|
changeValue(index, newValue) {
|
|
|
|
this._replaceValue(index, newValue);
|
|
|
|
},
|
2015-06-10 00:19:41 +08:00
|
|
|
|
2018-08-04 04:41:37 +08:00
|
|
|
addValue(newValue) {
|
2019-05-27 16:15:39 +08:00
|
|
|
if (this.inputInvalid) return;
|
2015-07-29 03:58:49 +08:00
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
this.set("newValue", null);
|
2018-08-04 04:41:37 +08:00
|
|
|
this._addValue(newValue);
|
2015-06-10 00:19:41 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
removeValue(value) {
|
2018-08-04 04:41:37 +08:00
|
|
|
this._removeValue(value);
|
|
|
|
},
|
|
|
|
|
|
|
|
selectChoice(choice) {
|
|
|
|
this._addValue(choice);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_addValue(value) {
|
2019-05-27 16:15:39 +08:00
|
|
|
this.collection.addObject(value);
|
2020-02-03 21:22:14 +08:00
|
|
|
|
|
|
|
if (this.choices) {
|
|
|
|
this.set("choices", this.choices.rejectBy("id", value));
|
|
|
|
} else {
|
|
|
|
this.set("choices", []);
|
|
|
|
}
|
|
|
|
|
2018-08-04 04:41:37 +08:00
|
|
|
this._saveValues();
|
|
|
|
},
|
|
|
|
|
|
|
|
_removeValue(value) {
|
2020-02-03 21:22:14 +08:00
|
|
|
this.collection.removeObject(value);
|
|
|
|
|
|
|
|
if (this.choices) {
|
2020-02-15 00:33:17 +08:00
|
|
|
this.set("choices", this.choices.concat([value]).uniq());
|
2020-02-03 21:22:14 +08:00
|
|
|
} else {
|
2020-02-15 00:21:06 +08:00
|
|
|
this.set("choices", makeArray(value));
|
2020-02-03 21:22:14 +08:00
|
|
|
}
|
|
|
|
|
2018-08-04 04:41:37 +08:00
|
|
|
this._saveValues();
|
|
|
|
},
|
|
|
|
|
|
|
|
_replaceValue(index, newValue) {
|
2019-05-27 16:15:39 +08:00
|
|
|
this.collection.replace(index, 1, [newValue]);
|
2018-08-04 04:41:37 +08:00
|
|
|
this._saveValues();
|
|
|
|
},
|
|
|
|
|
|
|
|
_saveValues() {
|
2019-05-27 16:15:39 +08:00
|
|
|
if (this.inputType === "array") {
|
|
|
|
this.set("values", this.collection);
|
2018-08-04 04:41:37 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-05-27 16:42:53 +08:00
|
|
|
this.set("values", this.collection.join(this.inputDelimiter || "\n"));
|
2018-08-04 04:41:37 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
_splitValues(values, delimiter) {
|
|
|
|
if (values && values.length) {
|
|
|
|
return values.split(delimiter).filter(x => x);
|
|
|
|
} else {
|
|
|
|
return [];
|
2015-06-10 00:19:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|