Skip to content
This repository has been archived by the owner on Sep 28, 2018. It is now read-only.

Commit

Permalink
tag filter function done.
Browse files Browse the repository at this point in the history
  • Loading branch information
yssk22 committed May 7, 2011
1 parent b845a4d commit 95dcac2
Show file tree
Hide file tree
Showing 15 changed files with 197 additions and 62 deletions.
1 change: 0 additions & 1 deletion apps/posts/_attachments/js/posts.js
Expand Up @@ -16,7 +16,6 @@
startkey_docid: last.attr('data-docid'),
skip: 1
};

$.get(path, param, function(data){
link.before(data);
var newlast = link.prev();
Expand Down
1 change: 1 addition & 0 deletions apps/posts/_attachments/messages/en.json
Expand Up @@ -2,6 +2,7 @@
"posts.top.title": "Latest Posts",
"posts.sidebar.menu.title": "Menu",
"posts.sidebar.archives.title": "Archives",
"posts.sidebar.tags.title": "Tags",
"posts.archives.title": "%d/%02d",

"posts.admin.managePosts" : "Manage posts",
Expand Down
2 changes: 1 addition & 1 deletion apps/posts/_attachments/templates/admin/edit.ejs
Expand Up @@ -5,6 +5,6 @@
<%- partial('parts/post_editor', {object: post.data, as: this}) %>
</div>
<aside class="sidebar">
<%- partial('parts/menu') %>
<%- partial('parts/sidebar') %>
</aside>
</div>
2 changes: 1 addition & 1 deletion apps/posts/_attachments/templates/admin/index.ejs
Expand Up @@ -17,6 +17,6 @@
<% } %>
</div>
<aside class="sidebar">
<%- partial('parts/menu') %>
<%- partial('parts/sidebar') %>
</aside>
</div>
6 changes: 3 additions & 3 deletions apps/posts/_attachments/templates/admin/new.ejs
@@ -1,10 +1,10 @@
<% title( _("posts.admin.createPost")) %>
<div class="posts 2columns">
<div class="posts">
<h2><%= _("posts.admin.createPost") %></h2>
<div class="main">
<h3><%= _("posts.admin.createPost") %></h3>
<%- partial('parts/post_editor', {object: post, as: this}) %>
</div>
<aside class="sidebar">
<%- partial('parts/menu') %>
<%- partial('parts/sidebar') %>
</aside>
</div>
3 changes: 1 addition & 2 deletions apps/posts/_attachments/templates/by_month.ejs
Expand Up @@ -14,7 +14,6 @@
<% } %>
</div>
<aside class="sidebar">
<%- partial('parts/menu') %>
<%- partial('parts/archives', {object: archives.data, as: this}) %>
<%- partial('parts/sidebar') %>
</aside>
</div>
22 changes: 22 additions & 0 deletions apps/posts/_attachments/templates/by_tag.ejs
@@ -0,0 +1,22 @@
<% title(request.params.tag) %>
<div class="posts">
<h2><%= request.params.tag %></h2>
<div class="main">
<% if( posts.data.length > 0 ){ %>
<div class="list">
<%- partial('parts/posts', {object: posts.data, as: this}) %>
<div class="readmore" data-apipath="<%= url('/-/t/' + encodeURIComponent(request.params.tag) + '/') %>">
<%= _('posts.read_more') %>
</div>
</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/sidebar') %>
</aside>
</div>
4 changes: 1 addition & 3 deletions apps/posts/_attachments/templates/index.ejs
Expand Up @@ -17,8 +17,6 @@
<% } %>
</div>
<aside class="sidebar">
<%- partial('parts/menu') %>
<%- partial('parts/archives', {object: archives.data, as: this}) %>

<%- partial('parts/sidebar') %>
</aside>
</div>
9 changes: 6 additions & 3 deletions apps/posts/_attachments/templates/parts/post.ejs
Expand Up @@ -7,6 +7,11 @@
<header>
<h3><%= this.title %></h3>
</header>
<ul class="tags">
<% (this.tags || []).filter(function(t){return (t && t != ''); }).forEach(function(t){ %>
<li><a class="tag-link" href="/posts/t/<%= encodeURIComponent(t) %>/"><%= t %></a></li>
<% }) %>
</ul>
<div class="content">
<%- markdown(this.content) %>
</div>
Expand All @@ -18,9 +23,7 @@
<a class="delete" title="<%= _('posts.delete') %>: <%= this.title %>"
href="<%= url('/p/' + this._id) %>"><%= _("posts.delete") %></a>
<span class="updated_by"><%= this.updated_by.user_name %></span>
<time datetime="<%= this.updated_at %>" class="updated_at">
<%= this.updated_at %>
</time>
<time datetime="<%= this.updated_at %>" class="updated_at"><%= this.updated_at %></time>
</footer>
<script type="text/javascript">
$(function(){$('article.post[data-docid="<%= this._id %>"]').bindDelete();});
Expand Down
3 changes: 3 additions & 0 deletions apps/posts/_attachments/templates/parts/sidebar.ejs
@@ -0,0 +1,3 @@
<%- partial('parts/menu') %>
<%- partial('parts/archives', {object: archives.data, as: this}) %>
<%- partial('parts/tags', {object: tags.data, as: this}) %>
15 changes: 15 additions & 0 deletions apps/posts/_attachments/templates/parts/tags.ejs
@@ -0,0 +1,15 @@
<div class="menu">
<h3><%= _("posts.sidebar.tags.title") %></h3>
<% if( this.length > 0 ){ %>
<ul>
<% for(var i in this){ %>
<% var k = this[i].key,
v = this[i].value; %>
<li><a href="<%= url('/t/' + encodeURIComponent(k) + '/') %>"
class="tag-link"
title="<%= k %>"><%= k %> (<%= v %>)</a></li>
<% }; %>
</ul>
<% }else{ %>
<% } %>
</div>
2 changes: 1 addition & 1 deletion apps/posts/_attachments/templates/show.ejs
Expand Up @@ -5,7 +5,7 @@
<%- partial('parts/post', {object: post.data, as: this}) %>
</div>
<aside class="sidebar">
<%- partial('parts/menu') %>
<%- partial('parts/sidebar') %>
</aside>
</div>
<script type="text/javascript">
Expand Down
63 changes: 50 additions & 13 deletions apps/posts/app.js
Expand Up @@ -91,7 +91,8 @@ ddoc.init = function(app, config){
app.get('/',
parallel(
m.byUpdatedAt({perPage: config.postsPerPage}),
m.countByDate()
m.countByDate(),
m.countByTag()
),
m.feedOrHtml('index.ejs'));

Expand All @@ -114,13 +115,32 @@ ddoc.init = function(app, config){
},
parallel(
m.byUpdatedAt({perPage: 'unlimited'}),
m.countByDate()
m.countByDate(),
m.countByTag()
),
m.feedOrHtml('by_month.ejs'));

app.get('/t/:tag/',
function(req, res, next){
var t = req.params.tag;
req.query.startkey = [t, "\uff00"];
req.query.endkey = [t];
req.query.descending = true;
next();
},
parallel(
m.byTag({perPage: config.postsPerPage}),
m.countByDate(),
m.countByTag()
),
m.feedOrHtml('by_tag.ejs'));


app.get('/p/:id', // get an entry
m.byId('id'),
parallel(
m.countByDate(),
m.countByTag()
),
function(req, res, next){
// TODO; content negotiated response
if( res.local('post').error ){
Expand Down Expand Up @@ -183,29 +203,46 @@ ddoc.init = function(app, config){
m.byUpdatedAt({perPage: config.postsPerPage}),
renderList);

app.get('/-/count/tag',
m.countByTag());

app.get('/-/count/date',
m.countByTag());

app.get('/-/t/:tag/',
function(req, res, next){
var t = req.params.tag;
req.query.startkey = [t, req.query.startkey || "\uff00"];
req.query.endkey = [t];
req.query.descending = true;
next();
},
m.byTag({perPage: config.postsPerPage}),
renderList);

// Admin URIs
app.get('/admin/',
m.byUpdatedAt({
perPage: config.postsPerPage,
includeDraft: true
}),
parallel(
m.byUpdatedAt({
perPage: config.postsPerPage,
includeDraft: true
}),
m.countByDate(),
m.countByTag()
),
function(req, res, next){
res.render('admin/index.ejs');
});

app.get('/admin/new',
parallel(
m.countByDate(),
m.countByTag()
),
function(req, res, next){
res.local('post', ddoc.docTemplates.post);
res.render('admin/new.ejs');
});

app.get('/admin/edit/:id',
parallel(
m.countByDate(),
m.countByTag()
),
function(req, res, next){
res.render('admin/edit.ejs');
});
Expand Down
93 changes: 66 additions & 27 deletions apps/posts/middleware.js
@@ -1,3 +1,4 @@
// TODO: all scripts should be exported to be pushed onto Couch.
var merge = require('sunrise').utils.merge;

var allowed_names = [
Expand All @@ -7,6 +8,43 @@ var allowed_names = [


module.exports = {
postList: function(options){
options = merge({
perPage: 10,
bindAs: 'posts',
viewName: 'posts/by_updated_at'
}, options);
var db = this.db;
return function(req, res, next){
var params = {
limit: options.perPage,
descending: true
};
if( params.limit === 'unlimited' ){
delete(params.limit);
}
allowed_names.forEach(function(name){
if( req.query[name] !== undefined ){
params[name] = req.query[name];
}
});
(db.bind('view', options.viewName, params, {
success: function(req, res, next){
var rows = res.locals().posts.data;
for(var i in rows){
if( options.keyFormatter ){
rows[i].value._key = options.keyFormatter(rows[i].key);
}else{
rows[i].value._key = rows[i].key;
}
}
next();
},
as: options.bindAs
}))(req, res, next);
};
},

feedOrHtml: function(filename){
return function(req, res, next){
res.render(filename);
Expand All @@ -33,39 +71,40 @@ module.exports = {
includeDraft: false
}, options);
var db = this.db;
var viewName = 'posts/' + (options.includeDraft === true ?
'all_by_updated_at' : 'by_updated_at');
return function(req, res, next){
var params = {
limit:options.perPage,
descending: true
};
if( options.perPage === 'unlimited' ){
delete(params.limit);
}
options.viewName = 'posts/' + (options.includeDraft === true ?
'all_by_updated_at' : 'by_updated_at');
return this.postList(options);
},

allowed_names.forEach(function(name){
if( req.query[name] !== undefined ){
params[name] = req.query[name];
}
});
(db.bind('view', viewName, params, {
success: function(req, res, next){
var rows = res.locals().posts.data;
for(var i in rows){
rows[i].value._key = rows[i].key;
}
next();
},
as: options.bindAs
}))(req, res, next);
byTag: function(options){
options = merge({
perPage: 10,
bindAs: 'posts',
includeDraft: false
}, options);
var db = this.db;
options.viewName = 'posts/' + (options.includeDraft === true ?
'all_by_tag' : 'by_tag');
options.keyFormatter = function(key){
return key[1];
};
return this.postList(options);
},


countByTag: function(options){
return function(req, res, next){
next();
options = merge({
bindAs: 'tags'
}, options);

var db = this.db;
var params = {
group: true
};

return db.bind('view', 'posts/count_by_tag', params, {
as: options.bindAs
});
},

countByDate: function(options){
Expand Down

0 comments on commit 95dcac2

Please sign in to comment.