2014-06-03 17:05:25 +04:00
|
|
|
// # Themes API
|
|
|
|
// RESTful API for Themes
|
2014-08-17 10:17:23 +04:00
|
|
|
var Promise = require('bluebird'),
|
2014-05-13 18:33:34 +04:00
|
|
|
_ = require('lodash'),
|
|
|
|
config = require('../config'),
|
|
|
|
errors = require('../errors'),
|
|
|
|
settings = require('./settings'),
|
2015-10-21 16:57:01 +03:00
|
|
|
pipeline = require('../utils/pipeline'),
|
|
|
|
utils = require('./utils'),
|
2015-11-12 15:29:45 +03:00
|
|
|
i18n = require('../i18n'),
|
2015-10-21 16:57:01 +03:00
|
|
|
|
|
|
|
docName = 'themes',
|
2014-05-13 18:33:34 +04:00
|
|
|
themes;
|
|
|
|
|
2015-10-21 16:57:01 +03:00
|
|
|
/**
|
|
|
|
* ### Fetch Active Theme
|
|
|
|
* @returns {Theme} theme
|
|
|
|
*/
|
|
|
|
|
|
|
|
function fetchActiveTheme() {
|
|
|
|
return settings.read({
|
|
|
|
key: 'activeTheme',
|
|
|
|
context: {
|
|
|
|
internal: true
|
|
|
|
}
|
|
|
|
}).then(function (response) {
|
|
|
|
return response.settings[0].value;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ### Fetch Available Themes
|
|
|
|
* @returns {Themes} themes
|
|
|
|
*/
|
|
|
|
|
|
|
|
function fetchAvailableThemes() {
|
|
|
|
var themes = {};
|
|
|
|
|
|
|
|
_.each(config.paths.availableThemes, function (theme, name) {
|
|
|
|
var isTheme = name.indexOf('.') !== 0 && name !== '_messages' && name.toLowerCase() !== 'readme.md';
|
|
|
|
|
|
|
|
if (!isTheme) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
themes[name] = theme;
|
|
|
|
});
|
|
|
|
|
|
|
|
return themes;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ### Activate Theme
|
|
|
|
* @param {Theme} theme
|
|
|
|
* @returns {Object} response
|
|
|
|
*/
|
|
|
|
|
|
|
|
function activateTheme(theme) {
|
|
|
|
return settings.edit({
|
|
|
|
settings: [{
|
|
|
|
key: 'activeTheme',
|
|
|
|
value: theme.name
|
|
|
|
}],
|
|
|
|
context: {
|
|
|
|
internal: true
|
|
|
|
}
|
|
|
|
}).then(function () {
|
|
|
|
theme.active = true;
|
|
|
|
|
|
|
|
return {themes: [theme]};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-06-03 17:05:25 +04:00
|
|
|
/**
|
|
|
|
* ## Themes API Methods
|
|
|
|
*
|
|
|
|
* **See:** [API Methods](index.js.html#api%20methods)
|
|
|
|
*/
|
2014-05-13 18:33:34 +04:00
|
|
|
themes = {
|
2014-06-03 17:05:25 +04:00
|
|
|
/**
|
|
|
|
* ### Browse
|
|
|
|
* Get a list of all the available themes
|
|
|
|
* @param {{context}} options
|
|
|
|
* @returns {Promise(Themes)}
|
|
|
|
*/
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
browse: function browse(options) {
|
2015-10-21 16:57:01 +03:00
|
|
|
var tasks;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ### Model Query
|
|
|
|
* @returns {Object} result
|
|
|
|
*/
|
|
|
|
|
|
|
|
function modelQuery() {
|
|
|
|
var result = {
|
|
|
|
availableThemes: fetchAvailableThemes(),
|
|
|
|
activeTheme: fetchActiveTheme()
|
|
|
|
};
|
|
|
|
|
|
|
|
return Promise.props(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ### Build response
|
|
|
|
* @param {Object} result - result from modelQuery()
|
|
|
|
* @returns {Object} response
|
|
|
|
*/
|
|
|
|
|
|
|
|
function buildResponse(result) {
|
|
|
|
var themes = [];
|
|
|
|
|
|
|
|
_.each(result.availableThemes, function (theme, name) {
|
|
|
|
var item = {
|
|
|
|
active: result.activeTheme === name,
|
|
|
|
uuid: name
|
|
|
|
};
|
|
|
|
|
|
|
|
// if theme has package.json file,
|
|
|
|
// merge its properties
|
|
|
|
if (theme['package.json']) {
|
|
|
|
item = _.merge(item, theme['package.json']);
|
|
|
|
}
|
2014-05-13 18:33:34 +04:00
|
|
|
|
2015-10-21 16:57:01 +03:00
|
|
|
themes.push(item);
|
2014-05-13 18:33:34 +04:00
|
|
|
});
|
2015-10-21 16:57:01 +03:00
|
|
|
|
|
|
|
return {themes: themes};
|
|
|
|
}
|
|
|
|
|
|
|
|
tasks = [
|
|
|
|
utils.validate(docName),
|
|
|
|
utils.handlePublicPermissions(docName, 'browse'),
|
|
|
|
modelQuery,
|
|
|
|
buildResponse
|
|
|
|
];
|
|
|
|
|
|
|
|
return pipeline(tasks, options || {});
|
2014-05-13 18:33:34 +04:00
|
|
|
},
|
|
|
|
|
2014-06-03 17:05:25 +04:00
|
|
|
/**
|
|
|
|
* ### Edit
|
|
|
|
* Change the active theme
|
|
|
|
* @param {Theme} object
|
|
|
|
* @param {{context}} options
|
|
|
|
* @returns {Promise(Theme)}
|
|
|
|
*/
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
edit: function edit(object, options) {
|
2015-10-21 16:57:01 +03:00
|
|
|
var tasks, themeName;
|
2014-05-13 18:33:34 +04:00
|
|
|
|
|
|
|
// Check whether the request is properly formatted.
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
if (!_.isArray(object.themes)) {
|
2015-11-12 15:29:45 +03:00
|
|
|
return Promise.reject(new errors.BadRequestError(i18n.t('errors.api.themes.invalidRequest')));
|
2014-05-13 18:33:34 +04:00
|
|
|
}
|
|
|
|
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
themeName = object.themes[0].uuid;
|
2014-05-13 18:33:34 +04:00
|
|
|
|
2015-10-21 16:57:01 +03:00
|
|
|
/**
|
|
|
|
* ### Model Query
|
|
|
|
* @param {Object} options
|
|
|
|
* @returns {Theme} theme
|
|
|
|
*/
|
2014-05-13 18:33:34 +04:00
|
|
|
|
2015-10-21 16:57:01 +03:00
|
|
|
function modelQuery(options) {
|
|
|
|
return themes.browse(options).then(function (response) {
|
|
|
|
var theme = _.find(response.themes, function (theme) {
|
|
|
|
return theme.uuid === themeName;
|
2014-05-13 18:33:34 +04:00
|
|
|
});
|
|
|
|
|
|
|
|
if (!theme) {
|
2015-11-12 15:29:45 +03:00
|
|
|
return Promise.reject(new errors.BadRequestError(i18n.t('errors.api.themes.themeDoesNotExist')));
|
2014-05-13 18:33:34 +04:00
|
|
|
}
|
|
|
|
|
2015-10-21 16:57:01 +03:00
|
|
|
if (!theme.name) {
|
|
|
|
theme.name = themeName;
|
|
|
|
}
|
|
|
|
|
|
|
|
return theme;
|
2014-05-13 18:33:34 +04:00
|
|
|
});
|
2015-10-21 16:57:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
tasks = [
|
|
|
|
utils.validate(docName),
|
|
|
|
utils.handlePermissions(docName, 'edit'),
|
|
|
|
modelQuery,
|
|
|
|
activateTheme
|
|
|
|
];
|
|
|
|
|
|
|
|
return pipeline(tasks, options || {});
|
2014-05-13 18:33:34 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = themes;
|