123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- var fs = require('fs'),
- path = require('path');
-
- var themesPath = path.join(__dirname, '..','settings','themes.json');
-
- function getThemes(cb){
- fs.readFile(themesPath, 'utf8', function (err, data) {
- if (err){
- if(err.code == 'ENOENT'){
- cb('');
- }
- }
- else{
- cb(data);
- }
- });
- }
-
- function writeThemeFile(data, res){
- var file = JSON.stringify(data, null, '\t');
- fs.writeFile(themesPath, file, function(err){
- if(err)
- res.status(500).send('There was a problem saving "themes.json".');
- else
- res.status(200).send('');
- });
- }
-
- function addTheme(body, res){
-
- function save(data){
- var themeFile = JSON.parse(data);
-
- // add the new addition to the JSON and remove the name and themeName attributes
- themeFile[body.name] = body;
- delete themeFile[body.name].currentName;
- delete themeFile[body.name].name;
-
- writeThemeFile(themeFile, res);
- }
-
- getThemes(save);
- }
-
- function updateTheme(body, res){
-
- function save(data){
- var themeFile = JSON.parse(data);
-
- // remove the old theme options
- delete themeFile[body.currentName];
-
- // add the new addition to the JSON and remove the name and themeName attributes
- themeFile[body.name] = body;
- delete themeFile[body.name].currentName;
- delete themeFile[body.name].name;
-
- writeThemeFile(themeFile, res);
- }
-
- getThemes(save);
- }
-
- function deleteTheme(body, res){
-
- function save(data){
- var themeFile = JSON.parse(data);
-
- // remove the deleted theme options
- delete themeFile[body.name];
-
- writeThemeFile(themeFile, res);
- }
-
- getThemes(save);
- }
-
- module.exports = function(req, res){
- switch (req.body.type) {
- case 'ADD':
- addTheme(req.body.data, res);
- break;
- case 'UPDATE':
- updateTheme(req.body.data, res);
- break;
- case 'DELETE':
- deleteTheme(req.body.data, res);
- }
- };
|