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

131 lines
3.3 KiB
Plaintext
Raw Normal View History

2018-06-15 11:03:24 -04:00
import { iconHTML } from "discourse-common/lib/icon-library";
import { bufferedRender } from "discourse-common/lib/buffered-render";
2019-05-07 04:51:11 -04:00
import {
default as computed,
on
} from "ember-addons/ember-computed-decorators";
2014-02-21 19:41:01 -05:00
/*global Resumable:true */
/**
Example usage:
{{resumable-upload
target="/admin/backups/upload"
success=(action "successAction")
error=(action "errorAction")
2014-02-21 19:41:01 -05:00
uploadText="UPLOAD"
}}
**/
2018-06-15 11:03:24 -04: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"],
2019-05-07 04:51:11 -04:00
@on("init")
_initialize() {
this.resumable = new Resumable({
target: Discourse.getURL(this.target),
2019-05-07 04:51:11 -04: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
Ember.run.later(() => this.set("isUploading", true));
});
this.resumable.on("fileProgress", file => {
// update progress
Ember.run.later(() =>
this.set("progress", parseInt(file.progress() * 100, 10))
);
});
this.resumable.on("fileSuccess", file => {
Ember.run.later(() => {
// 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) => {
Ember.run.later(() => {
// 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() {
Ember.run.schedule("afterRender", () =>
this.resumable.assignBrowse($(this.element))
);
},
@on("willDestroyElement")
_teardown() {
if (this.resumable) {
this.resumable.cancel();
this.resumable = null;
}
},
@computed("title", "text")
translatedTitle(title, text) {
return title ? I18n.t(title) : text;
},
2018-06-15 11:03:24 -04:00
@computed("isUploading", "progress")
text(isUploading, progress) {
if (isUploading) {
return progress + " %";
2018-06-15 11:03:24 -04:00
} else {
return this.uploadText;
2018-06-15 11:03:24 -04:00
}
},
2018-06-15 11:03:24 -04:00
buildBuffer(buffer) {
const icon = this.isUploading ? "times" : "upload";
2018-06-15 11:03:24 -04:00
buffer.push(iconHTML(icon));
buffer.push("<span class='ru-label'>" + this.text + "</span>");
2018-06-15 11:03:24 -04:00
buffer.push(
"<span class='ru-progress' style='width:" + this.progress + "%'></span>"
2018-06-15 11:03:24 -04:00
);
},
2019-05-07 04:51:11 -04:00
click() {
if (this.isUploading) {
2018-06-15 11:03:24 -04:00
this.resumable.cancel();
2019-05-07 04:51:11 -04:00
Ember.run.later(() => this._reset());
2018-06-15 11:03:24 -04:00
return false;
} else {
return true;
}
},
2019-05-07 04:51:11 -04:00
_reset() {
2018-06-15 11:03:24 -04:00
this.setProperties({ isUploading: false, progress: 0 });
2019-05-07 04:51:11 -04:00
}
2018-06-15 11:03:24 -04:00
})
);