diff --git a/core/misc/dialog.ajax.js b/core/misc/dialog.ajax.js index d76baf8..a30e0d9 100644 --- a/core/misc/dialog.ajax.js +++ b/core/misc/dialog.ajax.js @@ -4,8 +4,20 @@ */ (function ($, Drupal) { + "use strict"; + Drupal.behaviors.dialog = { + attach: function () { + // Provide a known 'drupal-modal' dom element for Drupal code to use for + // modal dialogs. Since there can be multiple non-modal dialogs at a time, + // it is the responsibility of calling code to create the elements it needs. + if (!$('#drupal-modal').length) { + $('
').hide().appendTo('body'); + } + } + }; + /** * Command to open a dialog. */ @@ -23,15 +35,20 @@ ajax.wrapper = $dialog.attr('id'); } - // Open the dialog itself. - response.dialogOptions = response.dialogOptions || {}; - var dialog = new Drupal.dialog($dialog, response.dialogOptions); - dialog.open(); - // 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(); + } }; /** @@ -42,22 +59,38 @@ Drupal.ajax.prototype.commands.closeDialog = function (ajax, response, status) { var $dialog = $(response.selector); if ($dialog.length) { - var dialog = new Drupal.dialog($dialog); - dialog.close(); + Drupal.dialog($dialog).close(); } }; /** * Command to set a dialog property. * - * If no selector is given, it defaults to setting the modal properties. + * jQuery UI specific way of setting dialog options. */ Drupal.ajax.prototype.commands.setDialogOption = function (ajax, response, status) { var $dialog = $(response.selector); if ($dialog.length) { - var dialog = new Drupal.dialog($dialog); - dialog.element.dialog('option', response.optionName, response.optionValue); + $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); diff --git a/core/misc/dialog.js b/core/misc/dialog.js index 1ddc93c..9f6f29c 100644 --- a/core/misc/dialog.js +++ b/core/misc/dialog.js @@ -10,64 +10,46 @@ drupalSettings.dialog = { autoOpen: true, - dialogClass: '' -}; - -Drupal.behaviors.dialog = { - attach: function () { - // Provide a known 'drupal-modal' dom element for Drupal code to use for - // modal dialogs. Since there can be multiple non-modal dialogs at a time, - // it is the responsibility of calling code to create the elements it needs. - if (!$('#drupal-modal').length) { - $('
').hide().appendTo('body'); - } + dialogClass: '', + close: function (e) { + Drupal.detachBehaviors(e.target, null, 'unload'); } }; Drupal.dialog = function (element, options) { - // Set a default for options, making it optional. - options = options ? options : {}; - - this.element = $(element); - this.isOpen = false; - this.returnValue = undefined; - this.options = $.extend({}, drupalSettings.dialog, options); -}; -Drupal.dialog.prototype.open = function (options) { - options = $.extend({}, drupalSettings.dialog, this.options, options); + function openDialog (settings) { + settings = $.extend({}, drupalSettings.dialog, options, settings); // Trigger a global event to allow scripts to bind events to the dialog. - $(window).trigger('dialog:beforecreate', [this, options]); - this.element.dialog(options); - this.isOpen = true; - $(window).trigger('dialog:aftercreate', [this, options]); -} + $(window).trigger('dialog:beforecreate', [dialog, $element, settings]); + $element.dialog(settings); + dialog.open = true; + $(window).trigger('dialog:aftercreate', [dialog, $element, settings]); + } -Drupal.dialog.prototype.close = function (value) { - $(window).trigger('dialog:beforeclose', [this]); - Drupal.detachBehaviors(this.element[0], null, 'unload'); - this.element.dialog('close'); - this.returnValue = value; - this.isOpen = false; - $(window).trigger('dialog:afterclose', [this]); -} + function closeDialog (value) { + $(window).trigger('dialog:beforeclose', [dialog, $element]); + $element.dialog('close'); + dialog.returnValue = value; + dialog.open = false; + $(window).trigger('dialog:afterclose', [dialog, $element]); + } -/** - * Binds a listener on dialog creation to handle the cancel link. - */ -$(window).on('dialog:aftercreate', function (e, dialog, settings) { - dialog.element.on('click.dialog', '.dialog-cancel', function (e) { - dialog.close('cancel'); - e.preventDefault(); - e.stopPropagation(); - }); -}); + var undef; + var $element = $(element); + var dialog = { + open: false, + returnValue: undef, + show: function () { + openDialog({modal: false}); + }, + showModal: function () { + openDialog({modal: true}); + }, + close: closeDialog +}; -/** - * Removes all 'dialog' listeners. - */ -$(window).on('dialog:beforeclose', function (e, dialog) { - dialog.element.off('.dialog'); -}); + return dialog; +}; })(jQuery, Drupal, drupalSettings); diff --git a/core/modules/system/system.module b/core/modules/system/system.module index a9ee89a..fe54561 100644 --- a/core/modules/system/system.module +++ b/core/modules/system/system.module @@ -1242,6 +1242,7 @@ function system_library_info() { array('system', 'drupal'), array('system', 'drupalSettings'), array('system', 'drupal.progress'), + array('system', 'jquery.once'), ), ); @@ -1313,6 +1314,9 @@ function system_library_info() { 'core/misc/dialog.ajax.js' => array('group' => JS_LIBRARY, 'weight' => 3), ), 'dependencies' => array( + array('system', 'jquery'), + array('system', 'drupal'), + array('system', 'drupalSettings'), array('system', 'drupal.ajax'), array('system', 'drupal.dialog'), ),