Skip to content
This repository has been archived by the owner on Jan 1, 2025. It is now read-only.

Commit

Permalink
Make some view
Browse files Browse the repository at this point in the history
  • Loading branch information
Anton Konev committed Jun 16, 2015
1 parent 06041bf commit e93c684
Show file tree
Hide file tree
Showing 21 changed files with 171 additions and 77 deletions.
31 changes: 31 additions & 0 deletions server/configs/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
};
30 changes: 0 additions & 30 deletions server/controllers/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});
}
};
11 changes: 4 additions & 7 deletions server/controllers/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -39,16 +39,13 @@ module.exports = {
db.getUsers(function(err, cursor)
{
cursor.toArray(function(err, arr) {
var page = '<ul>';
var result = '';

arr.forEach(function(user) {
console.log(user);

page += '<li>' + (user.profile && user.profile.username) + '</li><br>';
result += JSON.stringify(user, null, 2);
});
page += '</ul>';

res.send(page);
res.end(result);
});
});
}
Expand Down
12 changes: 7 additions & 5 deletions server/controllers/index.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
20 changes: 0 additions & 20 deletions server/controllers/user.js

This file was deleted.

2 changes: 1 addition & 1 deletion server/lib/db.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions server/middleware/express-checkAuth.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
function ensureAuthenticated(req, res, next) {
function ensureAuthenticated(req, res) {
if (req.isAuthenticated()) {
return next();
return;
}

res.redirect('/login')
Expand Down
20 changes: 9 additions & 11 deletions server/router.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,29 @@
var index = require('./controllers/index'),
user = require('./controllers/user'),

db = require('./controllers/db'),
auth = require('./controllers/auth'),
passport = require('./lib/passport');

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);
Expand Down
5 changes: 5 additions & 0 deletions static/desktop.blocks/b-link/_active/b-link_active.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.b-link.b-link_active_true
{
color: #444;
border-bottom: 1px dotted #888;
}
7 changes: 7 additions & 0 deletions static/desktop.blocks/b-link/b-link.bh.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = function (bh) {
bh.match('b-link', function (ctx, json) {
ctx
.tag('a')
.attr('href', json.url);
});
};
12 changes: 12 additions & 0 deletions static/desktop.blocks/b-link/b-link.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.b-link
{
text-decoration: none;

color: #888;
border-bottom: 1px dotted #fff;
}

.b-link:visited
{
color: #aaa;
}
11 changes: 11 additions & 0 deletions static/desktop.blocks/b-link/b-link.deps.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
({
mustDeps: [
],
shouldDeps: [
{
mods: {
active: 'yes'
}
}
]
});
6 changes: 6 additions & 0 deletions static/desktop.blocks/b-menu/__item/b-menu__item.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.b-menu__item
{
display: inline-block;

padding: 10px 20px;
}
6 changes: 6 additions & 0 deletions static/desktop.blocks/b-menu/b-menu.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.b-menu
{
display: block;

color: #aaa;
}
11 changes: 11 additions & 0 deletions static/desktop.blocks/b-menu/b-menu.deps.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
({
shouldDeps: [
{
block: 'b-link'
},
{
elems: [ 'item' ]
}
]
});

22 changes: 22 additions & 0 deletions static/desktop.blocks/b-menu/b-menu.priv.js
Original file line number Diff line number Diff line change
@@ -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
}
}
});
})()
}
];
});
};
14 changes: 14 additions & 0 deletions static/desktop.blocks/b-page/b-page.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
html
{
font-family: Arial, Helvetica, sans-serif;

background: #fff;
}

.b-page
{
margin: 20px;
padding: 20px;

background: #f1f1f1;
}
2 changes: 2 additions & 0 deletions static/desktop.blocks/b-page/b-page.deps.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
}
],
shouldDeps: [
{ block: 'b-menu' },
{ block: 'pre' }
]
});

3 changes: 2 additions & 1 deletion static/desktop.blocks/b-page/b-page.priv.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
]
}
];
Expand Down
14 changes: 14 additions & 0 deletions static/desktop.blocks/pre/pre.priv.js
Original file line number Diff line number Diff line change
@@ -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)
]
}
];
});
};
5 changes: 5 additions & 0 deletions static/desktop.bundles/goods/goods.bemdecl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
exports.deps = [
{
"block": "b-page"
}
];

0 comments on commit e93c684

Please sign in to comment.