Files
discourse-chat-integration/assets/javascripts/admin/controllers/modals/admin-plugins-chat-edit-channel.js.es6
T

130 lines
2.9 KiB
JavaScript
Raw Normal View History

2020-09-04 13:23:28 +02:00
import I18n from "I18n";
2018-09-12 16:16:18 +01:00
import ModalFunctionality from "discourse/mixins/modal-functionality";
import { popupAjaxError } from "discourse/lib/ajax-error";
import InputValidation from "discourse/models/input-validation";
import {
default as computed,
observes,
2020-09-04 13:23:28 +02:00
on,
2020-02-19 11:56:45 -05:00
} from "discourse-common/utils/decorators";
export default Ember.Controller.extend(ModalFunctionality, {
2018-09-12 16:16:18 +01:00
@on("init")
2017-10-03 17:42:07 +08:00
setupKeydown() {
2018-09-12 16:16:18 +01:00
Ember.run.schedule("afterRender", () => {
2020-09-04 13:23:28 +02:00
$("#chat-integration-edit-channel-modal").keydown((e) => {
2017-07-28 15:09:04 +01:00
if (e.keyCode === 13) {
2018-09-12 16:16:18 +01:00
this.send("save");
2017-07-28 15:09:04 +01:00
}
});
});
2017-10-03 18:11:58 +08:00
},
2017-07-28 15:09:04 +01:00
// The validation property must be defined at runtime since the possible parameters vary by provider
2018-09-12 16:16:18 +01:00
@observes("model")
2017-10-03 17:42:07 +08:00
setupValidations() {
2018-09-12 16:16:18 +01:00
if (this.get("model.provider")) {
const theKeys = this.get("model.provider.channel_parameters").map(
2020-09-04 13:23:28 +02:00
(param) => param["key"]
2018-09-12 16:16:18 +01:00
);
Ember.defineProperty(
this,
"paramValidation",
Ember.computed(
`model.channel.data.{${theKeys.join(",")}}`,
this._paramValidation
)
);
}
2017-10-03 17:42:07 +08:00
},
2017-10-03 17:42:07 +08:00
validate(parameter) {
const regString = parameter.regex;
const regex = new RegExp(regString);
let val = this.get(`model.channel.data.${parameter.key}`);
2017-10-03 17:42:07 +08:00
if (val === undefined) {
2017-07-18 20:42:00 +01:00
val = "";
}
2018-09-12 16:16:18 +01:00
if (val === "") {
// Fail silently if field blank
return InputValidation.create({
2020-09-04 13:23:28 +02:00
failed: true,
});
2018-09-12 16:16:18 +01:00
} else if (!regString) {
// Pass silently if no regex available for provider
return InputValidation.create({
2020-09-04 13:23:28 +02:00
ok: true,
2018-09-12 16:16:18 +01:00
});
} else if (regex.test(val)) {
// Test against regex
return InputValidation.create({
ok: true,
2018-09-12 16:16:18 +01:00
reason: I18n.t(
"chat_integration.edit_channel_modal.channel_validation.ok"
2020-09-04 13:23:28 +02:00
),
});
2018-09-12 16:16:18 +01:00
} else {
// Failed regex
return InputValidation.create({
failed: true,
2018-09-12 16:16:18 +01:00
reason: I18n.t(
"chat_integration.edit_channel_modal.channel_validation.fail"
2020-09-04 13:23:28 +02:00
),
});
}
},
2017-10-03 17:42:07 +08:00
_paramValidation() {
const response = {};
2018-09-12 16:16:18 +01:00
const parameters = this.get("model.provider.channel_parameters");
2017-10-03 17:42:07 +08:00
2020-09-04 13:23:28 +02:00
parameters.forEach((parameter) => {
response[parameter.key] = this.validate(parameter);
});
2017-10-03 17:42:07 +08:00
return response;
},
2018-09-12 16:16:18 +01:00
@computed("paramValidation")
2017-10-03 17:42:07 +08:00
saveDisabled(paramValidation) {
2020-09-22 17:11:12 +02:00
if (!paramValidation) {
return true;
}
2017-08-21 16:46:43 +01:00
2017-10-03 17:42:07 +08:00
let invalid = false;
2020-09-04 13:23:28 +02:00
Object.keys(paramValidation).forEach((key) => {
if (!paramValidation[key]) {
invalid = true;
}
2017-10-03 17:42:07 +08:00
2018-09-12 16:16:18 +01:00
if (!paramValidation[key]["ok"]) {
invalid = true;
}
});
2017-08-21 16:46:43 +01:00
return invalid;
2017-10-03 17:42:07 +08:00
},
actions: {
2017-10-03 17:42:07 +08:00
cancel() {
2018-09-12 16:16:18 +01:00
this.send("closeModal");
},
2017-10-03 17:42:07 +08:00
save() {
2020-09-22 17:11:12 +02:00
if (this.get("saveDisabled")) {
return;
}
2018-09-12 16:16:18 +01:00
this.get("model.channel")
.save()
.then(() => {
this.send("closeModal");
})
.catch(popupAjaxError);
2020-09-04 13:23:28 +02:00
},
},
2017-10-03 17:42:07 +08:00
});