Commit ec829de9 authored by catch's avatar catch

Issue #1751320 by madhavvyas, nod_, sxnc, annikaC, rteijeiro: Selectors clean-up: ajax.js

parent 45033115
...@@ -58,8 +58,9 @@ ...@@ -58,8 +58,9 @@
// For anchor tags, these will go to the target of the anchor rather // For anchor tags, these will go to the target of the anchor rather
// than the usual location. // than the usual location.
if ($(this).attr('href')) { var href = $(this).attr('href');
element_settings.url = $(this).attr('href'); if (href) {
element_settings.url = href;
element_settings.event = 'click'; element_settings.event = 'click';
} }
element_settings.dialogType = $(this).data('dialog-type'); element_settings.dialogType = $(this).data('dialog-type');
...@@ -929,7 +930,7 @@ ...@@ -929,7 +930,7 @@
insert: function (ajax, response, status) { insert: function (ajax, response, status) {
// Get information from the response. If it is not there, default to // Get information from the response. If it is not there, default to
// our presets. // our presets.
var wrapper = response.selector ? $(response.selector) : $(ajax.wrapper); var $wrapper = response.selector ? $(response.selector) : $(ajax.wrapper);
var method = response.method || ajax.method; var method = response.method || ajax.method;
var effect = ajax.getEffect(response); var effect = ajax.getEffect(response);
var settings; var settings;
...@@ -939,11 +940,11 @@ ...@@ -939,11 +940,11 @@
// $(response.data) as new HTML rather than a CSS selector. Also, if // $(response.data) as new HTML rather than a CSS selector. Also, if
// response.data contains top-level text nodes, they get lost with either // response.data contains top-level text nodes, they get lost with either
// $(response.data) or $('<div></div>').replaceWith(response.data). // $(response.data) or $('<div></div>').replaceWith(response.data).
var new_content_wrapped = $('<div></div>').html(response.data); var $new_content_wrapped = $('<div></div>').html(response.data);
var new_content = new_content_wrapped.contents(); var $new_content = $new_content_wrapped.contents();
// For legacy reasons, the effects processing code assumes that // For legacy reasons, the effects processing code assumes that
// new_content consists of a single top-level element. Also, it has not // $new_content consists of a single top-level element. Also, it has not
// been sufficiently tested whether attachBehaviors() can be successfully // been sufficiently tested whether attachBehaviors() can be successfully
// called with a context object that includes top-level text nodes. // called with a context object that includes top-level text nodes.
// However, to give developers full control of the HTML appearing in the // However, to give developers full control of the HTML appearing in the
...@@ -953,8 +954,8 @@ ...@@ -953,8 +954,8 @@
// top-level element, and only use the container DIV created above when // top-level element, and only use the container DIV created above when
// it doesn't. For more information, please see // it doesn't. For more information, please see
// https://www.drupal.org/node/736066. // https://www.drupal.org/node/736066.
if (new_content.length !== 1 || new_content.get(0).nodeType !== 1) { if ($new_content.length !== 1 || $new_content.get(0).nodeType !== 1) {
new_content = new_content_wrapped; $new_content = $new_content_wrapped;
} }
// If removing content from the wrapper, detach behaviors first. // If removing content from the wrapper, detach behaviors first.
...@@ -965,35 +966,35 @@ ...@@ -965,35 +966,35 @@
case 'empty': case 'empty':
case 'remove': case 'remove':
settings = response.settings || ajax.settings || drupalSettings; settings = response.settings || ajax.settings || drupalSettings;
Drupal.detachBehaviors(wrapper.get(0), settings); Drupal.detachBehaviors($wrapper.get(0), settings);
} }
// Add the new content to the page. // Add the new content to the page.
wrapper[method](new_content); $wrapper[method]($new_content);
// Immediately hide the new content if we're using any effects. // Immediately hide the new content if we're using any effects.
if (effect.showEffect !== 'show') { if (effect.showEffect !== 'show') {
new_content.hide(); $new_content.hide();
} }
// Determine which effect to use and what content will receive the // Determine which effect to use and what content will receive the
// effect, then show the new content. // effect, then show the new content.
if (new_content.find('.ajax-new-content').length > 0) { if ($new_content.find('.ajax-new-content').length > 0) {
new_content.find('.ajax-new-content').hide(); $new_content.find('.ajax-new-content').hide();
new_content.show(); $new_content.show();
new_content.find('.ajax-new-content')[effect.showEffect](effect.showSpeed); $new_content.find('.ajax-new-content')[effect.showEffect](effect.showSpeed);
} }
else if (effect.showEffect !== 'show') { else if (effect.showEffect !== 'show') {
new_content[effect.showEffect](effect.showSpeed); $new_content[effect.showEffect](effect.showSpeed);
} }
// Attach all JavaScript behaviors to the new content, if it was // Attach all JavaScript behaviors to the new content, if it was
// successfully added to the page, this if statement allows // successfully added to the page, this if statement allows
// `#ajax['wrapper']` to be optional. // `#ajax['wrapper']` to be optional.
if (new_content.parents('html').length > 0) { if ($new_content.parents('html').length > 0) {
// Apply any settings from the returned JSON if available. // Apply any settings from the returned JSON if available.
settings = response.settings || ajax.settings || drupalSettings; settings = response.settings || ajax.settings || drupalSettings;
Drupal.attachBehaviors(new_content.get(0), settings); Drupal.attachBehaviors($new_content.get(0), settings);
} }
}, },
...@@ -1024,10 +1025,11 @@ ...@@ -1024,10 +1025,11 @@
* @param {number} [status] * @param {number} [status]
*/ */
changed: function (ajax, response, status) { changed: function (ajax, response, status) {
if (!$(response.selector).hasClass('ajax-changed')) { var $element = $(response.selector);
$(response.selector).addClass('ajax-changed'); if (!$element.hasClass('ajax-changed')) {
$element.addClass('ajax-changed');
if (response.asterisk) { if (response.asterisk) {
$(response.selector).find(response.asterisk).append(' <abbr class="ajax-changed" title="' + Drupal.t('Changed') + '">*</abbr> '); $element.find(response.asterisk).append(' <abbr class="ajax-changed" title="' + Drupal.t('Changed') + '">*</abbr> ');
} }
} }
}, },
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment