2019-10-23 12:30:52 -04:00
|
|
|
import Component from "@ember/component";
|
2016-09-22 14:39:36 -04:00
|
|
|
import I18n from "I18n";
|
|
|
|
import { dasherize } from "@ember/string";
|
2019-11-07 16:38:28 -05:00
|
|
|
import discourseComputed from "discourse-common/utils/decorators";
|
2016-11-04 11:32:12 -04:00
|
|
|
import { getOwner } from "discourse-common/lib/get-owner";
|
2019-11-05 13:43:49 -05:00
|
|
|
import { getToken } from "wizard/lib/ajax";
|
2016-11-04 11:32:12 -04:00
|
|
|
import getUrl from "discourse-common/lib/get-url";
|
2016-09-08 16:58:07 -04:00
|
|
|
|
2019-10-23 12:30:52 -04:00
|
|
|
export default Component.extend({
|
2016-09-08 16:58:07 -04:00
|
|
|
classNames: ["wizard-image-row"],
|
|
|
|
uploading: false,
|
|
|
|
|
2019-11-07 16:38:28 -05:00
|
|
|
@discourseComputed("field.id")
|
2016-09-16 13:10:12 -04:00
|
|
|
previewComponent(id) {
|
2019-11-05 13:43:49 -05:00
|
|
|
const componentName = `image-preview-${dasherize(id)}`;
|
2016-11-04 11:32:12 -04:00
|
|
|
const exists = getOwner(this).lookup(`component:${componentName}`);
|
2016-09-16 13:10:12 -04:00
|
|
|
return exists ? componentName : "wizard-image-preview";
|
|
|
|
},
|
|
|
|
|
2016-09-08 16:58:07 -04:00
|
|
|
didInsertElement() {
|
2019-01-19 04:05:51 -05:00
|
|
|
this._super(...arguments);
|
2016-09-08 16:58:07 -04:00
|
|
|
|
2019-07-16 06:45:15 -04:00
|
|
|
const $upload = $(this.element);
|
2016-09-08 16:58:07 -04:00
|
|
|
|
|
|
|
const id = this.get("field.id");
|
|
|
|
|
|
|
|
$upload.fileupload({
|
2016-09-22 14:39:36 -04:00
|
|
|
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);
|
|
|
|
});
|
2018-07-12 02:36:48 -04:00
|
|
|
|
|
|
|
$upload.on("fileuploadfail", (e, response) => {
|
2018-07-12 03:12:11 -04:00
|
|
|
let message = I18n.t("wizard.upload_error");
|
2018-07-12 02:36:48 -04:00
|
|
|
if (response.jqXHR.responseJSON && response.jqXHR.responseJSON.errors) {
|
|
|
|
message = response.jqXHR.responseJSON.errors.join("\n");
|
|
|
|
}
|
|
|
|
|
2020-11-10 11:31:54 -05:00
|
|
|
window.bootbox.alert(message);
|
2018-07-12 02:36:48 -04:00
|
|
|
this.set("uploading", false);
|
|
|
|
});
|
2016-09-08 16:58:07 -04:00
|
|
|
},
|
|
|
|
});
|