2020-12-02 02:31:26 +08:00
|
|
|
import discourseComputed, { on } from "discourse-common/utils/decorators";
|
2020-03-10 00:38:37 +08:00
|
|
|
import { later, schedule } from "@ember/runloop";
|
2019-10-24 00:30:52 +08:00
|
|
|
import Component from "@ember/component";
|
2020-12-02 02:31:26 +08:00
|
|
|
import I18n from "I18n";
|
|
|
|
import getURL from "discourse-common/lib/get-url";
|
2017-07-27 04:33:17 +08:00
|
|
|
import { iconHTML } from "discourse-common/lib/icon-library";
|
2016-10-25 03:21:44 +08:00
|
|
|
|
2014-02-22 08:41:01 +08:00
|
|
|
/*global Resumable:true */
|
|
|
|
|
|
|
|
/**
|
|
|
|
Example usage:
|
|
|
|
|
|
|
|
{{resumable-upload
|
|
|
|
target="/admin/backups/upload"
|
2019-01-10 18:06:01 +08:00
|
|
|
success=(action "successAction")
|
|
|
|
error=(action "errorAction")
|
2014-02-22 08:41:01 +08:00
|
|
|
uploadText="UPLOAD"
|
|
|
|
}}
|
|
|
|
**/
|
2019-12-07 08:06:48 +08:00
|
|
|
export default Component.extend({
|
|
|
|
tagName: "button",
|
|
|
|
classNames: ["btn", "ru"],
|
|
|
|
classNameBindings: ["isUploading"],
|
|
|
|
attributeBindings: ["translatedTitle:title"],
|
|
|
|
resumable: null,
|
|
|
|
isUploading: false,
|
|
|
|
progress: 0,
|
|
|
|
rerenderTriggers: ["isUploading", "progress"],
|
|
|
|
uploadingIcon: null,
|
|
|
|
progressBar: null,
|
|
|
|
|
|
|
|
@on("init")
|
|
|
|
_initialize() {
|
|
|
|
this.resumable = new Resumable({
|
2020-06-04 00:45:26 +08:00
|
|
|
target: getURL(this.target),
|
2019-12-07 08:06:48 +08:00
|
|
|
maxFiles: 1, // only 1 file at a time
|
|
|
|
headers: {
|
|
|
|
"X-CSRF-Token": document.querySelector("meta[name='csrf-token']")
|
|
|
|
.content,
|
|
|
|
},
|
|
|
|
});
|
2019-05-07 16:51:11 +08:00
|
|
|
|
2019-12-07 08:06:48 +08:00
|
|
|
this.resumable.on("fileAdded", () => {
|
|
|
|
// automatically upload the selected file
|
|
|
|
this.resumable.upload();
|
2019-05-07 16:51:11 +08:00
|
|
|
|
2019-12-07 08:06:48 +08:00
|
|
|
// mark as uploading
|
|
|
|
later(() => {
|
|
|
|
this.set("isUploading", true);
|
|
|
|
this._updateIcon();
|
2019-05-07 16:51:11 +08:00
|
|
|
});
|
2019-12-07 08:06:48 +08:00
|
|
|
});
|
2019-05-07 16:51:11 +08:00
|
|
|
|
2019-12-07 08:06:48 +08:00
|
|
|
this.resumable.on("fileProgress", (file) => {
|
|
|
|
// update progress
|
|
|
|
later(() => {
|
|
|
|
this.set("progress", parseInt(file.progress() * 100, 10));
|
|
|
|
this._updateProgressBar();
|
2019-05-07 16:51:11 +08:00
|
|
|
});
|
2019-12-07 08:06:48 +08:00
|
|
|
});
|
2019-05-07 16:51:11 +08:00
|
|
|
|
2019-12-07 08:06:48 +08:00
|
|
|
this.resumable.on("fileSuccess", (file) => {
|
|
|
|
later(() => {
|
|
|
|
// mark as not uploading anymore
|
|
|
|
this._reset();
|
2019-05-07 16:51:11 +08:00
|
|
|
|
2019-12-07 08:06:48 +08:00
|
|
|
// fire an event to allow the parent route to reload its model
|
|
|
|
this.success(file.fileName);
|
2019-05-07 16:51:11 +08:00
|
|
|
});
|
2019-12-07 08:06:48 +08:00
|
|
|
});
|
2019-05-07 16:51:11 +08:00
|
|
|
|
2019-12-07 08:06:48 +08:00
|
|
|
this.resumable.on("fileError", (file, message) => {
|
|
|
|
later(() => {
|
|
|
|
// mark as not uploading anymore
|
|
|
|
this._reset();
|
2019-05-07 16:51:11 +08:00
|
|
|
|
2019-12-07 08:06:48 +08:00
|
|
|
// fire an event to allow the parent route to display the error message
|
|
|
|
this.error(file.fileName, message);
|
2019-05-07 16:51:11 +08:00
|
|
|
});
|
2019-12-07 08:06:48 +08:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
@on("didInsertElement")
|
|
|
|
_assignBrowse() {
|
|
|
|
schedule("afterRender", () => this.resumable.assignBrowse($(this.element)));
|
|
|
|
},
|
|
|
|
|
|
|
|
@on("willDestroyElement")
|
|
|
|
_teardown() {
|
|
|
|
if (this.resumable) {
|
|
|
|
this.resumable.cancel();
|
|
|
|
this.resumable = null;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
@discourseComputed("title", "text")
|
|
|
|
translatedTitle(title, text) {
|
|
|
|
return title ? I18n.t(title) : text;
|
|
|
|
},
|
|
|
|
|
|
|
|
@discourseComputed("isUploading", "progress")
|
|
|
|
text(isUploading, progress) {
|
|
|
|
if (isUploading) {
|
|
|
|
return progress + " %";
|
|
|
|
} else {
|
|
|
|
return this.uploadText;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
didReceiveAttrs() {
|
|
|
|
this._super(...arguments);
|
|
|
|
this._updateIcon();
|
|
|
|
},
|
|
|
|
|
|
|
|
click() {
|
|
|
|
if (this.isUploading) {
|
|
|
|
this.resumable.cancel();
|
|
|
|
later(() => this._reset());
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return true;
|
2019-05-07 16:51:11 +08:00
|
|
|
}
|
2019-12-07 08:06:48 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
_updateIcon() {
|
|
|
|
const icon = this.isUploading ? "times" : "upload";
|
|
|
|
this.set("uploadingIcon", `${iconHTML(icon)}`.htmlSafe());
|
|
|
|
},
|
|
|
|
|
|
|
|
_updateProgressBar() {
|
|
|
|
const pb = `${"width:" + this.progress + "%"}`.htmlSafe();
|
|
|
|
this.set("progressBar", pb);
|
|
|
|
},
|
|
|
|
|
|
|
|
_reset() {
|
|
|
|
this.setProperties({ isUploading: false, progress: 0 });
|
|
|
|
this._updateIcon();
|
|
|
|
this._updateProgressBar();
|
|
|
|
},
|
|
|
|
});
|