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