wizard-field-upload.js.es6 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import getUrl from "discourse-common/lib/get-url";
  2. import { getToken } from "wizard/lib/ajax";
  3. export default Ember.Component.extend({
  4. classNames: ["wizard-field-upload"],
  5. uploading: false,
  6. didInsertElement() {
  7. this._super();
  8. const $upload = this.$();
  9. const id = this.get("field.id");
  10. $upload.fileupload({
  11. url: getUrl("/uploads.json"),
  12. formData: {
  13. synchronous: true,
  14. type: `wizard_${id}`,
  15. authenticity_token: getToken()
  16. },
  17. dataType: "json",
  18. dropZone: $upload
  19. });
  20. $upload.on("fileuploadsubmit", () => this.set("uploading", true));
  21. $upload.on("fileuploaddone", (e, response) => {
  22. this.setProperties({
  23. "field.value": response.result,
  24. "uploading": false
  25. });
  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. });