blob: 8134b635d14ca66eedfed232b64492cdf6452e65 [file] [log] [blame]
Rushabh Mehtae67d1fb2013-08-05 14:59:54 +05301// Copyright (c) 2013, Web Notes Technologies Pvt. Ltd.
2// License: GNU General Public License v3. See license.txt
Anand Doshi10bcf5e2012-06-26 18:54:10 +05303
4// js inside blog page
Rushabh Mehtafd6ad192012-12-17 12:52:43 +05305
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 Mehta676a5682013-03-07 18:51:10 +053013
14 if(get_url_arg("by_name")) {
Rushabh Mehtab33693d2013-03-11 17:57:57 +053015 $("#blot-subtitle").html("Posts by " + get_url_arg("by_name")).toggle(true);
Rushabh Mehta676a5682013-03-07 18:51:10 +053016 }
Rushabh Mehtaa2deb682013-03-08 10:44:25 +053017
18 if(get_url_arg("category")) {
Rushabh Mehtab33693d2013-03-11 17:57:57 +053019 $("#blot-subtitle").html("Posts filed under " + get_url_arg("category")).toggle(true);
Rushabh Mehtaa2deb682013-03-08 10:44:25 +053020 }
21
Rushabh Mehtafd6ad192012-12-17 12:52:43 +053022});
23
24var 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 Mehta676a5682013-03-07 18:51:10 +053032 start: blog.start,
Rushabh Mehtaa2deb682013-03-08 10:44:25 +053033 by: get_url_arg("by"),
34 category: get_url_arg("category")
Rushabh Mehtafd6ad192012-12-17 12:52:43 +053035 },
36 dataType: "json",
37 success: function(data) {
Rushabh Mehtabbbceb62013-03-11 16:12:56 +053038 $(".progress").toggle(false);
Rushabh Mehta676a5682013-03-07 18:51:10 +053039 if(data.exc) console.log(data.exc);
Rushabh Mehtafd6ad192012-12-17 12:52:43 +053040 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 Mehtaaaa75492012-08-06 15:22:17 +053052 } else {
Rushabh Mehtafd6ad192012-12-17 12:52:43 +053053 b.comment_text = b.comments + ' comments.'
Anand Doshi643236a2013-03-12 20:26:33 +053054 }
55
56 b.page_name = encodeURIComponent(b.page_name);
Rushabh Mehtaaaa75492012-08-06 15:22:17 +053057
Rushabh Mehta676a5682013-03-07 18:51:10 +053058 $(repl('<div class="row">\
Rushabh Mehtacce21d12013-08-21 17:48:08 +053059 <div class="col-md-1">\
Rushabh Mehta676a5682013-03-07 18:51:10 +053060 <div class="avatar avatar-medium" style="margin-top: 6px;">\
61 <img src="%(avatar)s" />\
62 </div>\
63 </div>\
Rushabh Mehtacce21d12013-08-21 17:48:08 +053064 <div class="col-md-11">\
Rushabh Mehta676a5682013-03-07 18:51:10 +053065 <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 Mehtafd6ad192012-12-17 12:52:43 +053072 });
Rushabh Mehtae109fa42012-12-19 10:14:59 +053073 blog.start += (data.length || 0);
Rushabh Mehtabbbceb62013-03-11 16:12:56 +053074 if(!data.length || data.length < 20) {
Rushabh Mehtae109fa42012-12-19 10:14:59 +053075 if(blog.start) {
76 $("#next-page").toggle(false)
Rushabh Mehta74560b32013-05-27 14:47:56 +053077 .parent().append("<div class='text-muted'>Nothing more to show.</div>");
Rushabh Mehtae109fa42012-12-19 10:14:59 +053078 } else {
79 $("#next-page").toggle(false)
Rushabh Mehtacce21d12013-08-21 17:48:08 +053080 .parent().append("<div class='alert alert-warning'>No blogs written yet.</div>");
Rushabh Mehtae109fa42012-12-19 10:14:59 +053081 }
82 } else {
83 $("#next-page").toggle(true);
Rushabh Mehtafd6ad192012-12-17 12:52:43 +053084 }
85 }
Anand Doshi51146c02012-07-12 18:41:12 +053086}