3 * Extends the Drupal AJAX functionality to integrate the dialog API.
8 * Initialize dialogs for Ajax purposes.
10 * @type {Drupal~behavior}
12 * @prop {Drupal~behaviorAttach} attach
13 * Attaches the behaviors for dialog ajax functionality.
15 Drupal.behaviors.dialog = {
16 attach(context, settings) {
17 const $context = $(context);
19 // Provide a known 'drupal-modal' DOM element for Drupal-based modal
20 // dialogs. Non-modal dialogs are responsible for creating their own
21 // elements, since there can be multiple non-modal dialogs at a time.
22 if (!$('#drupal-modal').length) {
23 // Add 'ui-front' jQuery UI class so jQuery UI widgets like autocomplete
24 // sit on top of dialogs. For more information see
25 // http://api.jqueryui.com/theming/stacking-elements/.
26 $('<div id="drupal-modal" class="ui-front"/>')
31 // Special behaviors specific when attaching content within a dialog.
32 // These behaviors usually fire after a validation error inside a dialog.
33 const $dialog = $context.closest('.ui-dialog-content');
35 // Remove and replace the dialog buttons with those from the new form.
36 if ($dialog.dialog('option', 'drupalAutoButtons')) {
37 // Trigger an event to detect/sync changes to buttons.
38 $dialog.trigger('dialogButtonsChange');
41 // Force focus on the modal when the behavior is run.
42 $dialog.dialog('widget').trigger('focus');
45 const originalClose = settings.dialog.close;
46 // Overwrite the close method to remove the dialog on closing.
47 settings.dialog.close = function(event, ...args) {
48 originalClose.apply(settings.dialog, [event, ...args]);
49 $(event.target).remove();
54 * Scan a dialog for any primary buttons and move them to the button area.
56 * @param {jQuery} $dialog
57 * An jQuery object containing the element that is the dialog target.
60 * An array of buttons that need to be added to the button area.
62 prepareDialogButtons($dialog) {
64 const $buttons = $dialog.find(
65 '.form-actions input[type=submit], .form-actions a.button',
67 $buttons.each(function() {
68 // Hidden form buttons need special attention. For browser consistency,
69 // the button needs to be "visible" in order to have the enter key fire
70 // the form submit event. So instead of a simple "hide" or
71 // "display: none", we set its dimensions to zero.
72 // See http://mattsnider.com/how-forms-submit-when-pressing-enter/
73 const $originalButton = $(this).css({
82 text: $originalButton.html() || $originalButton.attr('value'),
83 class: $originalButton.attr('class'),
85 // If the original button is an anchor tag, triggering the "click"
86 // event will not simulate a click. Use the click method instead.
87 if ($originalButton.is('a')) {
88 $originalButton[0].click();
104 * Command to open a dialog.
106 * @param {Drupal.Ajax} ajax
107 * The Drupal Ajax object.
108 * @param {object} response
109 * Object holding the server response.
110 * @param {number} [status]
111 * The HTTP status code.
113 * @return {bool|undefined}
114 * Returns false if there was no selector property in the response object.
116 Drupal.AjaxCommands.prototype.openDialog = function(ajax, response, status) {
117 if (!response.selector) {
120 let $dialog = $(response.selector);
121 if (!$dialog.length) {
122 // Create the element if needed.
124 `<div id="${response.selector.replace(/^#/, '')}" class="ui-front"/>`,
127 // Set up the wrapper, if there isn't one.
129 ajax.wrapper = $dialog.attr('id');
132 // Use the ajax.js insert command to populate the dialog contents.
133 response.command = 'insert';
134 response.method = 'html';
135 ajax.commands.insert(ajax, response, status);
137 // Move the buttons to the jQuery UI dialog buttons area.
138 if (!response.dialogOptions.buttons) {
139 response.dialogOptions.drupalAutoButtons = true;
140 response.dialogOptions.buttons = Drupal.behaviors.dialog.prepareDialogButtons(
145 // Bind dialogButtonsChange.
146 $dialog.on('dialogButtonsChange', () => {
147 const buttons = Drupal.behaviors.dialog.prepareDialogButtons($dialog);
148 $dialog.dialog('option', 'buttons', buttons);
151 // Open the dialog itself.
152 response.dialogOptions = response.dialogOptions || {};
153 const dialog = Drupal.dialog($dialog.get(0), response.dialogOptions);
154 if (response.dialogOptions.modal) {
160 // Add the standard Drupal class for buttons for style consistency.
163 .find('.ui-dialog-buttonset')
164 .addClass('form-actions');
168 * Command to close a dialog.
170 * If no selector is given, it defaults to trying to close the modal.
172 * @param {Drupal.Ajax} [ajax]
174 * @param {object} response
175 * Object holding the server response.
176 * @param {string} response.selector
177 * The selector of the dialog.
178 * @param {bool} response.persist
179 * Whether to persist the dialog element or not.
180 * @param {number} [status]
181 * The HTTP status code.
183 Drupal.AjaxCommands.prototype.closeDialog = function(ajax, response, status) {
184 const $dialog = $(response.selector);
185 if ($dialog.length) {
186 Drupal.dialog($dialog.get(0)).close();
187 if (!response.persist) {
192 // Unbind dialogButtonsChange.
193 $dialog.off('dialogButtonsChange');
197 * Command to set a dialog property.
199 * JQuery UI specific way of setting dialog options.
201 * @param {Drupal.Ajax} [ajax]
202 * The Drupal Ajax object.
203 * @param {object} response
204 * Object holding the server response.
205 * @param {string} response.selector
206 * Selector for the dialog element.
207 * @param {string} response.optionsName
208 * Name of a key to set.
209 * @param {string} response.optionValue
211 * @param {number} [status]
212 * The HTTP status code.
214 Drupal.AjaxCommands.prototype.setDialogOption = function(
219 const $dialog = $(response.selector);
220 if ($dialog.length) {
221 $dialog.dialog('option', response.optionName, response.optionValue);
226 * Binds a listener on dialog creation to handle the cancel link.
228 * @param {jQuery.Event} e
229 * The event triggered.
230 * @param {Drupal.dialog~dialogDefinition} dialog
231 * The dialog instance.
232 * @param {jQuery} $element
233 * The jQuery collection of the dialog element.
234 * @param {object} [settings]
237 $(window).on('dialog:aftercreate', (e, dialog, $element, settings) => {
238 $element.on('click.dialog', '.dialog-cancel', e => {
239 dialog.close('cancel');
246 * Removes all 'dialog' listeners.
248 * @param {jQuery.Event} e
249 * The event triggered.
250 * @param {Drupal.dialog~dialogDefinition} dialog
251 * The dialog instance.
252 * @param {jQuery} $element
253 * jQuery collection of the dialog element.
255 $(window).on('dialog:beforeclose', (e, dialog, $element) => {
256 $element.off('.dialog');