@stackend/api
Version:
JS bindings to api.stackend.com
545 lines • 17.1 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.extractContentValues = exports.createContentValue = exports.removeAllChildNodes = exports.addContentToDom = exports._addContentToDom = exports.getDefaultPageId = exports.newSubSiteNode = exports.newSubSite = exports.searchSubSites = exports.removeSubSite = exports.storeSubSite = exports.getSubSite = exports.getAvailablePagePermalink = exports.searchPageContent = exports.getPages = exports.searchPages = exports.searchContentUse = exports.getPage = exports.removePage = exports.editPage = exports.newPage = exports.moveContent = exports.removeContent = exports.setModerationStatus = exports.editContent = exports.parseMenuVisibility = exports.MenuVisibility = exports.PageContentType = exports.search = exports.ContentSortCritera = exports.listContent = exports.populateTemplateContent = exports.getContent = exports.DEFAULT_CMS_CONTEXT = exports.COMPONENT_NAME = exports.COMPONENT_CLASS = exports.RICH_CONTENT_CSS_CLASS = void 0;
var api_1 = require("../api");
var permalink_1 = require("../api/permalink");
var tree_1 = require("../api/tree");
/**
* Xcap Cms api constants and methods.
*
* @since 6 feb 2017
*/
/**
* Css Class for elements containing rich content
* @type {string}
*/
exports.RICH_CONTENT_CSS_CLASS = 'stackend-rich-content';
/**
* Component class (used to look up privileges, etc)
*/
exports.COMPONENT_CLASS = 'se.josh.xcap.cms.CmsManager';
/**
* Component name
*/
exports.COMPONENT_NAME = 'cms';
/**
* Default context
* @type {string}
*/
exports.DEFAULT_CMS_CONTEXT = 'cms';
/**
* Get CMS content
* @param id Content id (required)
* @param permalink Content permalink (optional)
*/
function getContent(_a) {
var id = _a.id, permalink = _a.permalink;
return (0, api_1.getJson)({ url: '/cms/get', parameters: arguments });
}
exports.getContent = getContent;
/**
* Get CMS content, populate template values.
* Any extra parameters passed down are used
* @param id Content id (required)
* @param permalink Content permalink (optional)
*/
function populateTemplateContent(_a) {
var id = _a.id, permalink = _a.permalink;
return (0, api_1.getJson)({ url: '/cms/populate-template', parameters: arguments });
}
exports.populateTemplateContent = populateTemplateContent;
/**
* List CMS content by category/permalink.
* @param permalink Content category permalink (optional)
* @param p Page number (optional)
* @param pageSize Page size (optional)
*/
function listContent(_a) {
var permalink = _a.permalink, _b = _a.p, p = _b === void 0 ? 1 : _b, pageSize = _a.pageSize;
return (0, api_1.getJson)({
url: '/cms/list',
parameters: {
permalink: permalink,
getPermalinkFromUrl: false,
p: p,
pageSize: pageSize
}
});
}
exports.listContent = listContent;
var ContentSortCritera;
(function (ContentSortCritera) {
ContentSortCritera["CREATED"] = "CREATED";
ContentSortCritera["MODIFIED"] = "MODIFIED";
ContentSortCritera["NAME"] = "NAME";
ContentSortCritera["SORT"] = "SORT";
})(ContentSortCritera = exports.ContentSortCritera || (exports.ContentSortCritera = {}));
/**
* Search CMS content. Requires stack admin.
* @param q Search expression
* @param p Page number (optional)
* @param pageSize Page size (optional)
* @param orderBy ContentSortCritera by (optional)
* @param order: Order (optional)
*/
function search(_a) {
var q = _a.q, _b = _a.p, p = _b === void 0 ? 1 : _b, pageSize = _a.pageSize, orderBy = _a.orderBy, order = _a.order;
return (0, api_1.getJson)({
url: '/cms/search',
parameters: {
q: q,
p: p,
pageSize: pageSize,
orderBy: orderBy,
order: order
}
});
}
exports.search = search;
var PageContentType;
(function (PageContentType) {
PageContentType["CMS"] = "stackend-cms";
})(PageContentType = exports.PageContentType || (exports.PageContentType = {}));
var MenuVisibility;
(function (MenuVisibility) {
MenuVisibility["HORIZONTAL"] = "HORIZONTAL";
MenuVisibility["VERTICAL"] = "VERTICAL";
MenuVisibility["FIXED"] = "FIXED";
MenuVisibility["OFF"] = "OFF";
})(MenuVisibility = exports.MenuVisibility || (exports.MenuVisibility = {}));
function parseMenuVisibility(v) {
if (!v) {
return MenuVisibility.HORIZONTAL;
}
else if (v === 'false') {
return MenuVisibility.OFF;
}
var x = v.toUpperCase();
if (x === MenuVisibility.VERTICAL) {
return MenuVisibility.VERTICAL;
}
else if (x === MenuVisibility.FIXED) {
return MenuVisibility.FIXED;
}
return MenuVisibility.HORIZONTAL;
}
exports.parseMenuVisibility = parseMenuVisibility;
/**
* Edit CMS content.
* @param id (may be 0 to create a new content object)
* @param permalink
* @param headline
* @param teaser
* @param body
* @param categoryId
*
* The body may be up to 65KB html.
*
* @returns {Promise}
*/
function editContent(_a) {
var id = _a.id, permalink = _a.permalink, headline = _a.headline, teaser = _a.teaser, body = _a.body, categoryId = _a.categoryId;
return (0, api_1.post)({ url: '/cms/edit', parameters: arguments });
}
exports.editContent = editContent;
/**
* Set moderation status of cms content
* @param id
* @param moderationStatus
*/
function setModerationStatus(_a) {
var id = _a.id, moderationStatus = _a.moderationStatus;
return (0, api_1.post)({ url: '/cms/set-modstatus', parameters: arguments });
}
exports.setModerationStatus = setModerationStatus;
/**
* Remove CMS content.
*
* @param id Cms content id (required)
* @returns {Promise}
*/
function removeContent(_a) {
var id = _a.id;
return (0, api_1.post)({ url: '/cms/remove', parameters: arguments });
}
exports.removeContent = removeContent;
/**
* Move CMS content
* @returns {Thunk<XcapJsonResult>}
*/
function moveContent(_a) {
var id = _a.id, newCategoryId = _a.newCategoryId, oldCategoryId = _a.oldCategoryId, insertion = _a.insertion, insertionPoint = _a.insertionPoint;
return (0, api_1.post)({
url: '/cms/move',
parameters: {
referenceId: id,
newCategoryId: newCategoryId,
oldCategoryId: oldCategoryId,
insertion: insertion,
insertionPoint: insertionPoint
}
});
}
exports.moveContent = moveContent;
/**
* Create a new page
* @param name
* @param permalink
*/
function newPage(name, permalink) {
var pl = '/' + (permalink ? permalink : (0, permalink_1.generatePermalink)(name));
return {
id: 0,
name: name,
permalink: pl,
enabled: true,
metaDescription: null,
ogImageUrl: null,
content: [],
parentPageId: 0,
childCount: 0
};
}
exports.newPage = newPage;
/**
* Edit a cms page
*
* @returns {Thunk<EditPageResult>}
*/
function editPage(_a) {
var page = _a.page, parentPageId = _a.parentPageId;
return (0, api_1.post)({
url: '/cms/pages/edit',
parameters: {
page: JSON.stringify(page),
parentPageId: parentPageId
}
});
}
exports.editPage = editPage;
/**
* Remove a cms page
* @param id
* @returns {Thunk<XcapJsonResult>}
*/
function removePage(_a) {
var id = _a.id;
return (0, api_1.post)({ url: '/cms/pages/remove', parameters: arguments });
}
exports.removePage = removePage;
/**
* List all content for a cms page.
* @param id
* @param permalink
* @param p
* @param pageSize
* @returns {Thunk<GetPageResult>}
*/
function getPage(_a) {
var id = _a.id, permalink = _a.permalink, _b = _a.p, p = _b === void 0 ? 1 : _b, _c = _a.pageSize, pageSize = _c === void 0 ? 100 : _c;
return (0, api_1.getJson)({
url: '/cms/pages/get',
parameters: {
id: id,
permalink: permalink,
p: p,
pageSize: pageSize
}
});
}
exports.getPage = getPage;
/**
* Search for pages where a content is used.
* @param contentId
* @param p
* @param pageSize
* @returns {Thunk<SearchPagesResult>}
*/
function searchContentUse(_a) {
var contentId = _a.contentId, _b = _a.p, p = _b === void 0 ? 1 : _b, _c = _a.pageSize, pageSize = _c === void 0 ? 10 : _c;
return (0, api_1.getJson)({
url: '/cms/find-uses',
parameters: {
contentId: contentId,
p: p,
pageSize: pageSize
}
});
}
exports.searchContentUse = searchContentUse;
/**
* Search for cms pages
* @param q
* @param orderBy "name" or "createdDate"
* @param order
* @param p
* @param pageSize
* @returns {Thunk<SearchPagesResult>}
*/
function searchPages(_a) {
var q = _a.q, p = _a.p, pageSize = _a.pageSize, orderBy = _a.orderBy, order = _a.order;
return (0, api_1.getJson)({
url: '/cms/pages/search',
parameters: arguments
});
}
exports.searchPages = searchPages;
/**
* Get multiple pages by id.
* @param pageIds
* @param permalinks
* @param communityPermalink
* @returns {Thunk<GetPagesResult>}
*/
function getPages(_a) {
var _b;
var pageIds = _a.pageIds, permalinks = _a.permalinks, communityPermalink = _a.communityPermalink;
return (0, api_1.getJson)({
url: '/cms/pages/get-multiple',
parameters: (_b = {
pageId: pageIds,
permalink: permalinks
},
_b[api_1.COMMUNITY_PARAMETER] = communityPermalink,
_b)
});
}
exports.getPages = getPages;
/**
* Search for page content
* @param q
* @param codeBinOnly
* @returns {Thunk<SearchPageContentResult>}
*/
function searchPageContent(_a) {
var q = _a.q, codeBinOnly = _a.codeBinOnly;
return (0, api_1.getJson)({
url: '/cms/pages/search-page-content',
parameters: arguments
});
}
exports.searchPageContent = searchPageContent;
/**
* Construct an available permalink for cms pages
* @param pageId
* @param permalink
* @returns {Thunk<XcapJsonResult>}
*/
function getAvailablePagePermalink(_a) {
var pageId = _a.pageId, permalink = _a.permalink;
return (0, api_1.getJson)({
url: '/cms/pages/get-available-permalink',
parameters: arguments
});
}
exports.getAvailablePagePermalink = getAvailablePagePermalink;
/**
* Get a subsite by id or permalink
* @param id
*
*/
function getSubSite(_a) {
var id = _a.id, permalink = _a.permalink;
return (0, api_1.getJson)({
url: '/cms/subsite/get',
parameters: arguments
});
}
exports.getSubSite = getSubSite;
/**
* Store a subsite
* @param subSite
*/
function storeSubSite(_a) {
var subSite = _a.subSite;
return (0, api_1.post)({
url: '/cms/subsite/store',
parameters: {
tree: JSON.stringify(subSite)
}
});
}
exports.storeSubSite = storeSubSite;
/**
* Remove a sub site
* @param id
*/
function removeSubSite(_a) {
var id = _a.id;
return (0, api_1.post)({
url: '/cms/subsite/remove',
parameters: arguments
});
}
exports.removeSubSite = removeSubSite;
/**
* Search for sub sites
* @param q
* @param p
* @param pageSize
*/
function searchSubSites(_a) {
var q = _a.q, p = _a.p, pageSize = _a.pageSize;
return (0, api_1.getJson)({
url: '/cms/subsite/list',
parameters: arguments
});
}
exports.searchSubSites = searchSubSites;
/**
* Create, but does not store a new sub site
*/
function newSubSite(name) {
return (0, tree_1.newTree)(name);
}
exports.newSubSite = newSubSite;
/**
* Create, but does not store a new sub site node
*/
function newSubSiteNode(name) {
return (0, tree_1.newTreeNode)(name);
}
exports.newSubSiteNode = newSubSiteNode;
/**
* Get the default page (start page) id.
* @param subSite
* @returns {number}
*/
function getDefaultPageId(subSite) {
if (!subSite) {
return 0;
}
if (subSite.referenceId) {
return subSite.referenceId;
}
for (var i = 0; i < subSite.children.length; i++) {
var c = subSite.children[i];
if (c.referenceId) {
return c.referenceId;
}
}
return 0;
}
exports.getDefaultPageId = getDefaultPageId;
/**
* Add content to the dom. Client side only
* @param contentId
* @param html
* @param css
* @param javascript
* @param parent
*/
function _addContentToDom(parent, contentId, html, css, javascript) {
var document = parent.ownerDocument;
if (parent && (html || css)) {
var addHtml = (css ? css : '') + (html ? html : '');
try {
var f = document.createRange().createContextualFragment(addHtml);
// Replace children
while (parent.childNodes.length > 0) {
parent.removeChild(parent.childNodes[0]);
}
parent.appendChild(f);
}
catch (x) {
console.warn('Error in javascript tag: ', addHtml, ' from cms module: ', contentId);
}
}
if (javascript) {
try {
var range = document.createRange();
range.setStart(document.getElementsByTagName('BODY')[0], 0);
document.getElementsByTagName('BODY')[0].appendChild(range.createContextualFragment(javascript));
}
catch (x) {
console.warn('Error in javascript: ', x, ' from cms module: ', contentId);
}
}
}
exports._addContentToDom = _addContentToDom;
/**
* Add content to the dom. Client side only
* @param parent
* @param content
*/
function addContentToDom(parent, content) {
if (!parent || !content) {
return;
}
if ((0, api_1.isRunningServerSide)()) {
throw Error('Stackend: addContentToDom can not be executed serverside');
}
var _a = extractContentValues(content), htmlValue = _a.htmlValue, javascriptValue = _a.javascriptValue, cssValue = _a.cssValue;
var style = "<style>".concat(cssValue, "</style>");
var javascript = '';
if (javascriptValue !== '') {
javascript = "<script type=\"text/javascript\" id=\"stackend-cms-js-".concat(content.id, "\">").concat(javascriptValue, "</script>");
}
var html = style + "<div class='".concat(exports.RICH_CONTENT_CSS_CLASS, "'>").concat(htmlValue, "</div>");
_addContentToDom(parent, content.id, html, '', javascript);
}
exports.addContentToDom = addContentToDom;
/**
* Remove all child nodes of an element
* @param element
*/
function removeAllChildNodes(element) {
while (element.childNodes.length > 0) {
element.removeChild(element.childNodes[0]);
}
}
exports.removeAllChildNodes = removeAllChildNodes;
/**
* Construct a composite content value
* @param html
* @param css
* @param js
*/
function createContentValue(html, css, js) {
var stringStyle = "<style type=\"text/css\" data-stackend-cms=\"css\">".concat(css, "</style>");
var stringJavascript = "<script type=\"text/javascript\" data-stackend-cms=\"js\">".concat(js, "</script>");
var stringHtml = "<div data-stackend-cms=\"html\">".concat(html, "</div>");
// HTML should be the last one to not mess up the other two in case of backend tag balancing.
// However, javascript needs to be last for preview to work
return "".concat(stringStyle).concat(stringHtml).concat(stringJavascript);
}
exports.createContentValue = createContentValue;
/**
* Split a content object into HTML, CSS and JS.
* NOTE: Browser only
* @param content
* @returns {{htmlValue: (*|string), javascriptValue: (*|string), cssValue: (*|string)}}
*/
function extractContentValues(content) {
var html = null;
var javascript = null;
var css = null;
if (content && content.body) {
var xmlDoc = new DOMParser().parseFromString(content.body, 'text/html');
css = xmlDoc.evaluate('//style[@data-stackend-cms="css"]', xmlDoc, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null)
.singleNodeValue;
javascript = xmlDoc.evaluate('//script[@data-stackend-cms="js"]', xmlDoc, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
html = xmlDoc.evaluate('//div[@data-stackend-cms="html"]', xmlDoc, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null)
.singleNodeValue;
// Fall backs to old style code
if (css === null) {
css = xmlDoc.evaluate('/html/head/style', xmlDoc, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null)
.singleNodeValue;
}
if (html == null) {
html = xmlDoc.evaluate('/html/body/div', xmlDoc, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null)
.singleNodeValue;
}
if (javascript == null) {
javascript = xmlDoc.evaluate('/html/body/script', xmlDoc, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null)
.singleNodeValue;
}
}
return {
htmlValue: html ? html.innerHTML : '',
javascriptValue: javascript ? javascript.innerHTML : '',
cssValue: css ? css.innerHTML : ''
};
}
exports.extractContentValues = extractContentValues;
//# sourceMappingURL=index.js.map