discourse/app/assets/javascripts/admin/components/resumable-upload.js.es6

132 lines
3.3 KiB
Plaintext
Raw Normal View History

import { schedule } from "@ember/runloop";
import { later } from "@ember/runloop";
import Component from "@ember/component";
2018-06-15 23:03:24 +08:00
import { iconHTML } from "discourse-common/lib/icon-library";
import { bufferedRender } from "discourse-common/lib/buffered-render";
2019-05-07 16:51:11 +08:00
import {
default as discourseComputed,
2019-05-07 16:51:11 +08:00
on
} from "discourse-common/utils/decorators";
2014-02-22 08:41:01 +08:00
/*global Resumable:true */
/**
Example usage:
{{resumable-upload
target="/admin/backups/upload"
success=(action "successAction")
error=(action "errorAction")
2014-02-22 08:41:01 +08:00
uploadText="UPLOAD"
}}
**/
export default Component.extend(
2018-06-15 23:03:24 +08:00
bufferedRender({
tagName: "button",
classNames: ["btn", "ru"],
classNameBindings: ["isUploading"],
attributeBindings: ["translatedTitle:title"],
resumable: null,
isUploading: false,
progress: 0,
rerenderTriggers: ["isUploading", "progress"],
2019-05-07 16:51:11 +08:00
@on("init")
_initialize() {
this.resumable = new Resumable({
target: Discourse.getURL(this.target),
2019-05-07 16:51:11 +08:00
maxFiles: 1, // only 1 file at a time
headers: {
"X-CSRF-Token": document.querySelector("meta[name='csrf-token']")
.content
}
});
this.resumable.on("fileAdded", () => {
// automatically upload the selected file
this.resumable.upload();
// mark as uploading
later(() => this.set("isUploading", true));
2019-05-07 16:51:11 +08:00
});
this.resumable.on("fileProgress", file => {
// update progress
later(() => this.set("progress", parseInt(file.progress() * 100, 10)));
2019-05-07 16:51:11 +08:00
});
this.resumable.on("fileSuccess", file => {
later(() => {
2019-05-07 16:51:11 +08:00
// mark as not uploading anymore
this._reset();
// fire an event to allow the parent route to reload its model
this.success(file.fileName);
});
});
this.resumable.on("fileError", (file, message) => {
later(() => {
2019-05-07 16:51:11 +08:00
// mark as not uploading anymore
this._reset();
// fire an event to allow the parent route to display the error message
this.error(file.fileName, message);
});
});
},
@on("didInsertElement")
_assignBrowse() {
schedule("afterRender", () =>
2019-05-07 16:51:11 +08:00
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;
},
2018-06-15 23:03:24 +08:00
@discourseComputed("isUploading", "progress")
text(isUploading, progress) {
if (isUploading) {
return progress + " %";
2018-06-15 23:03:24 +08:00
} else {
return this.uploadText;
2018-06-15 23:03:24 +08:00
}
},
2018-06-15 23:03:24 +08:00
buildBuffer(buffer) {
const icon = this.isUploading ? "times" : "upload";
2018-06-15 23:03:24 +08:00
buffer.push(iconHTML(icon));
buffer.push("<span class='ru-label'>" + this.text + "</span>");
2018-06-15 23:03:24 +08:00
buffer.push(
"<span class='ru-progress' style='width:" + this.progress + "%'></span>"
2018-06-15 23:03:24 +08:00
);
},
2019-05-07 16:51:11 +08:00
click() {
if (this.isUploading) {
2018-06-15 23:03:24 +08:00
this.resumable.cancel();
later(() => this._reset());
2018-06-15 23:03:24 +08:00
return false;
} else {
return true;
}
},
2019-05-07 16:51:11 +08:00
_reset() {
2018-06-15 23:03:24 +08:00
this.setProperties({ isUploading: false, progress: 0 });
2019-05-07 16:51:11 +08:00
}
2018-06-15 23:03:24 +08:00
})
);