blob: 24d5033719665d685073112233b7a01036d0c669 [file] [log] [blame]
Anand Doshi72c945b2012-06-22 20:01:07 +05301{% extends "page.html" %}
2
3{% block javascript %}
Rushabh Mehta3966f1d2012-02-23 12:35:32 +05304// ERPNext - web based ERP (http://erpnext.com)
5// Copyright (C) 2012 Web Notes Technologies Pvt Ltd
6//
7// This program is free software: you can redistribute it and/or modify
8// it under the terms of the GNU General Public License as published by
9// the Free Software Foundation, either version 3 of the License, or
10// (at your option) any later version.
11//
12// This program is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15// GNU General Public License for more details.
16//
17// You should have received a copy of the GNU General Public License
18// along with this program. If not, see <http://www.gnu.org/licenses/>.
19
Rushabh Mehtac8d2e732012-01-27 15:57:17 +053020// js inside blog page
Anand Doshi40fce892012-07-09 20:02:52 +053021wn.provide('erpnext.blog');
Anand Doshi72c945b2012-06-22 20:01:07 +053022wn.pages['{{ name }}'].onload = function(wrapper) {
Anand Doshi40fce892012-07-09 20:02:52 +053023 erpnext.blog.wrapper = wrapper;
24
Rushabh Mehtac8d2e732012-01-27 15:57:17 +053025 // sidebar
Anand Doshi8c7e76b2012-07-11 18:40:57 +053026 erpnext.blog.render_recent_list(wrapper);
27
28 // unhide no-result if no comments found
29 erpnext.blog.toggle_no_result(wrapper);
30
31 // bind add comment button to comment dialog
32 erpnext.blog.make_comment_dialog(wrapper);
33
34 // hide add comment button after 50 comments
35 erpnext.blog.toggle_add_comment_btn(wrapper);
Anand Doshi72c945b2012-06-22 20:01:07 +053036}
37
Anand Doshi40fce892012-07-09 20:02:52 +053038erpnext.blog.adjust_page_height = function(wrapper) {
39 if (!wrapper) { wrapper = erpnext.blog.wrapper; }
40 if (!wrapper) { return; }
41
42 // adjust page height based on sidebar height
43 var $main_page = $(wrapper).find('.layout-main-section');
44 var $sidebar = $(wrapper).find('.layout-side-section');
45 if ($sidebar.height() > $main_page.height()) {
46 $main_page.height($sidebar.height());
47 }
48}
Anand Doshi40fce892012-07-09 20:02:52 +053049
Anand Doshi8c7e76b2012-07-11 18:40:57 +053050erpnext.blog.render_recent_list = function(wrapper) {
51 if (!wrapper) { wrapper = erpnext.blog.wrapper; }
52 if (!wrapper) { return; }
53
54 wrapper.recent_list = new wn.ui.Listing({
55 parent: $(wrapper).find('.recent-posts'),
56 no_toolbar: true,
57 method: 'website.blog.get_recent_blog_list',
58 get_args: function() {
59 return { name: '{{ name }}' }
60 },
61 hide_refresh: true,
62 render_row: function(parent, data) {
63 if(data.content && data.content.length>=100) data.content += '...';
64 parent.innerHTML = repl('<a href="%(name)s.html">%(title)s</a>\
65 <div class="comment">%(content)s</div><br>', data);
66
67 // adjust page height depending on sidebar height
68 erpnext.blog.adjust_page_height(wrapper);
69 },
70 page_length: 5,
71 });
72 wrapper.recent_list.run();
73}
74
75erpnext.blog.toggle_no_result = function(wrapper) {
76 if (!wrapper) { wrapper = erpnext.blog.wrapper; }
77 if (!wrapper) { return; }
78
79 var $blog_comments = $(wrapper).find('.blog-comments');
80 var $comment_rows = $blog_comments.find('.comment-row');
81 var $no_result = $blog_comments.find('.no-result');
82 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) {
97 var d = new wn.widgets.Dialog({
98 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) {
159 $comment_rows.last().after(comment);
160 } else {
161 $blog_comments.find('.no-result').after(comment);
162 }
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');
173 }
174}
Anand Doshi40fce892012-07-09 20:02:52 +0530175
Anand Doshi72c945b2012-06-22 20:01:07 +0530176{% endblock %}