2019-10-30 02:52:36 +08:00
|
|
|
import { schedule } from "@ember/runloop";
|
|
|
|
import { later } from "@ember/runloop";
|
2019-10-24 00:30:52 +08:00
|
|
|
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 computed,
|
|
|
|
on
|
|
|
|
} from "ember-addons/ember-computed-decorators";
|
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-10-24 00:30:52 +08:00
|
|
|
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({
|
2019-05-27 16:15:39 +08:00
|
|
|
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
|
2019-10-30 02:52:36 +08:00
|
|
|
later(() => this.set("isUploading", true));
|
2019-05-07 16:51:11 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
this.resumable.on("fileProgress", file => {
|
|
|
|
// update progress
|
2019-10-30 04:14:46 +08:00
|
|
|
later(() => this.set("progress", parseInt(file.progress() * 100, 10)));
|
2019-05-07 16:51:11 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
this.resumable.on("fileSuccess", file => {
|
2019-10-30 02:52:36 +08:00
|
|
|
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) => {
|
2019-10-30 02:52:36 +08:00
|
|
|
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() {
|
2019-10-30 02:52:36 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-04-26 18:16:21 +08:00
|
|
|
@computed("title", "text")
|
|
|
|
translatedTitle(title, text) {
|
|
|
|
return title ? I18n.t(title) : text;
|
|
|
|
},
|
2018-06-15 23:03:24 +08:00
|
|
|
|
2019-04-26 18:16:21 +08:00
|
|
|
@computed("isUploading", "progress")
|
|
|
|
text(isUploading, progress) {
|
|
|
|
if (isUploading) {
|
|
|
|
return progress + " %";
|
2018-06-15 23:03:24 +08:00
|
|
|
} else {
|
2019-05-27 16:15:39 +08:00
|
|
|
return this.uploadText;
|
2018-06-15 23:03:24 +08:00
|
|
|
}
|
2019-04-26 18:16:21 +08:00
|
|
|
},
|
2018-06-15 23:03:24 +08:00
|
|
|
|
|
|
|
buildBuffer(buffer) {
|
2019-05-27 16:15:39 +08:00
|
|
|
const icon = this.isUploading ? "times" : "upload";
|
2018-06-15 23:03:24 +08:00
|
|
|
buffer.push(iconHTML(icon));
|
2019-05-27 16:15:39 +08:00
|
|
|
buffer.push("<span class='ru-label'>" + this.text + "</span>");
|
2018-06-15 23:03:24 +08:00
|
|
|
buffer.push(
|
2019-05-27 16:42:53 +08:00
|
|
|
"<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() {
|
2019-05-27 16:15:39 +08:00
|
|
|
if (this.isUploading) {
|
2018-06-15 23:03:24 +08:00
|
|
|
this.resumable.cancel();
|
2019-10-30 02:52:36 +08:00
|
|
|
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
|
|
|
})
|
|
|
|
);
|