Commit ea72375a authored by webchick's avatar webchick

Issue #2107199 by Wim Leers: Allow Close(Modal)DialogCommand to remove the...

Issue #2107199 by Wim Leers: Allow Close(Modal)DialogCommand to remove the dialog from the DOM, document how client-side dialogs can do that too.
parent 9b4624d7
......@@ -19,14 +19,24 @@ class CloseDialogCommand implements CommandInterface {
*/
protected $selector;
/**
* Whether to persist the dialog in the DOM or not.
*
* @var bool
*/
protected $persist;
/**
* Constructs a CloseDialogCommand object.
*
* @param string $selector
* A CSS selector string of the dialog to close.
* @param bool $persist
* (optional) Whether to persist the dialog in the DOM or not.
*/
public function __construct($selector = NULL) {
public function __construct($selector = NULL, $persist = FALSE) {
$this->selector = $selector ? $selector : '#drupal-modal';
$this->persist = $persist;
}
/**
......@@ -36,6 +46,7 @@ public function render() {
return array(
'command' => 'closeDialog',
'selector' => $this->selector,
'persist' => $this->persist,
);
}
}
......@@ -13,10 +13,16 @@
* Defines an AJAX command that closes the currently visible modal dialog.
*/
class CloseModalDialogCommand extends CloseDialogCommand {
/**
* Constructs a CloseModalDialogCommand object.
*
* @param bool $persist
* (optional) Whether to persist the dialog in the DOM or not.
*/
public function __construct() {
public function __construct($persist = FALSE) {
$this->selector = '#drupal-modal';
$this->persist = $persist;
}
}
......@@ -118,6 +118,9 @@
var $dialog = $(response.selector);
if ($dialog.length) {
Drupal.dialog($dialog).close();
if (!response.persist) {
$dialog.remove();
}
}
};
......
......@@ -14,6 +14,12 @@ drupalSettings.dialog = {
autoResize: true,
maxHeight: '95%',
dialogClass: '',
// When using this API directly (when generating dialogs on the client side),
// you may want to override this method and do
// @code
// jQuery(event.target).remove()
// @endcode
// as well, to remove the dialog on closing.
close: function (event) {
Drupal.detachBehaviors(event.target, null, 'unload');
}
......
......@@ -368,7 +368,11 @@ Drupal.edit.AppView = Backbone.View.extend({
create: function () {
$(this).parent().find('.ui-dialog-titlebar-close').remove();
},
beforeClose: false
beforeClose: false,
close: function (event) {
// Automatically destroy the DOM element that was used for the dialog.
$(event.target).remove();
}
});
this.model.set('activeModal', discardDialog);
......
......@@ -94,6 +94,7 @@ public function testDialog() {
$close_expected_response = array(
'command' => 'closeDialog',
'selector' => '#ajax-test-dialog-wrapper-1',
'persist' => FALSE,
);
// Check that requesting a modal dialog without JS goes to a page.
......
......@@ -360,6 +360,7 @@ public function testCloseModalDialogCommand() {
$expected = array(
'command' => 'closeDialog',
'selector' => '#drupal-modal',
'persist' => FALSE,
);
$this->assertEquals($command->render(), $expected, "CloseModalDialogCommand::render() didn't return the expected array.");
......@@ -369,13 +370,14 @@ public function testCloseModalDialogCommand() {
* Tests that CloseDialogCommand objects can be constructed and rendered.
*/
public function testCloseDialogCommand() {
$command = new CloseDialogCommand('#some-dialog');
$command = new CloseDialogCommand('#some-dialog', TRUE);
$expected = array(
'command' => 'closeDialog',
'selector' => '#some-dialog',
'persist' => TRUE,
);
$this->assertEquals($command->render(), $expected, "CloseDialogCommand::render() with a selector didn't return the expected array.");
$this->assertEquals($command->render(), $expected, "CloseDialogCommand::render() with a selector and persistence enabled didn't return the expected array.");
}
/**
......
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