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

43 lines
1.1 KiB
Plaintext
Raw Normal View History

2018-06-15 11:03:24 -04: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";
2016-09-08 16:58:07 -04:00
export default Ember.Component.extend({
2018-06-15 11:03:24 -04:00
classNames: ["wizard-image-row"],
2016-09-08 16:58:07 -04:00
uploading: false,
2018-06-15 11:03:24 -04:00
@computed("field.id")
previewComponent(id) {
const componentName = `image-preview-${Ember.String.dasherize(id)}`;
const exists = getOwner(this).lookup(`component:${componentName}`);
2018-06-15 11:03:24 -04:00
return exists ? componentName : "wizard-image-preview";
},
2016-09-08 16:58:07 -04:00
didInsertElement() {
this._super();
const $upload = this.$();
2018-06-15 11:03:24 -04:00
const id = this.get("field.id");
2016-09-08 16:58:07 -04:00
$upload.fileupload({
url: getUrl("/uploads.json"),
2018-06-15 11:03:24 -04:00
formData: {
synchronous: true,
type: `wizard_${id}`,
authenticity_token: getToken()
},
dataType: "json",
dropZone: $upload
2016-09-08 16:58:07 -04:00
});
2018-06-15 11:03:24 -04:00
$upload.on("fileuploadsubmit", () => this.set("uploading", true));
2016-09-08 16:58:07 -04:00
2018-06-15 11:03:24 -04:00
$upload.on("fileuploaddone", (e, response) => {
this.set("field.value", response.result.url);
this.set("uploading", false);
2016-09-08 16:58:07 -04:00
});
2018-06-15 11:03:24 -04:00
}
2016-09-08 16:58:07 -04:00
});