Permalink
Browse files

archive function implemented

  • Loading branch information...
1 parent 8a12e83 commit fca099548f458f303b6c1ea23028b1408f1b0371 @yssk22 committed May 7, 2011
@@ -1,6 +1,8 @@
{
- "posts.mainTitle": "Latest Posts",
+ "posts.top.title": "Latest Posts",
"posts.sidebar.menu.title": "Menu",
+ "posts.sidebar.archives.title": "Archives",
+ "posts.archives.title": "%d/%02d",
"posts.admin.managePosts" : "Manage posts",
"posts.admin.createPost" : "New post",
@@ -0,0 +1,20 @@
+<% title(sprintf(_("posts.archives.title"), request.params.year, request.params.month)) %>
+<div class="posts">
+ <h2><%= sprintf(_("posts.archives.title"), request.params.year, request.params.month) %></h2>
+ <div class="main">
+ <% if( posts.data.length > 0 ){ %>
+ <div class="list">
+ <%- partial('parts/posts', {object: posts.data, as: this}) %>
+ </div>
+ <script type="text/javascript">
+ $(function(){ $('div.posts. div.main div.list').bindList(); });
+ </script>
+ <% }else{ %>
+ <p><%= _('posts.no_posts_found') %></p>
+ <% } %>
+ </div>
+ <aside class="sidebar">
+ <%- partial('parts/menu') %>
+ <%- partial('parts/archives', {object: archives.data, as: this}) %>
+ </aside>
+</div>
@@ -1,6 +1,6 @@
-<% title(_("posts.mainTitle")) %>
+<% title(_("posts.top.title")) %>
<div class="posts">
- <h2><%= _("posts.mainTitle") %></h2>
+ <h2><%= _("posts.top.title") %></h2>
<div class="main">
<% if( posts.data.length > 0 ){ %>
<div class="list">
@@ -18,5 +18,7 @@
</div>
<aside class="sidebar">
<%- partial('parts/menu') %>
+ <%- partial('parts/archives', {object: archives.data, as: this}) %>
+
</aside>
</div>
@@ -0,0 +1,17 @@
+<div class="menu">
+ <h3><%= _("posts.sidebar.archives.title") %></h3>
+ <% if( this.length > 0 ){ %>
+ <ul>
+ <% for(var i in this){ %>
+ <% var k = this[i].key,
+ v = this[i].value,
+ t = sprintf(_("posts.archives.title"), k[0], k[1]) %>
+ <li><a href="<%= url(sprintf('/a/%d/%02d/', k[0], k[1])) %>"
+ class="archive-link"
+ title="<%= t %>"><%= t %> (<%= v %>)</a></li>
+ <% }; %>
+ </ul>
+ <% }else{ %>
+ <% } %>
+</div>
+
View
@@ -25,47 +25,7 @@ var ddoc = {
module.exports = ddoc;
ddoc.middleware = require('./middleware');
-
-ddoc.views.all_by_updated_at = {
- map: function(doc){
- if( doc.type == 'post' ){
- emit(doc.updated_at, doc);
- }
- }
-};
-
-ddoc.views.by_updated_at = {
- map: function(doc){
- if( doc.type == 'post' && doc.is_draft != true ){
- emit(doc.updated_at, doc);
- }
- }
-}
-
-ddoc.views.all_by_tag = {
- map: function(doc){
- if( doc.type == 'post' ){
- if( doc.tags && typeof(doc.tags) === 'object'){
- for(var i in doc.tags){
- emit([doc.tags[i], doc.updated_at], doc);
- }
- }
- }
- }
-}
-
-ddoc.views.by_tag = {
- map: function(){
- if( doc.type == 'post' && doc.is_draft != true ){
- if( doc.tags && typeof(doc.tags) === 'object'){
- for(var i in doc.tags){
- emit([doc.tags[i], doc.updated_at], doc);
- }
- }
- }
- }
-};
-
+ddoc.views = require('./views');
ddoc.init = function(app, config){
config = merge({
@@ -130,10 +90,31 @@ ddoc.init = function(app, config){
// Public URIs
app.get('/',
parallel(
- m.byUpdatedAt({perPage: config.postsPerPage})
+ m.byUpdatedAt({perPage: config.postsPerPage}),
+ m.countByDate()
),
m.feedOrHtml('index.ejs'));
+ app.get('/a/:year/:month/',
+ function(req, res, next){
+ try{
+ var y = parseInt(req.params.year);
+ var m = parseInt(req.params.month[0] == '0' ? req.params.month[1] : req.params.month);
+ }catch(e){
+ res.redirect('/');
+ }
+ req.query.startkey = (new Date(y, m - 1, 1)).toJSON();
+ req.query.endkey = (new Date(y, m , 1)).toJSON();
+ req.query.descending = false;
+ next();
+ },
+ parallel(
+ m.byUpdatedAt({perPage: 'unlimited'}),
+ m.countByDate()
+ ),
+ m.feedOrHtml('by_month.ejs'));
+
+
app.get('/p/:id', // get an entry
m.byId('id'),
function(req, res, next){
View
@@ -2,7 +2,7 @@ var merge = require('sunrise').utils.merge;
var allowed_names = [
'startkey', 'startkey_docid', 'endkey', 'endkey_docid',
- 'limit', 'decending', 'skip'
+ 'limit', 'descending', 'skip'
];
@@ -26,7 +26,6 @@ module.exports = {
};
},
-
byUpdatedAt: function(options){
options = merge({
perPage: 10,
@@ -41,6 +40,10 @@ module.exports = {
limit:options.perPage,
descending: true
};
+ if( options.perPage === 'unlimited' ){
+ delete(params.limit);
+ }
+
allowed_names.forEach(function(name){
if( req.query[name] !== undefined ){
params[name] = req.query[name];
@@ -63,5 +66,23 @@ module.exports = {
return function(req, res, next){
next();
};
+ },
+
+ countByDate: function(options){
+ options = merge({
+ group: 'month',
+ bindAs: 'archives'
+ }, options);
+ var db = this.db;
+ var GROUP_LEVEL = {
+ year: 1, month: 2, day: 3
+ };
+ var params = {
+ group: true,
+ group_level: GROUP_LEVEL[options.group] || GROUP_LEVEL['month']
+ };
+ return db.bind('view', 'posts/count_by_date', params, {
+ as: options.bindAs
+ });
}
};
View
@@ -0,0 +1,59 @@
+module.exports = {
+ all_by_updated_at : {
+ map: function(doc){
+ if( doc.type == 'post' ){
+ emit(doc.updated_at, doc);
+ }
+ }
+ },
+
+ all_by_tag : {
+ map: function(doc){
+ if( doc.type == 'post' ){
+ if( doc.tags && typeof(doc.tags) === 'object'){
+ for(var i in doc.tags){
+ emit([doc.tags[i], doc.updated_at], doc);
+ }
+ }
+ }
+ }
+ },
+
+ by_updated_at : {
+ map: function(doc){
+ if( doc.type == 'post' && doc.is_draft != true ){
+ emit(doc.updated_at, doc);
+ }
+ }
+ },
+
+ by_tag : {
+ map: function(){
+ if( doc.type == 'post' && doc.is_draft != true ){
+ if( doc.tags && typeof(doc.tags) === 'object'){
+ for(var i in doc.tags){
+ emit([doc.tags[i], doc.updated_at], doc);
+ }
+ }
+ }
+ }
+ },
+
+ count_by_date: {
+ map: function(doc){
+ if( doc.type == 'post' ){
+ var yyyymmdd = doc.created_at.split('T')[0].split('-').map(function(i){
+ if( i[0] == '0' ){ // avoid parseInt('0N') for failure.
+ return parseInt(i[1]);
+ }else{
+ return parseInt(i);
+ }
+ });
+ emit(yyyymmdd, 1);
+ }
+ },
+ reduce: function(keys, values, rereduce){
+ return sum(values);
+ }
+ }
+};

0 comments on commit fca0995

Please sign in to comment.