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

132 lines
3.5 KiB
Plaintext
Raw Normal View History

2018-06-15 23:03:24 +08:00
import { iconHTML } from "discourse-common/lib/icon-library";
import { bufferedRender } from "discourse-common/lib/buffered-render";
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"
}}
**/
2018-06-15 23:03:24 +08:00
export default Ember.Component.extend(
bufferedRender({
tagName: "button",
classNames: ["btn", "ru"],
classNameBindings: ["isUploading"],
attributeBindings: ["translatedTitle:title"],
resumable: null,
isUploading: false,
progress: 0,
rerenderTriggers: ["isUploading", "progress"],
translatedTitle: function() {
const title = this.get("title");
return title ? I18n.t(title) : this.get("text");
}.property("title", "text"),
text: function() {
if (this.get("isUploading")) {
return this.get("progress") + " %";
} else {
return this.get("uploadText");
}
}.property("isUploading", "progress"),
buildBuffer(buffer) {
const icon = this.get("isUploading") ? "times" : "upload";
buffer.push(iconHTML(icon));
buffer.push("<span class='ru-label'>" + this.get("text") + "</span>");
buffer.push(
"<span class='ru-progress' style='width:" +
this.get("progress") +
"%'></span>"
);
},
click: function() {
if (this.get("isUploading")) {
this.resumable.cancel();
var self = this;
2019-01-12 00:54:23 +08:00
Ember.run.later(function() {
2018-06-15 23:03:24 +08:00
self._reset();
});
return false;
} else {
return true;
}
},
_reset: function() {
this.setProperties({ isUploading: false, progress: 0 });
},
_initialize: function() {
this.resumable = new Resumable({
target: Discourse.getURL(this.get("target")),
maxFiles: 1, // only 1 file at a time
headers: {
"X-CSRF-Token": $("meta[name='csrf-token']").attr("content")
}
2014-02-22 08:41:01 +08:00
});
2018-06-15 23:03:24 +08:00
var self = this;
this.resumable.on("fileAdded", function() {
// automatically upload the selected file
self.resumable.upload();
// mark as uploading
2019-01-12 00:54:23 +08:00
Ember.run.later(function() {
2018-06-15 23:03:24 +08:00
self.set("isUploading", true);
});
2014-02-22 08:41:01 +08:00
});
2018-06-15 23:03:24 +08:00
this.resumable.on("fileProgress", function(file) {
// update progress
2019-01-12 00:54:23 +08:00
Ember.run.later(function() {
2018-06-15 23:03:24 +08:00
self.set("progress", parseInt(file.progress() * 100, 10));
});
});
2014-02-22 08:41:01 +08:00
2018-06-15 23:03:24 +08:00
this.resumable.on("fileSuccess", function(file) {
2019-01-12 00:54:23 +08:00
Ember.run.later(function() {
2018-06-15 23:03:24 +08:00
// mark as not uploading anymore
self._reset();
// fire an event to allow the parent route to reload its model
self.success(file.fileName);
2018-06-15 23:03:24 +08:00
});
});
2014-02-22 08:41:01 +08:00
2018-06-15 23:03:24 +08:00
this.resumable.on("fileError", function(file, message) {
2019-01-12 00:54:23 +08:00
Ember.run.later(function() {
2018-06-15 23:03:24 +08:00
// mark as not uploading anymore
self._reset();
// fire an event to allow the parent route to display the error message
self.error(file.fileName, message);
2018-06-15 23:03:24 +08:00
});
});
}.on("init"),
2014-02-22 08:41:01 +08:00
2018-06-15 23:03:24 +08:00
_assignBrowse: function() {
var self = this;
2019-01-12 00:54:23 +08:00
Ember.run.schedule("afterRender", function() {
2018-06-15 23:03:24 +08:00
self.resumable.assignBrowse(self.$());
});
}.on("didInsertElement"),
_teardown: function() {
if (this.resumable) {
this.resumable.cancel();
this.resumable = null;
}
}.on("willDestroyElement")
})
);