0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2025-02-02 19:37:01 +01:00
discourse-custom-wizard/assets/javascripts/discourse/models/custom-wizard.js.es6

128 Zeilen
2,9 KiB
Text

2020-03-22 04:30:11 +11:00
import EmberObject from "@ember/object";
2022-07-27 11:47:50 +01:00
import { ajax } from "discourse/lib/ajax";
import { popupAjaxError } from "discourse/lib/ajax-error";
2022-07-27 11:47:50 +01:00
import discourseComputed from "discourse-common/utils/decorators";
import getUrl from "discourse-common/lib/get-url";
import CustomWizardField from "./custom-wizard-field";
import CustomWizardStep from "./custom-wizard-step";
2017-11-01 12:21:14 +08:00
2020-03-22 04:30:11 +11:00
const CustomWizard = EmberObject.extend({
2022-07-27 11:47:50 +01:00
@discourseComputed("steps.length")
totalSteps: (length) => length,
2017-09-24 11:01:18 +08:00
2022-07-27 11:47:50 +01:00
skip() {
if (this.required && !this.completed && this.permitted) {
return;
2020-04-08 17:59:54 +10:00
}
2022-07-27 11:47:50 +01:00
CustomWizard.skip(this.id);
},
2022-07-27 11:47:50 +01:00
restart() {
CustomWizard.restart(this.id);
},
});
2022-07-27 11:47:50 +01:00
CustomWizard.reopenClass({
skip(wizardId) {
ajax({ url: `/w/${wizardId}/skip`, type: "PUT" })
.then((result) => {
CustomWizard.finished(result);
})
.catch(popupAjaxError);
},
2022-07-27 11:47:50 +01:00
restart(wizardId) {
ajax({ url: `/w/${wizardId}/skip`, type: "PUT" })
.then(() => {
window.location.href = `/w/${wizardId}`;
})
.catch(popupAjaxError);
2020-04-08 17:59:54 +10:00
},
2022-07-27 11:47:50 +01:00
finished(result) {
let url = "/";
if (result.redirect_on_complete) {
url = result.redirect_on_complete;
2020-04-10 17:57:49 +10:00
}
2022-07-27 11:47:50 +01:00
window.location.href = getUrl(url);
},
2022-07-27 11:47:50 +01:00
build(wizardJson) {
if (!wizardJson) {
return null;
2020-04-10 17:57:49 +10:00
}
2022-07-27 11:47:50 +01:00
if (!wizardJson.completed && wizardJson.steps) {
wizardJson.steps = wizardJson.steps
.map((step) => {
const stepObj = CustomWizardStep.create(step);
stepObj.wizardId = wizardJson.id;
stepObj.fields.sort((a, b) => {
return parseFloat(a.number) - parseFloat(b.number);
});
let tabindex = 1;
stepObj.fields.forEach((f) => {
f.tabindex = tabindex;
if (["date_time"].includes(f.type)) {
tabindex = tabindex + 2;
} else {
tabindex++;
}
});
stepObj.fields = stepObj.fields.map((f) => {
f.wizardId = wizardJson.id;
f.stepId = stepObj.id;
return CustomWizardField.create(f);
});
return stepObj;
})
.sort((a, b) => {
return parseFloat(a.index) - parseFloat(b.index);
});
2020-04-10 17:57:49 +10:00
}
2022-07-27 11:47:50 +01:00
return CustomWizard.create(wizardJson);
2020-04-10 17:57:49 +10:00
},
2022-07-27 11:47:50 +01:00
});
2020-04-08 12:52:07 +10:00
2022-07-27 11:47:50 +01:00
export function findCustomWizard(wizardId, params = {}) {
let url = `/w/${wizardId}.json`;
let paramKeys = Object.keys(params).filter((k) => {
if (k === "wizard_id") {
2021-04-12 16:12:20 +10:00
return false;
}
2022-07-27 11:47:50 +01:00
return !!params[k];
});
if (paramKeys.length) {
url += "?";
paramKeys.forEach((k, i) => {
if (i > 0) {
url += "&";
2020-04-08 12:52:07 +10:00
}
2022-07-27 11:47:50 +01:00
url += `${k}=${params[k]}`;
2020-04-08 12:52:07 +10:00
});
2022-07-27 11:47:50 +01:00
}
2022-07-27 11:47:50 +01:00
return ajax(url).then((result) => {
return CustomWizard.build(result);
});
}
2017-09-23 10:34:07 +08:00
2022-07-27 11:47:50 +01:00
let _wizard_store;
2017-09-23 10:34:07 +08:00
2022-07-27 11:47:50 +01:00
export function updateCachedWizard(wizard) {
_wizard_store = wizard;
}
2017-10-05 08:36:46 +08:00
2022-07-27 11:47:50 +01:00
export function getCachedWizard() {
return _wizard_store;
}
2017-09-23 10:34:07 +08:00
export default CustomWizard;