comments.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. elgg.provide('elgg.comments');
  2. /**
  3. * @param {Number} guid
  4. * @constructor
  5. */
  6. elgg.comments.Comment = function (guid) {
  7. this.guid = guid;
  8. this.$item = $('#elgg-object-' + guid);
  9. };
  10. elgg.comments.Comment.prototype = {
  11. /**
  12. * Get a jQuery-wrapped reference to the form
  13. *
  14. * @returns {jQuery} note: may be empty
  15. */
  16. getForm: function () {
  17. return this.$item.find('.elgg-form-comment-save');
  18. },
  19. /**
  20. * @param {Function} complete Optional function to run when animation completes
  21. */
  22. hideForm: function (complete) {
  23. complete = complete || function () {};
  24. this.getForm().slideUp('fast', complete).data('hidden', 1);
  25. },
  26. showForm: function () {
  27. this.getForm().slideDown('medium').data('hidden', 0);
  28. },
  29. loadForm: function () {
  30. var that = this;
  31. // Get the form using ajax
  32. elgg.ajax('ajax/view/core/ajax/edit_comment?guid=' + this.guid, {
  33. success: function(html) {
  34. // Add the form to DOM
  35. that.$item.find('.elgg-body').first().append(html);
  36. that.showForm();
  37. var $form = that.getForm();
  38. $form.find('.elgg-button-cancel').on('click', function () {
  39. that.hideForm();
  40. return false;
  41. });
  42. // save
  43. $form.on('submit', function () {
  44. that.submitForm();
  45. return false;
  46. });
  47. }
  48. });
  49. },
  50. submitForm: function () {
  51. var that = this,
  52. $form = this.getForm(),
  53. value = $form.find('textarea[name=generic_comment]').val();
  54. elgg.action('comment/save', {
  55. data: $form.serialize(),
  56. success: function(json) {
  57. // https://github.com/kvz/phpjs/blob/master/LICENSE.txt
  58. function nl2br(content) {
  59. if (/<(?:p|br)\b/.test(content)) {
  60. // probably formatted already
  61. return content;
  62. }
  63. return content.replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1<br>$2');
  64. }
  65. if (json.status === 0) {
  66. // Update list item content
  67. if (json.output) {
  68. that.$item.find('[data-role="comment-text"]').replaceWith(json.output);
  69. } else {
  70. // action has been overridden and doesn't return comment content
  71. that.$item.find('[data-role="comment-text"]').html(nl2br(value));
  72. }
  73. }
  74. that.hideForm(function () {
  75. that.getForm().remove();
  76. });
  77. }
  78. });
  79. return false;
  80. },
  81. toggleEdit: function () {
  82. var $form = this.getForm();
  83. if ($form.length) {
  84. if ($form.data('hidden')) {
  85. this.showForm();
  86. } else {
  87. this.hideForm();
  88. }
  89. } else {
  90. this.loadForm();
  91. }
  92. return false;
  93. }
  94. };
  95. /**
  96. * Initialize comment inline editing
  97. *
  98. * @return void
  99. */
  100. elgg.comments.init = function() {
  101. $(document).on('click', '.elgg-item-object-comment .elgg-menu-item-edit > a', function () {
  102. // store object as data in the edit link
  103. var dc = $(this).data('Comment'),
  104. guid;
  105. if (!dc) {
  106. guid = this.href.split('/').pop();
  107. dc = new elgg.comments.Comment(guid);
  108. $(this).data('Comment', dc);
  109. }
  110. dc.toggleEdit();
  111. return false;
  112. });
  113. };
  114. elgg.register_hook_handler('init', 'system', elgg.comments.init);