CSOL-site/controllers/learn.js

129 строки
3.0 KiB
JavaScript
Исходник Обычный вид История

var querystring = require('querystring');
module.exports = function(app) {
app.param('section', function(req, res, next, section) {
if (section == 'programs') {
req.params.section = {
title: 'Programs',
set: 'program'
};
} else if (section == 'orgs') {
req.params.section = {
title: 'Organizations',
set: 'organization'
};
} else if (section == 'activities') {
req.params.section = {
title: 'Activities',
set: 'activity'
};
} else {
return next(new Error('Learn section "' + section + '" not found'));
}
req.params.section.path = section;
next();
});
app.param('item_id', function(req, res, next, id) {
var section = req.params.section;
// TODO - hook this into a model somewhere
req.params.item = {};
next();
});
2013-06-02 02:44:36 +04:00
app.get('/explore/:section?/:item_id?', function(req, res) {
var section = req.params.section;
var item = req.params.item;
var items = [];
var itemOptions = [{
thumbnail: '/media/images/org.png',
description: 'Organization blah irure...',
2013-06-02 02:44:36 +04:00
url: '/explore/orgs/some-organization'
},{
thumbnail: '/media/images/program.png',
description: 'Program blah sed eiusmod...',
2013-06-02 02:44:36 +04:00
url: '/explore/programs/some-program'
},{
2013-04-22 16:13:31 +04:00
thumbnail: '/media/images/activity.png',
description: 'Activity id est laborum...',
2013-06-02 02:44:36 +04:00
url: '/explore/activities/some-activity'
}];
if (item)
2013-04-22 16:13:31 +04:00
return res.render('learn/' + section.set + '/single.html', {
item: item
});
if ('category' in req.query) {
var category = req.query['category'];
delete req.query['category'];
var query = querystring.stringify(req.query);
2013-06-02 02:44:36 +04:00
return res.redirect('/explore/' + category + '?' + query);
}
for (var i = 0; i < 12; ++i) {
items.push(itemOptions[Math.floor(Math.random()*itemOptions.length)]);
}
var filters = [{
name: 'category',
label: 'Category',
selected: (section || {}).path,
options: {
activities: 'Activities',
orgs: 'Organizations',
programs: 'Programs'
}
},{
name: 'age',
label: 'Age',
options: {}
},{
name: 'topic',
label: 'Topic',
options: {}
},{
name: 'date',
label: 'Date',
options: {}
},{
name: 'search',
label: 'Search',
value: '',
type: 'search'
}];
if (section)
return res.render('learn/' + section.set + '/list.html', {
filters: filters,
pageTitle: section.title,
items: items
});
res.render('learn/list.html', {
filters: filters,
items: items
});
});
2013-06-02 02:44:36 +04:00
app.get('/explore/:section/:item_id/:action', function(req, res, next) {
var item = req.params.item;
var action = req.params.action;
if (action === 'favorite')
return res.send('Favoriting ' + item);
if (action === 'unfavorite')
return res.send('Unfavoriting ' + item);
next();
});
}