discourse/app/assets/javascripts/wizard/components/wizard-field-image.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

56 lines
1.6 KiB
JavaScript
Raw Normal View History

import Component from "@ember/component";
import I18n from "I18n";
import { dasherize } from "@ember/string";
import discourseComputed from "discourse-common/utils/decorators";
import { getOwner } from "discourse-common/lib/get-owner";
import { getToken } from "wizard/lib/ajax";
import getUrl from "discourse-common/lib/get-url";
2016-09-08 16:58:07 -04:00
export default Component.extend({
2016-09-08 16:58:07 -04:00
classNames: ["wizard-image-row"],
uploading: false,
@discourseComputed("field.id")
previewComponent(id) {
const componentName = `image-preview-${dasherize(id)}`;
const exists = getOwner(this).lookup(`component:${componentName}`);
return exists ? componentName : "wizard-image-preview";
},
2016-09-08 16:58:07 -04:00
didInsertElement() {
this._super(...arguments);
2016-09-08 16:58:07 -04:00
const $upload = $(this.element);
2016-09-08 16:58:07 -04:00
const id = this.get("field.id");
$upload.fileupload({
url: getUrl("/uploads.json"),
2016-09-08 16:58:07 -04:00
formData: {
synchronous: true,
type: `wizard_${id}`,
authenticity_token: getToken(),
},
dataType: "json",
dropZone: $upload,
});
$upload.on("fileuploadsubmit", () => this.set("uploading", true));
$upload.on("fileuploaddone", (e, response) => {
this.set("field.value", response.result.url);
this.set("uploading", false);
});
$upload.on("fileuploadfail", (e, response) => {
2018-07-12 03:12:11 -04:00
let message = I18n.t("wizard.upload_error");
if (response.jqXHR.responseJSON && response.jqXHR.responseJSON.errors) {
message = response.jqXHR.responseJSON.errors.join("\n");
}
window.bootbox.alert(message);
this.set("uploading", false);
});
2016-09-08 16:58:07 -04:00
},
});