From 09cd83352ffd486e4f2e8138cdea0c4875f4ba7e Mon Sep 17 00:00:00 2001 From: Mark Moffat Date: Sun, 11 Feb 2018 20:47:26 +0100 Subject: [PATCH] Fixed theme and partials structure --- app.js | 18 ++++-- lib/common.js | 8 ++- public/themes/Cloth/page.hbs | 64 ------------------- routes/admin.js | 4 +- routes/index.js | 1 + views/admin.hbs | 2 +- views/customer.hbs | 2 +- views/customers.hbs | 2 +- views/layouts/layout.hbs | 4 +- views/order.hbs | 14 ++-- views/orders.hbs | 2 +- views/product_edit.hbs | 2 +- views/product_new.hbs | 4 +- views/products.hbs | 2 +- views/settings.hbs | 3 +- views/settings_menu.hbs | 2 +- views/settings_page_edit.hbs | 6 +- views/settings_pages.hbs | 10 +-- {public => views}/themes/Cloth/checkout.hbs | 2 +- views/themes/Cloth/front-menu.hbs | 25 ++++++++ {public => views}/themes/Cloth/index.hbs | 23 +------ views/themes/Cloth/page.hbs | 8 +++ {public => views}/themes/Cloth/pay.hbs | 8 +-- .../themes/Cloth/payment_complete.hbs | 0 {public => views}/themes/Cloth/product.hbs | 31 ++------- {public => views}/themes/Cloth/style.css | 0 views/user_edit.hbs | 2 +- views/user_new.hbs | 2 +- views/users.hbs | 6 +- 29 files changed, 100 insertions(+), 157 deletions(-) delete mode 100644 public/themes/Cloth/page.hbs rename {public => views}/themes/Cloth/checkout.hbs (70%) create mode 100644 views/themes/Cloth/front-menu.hbs rename {public => views}/themes/Cloth/index.hbs (73%) create mode 100644 views/themes/Cloth/page.hbs rename {public => views}/themes/Cloth/pay.hbs (93%) rename {public => views}/themes/Cloth/payment_complete.hbs (100%) rename {public => views}/themes/Cloth/product.hbs (74%) rename {public => views}/themes/Cloth/style.css (100%) diff --git a/app.js b/app.js index 6f9a47b..1ea9c7a 100644 --- a/app.js +++ b/app.js @@ -12,7 +12,6 @@ const helmet = require('helmet'); const colors = require('colors'); const common = require('./lib/common'); const mongodbUri = require('mongodb-uri'); - let handlebars = require('express-handlebars'); // Validate our settings schema @@ -66,14 +65,16 @@ const app = express(); // view engine setup app.set('views', path.join(__dirname, '/views')); -app.engine('hbs', handlebars({extname: 'hbs', layoutsDir: path.join(__dirname, 'views', 'layouts'), defaultLayout: 'layout.hbs'})); +app.engine('hbs', handlebars({ + extname: 'hbs', + layoutsDir: path.join(__dirname, 'views', 'layouts'), + defaultLayout: 'layout.hbs', + partialsDir: [ path.join(__dirname, 'views') ] +})); app.set('view engine', 'hbs'); // helpers for the handlebar templating platform handlebars = handlebars.create({ - partialsDir: [ - 'views/partials/' - ], helpers: { perRowClass: function(numProducts){ if(parseInt(numProducts) === 1){ @@ -159,6 +160,12 @@ handlebars = handlebars.create({ } return options.inverse(this); }, + toLower: function (value){ + if(value){ + return value.toLowerCase(); + } + return null; + }, formatDate: function (date, format){ return moment(date).format(format); }, @@ -222,6 +229,7 @@ app.use(session({ // serving static content app.use(express.static(path.join(__dirname, 'public'))); +app.use(express.static(path.join(__dirname, 'views', 'themes'))); // Make stuff accessible to our router app.use((req, res, next) => { diff --git a/lib/common.js b/lib/common.js index 42cf8c5..69bcc6d 100644 --- a/lib/common.js +++ b/lib/common.js @@ -148,7 +148,7 @@ exports.checkDirectorySync = (directory) => { }; exports.getThemes = () => { - return fs.readdirSync(path.join('public', 'themes')).filter(file => fs.statSync(path.join(path.join('public', 'themes'), file)).isDirectory()); + return fs.readdirSync(path.join(__dirname, '../', 'views', 'themes')).filter(file => fs.statSync(path.join(path.join(__dirname, '../', 'views', 'themes'), file)).isDirectory()); }; exports.getImages = (dir, req, res, callback) => { @@ -206,7 +206,7 @@ exports.getConfig = () => { config.theme = 'Cloth'; // Default to Cloth theme } - config.themeViews = '../public/themes/' + config.theme + '/'; + config.themeViews = '../views/themes/' + config.theme + '/'; // if db set to mongodb override connection with MONGODB_CONNECTION_STRING env var config.databaseConnectionString = process.env.MONGODB_CONNECTION_STRING || config.databaseConnectionString; @@ -458,6 +458,10 @@ exports.getData = (req, page, query) => { skip = (page - 1) * numberProducts; } + if(!query){ + query = {}; + } + query['productPublished'] = 'true'; // Run our queries diff --git a/public/themes/Cloth/page.hbs b/public/themes/Cloth/page.hbs deleted file mode 100644 index 4aa7412..0000000 --- a/public/themes/Cloth/page.hbs +++ /dev/null @@ -1,64 +0,0 @@ -
-
-
-
- {{#ifCond config.menuEnabled '==' 'true'}} - {{#ifCond config.menuLocation '==' "side"}} - -
- {{#ifCond config.menuEnabled '==' 'true'}} - {{#ifCond config.menuLocation '==' "side"}} -
- -
-
- {{else}} -
-
- -
-
-
- {{/ifCond}} - {{else}} -
- {{/ifCond}} - {{#if filtered}} -
- Showing results for: {{searchTerm}} -
- {{/if}} -
- {{{page.pageContent}}} -
-
-
-
\ No newline at end of file diff --git a/routes/admin.js b/routes/admin.js index 4b7c8a1..ea13281 100644 --- a/routes/admin.js +++ b/routes/admin.js @@ -188,7 +188,7 @@ router.post('/admin/settings/option/remove', common.restrict, common.checkAccess if(err){ console.info(err.stack); } - if(product.productOptions){ + if(product && product.productOptions){ let optJson = JSON.parse(product.productOptions); delete optJson[req.body.optName]; @@ -203,7 +203,7 @@ router.post('/admin/settings/option/remove', common.restrict, common.checkAccess } }); }else{ - res.status(400).json({message: 'Product not found.'}); + res.status(400).json({message: 'Product not found. Try saving before removing.'}); } }); }); diff --git a/routes/index.js b/routes/index.js index 93a1e32..c38d41c 100644 --- a/routes/index.js +++ b/routes/index.js @@ -490,6 +490,7 @@ router.get('/:page?', (req, res, next) => { res.render(`${config.themeViews}page`, { title: page.pageName, page: page, + searchTerm: req.params.page, session: req.session, message: common.clearSessionValue(req.session, 'message'), messageType: common.clearSessionValue(req.session, 'messageType'), diff --git a/views/admin.hbs b/views/admin.hbs index f6453c0..faff45a 100644 --- a/views/admin.hbs +++ b/views/admin.hbs @@ -1,4 +1,4 @@ -{{> menu}} +{{> partials/menu}}