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