25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

comments.js 3.1 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
  2. // MIT License. See license.txt
  3. wn.ui.form.Comments = Class.extend({
  4. init: function(opts) {
  5. $.extend(this, opts);
  6. this.make();
  7. },
  8. make: function() {
  9. var me = this;
  10. this.wrapper =this.parent;
  11. this.row = $("<div class='row'>").appendTo(this.parent);
  12. this.input = $('<div class="col-md-10" style="margin-top: 5px;">\
  13. <textarea rows="3" class="form-control"></textarea></div>')
  14. .appendTo(this.row)
  15. .find("textarea");
  16. this.button = $('<div class="col-md-1">\
  17. <button class="btn btn-default btn-go" class="col-md-1" style="margin-top: 5px;">\
  18. <i class="icon-ok"></i></button>\
  19. </div>')
  20. .appendTo(this.row)
  21. .find("button")
  22. .click(function() {
  23. me.add_comment(this);
  24. });
  25. this.list = $('<div class="comments" style="margin-top: 15px;"></div>')
  26. .appendTo(this.parent);
  27. },
  28. get_comments: function() {
  29. return this.frm.get_docinfo().comments;
  30. },
  31. refresh: function() {
  32. var me = this;
  33. if(this.frm.doc.__islocal) {
  34. this.wrapper.toggle(false);
  35. return;
  36. }
  37. this.wrapper.toggle(true);
  38. this.list.empty();
  39. var comments = this.get_comments();
  40. $.each(comments, function(i, c) {
  41. if(wn.model.can_delete("Comment")) {
  42. c["delete"] = '<a class="close" href="#">&times;</a>';
  43. } else {
  44. c["delete"] = "";
  45. }
  46. c.image = wn.user_info(c.comment_by).image;
  47. c.comment_on = dateutil.comment_when(c.creation);
  48. c.fullname = wn.user_info(c.comment_by).fullname;
  49. $(repl('<div class="comment alert alert-warning col-md-10" data-name="%(name)s">\
  50. <div class="row">\
  51. <div class="col-xs-1">\
  52. <span class="avatar avatar-small"><img src="%(image)s"></span>\
  53. </div>\
  54. <div class="col-xs-11">%(delete)s\
  55. <div>%(comment)s</div>\
  56. <span class="small text-muted">%(fullname)s / %(comment_on)s</span>\
  57. </div>\
  58. </div>\
  59. </div>', c))
  60. .appendTo(me.list)
  61. .on("click", ".close", function() {
  62. var name = $(this).parents(".comment:first").attr("data-name");
  63. me.delete_comment(name);
  64. return false;
  65. })
  66. });
  67. },
  68. add_comment: function(btn) {
  69. var me = this,
  70. txt = me.input.val();
  71. if(txt) {
  72. var comment = {
  73. doctype: "Comment",
  74. comment_doctype: me.frm.doctype,
  75. comment_docname: me.frm.docname,
  76. comment: txt,
  77. comment_by: user
  78. };
  79. return wn.call({
  80. method: "webnotes.widgets.form.utils.add_comment",
  81. args: {
  82. doclist:[comment]
  83. },
  84. btn: btn,
  85. callback: function(r) {
  86. if(!r.exc) {
  87. me.frm.get_docinfo().comments =
  88. r.message.concat(me.get_comments());
  89. me.frm.toolbar.show_infobar();
  90. me.input.val("");
  91. me.refresh();
  92. }
  93. }
  94. });
  95. }
  96. },
  97. delete_comment: function(name) {
  98. var me = this;
  99. return wn.call({
  100. method: "webnotes.client.delete",
  101. args: {
  102. doctype: "Comment",
  103. name: name
  104. },
  105. callback: function(r) {
  106. if(!r.exc) {
  107. me.frm.get_docinfo().comments =
  108. $.map(me.frm.get_docinfo().comments,
  109. function(v) {
  110. if(v.name==name) return null;
  111. else return v;
  112. }
  113. );
  114. me.refresh();
  115. me.frm.toolbar.show_infobar();
  116. }
  117. }
  118. });
  119. }
  120. })