2023-10-11 02:38:59 +08:00
|
|
|
import Component from "@ember/component";
|
2023-02-23 23:32:53 +08:00
|
|
|
import { action } from "@ember/object";
|
|
|
|
import { or } from "@ember/object/computed";
|
2023-10-11 02:38:59 +08:00
|
|
|
import { inject as service } from "@ember/service";
|
2019-11-01 01:37:24 +08:00
|
|
|
import { isEmpty } from "@ember/utils";
|
2023-10-11 02:38:59 +08:00
|
|
|
import { tagName } from "@ember-decorators/component";
|
2015-08-19 05:15:46 +08:00
|
|
|
import { popupAjaxError } from "discourse/lib/ajax-error";
|
2023-10-11 02:38:59 +08:00
|
|
|
import { bufferedProperty } from "discourse/mixins/buffered-content";
|
|
|
|
import Category from "discourse/models/category";
|
|
|
|
import discourseComputed from "discourse-common/utils/decorators";
|
2023-10-18 18:07:09 +08:00
|
|
|
import I18n from "discourse-i18n";
|
2015-08-19 05:15:46 +08:00
|
|
|
|
2023-02-23 23:32:53 +08:00
|
|
|
@tagName("tr")
|
|
|
|
export default class EmbeddableHost extends Component.extend(
|
|
|
|
bufferedProperty("host")
|
|
|
|
) {
|
|
|
|
@service dialog;
|
|
|
|
editToggled = false;
|
|
|
|
categoryId = null;
|
|
|
|
category = null;
|
2015-08-19 05:15:46 +08:00
|
|
|
|
2023-02-23 23:32:53 +08:00
|
|
|
@or("host.isNew", "editToggled") editing;
|
2015-08-19 05:15:46 +08:00
|
|
|
|
2023-02-24 17:44:50 +08:00
|
|
|
init() {
|
|
|
|
super.init(...arguments);
|
2021-10-06 00:59:27 +08:00
|
|
|
|
|
|
|
const host = this.host;
|
|
|
|
const categoryId = host.category_id || this.site.uncategorized_category_id;
|
|
|
|
const category = Category.findById(categoryId);
|
|
|
|
|
|
|
|
host.set("category", category);
|
2023-02-23 23:32:53 +08:00
|
|
|
}
|
2021-10-06 00:59:27 +08:00
|
|
|
|
2019-11-08 05:38:28 +08:00
|
|
|
@discourseComputed("buffered.host", "host.isSaving")
|
2015-08-19 05:15:46 +08:00
|
|
|
cantSave(host, isSaving) {
|
2019-11-01 01:37:24 +08:00
|
|
|
return isSaving || isEmpty(host);
|
2023-02-23 23:32:53 +08:00
|
|
|
}
|
2015-08-19 05:15:46 +08:00
|
|
|
|
2023-02-23 23:32:53 +08:00
|
|
|
@action
|
|
|
|
edit() {
|
|
|
|
this.set("categoryId", this.get("host.category.id"));
|
|
|
|
this.set("editToggled", true);
|
|
|
|
}
|
2015-08-19 05:15:46 +08:00
|
|
|
|
2023-02-23 23:32:53 +08:00
|
|
|
@action
|
|
|
|
save() {
|
|
|
|
if (this.cantSave) {
|
|
|
|
return;
|
|
|
|
}
|
2015-08-19 05:15:46 +08:00
|
|
|
|
2023-02-23 23:32:53 +08:00
|
|
|
const props = this.buffered.getProperties(
|
|
|
|
"host",
|
|
|
|
"allowed_paths",
|
|
|
|
"class_name"
|
|
|
|
);
|
|
|
|
props.category_id = this.categoryId;
|
2015-08-19 05:15:46 +08:00
|
|
|
|
2023-02-23 23:32:53 +08:00
|
|
|
const host = this.host;
|
2016-08-24 02:55:52 +08:00
|
|
|
|
2023-02-23 23:32:53 +08:00
|
|
|
host
|
|
|
|
.save(props)
|
|
|
|
.then(() => {
|
|
|
|
host.set("category", Category.findById(this.categoryId));
|
|
|
|
this.set("editToggled", false);
|
|
|
|
})
|
|
|
|
.catch(popupAjaxError);
|
|
|
|
}
|
2015-08-19 05:15:46 +08:00
|
|
|
|
2023-02-23 23:32:53 +08:00
|
|
|
@action
|
|
|
|
delete() {
|
|
|
|
return this.dialog.confirm({
|
|
|
|
message: I18n.t("admin.embedding.confirm_delete"),
|
|
|
|
didConfirm: () => {
|
|
|
|
return this.host.destroyRecord().then(() => {
|
|
|
|
this.deleteHost(this.host);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
2015-08-19 05:15:46 +08:00
|
|
|
|
2023-02-23 23:32:53 +08:00
|
|
|
@action
|
|
|
|
cancel() {
|
|
|
|
const host = this.host;
|
|
|
|
if (host.get("isNew")) {
|
|
|
|
this.deleteHost(host);
|
|
|
|
} else {
|
|
|
|
this.rollbackBuffer();
|
|
|
|
this.set("editToggled", false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|