2019-10-31 13:37:24 -04:00
|
|
|
import { isEmpty } from "@ember/utils";
|
2019-10-23 12:30:52 -04:00
|
|
|
import Component from "@ember/component";
|
2019-11-07 16:38:28 -05:00
|
|
|
import { on } from "discourse-common/utils/decorators";
|
2019-11-05 13:43:49 -05:00
|
|
|
import { set } from "@ember/object";
|
2018-10-15 01:03:53 -04:00
|
|
|
|
2019-10-23 12:30:52 -04:00
|
|
|
export default Component.extend({
|
2018-10-15 01:03:53 -04:00
|
|
|
classNameBindings: [":value-list", ":secret-value-list"],
|
|
|
|
inputDelimiter: null,
|
|
|
|
collection: null,
|
|
|
|
values: null,
|
2018-11-04 15:18:58 -05:00
|
|
|
validationMessage: null,
|
2018-10-15 01:03:53 -04:00
|
|
|
|
|
|
|
@on("didReceiveAttrs")
|
|
|
|
_setupCollection() {
|
2019-05-27 04:15:39 -04:00
|
|
|
const values = this.values;
|
2018-10-15 01:03:53 -04:00
|
|
|
|
|
|
|
this.set(
|
|
|
|
"collection",
|
2019-05-27 04:15:39 -04:00
|
|
|
this._splitValues(values, this.inputDelimiter || "\n")
|
2018-10-15 01:03:53 -04:00
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
actions: {
|
|
|
|
changeKey(index, newValue) {
|
2018-11-04 15:18:58 -05:00
|
|
|
if (this._checkInvalidInput(newValue)) return;
|
2018-10-15 01:03:53 -04:00
|
|
|
this._replaceValue(index, newValue, "key");
|
|
|
|
},
|
|
|
|
|
|
|
|
changeSecret(index, newValue) {
|
2018-11-04 15:18:58 -05:00
|
|
|
if (this._checkInvalidInput(newValue)) return;
|
2018-10-15 01:03:53 -04:00
|
|
|
this._replaceValue(index, newValue, "secret");
|
|
|
|
},
|
|
|
|
|
|
|
|
addValue() {
|
2019-05-27 04:42:53 -04:00
|
|
|
if (this._checkInvalidInput([this.newKey, this.newSecret])) return;
|
2019-05-27 04:15:39 -04:00
|
|
|
this._addValue(this.newKey, this.newSecret);
|
2018-10-15 01:03:53 -04:00
|
|
|
this.setProperties({ newKey: "", newSecret: "" });
|
|
|
|
},
|
|
|
|
|
|
|
|
removeValue(value) {
|
|
|
|
this._removeValue(value);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-11-04 15:18:58 -05:00
|
|
|
_checkInvalidInput(inputs) {
|
|
|
|
this.set("validationMessage", null);
|
|
|
|
for (let input of inputs) {
|
2019-10-31 13:37:24 -04:00
|
|
|
if (isEmpty(input) || input.includes("|")) {
|
2018-11-04 15:18:58 -05:00
|
|
|
this.set(
|
|
|
|
"validationMessage",
|
|
|
|
I18n.t("admin.site_settings.secret_list.invalid_input")
|
|
|
|
);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-10-15 01:03:53 -04:00
|
|
|
_addValue(value, secret) {
|
2019-05-27 04:15:39 -04:00
|
|
|
this.collection.addObject({ key: value, secret: secret });
|
2018-10-15 01:03:53 -04:00
|
|
|
this._saveValues();
|
|
|
|
},
|
|
|
|
|
|
|
|
_removeValue(value) {
|
2019-05-27 04:15:39 -04:00
|
|
|
const collection = this.collection;
|
2018-10-15 01:03:53 -04:00
|
|
|
collection.removeObject(value);
|
|
|
|
this._saveValues();
|
|
|
|
},
|
|
|
|
|
|
|
|
_replaceValue(index, newValue, keyName) {
|
2019-05-27 04:15:39 -04:00
|
|
|
let item = this.collection[index];
|
2019-11-05 13:43:49 -05:00
|
|
|
set(item, keyName, newValue);
|
2018-10-15 01:03:53 -04:00
|
|
|
|
|
|
|
this._saveValues();
|
|
|
|
},
|
|
|
|
|
|
|
|
_saveValues() {
|
|
|
|
this.set(
|
|
|
|
"values",
|
2019-05-27 04:15:39 -04:00
|
|
|
this.collection
|
2018-10-15 01:03:53 -04: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 [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|