blob: 617993c5083db7b475d79c051e1863997648a9cb [file] [log] [blame]
Rushabh Mehta3966f1d2012-02-23 12:35:32 +05301// ERPNext - web based ERP (http://erpnext.com)
2// Copyright (C) 2012 Web Notes Technologies Pvt Ltd
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <http://www.gnu.org/licenses/>.
16
Rushabh Mehtac8d2e732012-01-27 15:57:17 +053017// js inside blog page
Anand Doshi51146c02012-07-12 18:41:12 +053018
Anand Doshi40fce892012-07-09 20:02:52 +053019wn.provide('erpnext.blog');
Anand Doshi72c945b2012-06-22 20:01:07 +053020wn.pages['{{ name }}'].onload = function(wrapper) {
Anand Doshi40fce892012-07-09 20:02:52 +053021 erpnext.blog.wrapper = wrapper;
22
Rushabh Mehtac8d2e732012-01-27 15:57:17 +053023 // sidebar
Anand Doshi8c7e76b2012-07-11 18:40:57 +053024 erpnext.blog.render_recent_list(wrapper);
25
26 // unhide no-result if no comments found
27 erpnext.blog.toggle_no_result(wrapper);
28
29 // bind add comment button to comment dialog
30 erpnext.blog.make_comment_dialog(wrapper);
31
32 // hide add comment button after 50 comments
33 erpnext.blog.toggle_add_comment_btn(wrapper);
Anand Doshi72c945b2012-06-22 20:01:07 +053034}
35
Anand Doshi40fce892012-07-09 20:02:52 +053036erpnext.blog.adjust_page_height = function(wrapper) {
37 if (!wrapper) { wrapper = erpnext.blog.wrapper; }
38 if (!wrapper) { return; }
39
40 // adjust page height based on sidebar height
41 var $main_page = $(wrapper).find('.layout-main-section');
42 var $sidebar = $(wrapper).find('.layout-side-section');
43 if ($sidebar.height() > $main_page.height()) {
44 $main_page.height($sidebar.height());
45 }
46}
Anand Doshi40fce892012-07-09 20:02:52 +053047
Anand Doshi8c7e76b2012-07-11 18:40:57 +053048erpnext.blog.render_recent_list = function(wrapper) {
49 if (!wrapper) { wrapper = erpnext.blog.wrapper; }
50 if (!wrapper) { return; }
51
52 wrapper.recent_list = new wn.ui.Listing({
53 parent: $(wrapper).find('.recent-posts'),
54 no_toolbar: true,
55 method: 'website.blog.get_recent_blog_list',
56 get_args: function() {
57 return { name: '{{ name }}' }
58 },
59 hide_refresh: true,
60 render_row: function(parent, data) {
61 if(data.content && data.content.length>=100) data.content += '...';
Rushabh Mehta5f21cc02012-08-03 14:13:30 +053062 parent.innerHTML = repl('<div style="font-size: 80%">\
63 <a href="%(page_name)s.html">%(title)s</a>\
64 <div class="comment">%(content)s</div><br></div>', data);
Anand Doshi8c7e76b2012-07-11 18:40:57 +053065
66 // adjust page height depending on sidebar height
67 erpnext.blog.adjust_page_height(wrapper);
68 },
69 page_length: 5,
70 });
71 wrapper.recent_list.run();
72}
73
74erpnext.blog.toggle_no_result = function(wrapper) {
75 if (!wrapper) { wrapper = erpnext.blog.wrapper; }
76 if (!wrapper) { return; }
77
78 var $blog_comments = $(wrapper).find('.blog-comments');
79 var $comment_rows = $blog_comments.find('.comment-row');
80 var $no_result = $blog_comments.find('.no-result');
Anand Doshi51146c02012-07-12 18:41:12 +053081
Anand Doshi8c7e76b2012-07-11 18:40:57 +053082 if ($comment_rows.length == 0) {
83 $no_result.removeClass('hide');
84 } else {
85 $no_result.addClass('hide');
86 }
87}
88
89erpnext.blog.make_comment_dialog = function(wrapper) {
90 if (!wrapper) { wrapper = erpnext.blog.wrapper; }
91 if (!wrapper) { return; }
92
93 var $comment_btn = $(wrapper).find('button.add-comment');
94
95 $comment_btn.click(function() {
96 if(!erpnext.blog.comment_dialog) {
Rushabh Mehta2d700dd2012-10-01 14:46:37 +053097 var d = new wn.ui.Dialog({
Anand Doshi8c7e76b2012-07-11 18:40:57 +053098 title: 'Add Comment',
99 fields: [
100 {
101 fieldname: 'comment_by_fullname', label: 'Your Name',
102 reqd: 1, fieldtype: 'Data'
103 },
104 {
105 fieldname: 'comment_by', label: 'Email Id',
106 reqd: 1, fieldtype: 'Data'
107 },
108 {
109 fieldname: 'comment', label: 'Comment',
110 reqd: 1, fieldtype: 'Text'
111 },
112 {
113 fieldname: 'post_comment', label: 'Post Comment',
114 fieldtype: 'Button'
115 },
116 ],
117 });
118
119 erpnext.blog.comment_dialog = d;
120 }
121
122 erpnext.blog.comment_dialog.fields_dict.post_comment
123 .input.onclick = function() {
124 erpnext.blog.add_comment(wrapper);
125 }
126
127 erpnext.blog.comment_dialog.show();
128 });
129
130}
131
132erpnext.blog.add_comment = function(wrapper) {
133 var args = erpnext.blog.comment_dialog.get_values();
134
135 if(!args) return;
136
137 args.comment_doctype = 'Blog';
138 args.comment_docname = '{{ name }}';
139 args.page_name = '{{ page_name }}';
140
141 wn.call({
142 method: 'website.blog.add_comment',
143 args: args,
144 btn: this,
145 callback: function(r) {
146 if(!r.exc) {
147 erpnext.blog.add_comment_to_page(wrapper, r.message);
148 erpnext.blog.comment_dialog.hide();
149 }
150 }
151 });
152}
153
154erpnext.blog.add_comment_to_page = function(wrapper, comment) {
155 $blog_comments = $(wrapper).find('.blog-comments');
156 $comment_rows = $blog_comments.find('.comment-row');
157
158 if ($comment_rows.length) {
Anand Doshi51146c02012-07-12 18:41:12 +0530159 $blog_comments.append(comment);
Anand Doshi8c7e76b2012-07-11 18:40:57 +0530160 } else {
Anand Doshi51146c02012-07-12 18:41:12 +0530161 $blog_comments.append(comment);
Anand Doshi8c7e76b2012-07-11 18:40:57 +0530162 }
163
164 erpnext.blog.toggle_no_result(wrapper);
165 erpnext.blog.toggle_add_comment_btn(wrapper);
166}
167
168erpnext.blog.toggle_add_comment_btn = function(wrapper) {
169 var $wrapper = $(wrapper);
170 if ($wrapper.find('.blog-comments .comment-row').length > 50) {
171 var $comment_btn = $wrapper.find('button.add-comment');
172 $comment_btn.addClass('hide');
Anand Doshi51146c02012-07-12 18:41:12 +0530173
174 // show comments are close
175 $wrapper.find('.blog-comments').append("\
176 <div class=\"help\"> \
177 <p>Comments Closed</p> \
178 <br /> \
179 </div>");
Anand Doshi8c7e76b2012-07-11 18:40:57 +0530180 }
Anand Doshi51146c02012-07-12 18:41:12 +0530181}