diff --git a/app.js b/app.js index a79fec1f0..6fcb6549d 100644 --- a/app.js +++ b/app.js @@ -178,6 +178,10 @@ function dbCallback(err, db) { function(next) { require('./src/settings/defaults').init(next); }, + function(next) { + winston.debug('Building dynamic sass...'); + require('./src/sass/buildsass').build(next); + }, function(next) { //Start Task Runners require('./src/taskrunner'); diff --git a/src/controllers/api/v1/settings.js b/src/controllers/api/v1/settings.js index 559b48399..d0ea49646 100644 --- a/src/controllers/api/v1/settings.js +++ b/src/controllers/api/v1/settings.js @@ -99,5 +99,14 @@ apiSettings.testMailer = function(req, res) { }); }; +apiSettings.buildsass = function(req, res) { + var buildsass = require('../../../sass/buildsass'); + buildsass.build(function(err) { + if (err) return res.status(400).json({success: false, error: err}); + + return res.json({success: true}); + }); +}; + module.exports = apiSettings; \ No newline at end of file diff --git a/src/helpers/viewdata/index.js b/src/helpers/viewdata/index.js index d14db12b1..5b07cc18a 100644 --- a/src/helpers/viewdata/index.js +++ b/src/helpers/viewdata/index.js @@ -17,79 +17,15 @@ var async = require('async'), winston = require('winston'), moment = require('moment'), permissions = require('../../permissions'), - settingSchema = require('../../models/setting'), - settingUtil = require('../../settings/settingsUtil'), - - sass = require('node-sass'); - + settingSchema = require('../../models/setting'); var viewController = {}; var viewdata = {}; viewdata.notifications = {}; viewdata.users = {}; -var nconf = require('nconf'); -var sassOptionsDefaults = { - indentedSyntax: true, - includePaths: [ - nconf.get('base_dir') + '/src/sass' - ], - outputStyle: 'compressed' -}; - -function sassVariable(name, value) { - return '$' + name + ': ' + value + '\n'; -} - -function sassVariables(variablesObj) { - return Object.keys(variablesObj).map(function (name) { - return sassVariable(name, variablesObj[name]); - }).join('\n'); -} - -function sassImport(path) { - return '@import \'' + path + '\'\n'; -} - -function dynamicSass(entry, vars, success, error) { - var dataString = - sassVariables(vars) + - sassImport(entry); - var sassOptions = _.assign({}, sassOptionsDefaults, { - data: dataString - }); - - sass.render(sassOptions, function (err, result) { - return (err) - ? error(err) - : success(result.css.toString()); - }); -} - viewController.getData = function(request, cb) { async.parallel([ - function(callback) { - settingUtil.getSettings(function(err, s) { - if (!err && s) { - var settings = s.data.settings; - - dynamicSass('app.sass', { - header_background: settings.colorHeaderBG.value, - header_primary: settings.colorHeaderPrimary.value, - primary: settings.colorPrimary.value, - secondary: settings.colorSecondary.value, - tertiary: settings.colorTertiary.value, - quaternary: settings.colorQuaternary.value - }, function(result) { - var fs = require('fs'); - var themeCss = nconf.get('base_dir') + '/public/css/app.min.css'; - fs.writeFileSync(themeCss, result); - return callback(); - }, callback); - } else - return callback(); - }); - }, function(callback) { settingSchema.getSetting('gen:sitetitle', function(err, setting) { if (!err && setting && setting.value) diff --git a/src/public/js/angularjs/controllers/settings.js b/src/public/js/angularjs/controllers/settings.js index 8f66f84d0..5ae1da151 100644 --- a/src/public/js/angularjs/controllers/settings.js +++ b/src/public/js/angularjs/controllers/settings.js @@ -1616,9 +1616,14 @@ define([ 'Content-Type': 'application/json' } }).then(function successCallback() { - helpers.UI.showSnackbar('Color Scheme Saved. Reloading...', false); // Call rebuild of app.min.css - $timeout(function() { $window.location.reload(); }, 1000); + $http.get('/api/v1/settings/buildsass') + .then(function successCallback() { + helpers.UI.showSnackbar('Color Scheme Saved. Reloading...', false); + $timeout(function() { $window.location.reload(); }, 1000); + }, function errorCallback(err) { + helpers.UI.showSnackbar(err, true); + }); }, function errorCallback(err) { helpers.UI.showSnackbar(err, true); }); diff --git a/src/routes/index.js b/src/routes/index.js index 9269291e5..f51f26142 100644 --- a/src/routes/index.js +++ b/src/routes/index.js @@ -231,6 +231,7 @@ function mainRoutes(router, middleware, controllers) { router.put('/api/v1/settings', middleware.api, controllers.api.settings.updateSetting); router.post('/api/v1/settings/testmailer', middleware.api, controllers.api.settings.testMailer); + router.get('/api/v1/settings/buildsass', middleware.api, controllers.api.settings.buildsass); router.get('/api/v1/plugins/list/installed', middleware.api, function(req, res) { return res.json({success: true, loadedPlugins: global.plugins}); }); router.get('/api/v1/plugins/install/:packageid', middleware.api, middleware.isAdmin, controllers.api.plugins.installPlugin); diff --git a/src/sass/buildsass.js b/src/sass/buildsass.js new file mode 100644 index 000000000..58a69f181 --- /dev/null +++ b/src/sass/buildsass.js @@ -0,0 +1,82 @@ +/* + . .o8 oooo + .o8 "888 `888 + .o888oo oooo d8b oooo oooo .oooo888 .ooooo. .oooo.o 888 oooo + 888 `888""8P `888 `888 d88' `888 d88' `88b d88( "8 888 .8P' + 888 888 888 888 888 888 888ooo888 `"Y88b. 888888. + 888 . 888 888 888 888 888 888 .o o. )88b 888 `88b. + "888" d888b `V88V"V8P' `Y8bod88P" `Y8bod8P' 8""888P' o888o o888o + ======================================================================== + Created: 11/7/2018 + Author: Chris Brame + + */ + +var _ = require('lodash'); +var sass = require('node-sass'); +var nconf = require('nconf'); +var settingUtil = require('../settings/settingsUtil'); + +var buildsass = {}; + +var sassOptionsDefaults = { + indentedSyntax: true, + includePaths: [ + nconf.get('base_dir') + '/src/sass' + ], + outputStyle: 'compressed' +}; + +function sassVariable(name, value) { + return '$' + name + ': ' + value + '\n'; +} + +function sassVariables(variablesObj) { + return Object.keys(variablesObj).map(function (name) { + return sassVariable(name, variablesObj[name]); + }).join('\n'); +} + +function sassImport(path) { + return '@import \'' + path + '\'\n'; +} + +function dynamicSass(entry, vars, success, error) { + var dataString = + sassVariables(vars) + + sassImport(entry); + var sassOptions = _.assign({}, sassOptionsDefaults, { + data: dataString + }); + + sass.render(sassOptions, function (err, result) { + return (err) + ? error(err) + : success(result.css.toString()); + }); +} + +buildsass.build = function(callback) { + settingUtil.getSettings(function(err, s) { + if (!err && s) { + var settings = s.data.settings; + + dynamicSass('app.sass', { + header_background: settings.colorHeaderBG.value, + header_primary: settings.colorHeaderPrimary.value, + primary: settings.colorPrimary.value, + secondary: settings.colorSecondary.value, + tertiary: settings.colorTertiary.value, + quaternary: settings.colorQuaternary.value + }, function(result) { + var fs = require('fs'); + var themeCss = nconf.get('base_dir') + '/public/css/app.min.css'; + fs.writeFileSync(themeCss, result); + return callback(); + }, callback); + } else + return callback(); + }); +}; + +module.exports = buildsass; \ No newline at end of file