wizard-field-upload.js.es6 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import getUrl from "discourse-common/lib/get-url";
  2. import computed from "ember-addons/ember-computed-decorators";
  3. import { getToken } from "wizard/lib/ajax";
  4. import { getOwner } from "discourse-common/lib/get-owner";
  5. export default Ember.Component.extend({
  6. classNames: ["wizard-image-row"],
  7. uploading: false,
  8. didInsertElement() {
  9. this._super();
  10. const $upload = this.$();
  11. const id = this.get("field.id");
  12. $upload.fileupload({
  13. url: getUrl("/uploads.json"),
  14. formData: {
  15. synchronous: true,
  16. type: `wizard_${id}`,
  17. authenticity_token: getToken()
  18. },
  19. dataType: "json",
  20. dropZone: $upload
  21. });
  22. $upload.on("fileuploadsubmit", () => this.set("uploading", true));
  23. $upload.on("fileuploaddone", (e, response) => {
  24. this.set("field.value", response.result.url);
  25. this.set("uploading", false);
  26. });
  27. $upload.on("fileuploadfail", (e, response) => {
  28. let message = I18n.t("wizard.upload_error");
  29. if (response.jqXHR.responseJSON && response.jqXHR.responseJSON.errors) {
  30. message = response.jqXHR.responseJSON.errors.join("\n");
  31. }
  32. window.swal({
  33. customClass: "wizard-warning",
  34. title: "",
  35. text: message,
  36. type: "warning",
  37. confirmButtonColor: "#6699ff"
  38. });
  39. this.set("uploading", false);
  40. });
  41. }
  42. });