0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2024-11-14 05:52:52 +01:00
discourse-custom-wizard/assets/javascripts/discourse/models/custom-wizard.js.es6

128 Zeilen
2,9 KiB
Text

2020-03-21 18:30:11 +01:00
import EmberObject from "@ember/object";
2022-07-27 12:47:50 +02:00
import { ajax } from "discourse/lib/ajax";
import { popupAjaxError } from "discourse/lib/ajax-error";
2022-07-27 12:47:50 +02: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 05:21:14 +01:00
2020-03-21 18:30:11 +01:00
const CustomWizard = EmberObject.extend({
2022-07-27 12:47:50 +02:00
@discourseComputed("steps.length")
totalSteps: (length) => length,
2017-09-24 05:01:18 +02:00
2022-07-27 12:47:50 +02:00
skip() {
if (this.required && !this.completed && this.permitted) {
return;
2020-04-08 09:59:54 +02:00
}
2022-07-27 12:47:50 +02:00
CustomWizard.skip(this.id);
},
2022-07-27 12:47:50 +02:00
restart() {
CustomWizard.restart(this.id);
},
});
2022-07-27 12:47:50 +02:00
CustomWizard.reopenClass({
skip(wizardId) {
ajax({ url: `/w/${wizardId}/skip`, type: "PUT" })
.then((result) => {
CustomWizard.finished(result);
})
.catch(popupAjaxError);
},
2022-07-27 12:47:50 +02:00
restart(wizardId) {
ajax({ url: `/w/${wizardId}/skip`, type: "PUT" })
.then(() => {
window.location.href = `/w/${wizardId}`;
})
.catch(popupAjaxError);
2020-04-08 09:59:54 +02:00
},
2022-07-27 12:47:50 +02:00
finished(result) {
let url = "/";
if (result.redirect_on_complete) {
url = result.redirect_on_complete;
2020-04-10 09:57:49 +02:00
}
2022-07-27 12:47:50 +02:00
window.location.href = getUrl(url);
},
2022-07-27 12:47:50 +02:00
build(wizardJson) {
if (!wizardJson) {
return null;
2020-04-10 09:57:49 +02:00
}
2022-07-27 12:47:50 +02: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 09:57:49 +02:00
}
2022-07-27 12:47:50 +02:00
return CustomWizard.create(wizardJson);
2020-04-10 09:57:49 +02:00
},
2022-07-27 12:47:50 +02:00
});
2020-04-08 04:52:07 +02:00
2022-07-27 12:47:50 +02: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 08:12:20 +02:00
return false;
}
2022-07-27 12:47:50 +02:00
return !!params[k];
});
if (paramKeys.length) {
url += "?";
paramKeys.forEach((k, i) => {
if (i > 0) {
url += "&";
2020-04-08 04:52:07 +02:00
}
2022-07-27 12:47:50 +02:00
url += `${k}=${params[k]}`;
2020-04-08 04:52:07 +02:00
});
2022-07-27 12:47:50 +02:00
}
2022-07-27 12:47:50 +02:00
return ajax(url).then((result) => {
return CustomWizard.build(result);
});
}
2017-09-23 04:34:07 +02:00
2022-07-27 12:47:50 +02:00
let _wizard_store;
2017-09-23 04:34:07 +02:00
2022-07-27 12:47:50 +02:00
export function updateCachedWizard(wizard) {
_wizard_store = wizard;
}
2017-10-05 02:36:46 +02:00
2022-07-27 12:47:50 +02:00
export function getCachedWizard() {
return _wizard_store;
}
2017-09-23 04:34:07 +02:00
export default CustomWizard;