2014-06-03 17:05:25 +04:00
|
|
|
// # Themes API
|
|
|
|
// RESTful API for Themes
|
2014-05-13 18:33:34 +04:00
|
|
|
var when = require('when'),
|
|
|
|
_ = require('lodash'),
|
|
|
|
canThis = require('../permissions').canThis,
|
|
|
|
config = require('../config'),
|
|
|
|
errors = require('../errors'),
|
|
|
|
settings = require('./settings'),
|
|
|
|
when = require('when'),
|
|
|
|
themes;
|
|
|
|
|
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) {
|
|
|
|
options = options || {};
|
|
|
|
|
|
|
|
return canThis(options.context).browse.theme().then(function () {
|
2014-05-13 18:33:34 +04:00
|
|
|
return when.all([
|
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
|
|
|
settings.read({key: 'activeTheme', context: {internal: true}}),
|
2014-07-17 18:33:21 +04:00
|
|
|
config.paths.availableThemes
|
2014-05-13 18:33:34 +04:00
|
|
|
]).then(function (result) {
|
|
|
|
var activeTheme = result[0].settings[0].value,
|
|
|
|
availableThemes = result[1],
|
|
|
|
themes = [],
|
|
|
|
themeKeys = Object.keys(availableThemes);
|
|
|
|
|
|
|
|
_.each(themeKeys, function (key) {
|
|
|
|
if (key.indexOf('.') !== 0
|
|
|
|
&& key !== '_messages'
|
|
|
|
&& key !== 'README.md'
|
|
|
|
) {
|
|
|
|
|
|
|
|
var item = {
|
|
|
|
uuid: key
|
|
|
|
};
|
|
|
|
|
|
|
|
if (availableThemes[key].hasOwnProperty('package.json')) {
|
|
|
|
item = _.merge(item, availableThemes[key]['package.json']);
|
|
|
|
}
|
|
|
|
|
|
|
|
item.active = item.uuid === activeTheme;
|
|
|
|
|
|
|
|
themes.push(item);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return { themes: themes };
|
|
|
|
});
|
|
|
|
}, function () {
|
|
|
|
return when.reject(new errors.NoPermissionError('You do not have permission to browse themes.'));
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
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) {
|
|
|
|
var 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)) {
|
2014-05-13 18:33:34 +04:00
|
|
|
return when.reject({type: 'BadRequest', message: 'Invalid request.'});
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
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
|
|
|
return canThis(options.context).edit.theme().then(function () {
|
|
|
|
return themes.browse(options).then(function (availableThemes) {
|
2014-05-13 18:33:34 +04:00
|
|
|
var theme;
|
|
|
|
|
|
|
|
// Check if the theme exists
|
|
|
|
theme = _.find(availableThemes.themes, function (currentTheme) {
|
|
|
|
return currentTheme.uuid === themeName;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!theme) {
|
|
|
|
return when.reject(new errors.BadRequestError('Theme does not exist.'));
|
|
|
|
}
|
|
|
|
|
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
|
|
|
// Activate the theme
|
|
|
|
return settings.edit(
|
|
|
|
{settings: [{ key: 'activeTheme', value: themeName }]}, {context: {internal: true }}
|
|
|
|
).then(function () {
|
2014-05-13 18:33:34 +04:00
|
|
|
theme.active = true;
|
|
|
|
return { themes: [theme]};
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}, function () {
|
|
|
|
return when.reject(new errors.NoPermissionError('You do not have permission to edit themes.'));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = themes;
|