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

79 lines
1.9 KiB
Plaintext
Raw Normal View History

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 computed from "ember-addons/ember-computed-decorators";
import { on, observes } from "ember-addons/ember-computed-decorators";
import { popupAjaxError } from "discourse/lib/ajax-error";
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
});
},
2018-06-15 23:03:24 +08:00
@computed("buffered.host", "host.isSaving")
cantSave(host, isSaving) {
return isSaving || Ember.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", Discourse.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);
}
}
}
});