Rushabh Mehta | e67d1fb | 2013-08-05 14:59:54 +0530 | [diff] [blame] | 1 | // Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. |
| 2 | // License: GNU General Public License v3. See license.txt |
Anand Doshi | 10bcf5e | 2012-06-26 18:54:10 +0530 | [diff] [blame] | 3 | |
| 4 | // js inside blog page |
Rushabh Mehta | fd6ad19 | 2012-12-17 12:52:43 +0530 | [diff] [blame] | 5 | |
| 6 | $(document).ready(function() { |
| 7 | // make list of blogs |
| 8 | blog.get_list(); |
| 9 | |
| 10 | $("#next-page").click(function() { |
| 11 | blog.get_list(); |
| 12 | }) |
Rushabh Mehta | 676a568 | 2013-03-07 18:51:10 +0530 | [diff] [blame] | 13 | |
| 14 | if(get_url_arg("by_name")) { |
Rushabh Mehta | b33693d | 2013-03-11 17:57:57 +0530 | [diff] [blame] | 15 | $("#blot-subtitle").html("Posts by " + get_url_arg("by_name")).toggle(true); |
Rushabh Mehta | 676a568 | 2013-03-07 18:51:10 +0530 | [diff] [blame] | 16 | } |
Rushabh Mehta | a2deb68 | 2013-03-08 10:44:25 +0530 | [diff] [blame] | 17 | |
| 18 | if(get_url_arg("category")) { |
Rushabh Mehta | b33693d | 2013-03-11 17:57:57 +0530 | [diff] [blame] | 19 | $("#blot-subtitle").html("Posts filed under " + get_url_arg("category")).toggle(true); |
Rushabh Mehta | a2deb68 | 2013-03-08 10:44:25 +0530 | [diff] [blame] | 20 | } |
| 21 | |
Rushabh Mehta | fd6ad19 | 2012-12-17 12:52:43 +0530 | [diff] [blame] | 22 | }); |
| 23 | |
| 24 | var blog = { |
| 25 | start: 0, |
| 26 | get_list: function() { |
| 27 | $.ajax({ |
| 28 | method: "GET", |
| 29 | url: "server.py", |
| 30 | data: { |
| 31 | cmd: "website.helpers.blog.get_blog_list", |
Rushabh Mehta | 676a568 | 2013-03-07 18:51:10 +0530 | [diff] [blame] | 32 | start: blog.start, |
Rushabh Mehta | a2deb68 | 2013-03-08 10:44:25 +0530 | [diff] [blame] | 33 | by: get_url_arg("by"), |
| 34 | category: get_url_arg("category") |
Rushabh Mehta | fd6ad19 | 2012-12-17 12:52:43 +0530 | [diff] [blame] | 35 | }, |
| 36 | dataType: "json", |
| 37 | success: function(data) { |
Rushabh Mehta | bbbceb6 | 2013-03-11 16:12:56 +0530 | [diff] [blame] | 38 | $(".progress").toggle(false); |
Rushabh Mehta | 676a568 | 2013-03-07 18:51:10 +0530 | [diff] [blame] | 39 | if(data.exc) console.log(data.exc); |
Rushabh Mehta | fd6ad19 | 2012-12-17 12:52:43 +0530 | [diff] [blame] | 40 | blog.render(data.message); |
| 41 | } |
| 42 | }); |
| 43 | }, |
| 44 | render: function(data) { |
| 45 | var $wrap = $("#blog-list"); |
| 46 | $.each(data, function(i, b) { |
| 47 | // comments |
| 48 | if(!b.comments) { |
| 49 | b.comment_text = 'No comments yet.' |
| 50 | } else if (b.comments===1) { |
| 51 | b.comment_text = '1 comment.' |
Rushabh Mehta | aaa7549 | 2012-08-06 15:22:17 +0530 | [diff] [blame] | 52 | } else { |
Rushabh Mehta | fd6ad19 | 2012-12-17 12:52:43 +0530 | [diff] [blame] | 53 | b.comment_text = b.comments + ' comments.' |
Anand Doshi | 643236a | 2013-03-12 20:26:33 +0530 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | b.page_name = encodeURIComponent(b.page_name); |
Rushabh Mehta | aaa7549 | 2012-08-06 15:22:17 +0530 | [diff] [blame] | 57 | |
Rushabh Mehta | 676a568 | 2013-03-07 18:51:10 +0530 | [diff] [blame] | 58 | $(repl('<div class="row">\ |
Rushabh Mehta | cce21d1 | 2013-08-21 17:48:08 +0530 | [diff] [blame] | 59 | <div class="col-md-1">\ |
Rushabh Mehta | 676a568 | 2013-03-07 18:51:10 +0530 | [diff] [blame] | 60 | <div class="avatar avatar-medium" style="margin-top: 6px;">\ |
| 61 | <img src="%(avatar)s" />\ |
| 62 | </div>\ |
| 63 | </div>\ |
Rushabh Mehta | cce21d1 | 2013-08-21 17:48:08 +0530 | [diff] [blame] | 64 | <div class="col-md-11">\ |
Rushabh Mehta | 676a568 | 2013-03-07 18:51:10 +0530 | [diff] [blame] | 65 | <h4><a href="%(page_name)s">%(title)s</a></h4>\ |
| 66 | <p>%(content)s</p>\ |
| 67 | <p style="color: #aaa; font-size: 90%">\ |
| 68 | <a href="blog?by=%(blogger)s&by_name=%(full_name)s">\ |
| 69 | %(full_name)s</a> wrote this on %(published)s / %(comment_text)s</p>\ |
| 70 | </div>\ |
| 71 | </div><hr>', b)).appendTo($wrap); |
Rushabh Mehta | fd6ad19 | 2012-12-17 12:52:43 +0530 | [diff] [blame] | 72 | }); |
Rushabh Mehta | e109fa4 | 2012-12-19 10:14:59 +0530 | [diff] [blame] | 73 | blog.start += (data.length || 0); |
Rushabh Mehta | bbbceb6 | 2013-03-11 16:12:56 +0530 | [diff] [blame] | 74 | if(!data.length || data.length < 20) { |
Rushabh Mehta | e109fa4 | 2012-12-19 10:14:59 +0530 | [diff] [blame] | 75 | if(blog.start) { |
| 76 | $("#next-page").toggle(false) |
Rushabh Mehta | 74560b3 | 2013-05-27 14:47:56 +0530 | [diff] [blame] | 77 | .parent().append("<div class='text-muted'>Nothing more to show.</div>"); |
Rushabh Mehta | e109fa4 | 2012-12-19 10:14:59 +0530 | [diff] [blame] | 78 | } else { |
| 79 | $("#next-page").toggle(false) |
Rushabh Mehta | cce21d1 | 2013-08-21 17:48:08 +0530 | [diff] [blame] | 80 | .parent().append("<div class='alert alert-warning'>No blogs written yet.</div>"); |
Rushabh Mehta | e109fa4 | 2012-12-19 10:14:59 +0530 | [diff] [blame] | 81 | } |
| 82 | } else { |
| 83 | $("#next-page").toggle(true); |
Rushabh Mehta | fd6ad19 | 2012-12-17 12:52:43 +0530 | [diff] [blame] | 84 | } |
| 85 | } |
Anand Doshi | 51146c0 | 2012-07-12 18:41:12 +0530 | [diff] [blame] | 86 | } |