diff --git a/server/configs/default.js b/server/configs/default.js index 7b8de36..4486d31 100644 --- a/server/configs/default.js +++ b/server/configs/default.js @@ -7,5 +7,36 @@ module.exports = { bundleName: 'desktop', availableBundles: ['desktop'], languageId: 'ru' + }, + + defaults: function(req ){ + return { + user: req.user, + language: 'ru', + menu: [ + { + title: 'Главная', + url: '/' + }, + { + title: 'Все пользователи', + url: '/users' + }, + { + title: req.user ? 'Мои товары' : 'Все товары', + url: '/goods' + }, + !req.user ? + { + title: 'Авторизоваться', + url: '/auth' + } : + { + title: req.user.username, + url: '/account' + } + ], + route: req.route + } } }; \ No newline at end of file diff --git a/server/controllers/auth.js b/server/controllers/auth.js index 8d8f4f1..d5c4399 100644 --- a/server/controllers/auth.js +++ b/server/controllers/auth.js @@ -5,35 +5,5 @@ module.exports = { passportCallback: function(req, res) { res.redirect('/'); - }, - - login: function(req, res) { - var data = { - user: req.user, - language: 'ru' - }; - - res.render('index', data, function(err, html) { - if (err) { - res.send(500, err); - } else { - res.send(html); - } - }); - }, - - logout: function(req, res) { - var data = { - user: req.user, - language: 'ru' - }; - - res.render('index', data, function(err, html) { - if (err) { - res.send(500, err); - } else { - res.send(html); - } - }); } }; diff --git a/server/controllers/db.js b/server/controllers/db.js index 77740a9..a7d0c84 100644 --- a/server/controllers/db.js +++ b/server/controllers/db.js @@ -3,7 +3,7 @@ var db = require('../lib/db'); module.exports = { getGoods: function(req, res) { - var user_id = req.query.user.split('/')[0]; + var user_id = (req.query.user && req.query.user.split('/')[0]) || req.user.id; db.getUser(user_id, function(err, user) { var page = user.name; @@ -39,16 +39,13 @@ module.exports = { db.getUsers(function(err, cursor) { cursor.toArray(function(err, arr) { - var page = ''; - res.send(page); + res.end(result); }); }); } diff --git a/server/controllers/index.js b/server/controllers/index.js index 72ac245..d89b560 100644 --- a/server/controllers/index.js +++ b/server/controllers/index.js @@ -1,10 +1,12 @@ +var ensureAuthanticated = require('../middleware/express-checkAuth'), + configs = require('../configs/default'); + module.exports = { - index: function(req, res) { + index: function(req, res, next) { + var data = configs.defaults(req); - var data = { - user: req.user, - language: 'ru' - }; + data.isAccount = req.route.path === '/account'; + data.isAccount && ensureAuthanticated(req, res, next); res.render('index', data, function(err, html) { if (err) { diff --git a/server/controllers/user.js b/server/controllers/user.js deleted file mode 100644 index b1ce708..0000000 --- a/server/controllers/user.js +++ /dev/null @@ -1,20 +0,0 @@ -var ensureAuthanticated = require('../middleware/express-checkAuth'); - -module.exports = { - index: function(req, res) { - ensureAuthanticated(); - - var data = { - user: req.user, - language: 'ru' - }; - - res.render('index', data, function(err, html) { - if (err) { - res.send(500, err); - } else { - res.send(html); - } - }); - } -}; \ No newline at end of file diff --git a/server/lib/db.js b/server/lib/db.js index 411120b..a2edde2 100644 --- a/server/lib/db.js +++ b/server/lib/db.js @@ -1,7 +1,7 @@ var MongoClient = require('mongodb').MongoClient, vow = require('vow'), - db_goods, db_users, users; + db_users, db_goods; MongoClient.connect('mongodb://localhost:27017/goods-shelf', function(err, db) { if (err) { diff --git a/server/middleware/express-checkAuth.js b/server/middleware/express-checkAuth.js index 9244385..3f12bc4 100644 --- a/server/middleware/express-checkAuth.js +++ b/server/middleware/express-checkAuth.js @@ -1,6 +1,6 @@ -function ensureAuthenticated(req, res, next) { +function ensureAuthenticated(req, res) { if (req.isAuthenticated()) { - return next(); + return; } res.redirect('/login') diff --git a/server/router.js b/server/router.js index 6ec375d..402cddc 100644 --- a/server/router.js +++ b/server/router.js @@ -1,5 +1,4 @@ var index = require('./controllers/index'), - user = require('./controllers/user'), db = require('./controllers/db'), auth = require('./controllers/auth'), @@ -7,25 +6,24 @@ var index = require('./controllers/index'), module.exports = function (app) { app.get('/', index.index); + app.get('/account', index.index); + app.get('/goods', index.index); + app.get('/users', index.index); // авторизация - app.get('/login', auth.login); - app.get('/logout', auth.logout); - app.get('/account', user.index); - - // паспорт + app.get('/login', function(req, res) { res.redirect('/auth') }); app.get('/auth', passport.authenticate('yandex'), auth.passportAuth); app.get('/auth/yandex/callback', - passport.authenticate('yandex', { failureRedirect: '/login' }), + passport.authenticate('yandex', { failureRedirect: '/auth' }), auth.passportCallback); //some mongodb routes - app.get('/get/', db.getGoods); - app.get('/post/', db.postGood); - app.get('/user/add', db.addUser); - app.get('/user/', db.getUsers); + //app.get('/get/', db.getGoods); + //app.get('/post/', db.postGood); + //app.get('/user/add', db.addUser); + //app.get('/users/', db.getUsers); app.get('*', function (req, res) { res.send(404); diff --git a/static/desktop.blocks/b-link/_active/b-link_active.css b/static/desktop.blocks/b-link/_active/b-link_active.css new file mode 100644 index 0000000..2dde9da --- /dev/null +++ b/static/desktop.blocks/b-link/_active/b-link_active.css @@ -0,0 +1,5 @@ +.b-link.b-link_active_true +{ + color: #444; + border-bottom: 1px dotted #888; +} diff --git a/static/desktop.blocks/b-link/b-link.bh.js b/static/desktop.blocks/b-link/b-link.bh.js new file mode 100644 index 0000000..23e9298 --- /dev/null +++ b/static/desktop.blocks/b-link/b-link.bh.js @@ -0,0 +1,7 @@ +module.exports = function (bh) { + bh.match('b-link', function (ctx, json) { + ctx + .tag('a') + .attr('href', json.url); + }); +}; diff --git a/static/desktop.blocks/b-link/b-link.css b/static/desktop.blocks/b-link/b-link.css new file mode 100644 index 0000000..1bd039b --- /dev/null +++ b/static/desktop.blocks/b-link/b-link.css @@ -0,0 +1,12 @@ +.b-link +{ + text-decoration: none; + + color: #888; + border-bottom: 1px dotted #fff; +} + +.b-link:visited +{ + color: #aaa; +} diff --git a/static/desktop.blocks/b-link/b-link.deps.js b/static/desktop.blocks/b-link/b-link.deps.js new file mode 100644 index 0000000..46ca8d4 --- /dev/null +++ b/static/desktop.blocks/b-link/b-link.deps.js @@ -0,0 +1,11 @@ +({ + mustDeps: [ + ], + shouldDeps: [ + { + mods: { + active: 'yes' + } + } + ] +}); diff --git a/static/desktop.blocks/b-menu/__item/b-menu__item.css b/static/desktop.blocks/b-menu/__item/b-menu__item.css new file mode 100644 index 0000000..fd5ca0d --- /dev/null +++ b/static/desktop.blocks/b-menu/__item/b-menu__item.css @@ -0,0 +1,6 @@ +.b-menu__item +{ + display: inline-block; + + padding: 10px 20px; +} diff --git a/static/desktop.blocks/b-menu/b-menu.css b/static/desktop.blocks/b-menu/b-menu.css new file mode 100644 index 0000000..079ff49 --- /dev/null +++ b/static/desktop.blocks/b-menu/b-menu.css @@ -0,0 +1,6 @@ +.b-menu +{ + display: block; + + color: #aaa; +} diff --git a/static/desktop.blocks/b-menu/b-menu.deps.js b/static/desktop.blocks/b-menu/b-menu.deps.js new file mode 100755 index 0000000..4eb6499 --- /dev/null +++ b/static/desktop.blocks/b-menu/b-menu.deps.js @@ -0,0 +1,11 @@ +({ + shouldDeps: [ + { + block: 'b-link' + }, + { + elems: [ 'item' ] + } + ] +}); + diff --git a/static/desktop.blocks/b-menu/b-menu.priv.js b/static/desktop.blocks/b-menu/b-menu.priv.js new file mode 100644 index 0000000..405262c --- /dev/null +++ b/static/desktop.blocks/b-menu/b-menu.priv.js @@ -0,0 +1,22 @@ +module.exports = function (blocks) { + blocks.declare('b-menu', function (data, page) { + return [ + { + block: 'b-menu', + content: (function(){ + return data.menu.map(function (item) { + return { + elem: 'item', + content: { + block: 'b-link', + mods: data.route.path === item.url ? { 'active': 'true' } : {}, + content: item.title, + url: item.url + } + } + }); + })() + } + ]; + }); +}; \ No newline at end of file diff --git a/static/desktop.blocks/b-page/b-page.css b/static/desktop.blocks/b-page/b-page.css new file mode 100644 index 0000000..b45e63e --- /dev/null +++ b/static/desktop.blocks/b-page/b-page.css @@ -0,0 +1,14 @@ +html +{ + font-family: Arial, Helvetica, sans-serif; + + background: #fff; +} + +.b-page +{ + margin: 20px; + padding: 20px; + + background: #f1f1f1; +} diff --git a/static/desktop.blocks/b-page/b-page.deps.js b/static/desktop.blocks/b-page/b-page.deps.js index e9e02bb..0bd6434 100755 --- a/static/desktop.blocks/b-page/b-page.deps.js +++ b/static/desktop.blocks/b-page/b-page.deps.js @@ -11,6 +11,8 @@ } ], shouldDeps: [ + { block: 'b-menu' }, + { block: 'pre' } ] }); diff --git a/static/desktop.blocks/b-page/b-page.priv.js b/static/desktop.blocks/b-page/b-page.priv.js index 7a378f3..b000667 100644 --- a/static/desktop.blocks/b-page/b-page.priv.js +++ b/static/desktop.blocks/b-page/b-page.priv.js @@ -21,7 +21,8 @@ module.exports = function (blocks) { 'x-ua-compatible': 'IE=EmulateIE7, IE=edge', content: [ - data.user.username + blocks.exec('b-menu', data), + blocks.exec('pre', data) ] } ]; diff --git a/static/desktop.blocks/pre/pre.priv.js b/static/desktop.blocks/pre/pre.priv.js new file mode 100644 index 0000000..19dc967 --- /dev/null +++ b/static/desktop.blocks/pre/pre.priv.js @@ -0,0 +1,14 @@ +module.exports = function (blocks) { + blocks.declare('pre', function (data, page) { + return [ + { + tag: 'pre', + content: [ + data.isAccount && JSON.stringify(data.user, null, 2), + data.isGoods && JSON.stringify(data.goods, null, 2), + data.isUsers && JSON.stringify(data.users, null, 2) + ] + } + ]; + }); +}; \ No newline at end of file diff --git a/static/desktop.bundles/goods/goods.bemdecl.js b/static/desktop.bundles/goods/goods.bemdecl.js new file mode 100644 index 0000000..042cc77 --- /dev/null +++ b/static/desktop.bundles/goods/goods.bemdecl.js @@ -0,0 +1,5 @@ +exports.deps = [ + { + "block": "b-page" + } +]; \ No newline at end of file