0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2024-11-25 10:40:28 +01:00
discourse-custom-wizard/assets/javascripts/discourse/models/custom-wizard-field.js.es6

79 Zeilen
1,8 KiB
Text

2022-03-16 12:33:34 +01:00
import EmberObject from "@ember/object";
import ValidState from "discourse/plugins/discourse-custom-wizard/discourse/mixins/valid-state";
2022-03-16 12:33:34 +01:00
import discourseComputed from "discourse-common/utils/decorators";
import { translationOrText } from "discourse/plugins/discourse-custom-wizard/discourse/lib/wizard";
2022-03-16 12:33:34 +01:00
const StandardFieldValidation = [
"text",
"number",
"textarea",
"dropdown",
"tag",
"image",
"user_selector",
"text_only",
"composer",
"category",
"group",
"date",
"time",
"date_time",
];
export default EmberObject.extend(ValidState, {
id: null,
type: null,
value: null,
required: null,
@discourseComputed("wizardId", "stepId", "id")
i18nKey(wizardId, stepId, id) {
return `${wizardId}.${stepId}.${id}`;
},
@discourseComputed("i18nKey", "label")
translatedLabel(i18nKey, label) {
return translationOrText(`${i18nKey}.label`, label);
2022-03-16 12:33:34 +01:00
},
@discourseComputed("i18nKey", "placeholder")
translatedPlaceholder(i18nKey, placeholder) {
return translationOrText(`${i18nKey}.placeholder`, placeholder);
2022-03-16 12:33:34 +01:00
},
@discourseComputed("i18nKey", "description")
translatedDescription(i18nKey, description) {
return translationOrText(`${i18nKey}.description`, description);
2022-03-16 12:33:34 +01:00
},
check() {
if (this.customCheck) {
return this.customCheck();
}
let valid = this.valid;
if (!this.required) {
this.setValid(true);
return true;
}
const val = this.get("value");
const type = this.get("type");
if (type === "checkbox") {
valid = val;
} else if (type === "upload") {
valid = val && val.id > 0;
} else if (StandardFieldValidation.indexOf(type) > -1) {
valid = val && val.toString().length > 0;
} else if (type === "url") {
valid = true;
}
this.setValid(Boolean(valid));
2022-03-16 12:33:34 +01:00
return valid;
2022-06-15 08:59:09 +02:00
},
2022-03-16 12:33:34 +01:00
});