discourse/app/assets/javascripts/admin/components/admin-backups-logs.js.es6

65 lines
2.0 KiB
Plaintext
Raw Normal View History

2018-06-15 23:03:24 +08:00
import debounce from "discourse/lib/debounce";
import { renderSpinner } from "discourse/helpers/loading-spinner";
import { escapeExpression } from "discourse/lib/utilities";
import { bufferedRender } from "discourse-common/lib/buffered-render";
2014-02-13 12:35:46 +08:00
2018-06-15 23:03:24 +08:00
export default Ember.Component.extend(
bufferedRender({
classNames: ["admin-backups-logs"],
2014-02-13 12:35:46 +08:00
2018-06-15 23:03:24 +08:00
init() {
this._super();
this._reset();
},
2014-02-13 12:35:46 +08:00
2018-06-15 23:03:24 +08:00
_reset() {
this.setProperties({ formattedLogs: "", index: 0 });
},
2014-02-13 12:35:46 +08:00
2018-06-15 23:03:24 +08:00
_scrollDown() {
const $div = this.$()[0];
$div.scrollTop = $div.scrollHeight;
},
2016-10-27 23:57:33 +08:00
2018-06-15 23:03:24 +08:00
_updateFormattedLogs: debounce(function() {
const logs = this.get("logs");
if (logs.length === 0) {
this._reset(); // reset the cached logs whenever the model is reset
} else {
// do the log formatting only once for HELLish performance
let formattedLogs = this.get("formattedLogs");
for (let i = this.get("index"), length = logs.length; i < length; i++) {
const date = logs[i].get("timestamp"),
message = escapeExpression(logs[i].get("message"));
formattedLogs += "[" + date + "] " + message + "\n";
}
// update the formatted logs & cache index
this.setProperties({
formattedLogs: formattedLogs,
index: logs.length
});
// force rerender
this.rerenderBuffer();
2014-02-13 12:35:46 +08:00
}
2018-06-15 23:03:24 +08:00
Ember.run.scheduleOnce("afterRender", this, this._scrollDown);
}, 150)
.observes("logs.[]")
.on("init"),
2014-02-13 12:35:46 +08:00
2018-06-15 23:03:24 +08:00
buildBuffer(buffer) {
const formattedLogs = this.get("formattedLogs");
if (formattedLogs && formattedLogs.length > 0) {
buffer.push("<pre>");
buffer.push(formattedLogs);
buffer.push("</pre>");
} else {
buffer.push("<p>" + I18n.t("admin.backups.logs.none") + "</p>");
}
// add a loading indicator
if (this.get("status.isOperationRunning")) {
buffer.push(renderSpinner("small"));
}
2014-02-13 12:35:46 +08:00
}
2018-06-15 23:03:24 +08:00
})
);