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('submit', '.elgg-form-comment-save', function (event) {
17  var $form = $(this);
18 
19  $form.find('.elgg-button-submit').prop('disabled', true);
20 
21  require(['elgg/Ajax'], function(Ajax) {
22  var ajax = new Ajax();
23 
24  ajax.action($form.attr('action'), {
25  data: ajax.objectify($form),
26  success: function(result) {
27  var $container = $form.closest('.elgg-comments');
28  var view_name = 'page/elements/comments';
29  var comment_guid = result.guid;
30  var data = {
31  guid: $form.find('input[name="entity_guid"]').val(),
32  id: $form.attr('id'),
33  show_guid: comment_guid,
34  inline: $form.find('.elgg-input-text').length
35  };
36 
37  if (!$container.length) {
38  $container = $form.closest('.elgg-river-responses');
39  view_name = 'river/elements/responses';
40  data.river_id = $container.closest('.elgg-river-item').parent().attr('id').replace('item-river-', '');
41  }
42 
43  if (!$container.length) {
44  $form.find('.elgg-button-submit').prop('disabled', true);
45  return;
46  }
47 
48  // the pagination returned will have a non-functional link that points to the current URL,
49  // but we want the the link to reload the page.
50  function fix_pagination($container) {
51  function normalize(url) {
52  return url.replace(/#.*/, '');
53  }
54 
55  var base_url = normalize(location.href);
56 
57  $container.find('.elgg-pagination a').each(function () {
58  if (normalize(this.href) === base_url) {
59  $(this).on('click', function () {
60  location.reload();
61  });
62  }
63  });
64  }
65 
66  ajax.view(view_name, {
67  data: data,
68  success: function(result) {
69  if (view_name === 'river/elements/responses') {
70  $container.html(result);
71  } else {
72  $container.html($(result).filter('.elgg-comments').html());
73  }
74 
75  var $comment = $container.find('#elgg-object-' + comment_guid);
76  $comment.addClass('elgg-state-highlight');
77 
78  $comment[0].scrollIntoView({behavior: 'smooth'});
79 
80  fix_pagination($container);
81  },
82  error: function() {
83  $form.find('.elgg-button-submit').prop('disabled', false);
84  }
85  });
86  },
87  error: function () {
88  $form.find('.elgg-button-submit').prop('disabled', false);
89  }
90  });
91  });
92 
93  event.preventDefault();
94  event.stopPropagation();
95  });
96 
97 
98  $(document).on('click', '.elgg-menu-item-edit > a', function () {
99  var $trigger = $(this).closest('.elgg-menu-hover').data('trigger');
100  if ((typeof $trigger === 'undefined') || !$trigger.is('.elgg-item-object-comment a')) {
101  return;
102  }
103 
104  // store object as data in the edit link
105  var dc = $(this).data('Comment');
106  if (!dc) {
107  var guid = $(this).data().commentGuid;
108  dc = new Comment(guid, $trigger.closest('.elgg-item-object-comment'));
109  $(this).data('Comment', dc);
110  }
111 
112  dc.toggleEdit();
113 
114  require(['elgg/popup'], function(popup) {
115  popup.close();
116  });
117 
118  return false;
119  });
120 
121  function Comment(guid, item) {
122  this.guid = guid;
123  this.$item = item;
124  }
125 
126  Comment.prototype = {
132  getForm: function () {
133  return this.$item.find('#edit-comment-' + this.guid);
134  },
135 
136  hideForm: function () {
137  this.getForm().toggleClass('hidden');
138  this.getForm().prev().toggleClass('hidden');
139  },
140 
141  showForm: function () {
142  this.getForm().toggleClass('hidden');
143  this.getForm().prev().toggleClass('hidden');
144 
145  this.getForm().find('textarea, [contenteditable]').filter(':visible').first().focus();
146  },
147 
148  loadForm: function () {
149  var that = this;
150 
151  require(['elgg/Ajax'], function(Ajax) {
152  var ajax = new Ajax();
153 
154  // Get the form using ajax
155  ajax.view('core/ajax/edit_comment?guid=' + that.guid, {
156  success: function(html) {
157  // Add the form to DOM
158  that.$item.find('.elgg-body').first().append(html);
159 
160  that.showForm();
161 
162  var $form = that.getForm();
163 
164  $form.find('.elgg-button-cancel').on('click', function () {
165  that.hideForm();
166  return false;
167  });
168 
169  // save
170  $form.on('submit', function () {
171  that.submitForm();
172  return false;
173  });
174  }
175  });
176  });
177  },
178 
179  submitForm: function () {
180  var $form = this.getForm();
181  $form.find('.elgg-button-submit').prop('disabled', true);
182 
183  require(['elgg/Ajax'], function(Ajax) {
184  var ajax = new Ajax();
185 
186  ajax.action($form.attr('action'), {
187  data: ajax.objectify($form),
188  success: function(result) {
189  if (result.output) {
190  // Update list item content
191  $form.closest('.elgg-item-object-comment').html(result.output);
192  }
193  },
194  error: function() {
195  $form.find('.elgg-button-submit').prop('disabled', false);
196  }
197  });
198  });
199 
200  return false;
201  },
202 
203  toggleEdit: function () {
204  var $form = this.getForm();
205  if ($form.length) {
206  if ($form.hasClass('hidden')) {
207  this.showForm();
208  } else {
209  this.hideForm();
210  }
211  } else {
212  this.loadForm();
213  }
214 
215  return false;
216  }
217  };
218 });
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
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
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('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;}};})