2019-10-24 00:30:52 +08:00
|
|
|
import Component from "@ember/component";
|
2018-06-15 23:03:24 +08:00
|
|
|
import getUrl from "discourse-common/lib/get-url";
|
|
|
|
import computed from "ember-addons/ember-computed-decorators";
|
|
|
|
import { getToken } from "wizard/lib/ajax";
|
|
|
|
import { getOwner } from "discourse-common/lib/get-owner";
|
2019-11-06 02:43:49 +08:00
|
|
|
import { dasherize } from "@ember/string";
|
2016-09-09 04:58:07 +08:00
|
|
|
|
2019-10-24 00:30:52 +08:00
|
|
|
export default Component.extend({
|
2018-06-15 23:03:24 +08:00
|
|
|
classNames: ["wizard-image-row"],
|
2016-09-09 04:58:07 +08:00
|
|
|
uploading: false,
|
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
@computed("field.id")
|
2016-09-17 01:10:12 +08:00
|
|
|
previewComponent(id) {
|
2019-11-06 02:43:49 +08:00
|
|
|
const componentName = `image-preview-${dasherize(id)}`;
|
2016-11-04 23:32:12 +08:00
|
|
|
const exists = getOwner(this).lookup(`component:${componentName}`);
|
2018-06-15 23:03:24 +08:00
|
|
|
return exists ? componentName : "wizard-image-preview";
|
2016-09-17 01:10:12 +08:00
|
|
|
},
|
|
|
|
|
2016-09-09 04:58:07 +08:00
|
|
|
didInsertElement() {
|
2019-01-19 17:05:51 +08:00
|
|
|
this._super(...arguments);
|
2016-09-09 04:58:07 +08:00
|
|
|
|
2019-07-16 18:45:15 +08:00
|
|
|
const $upload = $(this.element);
|
2016-09-09 04:58:07 +08:00
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
const id = this.get("field.id");
|
2016-09-09 04:58:07 +08:00
|
|
|
|
|
|
|
$upload.fileupload({
|
2016-09-23 02:39:36 +08:00
|
|
|
url: getUrl("/uploads.json"),
|
2018-06-15 23:03:24 +08:00
|
|
|
formData: {
|
|
|
|
synchronous: true,
|
|
|
|
type: `wizard_${id}`,
|
|
|
|
authenticity_token: getToken()
|
|
|
|
},
|
|
|
|
dataType: "json",
|
|
|
|
dropZone: $upload
|
2016-09-09 04:58:07 +08:00
|
|
|
});
|
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
$upload.on("fileuploadsubmit", () => this.set("uploading", true));
|
2016-09-09 04:58:07 +08:00
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
$upload.on("fileuploaddone", (e, response) => {
|
|
|
|
this.set("field.value", response.result.url);
|
|
|
|
this.set("uploading", false);
|
2016-09-09 04:58:07 +08:00
|
|
|
});
|
2018-07-12 14:36:48 +08:00
|
|
|
|
|
|
|
$upload.on("fileuploadfail", (e, response) => {
|
2018-07-12 15:12:11 +08:00
|
|
|
let message = I18n.t("wizard.upload_error");
|
2018-07-12 14:36:48 +08: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"
|
|
|
|
});
|
|
|
|
this.set("uploading", false);
|
|
|
|
});
|
2018-06-15 23:03:24 +08:00
|
|
|
}
|
2016-09-09 04:58:07 +08:00
|
|
|
});
|