2021-03-28 11:06:49 +02:00
|
|
|
import { default as computed } from "discourse-common/utils/decorators";
|
|
|
|
import getUrl from "discourse-common/lib/get-url";
|
2022-03-16 12:33:34 +01:00
|
|
|
import Field from "./field";
|
2022-07-26 16:18:09 +02:00
|
|
|
import { ajax } from "discourse/lib/ajax";
|
2021-11-02 09:11:46 +01:00
|
|
|
import { popupAjaxError } from "discourse/lib/ajax-error";
|
2022-03-16 12:33:34 +01:00
|
|
|
import Step from "./step";
|
2020-04-02 07:21:57 +02:00
|
|
|
import EmberObject from "@ember/object";
|
2021-10-06 13:55:50 +02:00
|
|
|
import Site from "./site";
|
2017-09-25 16:47:40 +02:00
|
|
|
|
2020-04-02 07:21:57 +02:00
|
|
|
const CustomWizard = EmberObject.extend({
|
2021-03-28 11:06:49 +02:00
|
|
|
@computed("steps.length")
|
|
|
|
totalSteps: (length) => length,
|
2017-11-01 05:21:14 +01:00
|
|
|
|
|
|
|
skip() {
|
2021-04-12 08:26:22 +02:00
|
|
|
if (this.required && !this.completed && this.permitted) {
|
|
|
|
return;
|
|
|
|
}
|
2020-04-02 07:21:57 +02:00
|
|
|
CustomWizard.skip(this.id);
|
2019-07-26 10:59:21 +02:00
|
|
|
},
|
2021-09-22 10:22:05 +02:00
|
|
|
|
|
|
|
restart() {
|
|
|
|
CustomWizard.restart(this.id);
|
|
|
|
},
|
2018-05-09 07:06:43 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
CustomWizard.reopenClass({
|
|
|
|
skip(wizardId) {
|
2021-11-02 09:11:46 +01:00
|
|
|
ajax({ url: `/w/${wizardId}/skip`, type: "PUT" })
|
|
|
|
.then((result) => {
|
|
|
|
CustomWizard.finished(result);
|
|
|
|
})
|
|
|
|
.catch(popupAjaxError);
|
2017-11-01 05:21:14 +01:00
|
|
|
},
|
|
|
|
|
2021-09-22 10:22:05 +02:00
|
|
|
restart(wizardId) {
|
2021-11-02 09:11:46 +01:00
|
|
|
ajax({ url: `/w/${wizardId}/skip`, type: "PUT" })
|
|
|
|
.then(() => {
|
|
|
|
window.location.href = `/w/${wizardId}`;
|
|
|
|
})
|
|
|
|
.catch(popupAjaxError);
|
2021-09-22 10:22:05 +02:00
|
|
|
},
|
|
|
|
|
2017-11-01 05:21:14 +01:00
|
|
|
finished(result) {
|
|
|
|
let url = "/";
|
2019-07-02 06:49:14 +02:00
|
|
|
if (result.redirect_on_complete) {
|
|
|
|
url = result.redirect_on_complete;
|
2017-11-01 05:21:14 +01:00
|
|
|
}
|
|
|
|
window.location.href = getUrl(url);
|
2021-03-28 11:06:49 +02:00
|
|
|
},
|
2019-01-14 03:53:53 +01:00
|
|
|
|
2021-04-20 19:58:19 +02:00
|
|
|
build(wizardJson) {
|
|
|
|
if (!wizardJson) {
|
2021-04-12 08:26:22 +02:00
|
|
|
return null;
|
|
|
|
}
|
2017-10-22 05:37:58 +02:00
|
|
|
|
2021-04-20 19:58:19 +02:00
|
|
|
if (!wizardJson.completed && wizardJson.steps) {
|
|
|
|
wizardJson.steps = wizardJson.steps
|
|
|
|
.map((step) => {
|
|
|
|
const stepObj = Step.create(step);
|
2022-06-15 09:08:28 +02:00
|
|
|
stepObj.wizardId = wizardJson.id;
|
2021-03-28 11:06:49 +02:00
|
|
|
|
2021-04-20 19:58:19 +02:00
|
|
|
stepObj.fields.sort((a, b) => {
|
|
|
|
return parseFloat(a.number) - parseFloat(b.number);
|
|
|
|
});
|
2021-03-28 11:06:49 +02:00
|
|
|
|
2021-04-20 19:58:19 +02:00
|
|
|
let tabindex = 1;
|
|
|
|
stepObj.fields.forEach((f) => {
|
|
|
|
f.tabindex = tabindex;
|
2021-03-28 11:06:49 +02:00
|
|
|
|
2021-04-20 19:58:19 +02:00
|
|
|
if (["date_time"].includes(f.type)) {
|
|
|
|
tabindex = tabindex + 2;
|
|
|
|
} else {
|
|
|
|
tabindex++;
|
|
|
|
}
|
|
|
|
});
|
2021-03-28 11:06:49 +02:00
|
|
|
|
2022-03-16 12:33:34 +01:00
|
|
|
stepObj.fields = stepObj.fields.map((f) => {
|
|
|
|
f.wizardId = wizardJson.id;
|
|
|
|
f.stepId = stepObj.id;
|
|
|
|
return Field.create(f);
|
|
|
|
});
|
2021-03-28 11:06:49 +02:00
|
|
|
|
2021-04-20 19:58:19 +02:00
|
|
|
return stepObj;
|
|
|
|
})
|
|
|
|
.sort((a, b) => {
|
|
|
|
return parseFloat(a.index) - parseFloat(b.index);
|
|
|
|
});
|
2017-10-13 15:02:34 +02:00
|
|
|
}
|
2017-09-25 16:47:40 +02:00
|
|
|
|
2021-04-20 19:58:19 +02:00
|
|
|
if (wizardJson.categories) {
|
2019-07-26 10:59:21 +02:00
|
|
|
let subcatMap = {};
|
|
|
|
let categoriesById = {};
|
2021-04-20 19:58:19 +02:00
|
|
|
let categories = wizardJson.categories.map((c) => {
|
2019-07-26 10:59:21 +02:00
|
|
|
if (c.parent_category_id) {
|
|
|
|
subcatMap[c.parent_category_id] =
|
|
|
|
subcatMap[c.parent_category_id] || [];
|
|
|
|
subcatMap[c.parent_category_id].push(c.id);
|
|
|
|
}
|
2020-04-02 07:21:57 +02:00
|
|
|
return (categoriesById[c.id] = EmberObject.create(c));
|
2019-07-26 10:59:21 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
// Associate the categories with their parents
|
2021-03-28 11:06:49 +02:00
|
|
|
categories.forEach((c) => {
|
2019-07-26 10:59:21 +02:00
|
|
|
let subcategoryIds = subcatMap[c.get("id")];
|
|
|
|
if (subcategoryIds) {
|
2021-03-28 11:06:49 +02:00
|
|
|
c.set(
|
|
|
|
"subcategories",
|
|
|
|
subcategoryIds.map((id) => categoriesById[id])
|
|
|
|
);
|
2019-07-26 10:59:21 +02:00
|
|
|
}
|
|
|
|
if (c.get("parent_category_id")) {
|
|
|
|
c.set("parentCategory", categoriesById[c.get("parent_category_id")]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-03-16 14:09:23 +01:00
|
|
|
Site.currentProp("categories", categories);
|
2021-10-06 13:55:50 +02:00
|
|
|
Site.currentProp("listByActivity", categories);
|
|
|
|
Site.currentProp("categoriesById", categoriesById);
|
|
|
|
Site.currentProp(
|
2021-03-28 11:06:49 +02:00
|
|
|
"uncategorized_category_id",
|
2021-04-20 19:58:19 +02:00
|
|
|
wizardJson.uncategorized_category_id
|
2021-03-28 11:06:49 +02:00
|
|
|
);
|
2019-07-26 10:59:21 +02:00
|
|
|
}
|
|
|
|
|
2021-04-20 19:58:19 +02:00
|
|
|
return CustomWizard.create(wizardJson);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
export function findCustomWizard(wizardId, params = {}) {
|
|
|
|
let url = `/w/${wizardId}`;
|
|
|
|
|
|
|
|
let paramKeys = Object.keys(params).filter((k) => {
|
|
|
|
if (k === "wizard_id") {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return !!params[k];
|
2017-09-25 16:47:40 +02:00
|
|
|
});
|
2021-04-20 19:58:19 +02:00
|
|
|
|
|
|
|
if (paramKeys.length) {
|
|
|
|
url += "?";
|
|
|
|
paramKeys.forEach((k, i) => {
|
|
|
|
if (i > 0) {
|
|
|
|
url += "&";
|
|
|
|
}
|
|
|
|
url += `${k}=${params[k]}`;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return ajax({ url, cache: false, dataType: "json" }).then((result) => {
|
|
|
|
return CustomWizard.build(result);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
let _wizard_store;
|
|
|
|
|
|
|
|
export function updateCachedWizard(wizard) {
|
|
|
|
_wizard_store = wizard;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getCachedWizard() {
|
|
|
|
return _wizard_store;
|
2021-03-28 11:06:49 +02:00
|
|
|
}
|
2017-09-29 13:27:03 +02:00
|
|
|
|
|
|
|
export default CustomWizard;
|