You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

176 lines
5.6 KiB

  1. wn.widgets.form.comments = {
  2. n_comments: {},
  3. comment_list: {},
  4. sync: function(dt, dn, r) {
  5. var f = wn.widgets.form.comments;
  6. f.n_comments[dn] = r.n_comments;
  7. f.comment_list[dn] = r.comment_list;
  8. },
  9. add: function(input, dt, dn, callback) {
  10. $c('webnotes.widgets.form.add_comment', wn.widgets.form.comments.get_args(input, dt, dn),
  11. function(r,rt) {
  12. // update the comments
  13. wn.widgets.form.comments.update_comment_list(input, dt, dn);
  14. // clean up the text area
  15. input.value = '';
  16. callback(input, dt, dn);
  17. }
  18. );
  19. },
  20. remove: function(dt, dn, comment_id, callback) {
  21. $c('webnotes.widgets.form.remove_comment',{
  22. id:comment_id,
  23. dt:dt,
  24. dn:dn
  25. }, callback
  26. );
  27. },
  28. get_args: function(input, dt, dn) {
  29. return {
  30. comment: input.value,
  31. comment_by: user,
  32. comment_by_fullname: user_fullname,
  33. comment_doctype: dt,
  34. comment_docname: dn
  35. }
  36. },
  37. update_comment_list: function(input, dt, dn) {
  38. var f = wn.widgets.form.comments;
  39. // update no of comments
  40. f.n_comments[dn] = cint(f.n_comments[dn]) + 1;
  41. // update comment list
  42. f.comment_list[dn] = add_lists(
  43. [f.get_args(input, dt, dn)],
  44. f.comment_list[dn]
  45. );
  46. }
  47. }
  48. // Comment Listing
  49. // ===============
  50. CommentList = function(parent, dt, dn) {
  51. this.wrapper = $a(parent, 'div', '', {margin:'16px'});
  52. this.input_area = $a(this.wrapper, 'div', '', {margin:'2px'});
  53. this.lst_area = $a(this.wrapper, 'div', '', {margin:'2px'});
  54. this.make_input();
  55. this.make_lst();
  56. this.dt;
  57. this.dn;
  58. }
  59. CommentList.prototype.run = function() {
  60. this.lst.run();
  61. }
  62. CommentList.prototype.make_input = function() {
  63. var me = this;
  64. // make the input text area and button
  65. this.input = $a(this.input_area, 'textarea', '', {height:'60px', width:'300px', fontSize:'14px'});
  66. this.btn = $btn($a(this.input_area, 'div'), 'Post', function() {me.add_comment();},{marginTop:'8px'});
  67. }
  68. // Add comment listing
  69. // --------------------
  70. CommentList.prototype.add_comment = function() {
  71. var me = this;
  72. var callback = function(input, dt, dn) {
  73. me.lst.run();
  74. }
  75. wn.widgets.form.comments.add(this.input, cur_frm.docname, cur_frm.doctype, callback)
  76. }
  77. // Make comment listing
  78. // --------------------
  79. CommentList.prototype.make_lst = function() {
  80. if(!this.lst) {
  81. var l = new Listing('Comments', 1);
  82. var me = this;
  83. // define the columns etc
  84. l.colwidths = ['100%'];
  85. // define options
  86. l.opts.hide_export = 1; l.opts.hide_print = 1; l.opts.hide_refresh = 1; l.opts.no_border = 1;
  87. l.opts.hide_rec_label = 0; l.opts.show_calc = 0; l.opts.round_corners = 0;
  88. l.opts.alt_cell_style = {};
  89. l.opts.cell_style = {padding:'3px'};
  90. l.no_rec_message = 'No comments yet. Be the first one to comment!';
  91. l.get_query = function(){
  92. //---------------------- 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
  93. this.query = repl("select t1.name, t1.comment, t1.comment_by, '', t1.creation, t1.comment_doctype, t1.comment_docname, ifnull(concat_ws(' ',ifnull(t2.first_name,''),ifnull(t2.middle_name,''),ifnull(t2.last_name,'')),''), '', DAYOFMONTH(t1.creation), MONTHNAME(t1.creation), YEAR(t1.creation), hour(t1.creation), minute(t1.creation), second(t1.creation) from `tabComment Widget Record` t1, `tabProfile` t2 where t1.comment_doctype = '%(dt)s' and t1.comment_docname = '%(dn)s' and t1.comment_by = t2.name order by t1.creation desc",{dt:me.dt, dn:me.dn});
  94. this.query_max = repl("select count(name) from `tabComment Widget Record` where comment_doctype='%(dt)s' and comment_docname='%(dn)s'",{'dt': me.dt, 'dn': me.dn});
  95. }
  96. l.show_cell = function(cell, ri, ci, d){
  97. new CommentItem(cell, ri, ci, d, me)
  98. }
  99. this.lst = l;
  100. this.lst.make(this.lst_area);
  101. }
  102. }
  103. // Comment Item
  104. //=============
  105. CommentItem = function(cell, ri, ci, d, comment) {
  106. this.comment = comment;
  107. $y(cell, {padding:'4px 0px'})
  108. var t = make_table(cell, 1, 3, '100%', ['15%', '65%', '20%'], {padding:'4px'});
  109. // image
  110. this.img = $a($td(t,0,0), 'img', '', {width:'40px'});
  111. this.cmt_by = $a($td(t,0,0), 'div');
  112. this.set_picture(d, ri);
  113. // comment
  114. this.cmt_dtl = $a($td(t,0,1), 'div', 'comment', {fontSize:'11px'});
  115. this.cmt = $a($td(t,0,1), 'div','',{fontSize:'14px'});
  116. this.show_cmt($td(t,0,1), ri, ci, d);
  117. this.cmt_delete($td(t,0,2), ri, ci, d);
  118. }
  119. // Set picture
  120. // -----------
  121. CommentItem.prototype.set_picture = function(d, ri){
  122. set_user_img(this.img, user)
  123. this.cmt_by.innerHTML = d[ri][7] ? d[ri][7] : d[ri][2];
  124. }
  125. // Set comment details
  126. // -------------------
  127. CommentItem.prototype.show_cmt = function(cell, ri, ci, d) {
  128. //time and date of comment
  129. if(d[ri][4]){
  130. hr = d[ri][12]; min = d[ri][13]; sec = d[ri][14];
  131. if(parseInt(hr) > 12) { time = (parseInt(hr)-12) + ':' + min + ' PM' }
  132. else{ time = hr + ':' + min + ' AM'}
  133. }
  134. this.cmt_dtl.innerHTML = 'On ' + d[ri][10].substring(0,3) + ' ' + d[ri][9] + ', ' + d[ri][11] + ' at ' + time;
  135. this.cmt.innerHTML = replace_newlines(d[ri][1]);
  136. }
  137. // Set delete button
  138. // -----------------
  139. CommentItem.prototype.cmt_delete = function(cell, ri, ci, d) {
  140. var me = this;
  141. if(d[ri][2] == user || d[ri][3] == user) {
  142. del = $a(cell,'div','wn-icon ic-trash',{cursor:'pointer'});
  143. del.cmt_id = d[ri][0];
  144. del.onclick = function(){
  145. wn.widgets.form.comments.remove(cur_frm.doctype, cur_frm.docname, this.cmt_id,
  146. function() { me.comment.lst.run(); })
  147. }
  148. }
  149. }