Elgg  Version 2.2
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  // Get the form using ajax
40  elgg.ajax('ajax/view/core/ajax/edit_comment?guid=' + this.guid, {
41  success: function(html) {
42  // Add the form to DOM
43  that.$item.find('.elgg-body').first().append(html);
44 
45  that.showForm();
46 
47  var $form = that.getForm();
48 
49  $form.find('.elgg-button-cancel').on('click', function () {
50  that.hideForm();
51  return false;
52  });
53 
54  // save
55  $form.on('submit', function () {
56  that.submitForm();
57  return false;
58  });
59  }
60  });
61  },
62 
63  submitForm: function () {
64  var that = this,
65  $form = this.getForm(),
66  value = $form.find('textarea[name=generic_comment]').val();
67 
68  elgg.action('comment/save', {
69  data: $form.serialize(),
70  success: function(json) {
71  // https://github.com/kvz/phpjs/blob/master/LICENSE.txt
72  function nl2br(content) {
73  if (/<(?:p|br)\b/.test(content)) {
74  // probably formatted already
75  return content;
76  }
77  return content.replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1<br>$2');
78  }
79 
80  if (json.status === 0) {
81  // Update list item content
82  if (json.output) {
83  that.$item.find('[data-role="comment-text"]').replaceWith(json.output);
84  } else {
85  // action has been overridden and doesn't return comment content
86  that.$item.find('[data-role="comment-text"]').html(nl2br(value));
87  }
88  }
89  that.hideForm(function () {
90  that.getForm().remove();
91  });
92  }
93  });
94 
95  return false;
96  },
97 
98  toggleEdit: function () {
99  var $form = this.getForm();
100  if ($form.length) {
101  if ($form.data('hidden')) {
102  this.showForm();
103  } else {
104  this.hideForm();
105  }
106  } else {
107  this.loadForm();
108  }
109  return false;
110  }
111 };
112 
118 elgg.comments.init = function() {
119  $(document).on('click', '.elgg-item-object-comment .elgg-menu-item-edit > a', function () {
120  // store object as data in the edit link
121  var dc = $(this).data('Comment'),
122  guid;
123  if (!dc) {
124  guid = this.href.split('/').pop();
125  dc = new elgg.comments.Comment(guid, $(this).closest('.elgg-item-object-comment'));
126  $(this).data('Comment', dc);
127  }
128  dc.toggleEdit();
129  return false;
130  });
131 };
132 
133 elgg.register_hook_handler('init', 'system', elgg.comments.init);
elgg
Definition: install.js:23
html
Definition: admin.css.php:36
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
$item
Definition: item.php:12
friends picker navigation friends picker navigation r
Definition: admin.css.php:829
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:1274