2020-11-09 04:32:36 +01:00
|
|
|
import Controller from "@ember/controller";
|
2021-04-12 07:44:47 +02:00
|
|
|
import { observes } from "discourse-common/utils/decorators";
|
2020-11-09 04:32:36 +01:00
|
|
|
import { empty } from "@ember/object/computed";
|
2021-03-28 11:06:49 +02:00
|
|
|
import CustomWizardManager from "../models/custom-wizard-manager";
|
2020-11-09 04:32:36 +01:00
|
|
|
import { A } from "@ember/array";
|
|
|
|
import I18n from "I18n";
|
|
|
|
import { underscore } from "@ember/string";
|
|
|
|
|
|
|
|
export default Controller.extend({
|
2023-10-16 03:09:41 +02:00
|
|
|
messageUrl:
|
|
|
|
"https://pavilion.tech/products/discourse-custom-wizard-plugin/documentation/wizard-manager",
|
2021-03-28 11:06:49 +02:00
|
|
|
messageKey: "info",
|
|
|
|
messageIcon: "info-circle",
|
|
|
|
messageClass: "info",
|
|
|
|
importDisabled: empty("file"),
|
2020-11-09 04:32:36 +01:00
|
|
|
exportWizards: A(),
|
|
|
|
destroyWizards: A(),
|
2021-03-28 11:06:49 +02:00
|
|
|
exportDisabled: empty("exportWizards"),
|
|
|
|
destoryDisabled: empty("destroyWizards"),
|
2020-11-09 04:32:36 +01:00
|
|
|
|
2021-03-28 11:06:49 +02:00
|
|
|
setMessage(type, key, opts = {}, items = []) {
|
2020-11-09 04:32:36 +01:00
|
|
|
this.setProperties({
|
|
|
|
messageKey: key,
|
|
|
|
messageOpts: opts,
|
|
|
|
messageType: type,
|
2021-03-28 11:06:49 +02:00
|
|
|
messageItems: items,
|
2020-11-09 04:32:36 +01:00
|
|
|
});
|
|
|
|
setTimeout(() => {
|
2024-11-22 15:04:06 +01:00
|
|
|
if (this.isDestroyed) {
|
|
|
|
return;
|
|
|
|
};
|
2020-11-09 04:32:36 +01:00
|
|
|
this.setProperties({
|
2021-03-28 11:06:49 +02:00
|
|
|
messageKey: "info",
|
2020-11-09 04:32:36 +01:00
|
|
|
messageOpts: null,
|
|
|
|
messageType: null,
|
2021-03-28 11:06:49 +02:00
|
|
|
messageItems: null,
|
|
|
|
});
|
2020-11-09 04:32:36 +01:00
|
|
|
}, 10000);
|
|
|
|
},
|
2021-03-28 11:06:49 +02:00
|
|
|
|
2020-11-09 04:32:36 +01:00
|
|
|
buildWizardLink(wizard) {
|
|
|
|
let html = `<a href='/admin/wizards/wizard/${wizard.id}'>${wizard.name}</a>`;
|
2021-03-28 11:06:49 +02:00
|
|
|
html += `<span class='action'>${I18n.t(
|
|
|
|
"admin.wizard.manager.imported"
|
|
|
|
)}</span>`;
|
2020-11-09 04:32:36 +01:00
|
|
|
return {
|
2021-03-28 11:06:49 +02:00
|
|
|
icon: "check-circle",
|
|
|
|
html,
|
2020-11-09 04:32:36 +01:00
|
|
|
};
|
|
|
|
},
|
2021-03-28 11:06:49 +02:00
|
|
|
|
2020-11-09 04:32:36 +01:00
|
|
|
buildDestroyedItem(destroyed) {
|
|
|
|
let html = `<span data-wizard-id="${destroyed.id}">${destroyed.name}</span>`;
|
2021-03-28 11:06:49 +02:00
|
|
|
html += `<span class='action'>${I18n.t(
|
|
|
|
"admin.wizard.manager.destroyed"
|
|
|
|
)}</span>`;
|
2020-11-09 04:32:36 +01:00
|
|
|
return {
|
2021-03-28 11:06:49 +02:00
|
|
|
icon: "check-circle",
|
|
|
|
html,
|
2020-11-09 04:32:36 +01:00
|
|
|
};
|
|
|
|
},
|
2021-03-28 11:06:49 +02:00
|
|
|
|
2020-11-09 04:32:36 +01:00
|
|
|
buildFailureItem(failure) {
|
|
|
|
return {
|
2021-03-28 11:06:49 +02:00
|
|
|
icon: "times-circle",
|
|
|
|
html: `${failure.id}: ${failure.messages}`,
|
2020-11-09 04:32:36 +01:00
|
|
|
};
|
|
|
|
},
|
2021-03-28 11:06:49 +02:00
|
|
|
|
2020-11-09 04:32:36 +01:00
|
|
|
clearFile() {
|
|
|
|
this.setProperties({
|
|
|
|
file: null,
|
2021-03-28 11:06:49 +02:00
|
|
|
filename: null,
|
2020-11-09 04:32:36 +01:00
|
|
|
});
|
2022-08-15 16:16:18 +02:00
|
|
|
document.getElementById("custom-wizard-file-upload").value = "";
|
2020-11-09 04:32:36 +01:00
|
|
|
},
|
2021-03-28 11:06:49 +02:00
|
|
|
|
|
|
|
@observes("importing", "destroying")
|
2020-11-09 04:32:36 +01:00
|
|
|
setLoadingMessages() {
|
|
|
|
if (this.importing) {
|
|
|
|
this.setMessage("loading", "importing");
|
|
|
|
}
|
|
|
|
if (this.destroying) {
|
|
|
|
this.setMessage("loading", "destroying");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
actions: {
|
|
|
|
upload() {
|
2022-08-15 16:16:18 +02:00
|
|
|
document.getElementById("custom-wizard-file-upload").click();
|
2020-11-09 04:32:36 +01:00
|
|
|
},
|
2021-03-28 11:06:49 +02:00
|
|
|
|
2020-11-09 04:32:36 +01:00
|
|
|
clearFile() {
|
|
|
|
this.clearFile();
|
|
|
|
},
|
2021-03-28 11:06:49 +02:00
|
|
|
|
2020-11-09 04:32:36 +01:00
|
|
|
setFile(event) {
|
|
|
|
let maxFileSize = 512 * 1024;
|
|
|
|
const file = event.target.files[0];
|
|
|
|
|
|
|
|
if (file === undefined) {
|
2021-03-28 11:06:49 +02:00
|
|
|
this.set("file", null);
|
2020-11-09 04:32:36 +01:00
|
|
|
return;
|
|
|
|
}
|
2021-03-28 11:06:49 +02:00
|
|
|
|
2020-11-09 04:32:36 +01:00
|
|
|
if (maxFileSize < file.size) {
|
|
|
|
this.setMessage("error", "file_size_error");
|
|
|
|
this.set("file", null);
|
2022-08-15 16:16:18 +02:00
|
|
|
document.getElementById("custom-wizard-file-upload").value = "";
|
2020-11-09 04:32:36 +01:00
|
|
|
} else {
|
|
|
|
this.setProperties({
|
|
|
|
file,
|
2021-03-28 11:06:49 +02:00
|
|
|
filename: file.name,
|
2020-11-09 04:32:36 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
2021-03-28 11:06:49 +02:00
|
|
|
|
2020-11-09 04:32:36 +01:00
|
|
|
selectWizard(event) {
|
2021-03-28 11:06:49 +02:00
|
|
|
const type = event.target.classList.contains("export")
|
|
|
|
? "export"
|
|
|
|
: "destroy";
|
2020-11-09 04:32:36 +01:00
|
|
|
const wizards = this.get(`${type}Wizards`);
|
|
|
|
const checked = event.target.checked;
|
2021-03-28 11:06:49 +02:00
|
|
|
|
|
|
|
let wizardId = event.target.closest("tr").getAttribute("data-wizard-id");
|
|
|
|
|
2020-11-09 04:32:36 +01:00
|
|
|
if (wizardId) {
|
|
|
|
wizardId = underscore(wizardId);
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2021-03-28 11:06:49 +02:00
|
|
|
|
2020-11-09 04:32:36 +01:00
|
|
|
if (checked) {
|
|
|
|
wizards.addObject(wizardId);
|
|
|
|
} else {
|
|
|
|
wizards.removeObject(wizardId);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
import() {
|
2021-03-28 11:06:49 +02:00
|
|
|
const file = this.get("file");
|
|
|
|
|
2020-11-09 04:32:36 +01:00
|
|
|
if (!file) {
|
2021-03-28 11:06:49 +02:00
|
|
|
this.setMessage("error", "no_file");
|
2020-11-09 04:32:36 +01:00
|
|
|
return;
|
|
|
|
}
|
2021-03-28 11:06:49 +02:00
|
|
|
|
2020-11-09 04:32:36 +01:00
|
|
|
let $formData = new FormData();
|
2021-03-28 11:06:49 +02:00
|
|
|
$formData.append("file", file);
|
|
|
|
|
|
|
|
this.set("importing", true);
|
2020-11-09 04:32:36 +01:00
|
|
|
this.setMessage("loading", "importing");
|
2021-03-28 11:06:49 +02:00
|
|
|
|
|
|
|
CustomWizardManager.import($formData)
|
|
|
|
.then((result) => {
|
|
|
|
if (result.error) {
|
|
|
|
this.setMessage("error", "server_error", {
|
|
|
|
message: result.error,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.setMessage(
|
|
|
|
"success",
|
|
|
|
"import_complete",
|
|
|
|
{},
|
|
|
|
result.imported
|
|
|
|
.map((imported) => {
|
|
|
|
return this.buildWizardLink(imported);
|
|
|
|
})
|
|
|
|
.concat(
|
|
|
|
result.failures.map((failure) => {
|
|
|
|
return this.buildFailureItem(failure);
|
|
|
|
})
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
if (result.imported.length) {
|
|
|
|
this.get("wizards").addObjects(result.imported);
|
|
|
|
}
|
2020-11-09 04:32:36 +01:00
|
|
|
}
|
2021-03-28 11:06:49 +02:00
|
|
|
this.clearFile();
|
|
|
|
})
|
|
|
|
.finally(() => {
|
|
|
|
this.set("importing", false);
|
|
|
|
});
|
2020-11-09 04:32:36 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
export() {
|
2021-03-28 11:06:49 +02:00
|
|
|
const exportWizards = this.get("exportWizards");
|
2020-11-09 04:32:36 +01:00
|
|
|
|
|
|
|
if (!exportWizards.length) {
|
2021-03-28 11:06:49 +02:00
|
|
|
this.setMessage("error", "none_selected");
|
2020-11-09 04:32:36 +01:00
|
|
|
} else {
|
|
|
|
CustomWizardManager.export(exportWizards);
|
|
|
|
exportWizards.clear();
|
2021-03-28 11:06:49 +02:00
|
|
|
$("input.export").prop("checked", false);
|
2020-11-09 04:32:36 +01:00
|
|
|
}
|
|
|
|
},
|
2021-03-28 11:06:49 +02:00
|
|
|
|
2020-11-09 04:32:36 +01:00
|
|
|
destroy() {
|
2021-04-12 07:44:47 +02:00
|
|
|
let destroyWizards = this.get("destroyWizards");
|
2020-11-09 04:32:36 +01:00
|
|
|
|
|
|
|
if (!destroyWizards.length) {
|
2021-03-28 11:06:49 +02:00
|
|
|
this.setMessage("error", "none_selected");
|
2020-11-09 04:32:36 +01:00
|
|
|
} else {
|
2021-03-28 11:06:49 +02:00
|
|
|
this.set("destroying", true);
|
|
|
|
|
|
|
|
CustomWizardManager.destroy(destroyWizards)
|
|
|
|
.then((result) => {
|
|
|
|
if (result.error) {
|
|
|
|
this.setMessage("error", "server_error", {
|
|
|
|
message: result.error,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.setMessage(
|
|
|
|
"success",
|
|
|
|
"destroy_complete",
|
|
|
|
{},
|
|
|
|
result.destroyed
|
|
|
|
.map((destroyed) => {
|
|
|
|
return this.buildDestroyedItem(destroyed);
|
|
|
|
})
|
|
|
|
.concat(
|
|
|
|
result.failures.map((failure) => {
|
|
|
|
return this.buildFailureItem(failure);
|
|
|
|
})
|
|
|
|
)
|
2020-11-09 04:32:36 +01:00
|
|
|
);
|
2021-03-28 11:06:49 +02:00
|
|
|
|
|
|
|
if (result.destroyed.length) {
|
|
|
|
const destroyedIds = result.destroyed.map((d) => d.id);
|
2021-04-12 07:44:47 +02:00
|
|
|
destroyWizards = this.get("destroyWizards");
|
2021-03-28 11:06:49 +02:00
|
|
|
const wizards = this.get("wizards");
|
|
|
|
|
|
|
|
wizards.removeObjects(
|
|
|
|
wizards.filter((w) => {
|
|
|
|
return destroyedIds.includes(w.id);
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
destroyWizards.removeObjects(destroyedIds);
|
|
|
|
}
|
2020-11-09 04:32:36 +01:00
|
|
|
}
|
2021-03-28 11:06:49 +02:00
|
|
|
})
|
|
|
|
.finally(() => {
|
|
|
|
this.set("destroying", false);
|
|
|
|
});
|
2020-11-09 04:32:36 +01:00
|
|
|
}
|
2021-03-28 11:06:49 +02:00
|
|
|
},
|
|
|
|
},
|
2020-11-09 04:32:36 +01:00
|
|
|
});
|