|
|
@@ -13,6 +13,8 @@ module.exports = function(crowi, app) {
|
|
|
, ApiResponse = require('../util/apiResponse')
|
|
|
, interceptorManager = crowi.getInterceptorManager()
|
|
|
, pagePathUtil = require('../util/pagePathUtil')
|
|
|
+ , swig = require('swig-templates')
|
|
|
+ , getToday = require('../util/getToday')
|
|
|
|
|
|
, actions = {};
|
|
|
|
|
|
@@ -252,14 +254,11 @@ module.exports = function(crowi, app) {
|
|
|
tree: [],
|
|
|
pageRelatedGroup: null,
|
|
|
template: null,
|
|
|
- childrenTemplateExists: false,
|
|
|
- decendantsTemplateExists: false,
|
|
|
};
|
|
|
|
|
|
var pageTeamplate = 'customlayout-selector/page';
|
|
|
|
|
|
var isRedirect = false;
|
|
|
- var originalPath = path;
|
|
|
Page.findPage(path, req.user, req.query.revision)
|
|
|
.then(function(page) {
|
|
|
debug('Page found', page._id, page.path);
|
|
|
@@ -283,11 +282,7 @@ module.exports = function(crowi, app) {
|
|
|
renderVars.tree = tree;
|
|
|
})
|
|
|
.then(function() {
|
|
|
- return Page.checkIfTemplatesExist(originalPath);
|
|
|
- })
|
|
|
- .then(function(templateInfo) {
|
|
|
- renderVars.childrenTemplateExists = templateInfo.childrenTemplateExists;
|
|
|
- renderVars.decendantsTemplateExists = templateInfo.decendantsTemplateExists;
|
|
|
+ return Page.checkIfTemplatesExist(path);
|
|
|
})
|
|
|
.then(() => {
|
|
|
return PageGroupRelation.findByPage(renderVars.page);
|
|
|
@@ -333,8 +328,12 @@ module.exports = function(crowi, app) {
|
|
|
.catch(function(err) {
|
|
|
pageTeamplate = 'customlayout-selector/not_found';
|
|
|
|
|
|
- return Page.findTemplate(originalPath)
|
|
|
+ return Page.findTemplate(path)
|
|
|
.then(template => {
|
|
|
+ if (template) {
|
|
|
+ template = replacePlaceholders(template, req);
|
|
|
+ }
|
|
|
+
|
|
|
renderVars.template = template;
|
|
|
});
|
|
|
})
|
|
|
@@ -371,6 +370,17 @@ module.exports = function(crowi, app) {
|
|
|
});
|
|
|
};
|
|
|
|
|
|
+ const replacePlaceholders = (template, req) => {
|
|
|
+ const definitions = {
|
|
|
+ pagepath: getPathFromRequest(req),
|
|
|
+ username: req.user.name,
|
|
|
+ today: getToday(),
|
|
|
+ };
|
|
|
+ const compiledTemplate = swig.compile(template);
|
|
|
+
|
|
|
+ return compiledTemplate(definitions);
|
|
|
+ };
|
|
|
+
|
|
|
actions.deletedPageListShow = function(req, res) {
|
|
|
var path = '/trash' + getPathFromRequest(req);
|
|
|
var limit = 50;
|
|
|
@@ -443,8 +453,13 @@ module.exports = function(crowi, app) {
|
|
|
function renderPage(pageData, req, res) {
|
|
|
// create page
|
|
|
if (!pageData) {
|
|
|
- return Page.findTemplate(getPathFromRequest(req))
|
|
|
- .then((template) => {
|
|
|
+ const path = getPathFromRequest(req);
|
|
|
+ return Page.findTemplate(path)
|
|
|
+ .then(template => {
|
|
|
+ if (template) {
|
|
|
+ template = replacePlaceholders(template, req);
|
|
|
+ }
|
|
|
+
|
|
|
return res.render('customlayout-selector/not_found', {
|
|
|
author: {},
|
|
|
page: false,
|
|
|
@@ -463,8 +478,6 @@ module.exports = function(crowi, app) {
|
|
|
page: pageData,
|
|
|
revision: pageData.revision || {},
|
|
|
author: pageData.revision.author || false,
|
|
|
- childrenTemplateExists: false,
|
|
|
- decendantsTemplateExists: false,
|
|
|
};
|
|
|
var userPage = isUserPage(pageData.path);
|
|
|
var userData = null;
|
|
|
@@ -509,12 +522,6 @@ module.exports = function(crowi, app) {
|
|
|
}
|
|
|
}).then(function() {
|
|
|
return interceptorManager.process('beforeRenderPage', req, res, renderVars);
|
|
|
- }).then(function() {
|
|
|
- return Page.checkIfTemplatesExist(pageData.path)
|
|
|
- .then(function(templateInfo) {
|
|
|
- renderVars.childrenTemplateExists = templateInfo.childrenTemplateExists;
|
|
|
- renderVars.decendantsTemplateExists = templateInfo.decendantsTemplateExists;
|
|
|
- });
|
|
|
}).then(function() {
|
|
|
var defaultPageTeamplate = 'customlayout-selector/page';
|
|
|
if (userData) {
|
|
|
@@ -670,13 +677,6 @@ module.exports = function(crowi, app) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- return res.redirect(redirectPath);
|
|
|
- }).catch(function(err) {
|
|
|
- debug('Page create or edit error.', err);
|
|
|
- if (pageData && !req.form.isValid) {
|
|
|
- return renderPage(pageData, req, res);
|
|
|
- }
|
|
|
-
|
|
|
return res.redirect(redirectPath);
|
|
|
});
|
|
|
};
|
|
|
@@ -1186,5 +1186,23 @@ module.exports = function(crowi, app) {
|
|
|
});
|
|
|
};
|
|
|
|
|
|
+ /**
|
|
|
+ * @api {get} /pages.templates Check if templates exist for page
|
|
|
+ * @apiName FindTemplates
|
|
|
+ * @apiGroup Page
|
|
|
+ *
|
|
|
+ * @apiParam {String} path
|
|
|
+ */
|
|
|
+ api.templates = function(req, res) {
|
|
|
+ const pagePath = req.query.path;
|
|
|
+ const templateFinder = Page.checkIfTemplatesExist(pagePath);
|
|
|
+
|
|
|
+ templateFinder.then(function(templateInfo) {
|
|
|
+ return res.json(ApiResponse.success(templateInfo));
|
|
|
+ }).catch(function(err) {
|
|
|
+ return res.json(ApiResponse.error(err));
|
|
|
+ });
|
|
|
+ };
|
|
|
+
|
|
|
return actions;
|
|
|
};
|