97 lines
2.6 KiB
JavaScript
97 lines
2.6 KiB
JavaScript
/**
|
|
* @file
|
|
* Extends the Drupal AJAX functionality to integrate the dialog API.
|
|
*/
|
|
|
|
(function ($, Drupal) {
|
|
|
|
"use strict";
|
|
|
|
Drupal.behaviors.dialog = {
|
|
attach: function () {
|
|
// Provide a known 'drupal-modal' DOM element for Drupal-based modal
|
|
// dialogs. Non-modal dialogs are responsible for creating their own
|
|
// elements, since there can be multiple non-modal dialogs at a time.
|
|
if (!$('#drupal-modal').length) {
|
|
$('<div id="drupal-modal" />').hide().appendTo('body');
|
|
}
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Command to open a dialog.
|
|
*/
|
|
Drupal.ajax.prototype.commands.openDialog = function (ajax, response, status) {
|
|
if (!response.selector) {
|
|
return false;
|
|
}
|
|
var $dialog = $(response.selector);
|
|
if (!$dialog.length) {
|
|
// Create the element if needed.
|
|
$dialog = $('<div id="' + response.selector.replace(/^#/, '') + '"/>').appendTo('body');
|
|
}
|
|
// Set up the wrapper, if there isn't one.
|
|
if (!ajax.wrapper) {
|
|
ajax.wrapper = $dialog.attr('id');
|
|
}
|
|
|
|
// Use the ajax.js insert command to populate the dialog contents.
|
|
response.command = 'insert';
|
|
response.method = 'html';
|
|
ajax.commands.insert(ajax, response, status);
|
|
|
|
// Open the dialog itself.
|
|
response.dialogOptions = response.dialogOptions || {};
|
|
var dialog = Drupal.dialog($dialog, response.dialogOptions);
|
|
if (response.dialogOptions.modal) {
|
|
dialog.showModal();
|
|
}
|
|
else {
|
|
dialog.show();
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Command to close a dialog.
|
|
*
|
|
* If no selector is given, it defaults to trying to close the modal.
|
|
*/
|
|
Drupal.ajax.prototype.commands.closeDialog = function (ajax, response, status) {
|
|
var $dialog = $(response.selector);
|
|
if ($dialog.length) {
|
|
Drupal.dialog($dialog).close();
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Command to set a dialog property.
|
|
*
|
|
* jQuery UI specific way of setting dialog options.
|
|
*/
|
|
Drupal.ajax.prototype.commands.setDialogOption = function (ajax, response, status) {
|
|
var $dialog = $(response.selector);
|
|
if ($dialog.length) {
|
|
$dialog.dialog('option', response.optionName, response.optionValue);
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Binds a listener on dialog creation to handle the cancel link.
|
|
*/
|
|
$(window).on('dialog:aftercreate', function (e, dialog, $element, settings) {
|
|
$element.on('click.dialog', '.dialog-cancel', function (e) {
|
|
dialog.close('cancel');
|
|
e.preventDefault();
|
|
e.stopPropagation();
|
|
});
|
|
});
|
|
|
|
/**
|
|
* Removes all 'dialog' listeners.
|
|
*/
|
|
$(window).on('dialog:beforeclose', function (e, dialog, $element) {
|
|
$element.off('.dialog');
|
|
});
|
|
|
|
})(jQuery, Drupal);
|