If the feature is enabled, staff members can construct a URL and publish a topic for others to browse without the regular Discourse chrome. This is useful if you want to use Discourse like a CMS and publish topics as articles, which can then be embedded into other systems.
122 lines
2.9 KiB
JavaScript
122 lines
2.9 KiB
JavaScript
import Controller from "@ember/controller";
|
|
import ModalFunctionality from "discourse/mixins/modal-functionality";
|
|
import { computed, action } from "@ember/object";
|
|
import { equal, not } from "@ember/object/computed";
|
|
import { ajax } from "discourse/lib/ajax";
|
|
import { popupAjaxError } from "discourse/lib/ajax-error";
|
|
|
|
const States = {
|
|
initializing: "initializing",
|
|
checking: "checking",
|
|
valid: "valid",
|
|
invalid: "invalid",
|
|
saving: "saving",
|
|
new: "new",
|
|
existing: "existing",
|
|
unpublishing: "unpublishing",
|
|
unpublished: "unpublished"
|
|
};
|
|
|
|
const StateHelpers = {};
|
|
Object.keys(States).forEach(name => {
|
|
StateHelpers[name] = equal("state", name);
|
|
});
|
|
|
|
export default Controller.extend(ModalFunctionality, StateHelpers, {
|
|
state: null,
|
|
reason: null,
|
|
publishedPage: null,
|
|
disabled: not("valid"),
|
|
publishedPage: null,
|
|
|
|
showUrl: computed("state", function() {
|
|
return (
|
|
this.state === States.valid ||
|
|
this.state === States.saving ||
|
|
this.state === States.existing
|
|
);
|
|
}),
|
|
showUnpublish: computed("state", function() {
|
|
return this.state === States.existing || this.state === States.unpublishing;
|
|
}),
|
|
|
|
onShow() {
|
|
this.set("state", States.initializing);
|
|
|
|
this.store
|
|
.find("published_page", this.model.id)
|
|
.then(page => {
|
|
this.setProperties({ state: States.existing, publishedPage: page });
|
|
})
|
|
.catch(this.startNew);
|
|
},
|
|
|
|
@action
|
|
startCheckSlug() {
|
|
if (this.state === States.existing) {
|
|
return;
|
|
}
|
|
|
|
this.set("state", States.checking);
|
|
},
|
|
|
|
@action
|
|
checkSlug() {
|
|
if (this.state === States.existing) {
|
|
return;
|
|
}
|
|
return ajax("/pub/check-slug", {
|
|
data: { slug: this.publishedPage.slug }
|
|
}).then(result => {
|
|
if (result.valid_slug) {
|
|
this.set("state", States.valid);
|
|
} else {
|
|
this.setProperties({ state: States.invalid, reason: result.reason });
|
|
}
|
|
});
|
|
},
|
|
|
|
@action
|
|
unpublish() {
|
|
this.set("state", States.unpublishing);
|
|
return this.publishedPage
|
|
.destroyRecord()
|
|
.then(() => {
|
|
this.set("state", States.unpublished);
|
|
this.model.set("publishedPage", null);
|
|
})
|
|
.catch(result => {
|
|
this.set("state", States.existing);
|
|
popupAjaxError(result);
|
|
});
|
|
},
|
|
|
|
@action
|
|
publish() {
|
|
this.set("state", States.saving);
|
|
|
|
return this.publishedPage
|
|
.update({ slug: this.publishedPage.slug })
|
|
.then(() => {
|
|
this.set("state", States.existing);
|
|
this.model.set("publishedPage", this.publishedPage);
|
|
})
|
|
.catch(errResult => {
|
|
popupAjaxError(errResult);
|
|
this.set("state", States.existing);
|
|
});
|
|
},
|
|
|
|
@action
|
|
startNew() {
|
|
this.setProperties({
|
|
state: States.new,
|
|
publishedPage: this.store.createRecord("published_page", {
|
|
id: this.model.id,
|
|
slug: this.model.slug
|
|
})
|
|
});
|
|
this.checkSlug();
|
|
}
|
|
});
|