Elgg  Version 2.3
comments.js
Go to the documentation of this file.
1 
2 elgg.provide('elgg.comments');
3 
9 elgg.comments.Comment = function (guid, item) {
10  this.guid = guid;
11  this.$item = item;
12 };
13 
14 elgg.comments.Comment.prototype = {
20  getForm: function () {
21  return this.$item.find('.elgg-form-comment-save');
22  },
23 
27  hideForm: function (complete) {
28  complete = complete || function () {};
29  this.getForm().slideUp('fast', complete).data('hidden', 1);
30  },
31 
32  showForm: function () {
33  this.getForm().slideDown('medium').data('hidden', 0);
34  },
35 
36  loadForm: function () {
37  var that = this;
38 
39  require(['elgg/Ajax'], function(Ajax) {
40  var ajax = new Ajax();
41 
42  // Get the form using ajax
43  ajax.view('core/ajax/edit_comment?guid=' + that.guid, {
44  success: function(html) {
45  // Add the form to DOM
46  that.$item.find('.elgg-body').first().append(html);
47 
48  that.showForm();
49 
50  var $form = that.getForm();
51 
52  $form.find('.elgg-button-cancel').on('click', function () {
53  that.hideForm();
54  return false;
55  });
56 
57  // save
58  $form.on('submit', function () {
59  that.submitForm();
60  return false;
61  });
62  }
63  });
64  });
65  },
66 
67  submitForm: function () {
68  var that = this,
69  $form = this.getForm(),
70  value = $form.find('textarea[name=generic_comment]').val();
71 
72  elgg.action('comment/save', {
73  data: $form.serialize(),
74  success: function(json) {
75  // https://github.com/kvz/phpjs/blob/master/LICENSE.txt
76  function nl2br(content) {
77  if (/<(?:p|br)\b/.test(content)) {
78  // probably formatted already
79  return content;
80  }
81  return content.replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1<br>$2');
82  }
83 
84  if (json.status === 0) {
85  // Update list item content
86  if (json.output) {
87  that.$item.find('[data-role="comment-text"]').replaceWith(json.output);
88  } else {
89  // action has been overridden and doesn't return comment content
90  that.$item.find('[data-role="comment-text"]').html(nl2br(value));
91  }
92  }
93  that.hideForm(function () {
94  that.getForm().remove();
95  });
96  }
97  });
98 
99  return false;
100  },
101 
102  toggleEdit: function () {
103  var $form = this.getForm();
104  if ($form.length) {
105  if ($form.data('hidden')) {
106  this.showForm();
107  } else {
108  this.hideForm();
109  }
110  } else {
111  this.loadForm();
112  }
113  return false;
114  }
115 };
116 
122 elgg.comments.init = function() {
123  $(document).on('click', '.elgg-item-object-comment .elgg-menu-item-edit > a', function () {
124  // store object as data in the edit link
125  var dc = $(this).data('Comment'),
126  guid;
127  if (!dc) {
128  guid = this.href.split('/').pop();
129  dc = new elgg.comments.Comment(guid, $(this).closest('.elgg-item-object-comment'));
130  $(this).data('Comment', dc);
131  }
132  dc.toggleEdit();
133  return false;
134  });
135 };
136 
137 elgg.register_hook_handler('init', 'system', elgg.comments.init);
elgg
Definition: install.js:23
if(!$items) $item
Definition: delete.php:17
and that you know you can do these things To protect your we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights These restrictions translate to certain responsibilities for you if you distribute copies of the or if you modify it For if you distribute copies of such a whether gratis or for a you must give the recipients all the rights that you have You must make sure that receive or can get the source code And you must show them these terms so they know their rights We protect your rights with two distribute and or modify the software for each author s protection and we want to make certain that everyone understands that there is no warranty for this free software If the software is modified by someone else and passed on
Definition: GPL-LICENSE.txt:43
$form
Definition: settings.php:18
elgg ajax
Wrapper function for jQuery.ajax which ensures that the url being called is relative to the elgg site...
Definition: ajax.js:19
friends picker navigation friends picker navigation r
Definition: admin.css.php:860
elgg require
Throw an error if the required package isn&#39;t present.
Definition: elgglib.js:164
p
Definition: admin.css.php:118
elgg item
Definition: admin.css.php:441
GNU GENERAL PUBLIC LICENSE June Free Software Inc Franklin Fifth MA USA Everyone is permitted to copy and distribute verbatim copies of this license document
Definition: GPL-LICENSE.txt:4
elgg message elgg state success
Definition: admin.css.php:252
b
Definition: admin.css.php:56
elgg widget content
Definition: admin.css.php:1305