Commit 4c49e1e5 authored by alexpott's avatar alexpott

Issue #1533366 by nod_, valthebald, Jelle_S, Wim Leers: Simplify and optimize...

Issue #1533366 by nod_, valthebald, Jelle_S, Wim Leers: Simplify and optimize Drupal.ajax() instantiation and implementation
parent 7ec2ab57
This diff is collapsed.
...@@ -158,23 +158,20 @@ ...@@ -158,23 +158,20 @@
// Add a "Loading…" message, hide it underneath the CKEditor toolbar, create // Add a "Loading…" message, hide it underneath the CKEditor toolbar, create
// a Drupal.ajax instance to load the dialog and trigger it. // a Drupal.ajax instance to load the dialog and trigger it.
var $content = $('<div class="ckeditor-dialog-loading"><span style="top: -40px;" class="ckeditor-dialog-loading-link"><a>' + Drupal.t('Loading...') + '</a></span></div>'); var $content = $('<div class="ckeditor-dialog-loading"><span style="top: -40px;" class="ckeditor-dialog-loading-link">' + Drupal.t('Loading...') + '</span></div>');
$content.appendTo($target); $content.appendTo($target);
new Drupal.ajax('ckeditor-dialog', $content.find('a').get(0), { var ckeditorAjaxDialog = Drupal.ajax({
dialog: dialogSettings, dialog: dialogSettings,
dialogType: 'modal', dialogType: 'modal',
selector: '.ckeditor-dialog-loading-link', selector: '.ckeditor-dialog-loading-link',
url: url, url: url,
event: 'ckeditor-internal.ckeditor',
progress: {'type': 'throbber'}, progress: {'type': 'throbber'},
submit: { submit: {
editor_object: existingValues editor_object: existingValues
} }
}); });
$content.find('a') ckeditorAjaxDialog.execute();
.on('click', function () { return false; })
.trigger('ckeditor-internal.ckeditor');
// After a short delay, show "Loading…" message. // After a short delay, show "Loading…" message.
window.setTimeout(function () { window.setTimeout(function () {
......
...@@ -171,11 +171,9 @@ ...@@ -171,11 +171,9 @@
var fieldID = this.fieldModel.get('fieldID'); var fieldID = this.fieldModel.get('fieldID');
// Create a Drupal.ajax instance to load the form. // Create a Drupal.ajax instance to load the form.
var textLoaderAjax = new Drupal.ajax(fieldID, this.$el, { var textLoaderAjax = Drupal.ajax({
url: Drupal.quickedit.util.buildUrl(fieldID, Drupal.url('editor/!entity_type/!id/!field_name/!langcode/!view_mode')), url: Drupal.quickedit.util.buildUrl(fieldID, Drupal.url('editor/!entity_type/!id/!field_name/!langcode/!view_mode')),
event: 'editor-internal.editor', submit: {nocssjs: true}
submit: {nocssjs: true},
progress: {type: null} // No progress indicator.
}); });
// Implement a scoped editorGetUntransformedText AJAX command: calls the // Implement a scoped editorGetUntransformedText AJAX command: calls the
...@@ -186,7 +184,7 @@ ...@@ -186,7 +184,7 @@
// This will ensure our scoped editorGetUntransformedText AJAX command // This will ensure our scoped editorGetUntransformedText AJAX command
// gets called. // gets called.
this.$el.trigger('editor-internal.editor'); textLoaderAjax.execute();
} }
}); });
......
...@@ -362,18 +362,10 @@ ...@@ -362,18 +362,10 @@
save: function (options) { save: function (options) {
var entityModel = this; var entityModel = this;
// @todo Simplify this once https://drupal.org/node/1533366 lands.
// @see https://drupal.org/node/2029999.
var id = 'quickedit-save-entity';
// Create a temporary element to be able to use Drupal.ajax.
var $el = $('#quickedit-entity-toolbar').find('.action-save'); // This is the span element inside the button.
// Create a Drupal.ajax instance to save the entity. // Create a Drupal.ajax instance to save the entity.
var entitySaverAjax = new Drupal.ajax(id, $el, { var entitySaverAjax = Drupal.ajax({
url: Drupal.url('quickedit/entity/' + entityModel.get('entityID')), url: Drupal.url('quickedit/entity/' + entityModel.get('entityID')),
event: 'quickedit-save.quickedit',
progress: {type: 'none'},
error: function () { error: function () {
$el.off('quickedit-save.quickedit');
// Let the Drupal.quickedit.EntityModel Backbone model's error()= // Let the Drupal.quickedit.EntityModel Backbone model's error()=
// method handle errors. // method handle errors.
options.error.call(entityModel); options.error.call(entityModel);
...@@ -381,8 +373,6 @@ ...@@ -381,8 +373,6 @@
}); });
// Entity saved successfully. // Entity saved successfully.
entitySaverAjax.commands.quickeditEntitySaved = function (ajax, response, status) { entitySaverAjax.commands.quickeditEntitySaved = function (ajax, response, status) {
// Clean up.
$(ajax.element).off('quickedit-save.quickedit');
// All fields have been moved from PrivateTempStore to permanent // All fields have been moved from PrivateTempStore to permanent
// storage, update the "inTempStore" attribute on FieldModels, on the // storage, update the "inTempStore" attribute on FieldModels, on the
// EntityModel and clear EntityModel's "fieldInTempStore" attribute. // EntityModel and clear EntityModel's "fieldInTempStore" attribute.
...@@ -399,7 +389,7 @@ ...@@ -399,7 +389,7 @@
}; };
// Trigger the AJAX request, which will will return the // Trigger the AJAX request, which will will return the
// quickeditEntitySaved AJAX command to which we then react. // quickeditEntitySaved AJAX command to which we then react.
$el.trigger('quickedit-save.quickedit'); entitySaverAjax.execute();
}, },
/** /**
......
...@@ -408,18 +408,11 @@ ...@@ -408,18 +408,11 @@
return; return;
} }
// @todo Simplify this once https://drupal.org/node/1533366 lands.
// @see https://drupal.org/node/2029999. // @see https://drupal.org/node/2029999.
var id = 'quickedit-load-editors'; // Create a Drupal.Ajax instance to load the form.
// Create a temporary element to be able to use Drupal.ajax. var loadEditorsAjax = Drupal.ajax({
var $el = $('<div id="' + id + '" class="hidden"></div>').appendTo('body');
// Create a Drupal.ajax instance to load the form.
var loadEditorsAjax = new Drupal.ajax(id, $el, {
url: Drupal.url('quickedit/attachments'), url: Drupal.url('quickedit/attachments'),
event: 'quickedit-internal.quickedit', submit: {'editors[]': missingEditors}
submit: {'editors[]': missingEditors},
// No progress indicator.
progress: {type: null}
}); });
// Implement a scoped insert AJAX command: calls the callback after all AJAX // Implement a scoped insert AJAX command: calls the callback after all AJAX
// command functions have been executed (hence the deferred calling). // command functions have been executed (hence the deferred calling).
...@@ -427,12 +420,10 @@ ...@@ -427,12 +420,10 @@
loadEditorsAjax.commands.insert = function (ajax, response, status) { loadEditorsAjax.commands.insert = function (ajax, response, status) {
_.defer(callback); _.defer(callback);
realInsert(ajax, response, status); realInsert(ajax, response, status);
$el.off('quickedit-internal.quickedit');
$el.remove();
}; };
// Trigger the AJAX request, which will should return AJAX commands to insert // Trigger the AJAX request, which will should return AJAX commands to insert
// any missing attachments. // any missing attachments.
$el.trigger('quickedit-internal.quickedit'); loadEditorsAjax.execute();
} }
/** /**
......
...@@ -89,21 +89,16 @@ ...@@ -89,21 +89,16 @@
* commands. * commands.
*/ */
load: function (options, callback) { load: function (options, callback) {
var $el = options.$el;
var fieldID = options.fieldID; var fieldID = options.fieldID;
// Create a Drupal.ajax instance to load the form. // Create a Drupal.ajax instance to load the form.
var formLoaderAjax = new Drupal.ajax(fieldID, $el, { var formLoaderAjax = Drupal.ajax({
url: Drupal.quickedit.util.buildUrl(fieldID, Drupal.url('quickedit/form/!entity_type/!id/!field_name/!langcode/!view_mode')), url: Drupal.quickedit.util.buildUrl(fieldID, Drupal.url('quickedit/form/!entity_type/!id/!field_name/!langcode/!view_mode')),
event: 'quickedit-internal.quickedit',
submit: { submit: {
nocssjs: options.nocssjs, nocssjs: options.nocssjs,
reset: options.reset reset: options.reset
}, },
progress: {type: null}, // No progress indicator.
error: function (xhr, url) { error: function (xhr, url) {
$el.off('quickedit-internal.quickedit');
// Show a modal to inform the user of the network error. // Show a modal to inform the user of the network error.
var fieldLabel = Drupal.quickedit.metadata.get(fieldID, 'label'); var fieldLabel = Drupal.quickedit.metadata.get(fieldID, 'label');
var message = Drupal.t('Could not load the form for <q>@field-label</q>, either due to a website problem or a network connection problem.<br>Please try again.', {'@field-label': fieldLabel}); var message = Drupal.t('Could not load the form for <q>@field-label</q>, either due to a website problem or a network connection problem.<br>Please try again.', {'@field-label': fieldLabel});
...@@ -118,11 +113,10 @@ ...@@ -118,11 +113,10 @@
// Implement a scoped quickeditFieldForm AJAX command: calls the callback. // Implement a scoped quickeditFieldForm AJAX command: calls the callback.
formLoaderAjax.commands.quickeditFieldForm = function (ajax, response, status) { formLoaderAjax.commands.quickeditFieldForm = function (ajax, response, status) {
callback(response.data, ajax); callback(response.data, ajax);
$el.off('quickedit-internal.quickedit'); Drupal.ajax.instances[this.instanceIndex] = null;
formLoaderAjax = null;
}; };
// This will ensure our scoped quickeditFieldForm AJAX command gets called. // This will ensure our scoped quickeditFieldForm AJAX command gets called.
$el.trigger('quickedit-internal.quickedit'); formLoaderAjax.execute();
}, },
/** /**
...@@ -143,7 +137,7 @@ ...@@ -143,7 +137,7 @@
url: $submit.closest('form').attr('action'), url: $submit.closest('form').attr('action'),
setClick: true, setClick: true,
event: 'click.quickedit', event: 'click.quickedit',
progress: {type: null}, progress: false,
submit: { submit: {
nocssjs: options.nocssjs, nocssjs: options.nocssjs,
other_view_modes: options.other_view_modes other_view_modes: options.other_view_modes
...@@ -156,10 +150,12 @@ ...@@ -156,10 +150,12 @@
this.commands[response[i].command](this, response[i], status); this.commands[response[i].command](this, response[i], status);
} }
} }
} },
base: $submit.attr('id'),
element: $submit[0]
}; };
return new Drupal.ajax($submit.attr('id'), $submit[0], settings); return Drupal.ajax(settings);
}, },
/** /**
......
...@@ -78,16 +78,23 @@ ...@@ -78,16 +78,23 @@
// @code // @code
// jQuery('.view-name').trigger('RefreshView'); // jQuery('.view-name').trigger('RefreshView');
// @endcode // @endcode
var self_settings = this.element_settings; var self_settings = $.extend({}, this.element_settings, {
self_settings.event = 'RefreshView'; event: 'RefreshView',
this.refreshViewAjax = new Drupal.ajax(this.selector, this.$view, self_settings); base: this.selector,
element: this.$view
});
this.refreshViewAjax = Drupal.ajax(self_settings);
}; };
Drupal.views.ajaxView.prototype.attachExposedFormAjax = function () { Drupal.views.ajaxView.prototype.attachExposedFormAjax = function () {
var button = $('input[type=submit], input[type=image]', this.$exposed_form); var button = $('input[type=submit], input[type=image]', this.$exposed_form);
button = button[0]; button = button[0];
this.exposedFormAjax = new Drupal.ajax($(button).attr('id'), button, this.element_settings); var self_settings = $.extend({}, this.element_settings, {
base: $(button).attr('id'),
element: button
});
this.exposedFormAjax = Drupal.ajax(self_settings);
}; };
Drupal.views.ajaxView.prototype.filterNestedViews = function () { Drupal.views.ajaxView.prototype.filterNestedViews = function () {
...@@ -121,8 +128,12 @@ ...@@ -121,8 +128,12 @@
Drupal.Views.parseViewArgs(href, this.settings.view_base_path) Drupal.Views.parseViewArgs(href, this.settings.view_base_path)
); );
this.element_settings.submit = viewData; var self_settings = $.extend({}, this.element_settings, {
this.pagerAjax = new Drupal.ajax(false, $link, this.element_settings); submit: viewData,
base: false,
element: $link
});
this.pagerAjax = Drupal.ajax(self_settings);
}; };
Drupal.AjaxCommands.prototype.viewsScrollTop = function (ajax, response) { Drupal.AjaxCommands.prototype.viewsScrollTop = function (ajax, response) {
......
...@@ -84,12 +84,14 @@ ...@@ -84,12 +84,14 @@
// Bind AJAX behaviors to all items showing the class. // Bind AJAX behaviors to all items showing the class.
$('a.views-ajax-link', context).once('views-ajax').each(function () { $('a.views-ajax-link', context).once('views-ajax').each(function () {
var element_settings = base_element_settings; var element_settings = base_element_settings;
element_settings.base = base;
element_settings.element = this;
// Set the URL to go to the anchor. // Set the URL to go to the anchor.
if ($(this).attr('href')) { if ($(this).attr('href')) {
element_settings.url = $(this).attr('href'); element_settings.url = $(this).attr('href');
} }
var base = $(this).attr('id'); var base = $(this).attr('id');
Drupal.ajax[base] = new Drupal.ajax(base, this, element_settings); Drupal.ajax(element_settings);
}); });
$('div#views-live-preview a') $('div#views-live-preview a')
...@@ -108,8 +110,10 @@ ...@@ -108,8 +110,10 @@
element_settings.wrapper = 'views-preview-wrapper'; element_settings.wrapper = 'views-preview-wrapper';
element_settings.method = 'replaceWith'; element_settings.method = 'replaceWith';
element_settings.base = base;
element_settings.element = this;
var base = $(this).attr('id'); var base = $(this).attr('id');
Drupal.ajax[base] = new Drupal.ajax(base, this, element_settings); Drupal.ajax(element_settings);
}); });
// Within a live preview, make exposed widget form buttons re-trigger the // Within a live preview, make exposed widget form buttons re-trigger the
...@@ -132,9 +136,11 @@ ...@@ -132,9 +136,11 @@
element_settings.wrapper = 'views-preview-wrapper'; element_settings.wrapper = 'views-preview-wrapper';
element_settings.method = 'replaceWith'; element_settings.method = 'replaceWith';
element_settings.event = 'click'; element_settings.event = 'click';
element_settings.base = base;
element_settings.element = this;
var base = $(this).attr('id'); var base = $(this).attr('id');
Drupal.ajax[base] = new Drupal.ajax(base, this, element_settings); Drupal.ajax(element_settings);
}); });
} }
......
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