discussion.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. elgg.provide('elgg.discussion');
  2. /**
  3. * @param {Number} guid
  4. * @constructor
  5. */
  6. elgg.discussion.Reply = function (guid) {
  7. this.guid = guid;
  8. this.$item = $('#elgg-object-' + guid);
  9. };
  10. elgg.discussion.Reply.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-discussion-reply-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 ajax/view/core/ajax/edit_comment
  32. elgg.ajax('ajax/view/ajax/discussion/reply/edit?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=description]').val();
  54. elgg.action('discussion/reply/save', {
  55. data: $form.serialize(),
  56. success: function(json) {
  57. if (json.status === 0) {
  58. // Update list item content
  59. that.$item.find('[data-role="discussion-reply-text"]').html(value);
  60. }
  61. that.hideForm(function () {
  62. that.getForm().remove();
  63. });
  64. }
  65. });
  66. return false;
  67. },
  68. toggleEdit: function () {
  69. var $form = this.getForm();
  70. if ($form.length) {
  71. if ($form.data('hidden')) {
  72. this.showForm();
  73. } else {
  74. this.hideForm();
  75. }
  76. } else {
  77. this.loadForm();
  78. }
  79. return false;
  80. }
  81. };
  82. /**
  83. * Initialize discussion reply inline editing
  84. *
  85. * @return void
  86. */
  87. elgg.discussion.init = function() {
  88. $(document).on('click', '.elgg-item-object-discussion_reply .elgg-menu-item-edit > a', function () {
  89. // store object as data in the edit link
  90. var dc = $(this).data('Reply'),
  91. guid;
  92. if (!dc) {
  93. guid = this.href.split('/').pop();
  94. dc = new elgg.discussion.Reply(guid);
  95. $(this).data('Reply', dc);
  96. }
  97. dc.toggleEdit();
  98. return false;
  99. });
  100. };
  101. elgg.register_hook_handler('init', 'system', elgg.discussion.init);