您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

form_comments.js 6.9 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. // Copyright (c) 2012 Web Notes Technologies Pvt Ltd (http://erpnext.com)
  2. //
  3. // MIT License (MIT)
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a
  6. // copy of this software and associated documentation files (the "Software"),
  7. // to deal in the Software without restriction, including without limitation
  8. // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. // and/or sell copies of the Software, and to permit persons to whom the
  10. // Software is furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  16. // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  17. // PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  19. // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
  20. // OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. //
  22. wn.widgets.form.comments = {
  23. n_comments: {},
  24. comment_list: {},
  25. sync: function(dt, dn, r) {
  26. var f = wn.widgets.form.comments;
  27. f.n_comments[dn] = r.n_comments;
  28. f.comment_list[dn] = r.comment_list;
  29. },
  30. add: function(input, dt, dn, callback) {
  31. $c('webnotes.widgets.form.comments.add_comment', wn.widgets.form.comments.get_args(input, dt, dn),
  32. function(r,rt) {
  33. // update the comments
  34. wn.widgets.form.comments.update_comment_list(input, dt, dn);
  35. // clean up the text area
  36. input.value = '';
  37. callback(input, dt, dn);
  38. }
  39. );
  40. },
  41. remove: function(dt, dn, comment_id, callback) {
  42. $c('webnotes.widgets.form.comments.remove_comment',{
  43. id:comment_id,
  44. dt:dt,
  45. dn:dn
  46. }, callback
  47. );
  48. },
  49. get_args: function(input, dt, dn) {
  50. return {
  51. comment: input.value,
  52. comment_by: user,
  53. comment_by_fullname: user_fullname,
  54. comment_doctype: dt,
  55. comment_docname: dn
  56. }
  57. },
  58. update_comment_list: function(input, dt, dn) {
  59. var f = wn.widgets.form.comments;
  60. // update no of comments
  61. f.n_comments[dn] = cint(f.n_comments[dn]) + 1;
  62. // update comment list
  63. f.comment_list[dn] = add_lists(
  64. [f.get_args(input, dt, dn)],
  65. f.comment_list[dn]
  66. );
  67. }
  68. }
  69. // Comment Listing
  70. // ===============
  71. CommentList = function(parent, dt, dn) {
  72. this.wrapper = $a(parent, 'div', '', {margin:'16px'});
  73. this.input_area = $a(this.wrapper, 'div', '', {margin:'2px'});
  74. this.lst_area = $a(this.wrapper, 'div', '', {margin:'2px'});
  75. this.make_input();
  76. this.make_lst();
  77. this.dt;
  78. this.dn;
  79. }
  80. CommentList.prototype.run = function() {
  81. this.lst.run();
  82. }
  83. CommentList.prototype.make_input = function() {
  84. var me = this;
  85. // make the input text area and button
  86. this.input = $a(this.input_area, 'textarea', '', {height:'60px', width:'300px', fontSize:'14px'});
  87. this.btn = $btn($a(this.input_area, 'div'), 'Post', function() {me.add_comment();},{marginTop:'8px'});
  88. }
  89. // Add comment listing
  90. // --------------------
  91. CommentList.prototype.add_comment = function() {
  92. var me = this;
  93. var callback = function(input, dt, dn) {
  94. me.lst.run();
  95. }
  96. wn.widgets.form.comments.add(this.input, cur_frm.docname, cur_frm.doctype, callback)
  97. }
  98. // Make comment listing
  99. // --------------------
  100. CommentList.prototype.make_lst = function() {
  101. if(!this.lst) {
  102. wn.require('js/listing.js');
  103. var l = new Listing('Comments', 1);
  104. var me = this;
  105. // define the columns etc
  106. l.colwidths = ['100%'];
  107. // define options
  108. l.opts.hide_export = 1; l.opts.hide_print = 1; l.opts.hide_refresh = 1; l.opts.no_border = 1;
  109. l.opts.hide_rec_label = 0; l.opts.show_calc = 0; l.opts.round_corners = 0;
  110. l.opts.alt_cell_style = {};
  111. l.opts.cell_style = {padding:'3px'};
  112. l.no_rec_message = 'No comments yet. Be the first one to comment!';
  113. l.get_query = function(){
  114. //---------------------- 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
  115. this.query = repl("select t1.name, t1.comment, t1.comment_by, '', \
  116. t1.creation, t1.comment_doctype, t1.comment_docname, \
  117. ifnull(concat_ws(' ',ifnull(t2.first_name,''),ifnull(t2.middle_name,''),\
  118. ifnull(t2.last_name,'')),''), '', \
  119. DAYOFMONTH(t1.creation), MONTHNAME(t1.creation), YEAR(t1.creation), \
  120. hour(t1.creation), minute(t1.creation), second(t1.creation) \
  121. from `tabComment` t1, `tabProfile` t2 \
  122. where t1.comment_doctype = '%(dt)s' and t1.comment_docname = '%(dn)s' \
  123. and t1.comment_by = t2.name order by t1.creation desc",{dt:me.dt, dn:me.dn});
  124. this.query_max = repl("select count(name) from `tabComment` where \
  125. comment_doctype='%(dt)s' and comment_docname='%(dn)s'",{'dt': me.dt, 'dn': me.dn});
  126. }
  127. l.show_cell = function(cell, ri, ci, d){
  128. new CommentItem(cell, ri, ci, d, me)
  129. }
  130. this.lst = l;
  131. this.lst.make(this.lst_area);
  132. }
  133. }
  134. // Comment Item
  135. //=============
  136. CommentItem = function(cell, ri, ci, d, comment) {
  137. this.comment = comment;
  138. $y(cell, {padding:'4px 0px'})
  139. var t = make_table(cell, 1, 3, '100%', ['15%', '65%', '20%'], {padding:'4px'});
  140. // image
  141. this.img = $a($td(t,0,0), 'img', '', {width:'40px'});
  142. this.cmt_by = $a($td(t,0,0), 'div');
  143. this.set_picture(d, ri);
  144. // comment
  145. this.cmt_dtl = $a($td(t,0,1), 'div', 'comment', {fontSize:'11px'});
  146. this.cmt = $a($td(t,0,1), 'div','',{fontSize:'14px'});
  147. this.show_cmt($td(t,0,1), ri, ci, d);
  148. this.cmt_delete($td(t,0,2), ri, ci, d);
  149. }
  150. // Set picture
  151. // -----------
  152. CommentItem.prototype.set_picture = function(d, ri){
  153. this.user.src = wn.user_info(d[ri][2]).image;
  154. this.cmt_by.innerHTML = d[ri][7] ? d[ri][7] : d[ri][2];
  155. }
  156. // Set comment details
  157. // -------------------
  158. CommentItem.prototype.show_cmt = function(cell, ri, ci, d) {
  159. //time and date of comment
  160. if(d[ri][4]){
  161. hr = d[ri][12]; min = d[ri][13]; sec = d[ri][14];
  162. if(parseInt(hr) > 12) { time = (parseInt(hr)-12) + ':' + min + ' PM' }
  163. else{ time = hr + ':' + min + ' AM'}
  164. }
  165. this.cmt_dtl.innerHTML = 'On ' + d[ri][10].substring(0,3) + ' ' + d[ri][9] + ', ' + d[ri][11] + ' at ' + time;
  166. this.cmt.innerHTML = replace_newlines(d[ri][1]);
  167. }
  168. // Set delete button
  169. // -----------------
  170. CommentItem.prototype.cmt_delete = function(cell, ri, ci, d) {
  171. var me = this;
  172. if(d[ri][2] == user || d[ri][3] == user) {
  173. del = $a(cell,'i','icon-remove-sign',{cursor:'pointer'});
  174. del.cmt_id = d[ri][0];
  175. del.onclick = function(){
  176. wn.widgets.form.comments.remove(cur_frm.doctype, cur_frm.docname, this.cmt_id,
  177. function() { me.comment.lst.run(); })
  178. }
  179. }
  180. }