discourse/app/assets/javascripts/admin/components/embeddable-host.js.es6

81 lines
2.0 KiB
Plaintext
Raw Normal View History

import discourseComputed from "discourse-common/utils/decorators";
import { isEmpty } from "@ember/utils";
import { or } from "@ember/object/computed";
import { schedule } from "@ember/runloop";
import Component from "@ember/component";
2018-06-15 23:03:24 +08:00
import { bufferedProperty } from "discourse/mixins/buffered-content";
import { on, observes } from "discourse-common/utils/decorators";
2018-06-15 23:03:24 +08:00
import { popupAjaxError } from "discourse/lib/ajax-error";
import Category from "discourse/models/category";
export default Component.extend(bufferedProperty("host"), {
editToggled: false,
2018-06-15 23:03:24 +08:00
tagName: "tr",
categoryId: null,
editing: or("host.isNew", "editToggled"),
2018-06-15 23:03:24 +08:00
@on("didInsertElement")
@observes("editing")
_focusOnInput() {
schedule("afterRender", () => {
this.element.querySelector(".host-name").focus();
2018-06-15 23:03:24 +08:00
});
},
@discourseComputed("buffered.host", "host.isSaving")
cantSave(host, isSaving) {
return isSaving || isEmpty(host);
},
actions: {
edit() {
2018-06-15 23:03:24 +08:00
this.set("categoryId", this.get("host.category.id"));
this.set("editToggled", true);
},
save() {
if (this.cantSave) {
2018-06-15 23:03:24 +08:00
return;
}
const props = this.buffered.getProperties(
2018-06-15 23:03:24 +08:00
"host",
"path_whitelist",
"class_name"
);
props.category_id = this.categoryId;
const host = this.host;
2018-06-15 23:03:24 +08:00
host
.save(props)
.then(() => {
host.set("category", Category.findById(this.categoryId));
2018-06-15 23:03:24 +08:00
this.set("editToggled", false);
})
.catch(popupAjaxError);
},
delete() {
2018-06-15 23:03:24 +08:00
bootbox.confirm(I18n.t("admin.embedding.confirm_delete"), result => {
if (result) {
this.host.destroyRecord().then(() => {
this.deleteHost(this.host);
});
}
});
},
cancel() {
const host = this.host;
2018-06-15 23:03:24 +08:00
if (host.get("isNew")) {
this.deleteHost(host);
} else {
this.rollbackBuffer();
2018-06-15 23:03:24 +08:00
this.set("editToggled", false);
}
}
}
});