2023-02-23 10:32:53 -05:00
|
|
|
import { action } from "@ember/object";
|
|
|
|
import { classNames } from "@ember-decorators/component";
|
2023-02-24 04:50:52 -05:00
|
|
|
import discourseComputed from "discourse-common/utils/decorators";
|
|
|
|
import { on } from "@ember-decorators/object";
|
2020-02-03 08:22:14 -05:00
|
|
|
import { empty, reads } from "@ember/object/computed";
|
2019-10-23 12:30:52 -04:00
|
|
|
import Component from "@ember/component";
|
2019-11-01 13:06:50 -04:00
|
|
|
import { makeArray } from "discourse-common/lib/helpers";
|
2018-08-03 16:41:37 -04:00
|
|
|
|
2023-02-23 10:32:53 -05:00
|
|
|
@classNames("value-list")
|
|
|
|
export default class ValueList extends Component {
|
|
|
|
@empty("newValue") inputInvalid;
|
|
|
|
|
|
|
|
inputDelimiter = null;
|
|
|
|
inputType = null;
|
|
|
|
newValue = "";
|
|
|
|
collection = null;
|
|
|
|
values = null;
|
|
|
|
|
|
|
|
@reads("addKey") noneKey;
|
2018-08-03 16:41:37 -04:00
|
|
|
|
|
|
|
@on("didReceiveAttrs")
|
|
|
|
_setupCollection() {
|
2019-05-27 04:15:39 -04:00
|
|
|
const values = this.values;
|
|
|
|
if (this.inputType === "array") {
|
2015-07-28 12:29:40 -04:00
|
|
|
this.set("collection", values || []);
|
2018-08-03 16:41:37 -04:00
|
|
|
return;
|
2015-07-28 12:29:40 -04:00
|
|
|
}
|
2015-06-09 12:19:41 -04:00
|
|
|
|
2018-08-03 16:41:37 -04:00
|
|
|
this.set(
|
|
|
|
"collection",
|
2019-05-27 04:15:39 -04:00
|
|
|
this._splitValues(values, this.inputDelimiter || "\n")
|
2018-08-03 16:41:37 -04:00
|
|
|
);
|
2023-02-23 10:32:53 -05:00
|
|
|
}
|
2015-06-09 12:19:41 -04:00
|
|
|
|
2019-11-07 16:38:28 -05:00
|
|
|
@discourseComputed("choices.[]", "collection.[]")
|
2018-08-03 16:41:37 -04:00
|
|
|
filteredChoices(choices, collection) {
|
2022-07-17 14:48:36 -04:00
|
|
|
return makeArray(choices).filter((i) => !collection.includes(i));
|
2023-02-23 10:32:53 -05:00
|
|
|
}
|
2015-06-09 12:19:41 -04:00
|
|
|
|
2018-08-03 16:41:37 -04:00
|
|
|
keyDown(event) {
|
2021-08-09 05:41:36 -04:00
|
|
|
if (event.key === "Enter") {
|
2020-09-22 10:28:28 -04:00
|
|
|
this.send("addValue", this.newValue);
|
|
|
|
}
|
2023-02-23 10:32:53 -05:00
|
|
|
}
|
2015-06-09 12:19:41 -04:00
|
|
|
|
2023-02-23 10:32:53 -05:00
|
|
|
@action
|
|
|
|
changeValue(index, event) {
|
|
|
|
this._replaceValue(index, event.target.value);
|
|
|
|
}
|
2015-06-09 12:19:41 -04:00
|
|
|
|
2023-02-23 10:32:53 -05:00
|
|
|
@action
|
|
|
|
addValue(newValue) {
|
|
|
|
if (this.inputInvalid) {
|
|
|
|
return;
|
|
|
|
}
|
2015-07-28 15:58:49 -04:00
|
|
|
|
2023-02-23 10:32:53 -05:00
|
|
|
this.set("newValue", null);
|
|
|
|
this._addValue(newValue);
|
|
|
|
}
|
2015-06-09 12:19:41 -04:00
|
|
|
|
2023-02-23 10:32:53 -05:00
|
|
|
@action
|
|
|
|
removeValue(value) {
|
|
|
|
this._removeValue(value);
|
|
|
|
}
|
2018-08-03 16:41:37 -04:00
|
|
|
|
2023-02-23 10:32:53 -05:00
|
|
|
@action
|
|
|
|
selectChoice(choice) {
|
|
|
|
this._addValue(choice);
|
|
|
|
}
|
2022-02-03 16:47:02 -05:00
|
|
|
|
2023-02-23 10:32:53 -05:00
|
|
|
@action
|
|
|
|
shift(operation, index) {
|
|
|
|
let futureIndex = index + operation;
|
2022-02-03 16:47:02 -05:00
|
|
|
|
2023-02-23 10:32:53 -05:00
|
|
|
if (futureIndex > this.collection.length - 1) {
|
|
|
|
futureIndex = 0;
|
|
|
|
} else if (futureIndex < 0) {
|
|
|
|
futureIndex = this.collection.length - 1;
|
|
|
|
}
|
2022-02-03 16:47:02 -05:00
|
|
|
|
2023-02-23 10:32:53 -05:00
|
|
|
const shiftedValue = this.collection[index];
|
|
|
|
this.collection.removeAt(index);
|
|
|
|
this.collection.insertAt(futureIndex, shiftedValue);
|
2022-02-03 16:47:02 -05:00
|
|
|
|
2023-02-23 10:32:53 -05:00
|
|
|
this._saveValues();
|
|
|
|
}
|
2018-08-03 16:41:37 -04:00
|
|
|
|
|
|
|
_addValue(value) {
|
2019-05-27 04:15:39 -04:00
|
|
|
this.collection.addObject(value);
|
2020-02-03 08:22:14 -05:00
|
|
|
|
|
|
|
if (this.choices) {
|
|
|
|
this.set("choices", this.choices.rejectBy("id", value));
|
|
|
|
} else {
|
|
|
|
this.set("choices", []);
|
|
|
|
}
|
|
|
|
|
2018-08-03 16:41:37 -04:00
|
|
|
this._saveValues();
|
2023-02-23 10:32:53 -05:00
|
|
|
}
|
2018-08-03 16:41:37 -04:00
|
|
|
|
|
|
|
_removeValue(value) {
|
2020-02-03 08:22:14 -05:00
|
|
|
this.collection.removeObject(value);
|
|
|
|
|
|
|
|
if (this.choices) {
|
2020-02-14 11:33:17 -05:00
|
|
|
this.set("choices", this.choices.concat([value]).uniq());
|
2020-02-03 08:22:14 -05:00
|
|
|
} else {
|
2020-02-14 11:21:06 -05:00
|
|
|
this.set("choices", makeArray(value));
|
2020-02-03 08:22:14 -05:00
|
|
|
}
|
|
|
|
|
2018-08-03 16:41:37 -04:00
|
|
|
this._saveValues();
|
2023-02-23 10:32:53 -05:00
|
|
|
}
|
2018-08-03 16:41:37 -04:00
|
|
|
|
|
|
|
_replaceValue(index, newValue) {
|
2019-05-27 04:15:39 -04:00
|
|
|
this.collection.replace(index, 1, [newValue]);
|
2018-08-03 16:41:37 -04:00
|
|
|
this._saveValues();
|
2023-02-23 10:32:53 -05:00
|
|
|
}
|
2018-08-03 16:41:37 -04:00
|
|
|
|
|
|
|
_saveValues() {
|
2019-05-27 04:15:39 -04:00
|
|
|
if (this.inputType === "array") {
|
|
|
|
this.set("values", this.collection);
|
2018-08-03 16:41:37 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-05-27 04:42:53 -04:00
|
|
|
this.set("values", this.collection.join(this.inputDelimiter || "\n"));
|
2023-02-23 10:32:53 -05:00
|
|
|
}
|
2018-08-03 16:41:37 -04:00
|
|
|
|
2022-02-03 16:47:02 -05:00
|
|
|
@discourseComputed("collection")
|
|
|
|
showUpDownButtons(collection) {
|
|
|
|
return collection.length - 1 ? true : false;
|
2023-02-23 10:32:53 -05:00
|
|
|
}
|
2022-02-03 16:47:02 -05:00
|
|
|
|
2018-08-03 16:41:37 -04:00
|
|
|
_splitValues(values, delimiter) {
|
|
|
|
if (values && values.length) {
|
|
|
|
return values.split(delimiter).filter((x) => x);
|
|
|
|
} else {
|
|
|
|
return [];
|
2015-06-09 12:19:41 -04:00
|
|
|
}
|
2023-02-23 10:32:53 -05:00
|
|
|
}
|
|
|
|
}
|