Elgg  Version 5.1
comments.js
Go to the documentation of this file.
1 define(['jquery', 'elgg'], function ($, elgg) {
2  /* Autofocuses first text input in a comment form when toggled */
3  $(document).on('elgg_ui_toggle', function (e, data) {
4  var $toggle = $(e.target);
5  var $elements = data.$toggled_elements;
6 
7  if ($elements.is('.elgg-river-responses > .elgg-form-comment-save')) {
8  if ($toggle.hasClass('elgg-state-active')) {
9  $elements.find('.elgg-input-text').focus();
10  } else {
11  $toggle.blur();
12  }
13  }
14  });
15 
16  $(document).on('click', '.elgg-toggle-comment', function (event) {
17  var $anchor = $(this);
18  var comment_guid = $anchor.data().loadComment;
19 
20  require(['elgg/Ajax', 'elgg/toggle'], function(Ajax) {
21  var ajax = new Ajax();
22 
23  ajax.form('comment/save', {
24  data: {
25  guid: comment_guid
26  },
27  success: function(result) {
28  $('div[data-comments-placeholder=' + comment_guid + ']')
29  .html(result)
30  .slideToggle('medium')
31  .find('textarea, [contenteditable]').filter(':visible').first().focus();
32 
33  $anchor.toggleClass('elgg-toggle elgg-toggle-comment');
34  }
35  });
36  });
37  });
38 
39  $(document).on('submit', '.elgg-form-comment-save', function (event) {
40  var $form = $(this);
41 
42  $form.find('.elgg-button-submit').prop('disabled', true);
43 
44  require(['elgg/Ajax'], function(Ajax) {
45  var ajax = new Ajax();
46 
47  ajax.action($form.attr('action'), {
48  data: ajax.objectify($form),
49  success: function(result) {
50  var $container = $form.closest('.elgg-comments');
51  var view_name = 'page/elements/comments';
52  var comment_guid = result.guid;
53  var data = {
54  guid: $form.find('input[name="entity_guid"]').val(),
55  id: $form.attr('id'),
56  show_guid: comment_guid,
57  inline: $form.find('.elgg-input-text').length
58  };
59 
60  if (!$container.length) {
61  $container = $form.closest('.elgg-river-responses');
62  view_name = 'river/elements/responses';
63  data.river_id = $container.closest('.elgg-river-item').parent().attr('id').replace('item-river-', '');
64  }
65 
66  if (!$container.length) {
67  $form.find('.elgg-button-submit').prop('disabled', true);
68  return;
69  }
70 
71  // the pagination returned will have a non-functional link that points to the current URL,
72  // but we want the the link to reload the page.
73  function fix_pagination($container) {
74  function normalize(url) {
75  return url.replace(/#.*/, '');
76  }
77 
78  var base_url = normalize(location.href);
79 
80  $container.find('.elgg-pagination a').each(function () {
81  if (normalize(this.href) === base_url) {
82  $(this).on('click', function () {
83  location.reload();
84  });
85  }
86  });
87  }
88 
89  ajax.view(view_name, {
90  data: data,
91  success: function(result) {
92  if (view_name === 'river/elements/responses') {
93  $container.html(result);
94  } else {
95  $container.html($(result).filter('.elgg-comments').html());
96  }
97 
98  var $comment = $container.find('#elgg-object-' + comment_guid);
99  $comment.addClass('elgg-state-highlight');
100 
101  $comment[0].scrollIntoView({behavior: 'smooth'});
102 
103  fix_pagination($container);
104  },
105  error: function() {
106  $form.find('.elgg-button-submit').prop('disabled', false);
107  }
108  });
109  },
110  error: function () {
111  $form.find('.elgg-button-submit').prop('disabled', false);
112  }
113  });
114  });
115 
116  event.preventDefault();
117  event.stopPropagation();
118  });
119 
120 
121  $(document).on('click', '.elgg-menu-item-edit > a', function () {
122  var $trigger = $(this).closest('.elgg-menu-hover').data('trigger');
123  if ((typeof $trigger === 'undefined') || !$trigger.is('.elgg-item-object-comment a')) {
124  return;
125  }
126 
127  // store object as data in the edit link
128  var dc = $(this).data('Comment');
129  if (!dc) {
130  var guid = $(this).data().commentGuid;
131  dc = new Comment(guid, $trigger.closest('.elgg-item-object-comment'));
132  $(this).data('Comment', dc);
133  }
134 
135  dc.toggleEdit();
136 
137  require(['elgg/popup'], function(popup) {
138  popup.close();
139  });
140 
141  return false;
142  });
143 
144  function Comment(guid, item) {
145  this.guid = guid;
146  this.$item = item;
147  }
148 
149  Comment.prototype = {
155  getForm: function () {
156  return this.$item.find('#edit-comment-' + this.guid);
157  },
158 
159  hideForm: function () {
160  this.getForm().toggleClass('hidden');
161  this.getForm().prev().toggleClass('hidden');
162  },
163 
164  showForm: function () {
165  this.getForm().toggleClass('hidden');
166  this.getForm().prev().toggleClass('hidden');
167 
168  this.getForm().find('textarea, [contenteditable]').filter(':visible').first().focus();
169  },
170 
171  loadForm: function () {
172  var that = this;
173 
174  require(['elgg/Ajax'], function(Ajax) {
175  var ajax = new Ajax();
176 
177  // Get the form using ajax
178  ajax.view('core/ajax/edit_comment?guid=' + that.guid, {
179  success: function(html) {
180  // Add the form to DOM
181  that.$item.find('.elgg-body').first().append(html);
182 
183  that.showForm();
184 
185  var $form = that.getForm();
186 
187  $form.find('.elgg-button-cancel').on('click', function () {
188  that.hideForm();
189  return false;
190  });
191 
192  // save
193  $form.on('submit', function () {
194  that.submitForm();
195  return false;
196  });
197  }
198  });
199  });
200  },
201 
202  submitForm: function () {
203  var $form = this.getForm();
204  $form.find('.elgg-button-submit').prop('disabled', true);
205 
206  require(['elgg/Ajax'], function(Ajax) {
207  var ajax = new Ajax();
208 
209  ajax.action($form.attr('action'), {
210  data: ajax.objectify($form),
211  success: function(result) {
212  if (result.output) {
213  // Update list item content
214  $form.closest('.elgg-item-object-comment').html(result.output);
215  }
216  },
217  error: function() {
218  $form.find('.elgg-button-submit').prop('disabled', false);
219  }
220  });
221  });
222 
223  return false;
224  },
225 
226  toggleEdit: function () {
227  var $form = this.getForm();
228  if ($form.length) {
229  if ($form.hasClass('hidden')) {
230  this.showForm();
231  } else {
232  this.hideForm();
233  }
234  } else {
235  this.loadForm();
236  }
237 
238  return false;
239  }
240  };
241 });
if(!$items) $item
Definition: delete.php:13
if(!$widget instanceof\ElggWidget) if(!$widget->canEdit()) $form
Definition: edit.php:19
$result error
$container
Definition: delete.php:24
function filter(array, term)
if(!$entity instanceof\ElggEntity) if(!$entity->canComment()) $comment
Definition: save.php:42
define(['jquery', 'elgg'], function($, elgg){$(document).on('elgg_ui_toggle', function(e, data){var $toggle=$(e.target);var $elements=data.$toggled_elements;if($elements.is('.elgg-river-responses >.elgg-form-comment-save')){if($toggle.hasClass('elgg-state-active')){$elements.find('.elgg-input-text').focus();}else{$toggle.blur();}}});$(document).on('click', '.elgg-toggle-comment', function(event){var $anchor=$(this);var comment_guid=$anchor.data().loadComment;require(['elgg/Ajax', 'elgg/toggle'], function(Ajax){var ajax=new Ajax();ajax.form('comment/save',{data:{guid:comment_guid}, success:function(result){$('div[data-comments-placeholder='+comment_guid+ ']').html(result).slideToggle('medium').find('textarea, [contenteditable]').filter(':visible').first().focus();$anchor.toggleClass('elgg-toggle elgg-toggle-comment');}});});});$(document).on('submit', '.elgg-form-comment-save', function(event){var $form=$(this);$form.find('.elgg-button-submit').prop('disabled', true);require(['elgg/Ajax'], function(Ajax){var ajax=new Ajax();ajax.action($form.attr('action'),{data:ajax.objectify($form), success:function(result){var $container=$form.closest('.elgg-comments');var view_name= 'page/elements/comments';var comment_guid=result.guid;var data={guid:$form.find('input[name="entity_guid"]').val(), id:$form.attr('id'), show_guid:comment_guid, inline:$form.find('.elgg-input-text').length};if(!$container.length){$container=$form.closest('.elgg-river-responses');view_name= 'river/elements/responses';data.river_id=$container.closest('.elgg-river-item').parent().attr('id').replace('item-river-', '');}if(!$container.length){$form.find('.elgg-button-submit').prop('disabled', true);return;}function fix_pagination($container){function normalize(url){return url.replace(/#.*/, '');}var base_url=normalize(location.href);$container.find('.elgg-pagination a').each(function(){if(normalize(this.href)===base_url){$(this).on('click', function(){location.reload();});}});}ajax.view(view_name,{data:data, success:function(result){if(view_name=== 'river/elements/responses'){$container.html(result);}else{$container.html($(result).filter('.elgg-comments').html());}var $comment=$container.find('#elgg-object-'+comment_guid);$comment.addClass('elgg-state-highlight');$comment[0].scrollIntoView({behavior: 'smooth'});fix_pagination($container);}, error:function(){$form.find('.elgg-button-submit').prop('disabled', false);}});}, error:function(){$form.find('.elgg-button-submit').prop('disabled', false);}});});event.preventDefault();event.stopPropagation();});$(document).on('click', '.elgg-menu-item-edit > a', function(){var $trigger=$(this).closest('.elgg-menu-hover').data('trigger');if((typeof $trigger=== 'undefined')||!$trigger.is('.elgg-item-object-comment a')){return;}var dc=$(this).data('Comment');if(!dc){var guid=$(this).data().commentGuid;dc=new Comment(guid, $trigger.closest('.elgg-item-object-comment'));$(this).data('Comment', dc);}dc.toggleEdit();require(['elgg/popup'], function(popup){popup.close();});return false;});function Comment(guid, item){this.guid=guid;this.$item=item;}Comment.prototype={getForm:function(){return this.$item.find('#edit-comment-'+this.guid);}, hideForm:function(){this.getForm().toggleClass('hidden');this.getForm().prev().toggleClass('hidden');}, showForm:function(){this.getForm().toggleClass('hidden');this.getForm().prev().toggleClass('hidden');this.getForm().find('textarea, [contenteditable]').filter(':visible').first().focus();}, loadForm:function(){var that=this;require(['elgg/Ajax'], function(Ajax){var ajax=new Ajax();ajax.view('core/ajax/edit_comment?guid='+that.guid,{success:function(html){that.$item.find('.elgg-body').first().append(html);that.showForm();var $form=that.getForm();$form.find('.elgg-button-cancel').on('click', function(){that.hideForm();return false;});$form.on('submit', function(){that.submitForm();return false;});}});});}, submitForm:function(){var $form=this.getForm();$form.find('.elgg-button-submit').prop('disabled', true);require(['elgg/Ajax'], function(Ajax){var ajax=new Ajax();ajax.action($form.attr('action'),{data:ajax.objectify($form), success:function(result){if(result.output){$form.closest('.elgg-item-object-comment').html(result.output);}}, error:function(){$form.find('.elgg-button-submit').prop('disabled', false);}});});return false;}, toggleEdit:function(){var $form=this.getForm();if($form.length){if($form.hasClass('hidden')){this.showForm();}else{this.hideForm();}}else{this.loadForm();}return false;}};})
Bundled plugins(the contents of the"/mod"directory) are available only under the GPLv2 license.The remainder of the project is available under either MIT or GPLv2.Both licenses can be found below.More info 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: LICENSE.txt:96
var elgg
Definition: elgglib.js:4