2020-10-01 07:43:29 +02:00
|
|
|
import { default as computed } from "discourse-common/utils/decorators";
|
2020-04-05 03:37:09 +02:00
|
|
|
import { dasherize } from "@ember/string";
|
2019-09-11 10:30:38 +02:00
|
|
|
|
2017-09-25 16:47:40 +02:00
|
|
|
export default {
|
2020-10-01 07:43:29 +02:00
|
|
|
name: "custom-routes",
|
|
|
|
after: "sniff-capabilities",
|
2019-07-26 10:59:21 +02:00
|
|
|
initialize(app) {
|
2020-10-01 07:43:29 +02:00
|
|
|
if (window.location.pathname.indexOf("/w/") < 0) return;
|
|
|
|
|
|
|
|
const EmberObject = requirejs("@ember/object").default;
|
|
|
|
const Router = requirejs("wizard/router").default;
|
|
|
|
const ApplicationRoute = requirejs("wizard/routes/application").default;
|
|
|
|
const ajax = requirejs("wizard/lib/ajax").ajax;
|
|
|
|
const StepModel = requirejs("wizard/models/step").default;
|
|
|
|
const CustomWizard = requirejs(
|
|
|
|
"discourse/plugins/discourse-custom-wizard/wizard/models/custom"
|
|
|
|
).default;
|
|
|
|
const WizardStep = requirejs("wizard/components/wizard-step").default;
|
|
|
|
const WizardField = requirejs("wizard/components/wizard-field").default;
|
|
|
|
const getUrl = requirejs("discourse-common/lib/get-url").default;
|
|
|
|
const FieldModel = requirejs("wizard/models/wizard-field").default;
|
|
|
|
const autocomplete = requirejs("discourse/lib/autocomplete").default;
|
|
|
|
const cook = requirejs(
|
|
|
|
"discourse/plugins/discourse-custom-wizard/wizard/lib/text-lite"
|
|
|
|
).cook;
|
2019-07-26 10:59:21 +02:00
|
|
|
const Singleton = requirejs("discourse/mixins/singleton").default;
|
2019-11-14 08:28:26 +01:00
|
|
|
const Store = requirejs("discourse/models/store").default;
|
2020-10-01 07:43:29 +02:00
|
|
|
const registerRawHelpers = requirejs(
|
|
|
|
"discourse-common/lib/raw-handlebars-helpers"
|
|
|
|
).registerRawHelpers;
|
|
|
|
const createHelperContext = requirejs("discourse-common/lib/helpers")
|
|
|
|
.createHelperContext;
|
|
|
|
const RawHandlebars = requirejs("discourse-common/lib/raw-handlebars")
|
|
|
|
.default;
|
|
|
|
const Site = requirejs(
|
|
|
|
"discourse/plugins/discourse-custom-wizard/wizard/models/site"
|
|
|
|
).default;
|
2020-03-31 05:14:49 +02:00
|
|
|
const RestAdapter = requirejs("discourse/adapters/rest").default;
|
2020-09-01 03:01:46 +02:00
|
|
|
const Session = requirejs("discourse/models/session").default;
|
2020-10-01 07:43:29 +02:00
|
|
|
const setDefaultOwner = requirejs("discourse-common/lib/get-owner")
|
|
|
|
.setDefaultOwner;
|
|
|
|
const messageBus = requirejs("message-bus-client").default;
|
2020-09-01 03:01:46 +02:00
|
|
|
const container = app.__container__;
|
2020-03-31 05:14:49 +02:00
|
|
|
Discourse.Model = EmberObject.extend();
|
2020-09-01 03:01:46 +02:00
|
|
|
Discourse.__container__ = container;
|
|
|
|
setDefaultOwner(container);
|
2019-11-20 13:08:04 +01:00
|
|
|
registerRawHelpers(RawHandlebars, Handlebars);
|
2019-09-11 10:30:38 +02:00
|
|
|
|
2018-11-19 22:10:33 +01:00
|
|
|
// IE11 Polyfill - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries#Polyfill
|
|
|
|
if (!Object.entries)
|
2020-10-01 07:43:29 +02:00
|
|
|
Object.entries = function (obj) {
|
|
|
|
var ownProps = Object.keys(obj),
|
|
|
|
i = ownProps.length,
|
|
|
|
resArray = new Array(i); // preallocate the Array
|
|
|
|
while (i--) resArray[i] = [ownProps[i], obj[ownProps[i]]];
|
2018-11-19 22:10:33 +01:00
|
|
|
|
|
|
|
return resArray;
|
|
|
|
};
|
|
|
|
|
2017-11-23 10:03:19 +01:00
|
|
|
$.fn.autocomplete = autocomplete;
|
2020-10-01 07:43:29 +02:00
|
|
|
|
|
|
|
Object.keys(Ember.TEMPLATES).forEach((k) => {
|
|
|
|
if (k.indexOf("select-kit") === 0) {
|
2020-09-01 13:40:12 +02:00
|
|
|
let template = Ember.TEMPLATES[k];
|
|
|
|
define(k, () => template);
|
|
|
|
}
|
|
|
|
});
|
2017-11-23 10:03:19 +01:00
|
|
|
|
2019-07-26 10:59:21 +02:00
|
|
|
const targets = ["controller", "component", "route", "model", "adapter"];
|
|
|
|
|
|
|
|
const siteSettings = Wizard.SiteSettings;
|
|
|
|
app.register("site-settings:main", siteSettings, { instantiate: false });
|
2020-08-19 05:23:10 +02:00
|
|
|
createHelperContext({ siteSettings });
|
2020-10-01 07:43:29 +02:00
|
|
|
targets.forEach((t) => app.inject(t, "siteSettings", "site-settings:main"));
|
2020-09-24 14:04:32 +02:00
|
|
|
|
|
|
|
app.register("message-bus:main", messageBus, { instantiate: false });
|
2020-10-01 07:43:29 +02:00
|
|
|
targets.forEach((t) => app.inject(t, "messageBus", "message-bus:main"));
|
2020-09-24 14:04:32 +02:00
|
|
|
|
2019-11-14 08:28:26 +01:00
|
|
|
app.register("service:store", Store);
|
2020-10-01 07:43:29 +02:00
|
|
|
targets.forEach((t) => app.inject(t, "store", "service:store"));
|
|
|
|
targets.forEach((t) => app.inject(t, "appEvents", "service:app-events"));
|
|
|
|
|
2020-03-31 05:14:49 +02:00
|
|
|
app.register("adapter:rest", RestAdapter);
|
2020-10-01 07:43:29 +02:00
|
|
|
|
2020-03-31 05:14:49 +02:00
|
|
|
const site = Site.current();
|
|
|
|
app.register("site:main", site, { instantiate: false });
|
2020-10-01 07:43:29 +02:00
|
|
|
targets.forEach((t) => app.inject(t, "site", "site:main"));
|
|
|
|
|
|
|
|
site.set("can_create_tag", false);
|
|
|
|
app.register("session:main", Session.current(), { instantiate: false });
|
|
|
|
targets.forEach((t) => app.inject(t, "session", "session:main"));
|
2020-09-01 03:01:46 +02:00
|
|
|
let context = {
|
|
|
|
siteSettings: container.lookup("site-settings:main"),
|
|
|
|
currentUser: container.lookup("current-user:main"),
|
|
|
|
site: container.lookup("site:main"),
|
|
|
|
session: container.lookup("session:main"),
|
2020-10-01 07:43:29 +02:00
|
|
|
capabilities: container.lookup("capabilities:main"),
|
2020-09-01 03:01:46 +02:00
|
|
|
};
|
|
|
|
createHelperContext(context);
|
2020-10-01 07:43:29 +02:00
|
|
|
const session = container.lookup("session:main");
|
|
|
|
const setupData = document.getElementById("data-discourse-setup").dataset;
|
|
|
|
session.set("highlightJsPath", setupData.highlightJsPath);
|
2017-10-13 15:02:34 +02:00
|
|
|
Router.reopen({
|
2020-10-01 07:43:29 +02:00
|
|
|
rootURL: getUrl("/w/"),
|
2017-10-13 15:02:34 +02:00
|
|
|
});
|
|
|
|
|
2020-10-01 07:43:29 +02:00
|
|
|
Router.map(function () {
|
|
|
|
this.route("custom", { path: "/:wizard_id" }, function () {
|
|
|
|
this.route("steps");
|
|
|
|
this.route("step", { path: "/steps/:step_id" });
|
2017-09-25 16:47:40 +02:00
|
|
|
});
|
|
|
|
});
|
2017-09-29 13:27:03 +02:00
|
|
|
|
2017-10-13 15:02:34 +02:00
|
|
|
ApplicationRoute.reopen({
|
|
|
|
redirect() {
|
2020-10-01 07:43:29 +02:00
|
|
|
this.transitionTo("custom");
|
2017-10-15 05:58:22 +02:00
|
|
|
},
|
|
|
|
|
2020-10-01 07:43:29 +02:00
|
|
|
model() {},
|
2017-09-29 13:27:03 +02:00
|
|
|
});
|
2017-10-05 02:36:46 +02:00
|
|
|
|
|
|
|
WizardStep.reopen({
|
2020-10-01 07:43:29 +02:00
|
|
|
classNameBindings: ["step.id"],
|
2017-11-15 08:36:44 +01:00
|
|
|
|
2018-02-04 10:23:28 +01:00
|
|
|
animateInvalidFields() {
|
2020-10-01 07:43:29 +02:00
|
|
|
Ember.run.scheduleOnce("afterRender", () => {
|
|
|
|
let $element = $(
|
|
|
|
".invalid input[type=text], .invalid textarea, .invalid input[type=checkbox], .invalid .select-kit"
|
|
|
|
);
|
|
|
|
|
2020-04-14 15:08:02 +02:00
|
|
|
if ($element.length) {
|
2020-10-01 07:43:29 +02:00
|
|
|
$([document.documentElement, document.body]).animate(
|
|
|
|
{
|
|
|
|
scrollTop: $element.offset().top - 200,
|
|
|
|
},
|
|
|
|
400,
|
|
|
|
function () {
|
|
|
|
$element.wiggle(2, 100);
|
|
|
|
}
|
|
|
|
);
|
2020-04-14 15:08:02 +02:00
|
|
|
}
|
2018-02-04 10:23:28 +01:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2020-10-01 07:43:29 +02:00
|
|
|
ensureStartsAtTop: function () {
|
|
|
|
window.scrollTo(0, 0);
|
|
|
|
}.observes("step.id"),
|
2018-02-01 06:11:23 +01:00
|
|
|
|
2020-10-01 07:43:29 +02:00
|
|
|
showQuitButton: function () {
|
|
|
|
const index = this.get("step.index");
|
|
|
|
const required = this.get("wizard.required");
|
2017-11-01 05:21:14 +01:00
|
|
|
return index === 0 && !required;
|
2020-10-01 07:43:29 +02:00
|
|
|
}.property("step.index", "wizard.required"),
|
2017-11-01 05:21:14 +01:00
|
|
|
|
2020-10-01 07:43:29 +02:00
|
|
|
cookedTitle: function () {
|
|
|
|
return cook(this.get("step.title"));
|
|
|
|
}.property("step.title"),
|
2019-06-19 06:32:03 +02:00
|
|
|
|
2020-10-01 07:43:29 +02:00
|
|
|
cookedDescription: function () {
|
|
|
|
return cook(this.get("step.description"));
|
|
|
|
}.property("step.description"),
|
2018-03-05 02:52:15 +01:00
|
|
|
|
2020-10-01 07:43:29 +02:00
|
|
|
bannerImage: function () {
|
|
|
|
const src = this.get("step.banner");
|
2017-10-06 04:59:02 +02:00
|
|
|
if (!src) return;
|
2018-05-24 07:34:58 +02:00
|
|
|
return getUrl(src);
|
2020-10-01 07:43:29 +02:00
|
|
|
}.property("step.banner"),
|
2017-10-06 04:59:02 +02:00
|
|
|
|
2020-10-01 07:43:29 +02:00
|
|
|
handleMessage: function () {
|
|
|
|
const message = this.get("step.message");
|
|
|
|
this.sendAction("showMessage", message);
|
|
|
|
}.observes("step.message"),
|
2017-10-13 15:02:34 +02:00
|
|
|
|
2017-10-05 02:36:46 +02:00
|
|
|
advance() {
|
2020-10-01 07:43:29 +02:00
|
|
|
this.set("saving", true);
|
|
|
|
this.get("step")
|
|
|
|
.save()
|
|
|
|
.then((response) => {
|
|
|
|
if (this.get("finalStep")) {
|
2018-05-09 07:06:43 +02:00
|
|
|
CustomWizard.finished(response);
|
2017-10-05 02:36:46 +02:00
|
|
|
} else {
|
2020-10-01 07:43:29 +02:00
|
|
|
this.sendAction("goNext", response);
|
2017-10-05 02:36:46 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(() => this.animateInvalidFields())
|
2020-10-01 07:43:29 +02:00
|
|
|
.finally(() => this.set("saving", false));
|
2019-11-20 13:08:04 +01:00
|
|
|
},
|
2017-10-05 02:36:46 +02:00
|
|
|
|
2020-10-01 07:43:29 +02:00
|
|
|
keyPress(key) {},
|
|
|
|
|
2017-10-05 02:36:46 +02:00
|
|
|
actions: {
|
|
|
|
quit() {
|
2020-10-01 07:43:29 +02:00
|
|
|
this.get("wizard").skip();
|
2017-11-03 14:24:09 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
done() {
|
2020-10-01 07:43:29 +02:00
|
|
|
this.set("finalStep", true);
|
|
|
|
this.send("nextStep");
|
2017-10-13 15:02:34 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
showMessage(message) {
|
2020-10-01 07:43:29 +02:00
|
|
|
this.sendAction("showMessage", message);
|
|
|
|
},
|
|
|
|
},
|
2017-10-05 02:36:46 +02:00
|
|
|
});
|
2017-10-09 07:52:09 +02:00
|
|
|
|
2017-10-13 15:02:34 +02:00
|
|
|
StepModel.reopen({
|
|
|
|
save() {
|
2020-10-01 07:43:29 +02:00
|
|
|
const wizardId = this.get("wizardId");
|
2017-10-13 15:02:34 +02:00
|
|
|
const fields = {};
|
2018-05-24 07:34:58 +02:00
|
|
|
|
2020-10-01 07:43:29 +02:00
|
|
|
this.get("fields").forEach((f) => {
|
|
|
|
if (f.type !== "text_only") {
|
2018-05-24 07:34:58 +02:00
|
|
|
fields[f.id] = f.value;
|
|
|
|
}
|
|
|
|
});
|
2020-10-01 07:43:29 +02:00
|
|
|
|
2017-10-13 15:02:34 +02:00
|
|
|
return ajax({
|
2020-10-01 07:43:29 +02:00
|
|
|
url: `/w/${wizardId}/steps/${this.get("id")}`,
|
|
|
|
type: "PUT",
|
|
|
|
data: { fields },
|
|
|
|
}).catch((response) => {
|
|
|
|
if (
|
|
|
|
response &&
|
|
|
|
response.responseJSON &&
|
|
|
|
response.responseJSON.errors
|
|
|
|
) {
|
2017-10-13 15:02:34 +02:00
|
|
|
let wizardErrors = [];
|
2020-10-01 07:43:29 +02:00
|
|
|
response.responseJSON.errors.forEach((err) => {
|
2017-10-13 15:02:34 +02:00
|
|
|
if (err.field === wizardId) {
|
|
|
|
wizardErrors.push(err.description);
|
|
|
|
} else if (err.field) {
|
|
|
|
this.fieldError(err.field, err.description);
|
|
|
|
} else if (err) {
|
|
|
|
wizardErrors.push(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (wizardErrors.length) {
|
2020-10-01 07:43:29 +02:00
|
|
|
this.handleWizardError(wizardErrors.join("\n"));
|
2017-10-13 15:02:34 +02:00
|
|
|
}
|
2020-04-14 07:46:06 +02:00
|
|
|
this.animateInvalidFields();
|
2017-10-13 15:02:34 +02:00
|
|
|
throw response;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (response && response.responseText) {
|
|
|
|
const responseText = response.responseText;
|
2020-10-01 07:43:29 +02:00
|
|
|
const start = responseText.indexOf(">") + 1;
|
|
|
|
const end = responseText.indexOf("plugins");
|
2017-10-13 15:02:34 +02:00
|
|
|
const message = responseText.substring(start, end);
|
|
|
|
this.handleWizardError(message);
|
|
|
|
throw message;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
handleWizardError(message) {
|
2020-10-01 07:43:29 +02:00
|
|
|
this.set("message", {
|
|
|
|
state: "error",
|
|
|
|
text: message,
|
2017-10-13 15:02:34 +02:00
|
|
|
});
|
2020-10-01 07:43:29 +02:00
|
|
|
Ember.run.later(() => this.set("message", null), 6000);
|
|
|
|
},
|
2017-10-13 15:02:34 +02:00
|
|
|
});
|
|
|
|
|
2018-01-30 14:19:32 +01:00
|
|
|
WizardField.reopen({
|
2020-10-01 07:43:29 +02:00
|
|
|
classNameBindings: ["field.id"],
|
|
|
|
|
|
|
|
cookedDescription: function () {
|
|
|
|
return cook(this.get("field.description"));
|
|
|
|
}.property("field.description"),
|
|
|
|
|
|
|
|
inputComponentName: function () {
|
|
|
|
const type = this.get("field.type");
|
|
|
|
const id = this.get("field.id");
|
|
|
|
if (["text_only"].includes(type)) return false;
|
|
|
|
return dasherize(type === "component" ? id : `wizard-field-${type}`);
|
|
|
|
}.property("field.type", "field.id"),
|
2018-01-30 14:19:32 +01:00
|
|
|
});
|
|
|
|
|
2019-07-31 08:57:25 +02:00
|
|
|
const StandardFieldValidation = [
|
2020-10-01 07:43:29 +02:00
|
|
|
"text",
|
|
|
|
"number",
|
|
|
|
"textarea",
|
|
|
|
"dropdown",
|
|
|
|
"tag",
|
|
|
|
"image",
|
|
|
|
"user_selector",
|
|
|
|
"text_only",
|
|
|
|
"composer",
|
|
|
|
"category",
|
|
|
|
"group",
|
|
|
|
"date",
|
|
|
|
"time",
|
|
|
|
"date_time",
|
2019-07-31 08:57:25 +02:00
|
|
|
];
|
2018-02-04 07:34:32 +01:00
|
|
|
|
2017-10-09 07:52:09 +02:00
|
|
|
FieldModel.reopen({
|
|
|
|
check() {
|
2020-04-15 10:58:57 +02:00
|
|
|
if (this.customCheck) {
|
|
|
|
return this.customCheck();
|
|
|
|
}
|
2020-10-01 07:43:29 +02:00
|
|
|
|
2020-04-15 10:58:57 +02:00
|
|
|
let valid = this.valid;
|
2017-10-09 07:52:09 +02:00
|
|
|
|
2020-04-15 10:58:57 +02:00
|
|
|
if (!this.required) {
|
2017-10-09 07:52:09 +02:00
|
|
|
this.setValid(true);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-10-01 07:43:29 +02:00
|
|
|
const val = this.get("value");
|
|
|
|
const type = this.get("type");
|
|
|
|
|
|
|
|
if (type === "checkbox") {
|
2020-04-15 10:58:57 +02:00
|
|
|
valid = val;
|
2020-10-01 07:43:29 +02:00
|
|
|
} else if (type === "upload") {
|
2020-04-15 10:58:57 +02:00
|
|
|
valid = val && val.id > 0;
|
|
|
|
} else if (StandardFieldValidation.indexOf(type) > -1) {
|
|
|
|
valid = val && val.toString().length > 0;
|
2020-10-01 07:43:29 +02:00
|
|
|
} else if (type === "url") {
|
2020-04-15 10:58:57 +02:00
|
|
|
valid = true;
|
2017-10-09 07:52:09 +02:00
|
|
|
}
|
2020-10-01 07:43:29 +02:00
|
|
|
|
2018-02-04 07:34:32 +01:00
|
|
|
this.setValid(valid);
|
|
|
|
|
2017-10-09 07:52:09 +02:00
|
|
|
return valid;
|
2020-10-01 07:43:29 +02:00
|
|
|
},
|
2017-10-09 07:52:09 +02:00
|
|
|
});
|
2020-10-01 07:43:29 +02:00
|
|
|
},
|
2017-09-25 16:47:40 +02:00
|
|
|
};
|