This repository has been archived on 2023-03-18. You can view files and clone it, but cannot push or open issues or pull requests.
osr-discourse-src/app/assets/javascripts/discourse/controllers/publish-page.js
Robin Ward e1f8014acd
FEATURE: Support for publishing topics as pages (#9364)
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.
2020-04-08 12:52:36 -04:00

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();
}
});