Rushabh Mehta | 3966f1d | 2012-02-23 12:35:32 +0530 | [diff] [blame] | 1 | // 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 Mehta | c8d2e73 | 2012-01-27 15:57:17 +0530 | [diff] [blame] | 17 | // js inside blog page |
Anand Doshi | 51146c0 | 2012-07-12 18:41:12 +0530 | [diff] [blame] | 18 | |
Anand Doshi | 40fce89 | 2012-07-09 20:02:52 +0530 | [diff] [blame] | 19 | wn.provide('erpnext.blog'); |
Anand Doshi | 72c945b | 2012-06-22 20:01:07 +0530 | [diff] [blame] | 20 | wn.pages['{{ name }}'].onload = function(wrapper) { |
Anand Doshi | 40fce89 | 2012-07-09 20:02:52 +0530 | [diff] [blame] | 21 | erpnext.blog.wrapper = wrapper; |
| 22 | |
Rushabh Mehta | c8d2e73 | 2012-01-27 15:57:17 +0530 | [diff] [blame] | 23 | // sidebar |
Anand Doshi | 8c7e76b | 2012-07-11 18:40:57 +0530 | [diff] [blame] | 24 | 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 Doshi | 72c945b | 2012-06-22 20:01:07 +0530 | [diff] [blame] | 34 | } |
| 35 | |
Anand Doshi | 40fce89 | 2012-07-09 20:02:52 +0530 | [diff] [blame] | 36 | erpnext.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 Doshi | 40fce89 | 2012-07-09 20:02:52 +0530 | [diff] [blame] | 47 | |
Anand Doshi | 8c7e76b | 2012-07-11 18:40:57 +0530 | [diff] [blame] | 48 | erpnext.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 Mehta | 5f21cc0 | 2012-08-03 14:13:30 +0530 | [diff] [blame] | 62 | 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 Doshi | 8c7e76b | 2012-07-11 18:40:57 +0530 | [diff] [blame] | 65 | |
| 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 | |
| 74 | erpnext.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 Doshi | 51146c0 | 2012-07-12 18:41:12 +0530 | [diff] [blame] | 81 | |
Anand Doshi | 8c7e76b | 2012-07-11 18:40:57 +0530 | [diff] [blame] | 82 | if ($comment_rows.length == 0) { |
| 83 | $no_result.removeClass('hide'); |
| 84 | } else { |
| 85 | $no_result.addClass('hide'); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | erpnext.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 Mehta | 2d700dd | 2012-10-01 14:46:37 +0530 | [diff] [blame] | 97 | var d = new wn.ui.Dialog({ |
Anand Doshi | 8c7e76b | 2012-07-11 18:40:57 +0530 | [diff] [blame] | 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 | |
| 132 | erpnext.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 | |
| 154 | erpnext.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 Doshi | 51146c0 | 2012-07-12 18:41:12 +0530 | [diff] [blame] | 159 | $blog_comments.append(comment); |
Anand Doshi | 8c7e76b | 2012-07-11 18:40:57 +0530 | [diff] [blame] | 160 | } else { |
Anand Doshi | 51146c0 | 2012-07-12 18:41:12 +0530 | [diff] [blame] | 161 | $blog_comments.append(comment); |
Anand Doshi | 8c7e76b | 2012-07-11 18:40:57 +0530 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | erpnext.blog.toggle_no_result(wrapper); |
| 165 | erpnext.blog.toggle_add_comment_btn(wrapper); |
| 166 | } |
| 167 | |
| 168 | erpnext.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 Doshi | 51146c0 | 2012-07-12 18:41:12 +0530 | [diff] [blame] | 173 | |
| 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 Doshi | 8c7e76b | 2012-07-11 18:40:57 +0530 | [diff] [blame] | 180 | } |
Anand Doshi | 51146c0 | 2012-07-12 18:41:12 +0530 | [diff] [blame] | 181 | } |