Skip to content
Browse files

tag filter function done.

  • Loading branch information...
1 parent b845a4d commit 95dcac2eaecb33abc69042dddd971bd48b7d727a @yssk22 committed May 8, 2011
View
1 apps/posts/_attachments/js/posts.js
@@ -16,7 +16,6 @@
startkey_docid: last.attr('data-docid'),
skip: 1
};
-
$.get(path, param, function(data){
link.before(data);
var newlast = link.prev();
View
1 apps/posts/_attachments/messages/en.json
@@ -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",
View
2 apps/posts/_attachments/templates/admin/edit.ejs
@@ -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>
View
2 apps/posts/_attachments/templates/admin/index.ejs
@@ -17,6 +17,6 @@
<% } %>
</div>
<aside class="sidebar">
- <%- partial('parts/menu') %>
+ <%- partial('parts/sidebar') %>
</aside>
</div>
View
6 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>
View
3 apps/posts/_attachments/templates/by_month.ejs
@@ -14,7 +14,6 @@
<% } %>
</div>
<aside class="sidebar">
- <%- partial('parts/menu') %>
- <%- partial('parts/archives', {object: archives.data, as: this}) %>
+ <%- partial('parts/sidebar') %>
</aside>
</div>
View
22 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>
View
4 apps/posts/_attachments/templates/index.ejs
@@ -17,8 +17,6 @@
<% } %>
</div>
<aside class="sidebar">
- <%- partial('parts/menu') %>
- <%- partial('parts/archives', {object: archives.data, as: this}) %>
-
+ <%- partial('parts/sidebar') %>
</aside>
</div>
View
9 apps/posts/_attachments/templates/parts/post.ejs
@@ -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>
@@ -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();});
View
3 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}) %>
View
15 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>
View
2 apps/posts/_attachments/templates/show.ejs
@@ -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">
View
63 apps/posts/app.js
@@ -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'));
@@ -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 ){
@@ -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');
});
View
93 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 = [
@@ -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);
@@ -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){
View
33 apps/posts/views.js
@@ -28,14 +28,31 @@ module.exports = {
},
by_tag : {
- map: function(){
+ map: function(doc){
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);
- }
- }
+ var tags = doc.tags || [];
+ tags.filter(function(t){
+ return (t && t != '');
+ }).forEach(function(t){
+ emit([t, doc.updated_at], doc);
+ });
+ }
+ }
+ },
+
+ count_by_tag: {
+ map: function(doc){
+ if( doc.type == 'post' ){
+ var tags = doc.tags || [];
+ tags.filter(function(t){
+ return (t && t != '');
+ }).forEach(function(t){
+ emit(t, 1);
+ });
}
+ },
+ reduce: function(keys, values, rereduce){
+ return sum(values);
}
},
@@ -55,5 +72,7 @@ module.exports = {
reduce: function(keys, values, rereduce){
return sum(values);
}
- }
+ },
+
+
};

0 comments on commit 95dcac2

Please sign in to comment.
Something went wrong with that request. Please try again.