2020-05-14 04:23:41 +08:00
|
|
|
import I18n from "I18n";
|
2019-11-01 01:37:24 +08:00
|
|
|
import { isEmpty } from "@ember/utils";
|
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";
|
2019-11-06 02:43:49 +08:00
|
|
|
import { set } from "@ember/object";
|
2018-10-15 13:03:53 +08:00
|
|
|
|
2019-10-24 00:30:52 +08:00
|
|
|
export default Component.extend({
|
2018-10-15 13:03:53 +08:00
|
|
|
classNameBindings: [":value-list", ":secret-value-list"],
|
|
|
|
inputDelimiter: null,
|
|
|
|
collection: null,
|
|
|
|
values: null,
|
2018-11-05 04:18:58 +08:00
|
|
|
validationMessage: null,
|
2018-10-15 13:03:53 +08:00
|
|
|
|
|
|
|
@on("didReceiveAttrs")
|
|
|
|
_setupCollection() {
|
2019-05-27 16:15:39 +08:00
|
|
|
const values = this.values;
|
2018-10-15 13:03:53 +08:00
|
|
|
|
|
|
|
this.set(
|
|
|
|
"collection",
|
2019-05-27 16:15:39 +08:00
|
|
|
this._splitValues(values, this.inputDelimiter || "\n")
|
2018-10-15 13:03:53 +08:00
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
actions: {
|
|
|
|
changeKey(index, newValue) {
|
2018-11-05 04:18:58 +08:00
|
|
|
if (this._checkInvalidInput(newValue)) return;
|
2018-10-15 13:03:53 +08:00
|
|
|
this._replaceValue(index, newValue, "key");
|
|
|
|
},
|
|
|
|
|
|
|
|
changeSecret(index, newValue) {
|
2018-11-05 04:18:58 +08:00
|
|
|
if (this._checkInvalidInput(newValue)) return;
|
2018-10-15 13:03:53 +08:00
|
|
|
this._replaceValue(index, newValue, "secret");
|
|
|
|
},
|
|
|
|
|
|
|
|
addValue() {
|
2019-05-27 16:42:53 +08:00
|
|
|
if (this._checkInvalidInput([this.newKey, this.newSecret])) return;
|
2019-05-27 16:15:39 +08:00
|
|
|
this._addValue(this.newKey, this.newSecret);
|
2018-10-15 13:03:53 +08:00
|
|
|
this.setProperties({ newKey: "", newSecret: "" });
|
|
|
|
},
|
|
|
|
|
|
|
|
removeValue(value) {
|
|
|
|
this._removeValue(value);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-11-05 04:18:58 +08:00
|
|
|
_checkInvalidInput(inputs) {
|
|
|
|
this.set("validationMessage", null);
|
|
|
|
for (let input of inputs) {
|
2019-11-01 01:37:24 +08:00
|
|
|
if (isEmpty(input) || input.includes("|")) {
|
2018-11-05 04:18:58 +08:00
|
|
|
this.set(
|
|
|
|
"validationMessage",
|
|
|
|
I18n.t("admin.site_settings.secret_list.invalid_input")
|
|
|
|
);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-10-15 13:03:53 +08:00
|
|
|
_addValue(value, secret) {
|
2019-05-27 16:15:39 +08:00
|
|
|
this.collection.addObject({ key: value, secret: secret });
|
2018-10-15 13:03:53 +08:00
|
|
|
this._saveValues();
|
|
|
|
},
|
|
|
|
|
|
|
|
_removeValue(value) {
|
2019-05-27 16:15:39 +08:00
|
|
|
const collection = this.collection;
|
2018-10-15 13:03:53 +08:00
|
|
|
collection.removeObject(value);
|
|
|
|
this._saveValues();
|
|
|
|
},
|
|
|
|
|
|
|
|
_replaceValue(index, newValue, keyName) {
|
2019-05-27 16:15:39 +08:00
|
|
|
let item = this.collection[index];
|
2019-11-06 02:43:49 +08:00
|
|
|
set(item, keyName, newValue);
|
2018-10-15 13:03:53 +08:00
|
|
|
|
|
|
|
this._saveValues();
|
|
|
|
},
|
|
|
|
|
|
|
|
_saveValues() {
|
|
|
|
this.set(
|
|
|
|
"values",
|
2019-05-27 16:15:39 +08:00
|
|
|
this.collection
|
2018-10-15 13:03:53 +08:00
|
|
|
.map(function(elem) {
|
|
|
|
return `${elem.key}|${elem.secret}`;
|
|
|
|
})
|
|
|
|
.join("\n")
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
_splitValues(values, delimiter) {
|
|
|
|
if (values && values.length) {
|
|
|
|
const keys = ["key", "secret"];
|
|
|
|
var res = [];
|
|
|
|
values.split(delimiter).forEach(function(str) {
|
|
|
|
var object = {};
|
|
|
|
str.split("|").forEach(function(a, i) {
|
|
|
|
object[keys[i]] = a;
|
|
|
|
});
|
|
|
|
res.push(object);
|
|
|
|
});
|
|
|
|
|
|
|
|
return res;
|
|
|
|
} else {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|