components 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. @computed("field.id")
  9. previewComponent(id) {
  10. const componentName = `image-preview-${Ember.String.dasherize(id)}`;
  11. const exists = getOwner(this).lookup(`component:${componentName}`);
  12. return exists ? componentName : "wizard-image-preview";
  13. },
  14. didInsertElement() {
  15. this._super();
  16. const $upload = this.$();
  17. const id = this.get("field.id");
  18. $upload.fileupload({
  19. url: getUrl("/uploads.json"),
  20. formData: {
  21. synchronous: true,
  22. type: `wizard_${id}`,
  23. authenticity_token: getToken()
  24. },
  25. dataType: "json",
  26. dropZone: $upload
  27. });
  28. $upload.on("fileuploadsubmit", () => this.set("uploading", true));
  29. $upload.on("fileuploaddone", (e, response) => {
  30. this.set("field.value", response.result.url);
  31. this.set("uploading", false);
  32. });
  33. $upload.on("fileuploadfail", (e, response) => {
  34. let message = I18n.t("wizard.upload_error");
  35. if (response.jqXHR.responseJSON && response.jqXHR.responseJSON.errors) {
  36. message = response.jqXHR.responseJSON.errors.join("\n");
  37. }
  38. window.swal({
  39. customClass: "wizard-warning",
  40. title: "",
  41. text: message,
  42. type: "warning",
  43. confirmButtonColor: "#6699ff"
  44. });
  45. this.set("uploading", false);
  46. });
  47. }
  48. });