0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2024-09-20 07:41:11 +02:00
discourse-custom-wizard/assets/javascripts/wizard/components/wizard-field-upload.js.es6

64 Zeilen
1,6 KiB
Text

2018-09-09 15:01:02 +02:00
import getUrl from "discourse-common/lib/get-url";
import { getToken } from "wizard/lib/ajax";
import WizardI18n from "../lib/wizard-i18n";
2018-09-09 15:01:02 +02:00
export default Ember.Component.extend({
classNames: ["wizard-field-upload"],
classNameBindings: ["isImage"],
2018-09-09 15:01:02 +02:00
uploading: false,
isImage: false,
2018-09-09 15:01:02 +02:00
didInsertElement() {
this._super();
2020-03-29 09:49:33 +02:00
const $upload = $(this.element);
2018-09-09 15:01:02 +02:00
const id = this.get("field.id");
$upload.fileupload({
url: getUrl("/uploads.json"),
formData: {
synchronous: true,
type: `wizard_${id}`,
authenticity_token: getToken(),
2018-09-09 15:01:02 +02:00
},
dataType: "json",
dropZone: $upload,
2018-09-09 15:01:02 +02:00
});
$upload.on("fileuploadsubmit", () => this.set("uploading", true));
$upload.on("fileuploaddone", (e, response) => {
this.setProperties({
"field.value": response.result,
uploading: false,
});
if (
Discourse.SiteSettings.wizard_recognised_image_upload_formats
.split("|")
.includes(response.result.extension)
) {
this.setProperties({
isImage: true,
});
}
2018-09-09 15:01:02 +02:00
});
$upload.on("fileuploadfail", (e, response) => {
let message = WizardI18n("wizard.upload_error");
2018-09-09 15:01:02 +02:00
if (response.jqXHR.responseJSON && response.jqXHR.responseJSON.errors) {
message = response.jqXHR.responseJSON.errors.join("\n");
}
window.swal({
customClass: "wizard-warning",
title: "",
text: message,
type: "warning",
confirmButtonColor: "#6699ff",
2018-09-09 15:01:02 +02:00
});
this.set("uploading", false);
});
},
2018-09-09 15:01:02 +02:00
});