dialog.ajax.es6.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /**
  2. * @file
  3. * Extends the Drupal AJAX functionality to integrate the dialog API.
  4. */
  5. (function($, Drupal) {
  6. /**
  7. * Initialize dialogs for Ajax purposes.
  8. *
  9. * @type {Drupal~behavior}
  10. *
  11. * @prop {Drupal~behaviorAttach} attach
  12. * Attaches the behaviors for dialog ajax functionality.
  13. */
  14. Drupal.behaviors.dialog = {
  15. attach(context, settings) {
  16. const $context = $(context);
  17. // Provide a known 'drupal-modal' DOM element for Drupal-based modal
  18. // dialogs. Non-modal dialogs are responsible for creating their own
  19. // elements, since there can be multiple non-modal dialogs at a time.
  20. if (!$('#drupal-modal').length) {
  21. // Add 'ui-front' jQuery UI class so jQuery UI widgets like autocomplete
  22. // sit on top of dialogs. For more information see
  23. // http://api.jqueryui.com/theming/stacking-elements/.
  24. $('<div id="drupal-modal" class="ui-front"></div>')
  25. .hide()
  26. .appendTo('body');
  27. }
  28. // Special behaviors specific when attaching content within a dialog.
  29. // These behaviors usually fire after a validation error inside a dialog.
  30. const $dialog = $context.closest('.ui-dialog-content');
  31. if ($dialog.length) {
  32. // Remove and replace the dialog buttons with those from the new form.
  33. if ($dialog.dialog('option', 'drupalAutoButtons')) {
  34. // Trigger an event to detect/sync changes to buttons.
  35. $dialog.trigger('dialogButtonsChange');
  36. }
  37. // Force focus on the modal when the behavior is run.
  38. $dialog.dialog('widget').trigger('focus');
  39. }
  40. const originalClose = settings.dialog.close;
  41. // Overwrite the close method to remove the dialog on closing.
  42. settings.dialog.close = function(event, ...args) {
  43. originalClose.apply(settings.dialog, [event, ...args]);
  44. $(event.target).remove();
  45. };
  46. },
  47. /**
  48. * Scan a dialog for any primary buttons and move them to the button area.
  49. *
  50. * @param {jQuery} $dialog
  51. * An jQuery object containing the element that is the dialog target.
  52. *
  53. * @return {Array}
  54. * An array of buttons that need to be added to the button area.
  55. */
  56. prepareDialogButtons($dialog) {
  57. const buttons = [];
  58. const $buttons = $dialog.find(
  59. '.form-actions input[type=submit], .form-actions a.button',
  60. );
  61. $buttons.each(function() {
  62. const $originalButton = $(this).css({ display: 'none' });
  63. buttons.push({
  64. text: $originalButton.html() || $originalButton.attr('value'),
  65. class: $originalButton.attr('class'),
  66. click(e) {
  67. // If the original button is an anchor tag, triggering the "click"
  68. // event will not simulate a click. Use the click method instead.
  69. if ($originalButton.is('a')) {
  70. $originalButton[0].click();
  71. } else {
  72. $originalButton
  73. .trigger('mousedown')
  74. .trigger('mouseup')
  75. .trigger('click');
  76. e.preventDefault();
  77. }
  78. },
  79. });
  80. });
  81. return buttons;
  82. },
  83. };
  84. /**
  85. * Command to open a dialog.
  86. *
  87. * @param {Drupal.Ajax} ajax
  88. * The Drupal Ajax object.
  89. * @param {object} response
  90. * Object holding the server response.
  91. * @param {number} [status]
  92. * The HTTP status code.
  93. *
  94. * @return {bool|undefined}
  95. * Returns false if there was no selector property in the response object.
  96. */
  97. Drupal.AjaxCommands.prototype.openDialog = function(ajax, response, status) {
  98. if (!response.selector) {
  99. return false;
  100. }
  101. let $dialog = $(response.selector);
  102. if (!$dialog.length) {
  103. // Create the element if needed.
  104. $dialog = $(
  105. `<div id="${response.selector.replace(
  106. /^#/,
  107. '',
  108. )}" class="ui-front"></div>`,
  109. ).appendTo('body');
  110. }
  111. // Set up the wrapper, if there isn't one.
  112. if (!ajax.wrapper) {
  113. ajax.wrapper = $dialog.attr('id');
  114. }
  115. // Use the ajax.js insert command to populate the dialog contents.
  116. response.command = 'insert';
  117. response.method = 'html';
  118. ajax.commands.insert(ajax, response, status);
  119. // Move the buttons to the jQuery UI dialog buttons area.
  120. if (!response.dialogOptions.buttons) {
  121. response.dialogOptions.drupalAutoButtons = true;
  122. response.dialogOptions.buttons = Drupal.behaviors.dialog.prepareDialogButtons(
  123. $dialog,
  124. );
  125. }
  126. // Bind dialogButtonsChange.
  127. $dialog.on('dialogButtonsChange', () => {
  128. const buttons = Drupal.behaviors.dialog.prepareDialogButtons($dialog);
  129. $dialog.dialog('option', 'buttons', buttons);
  130. });
  131. // Open the dialog itself.
  132. response.dialogOptions = response.dialogOptions || {};
  133. const dialog = Drupal.dialog($dialog.get(0), response.dialogOptions);
  134. if (response.dialogOptions.modal) {
  135. dialog.showModal();
  136. } else {
  137. dialog.show();
  138. }
  139. // Add the standard Drupal class for buttons for style consistency.
  140. $dialog
  141. .parent()
  142. .find('.ui-dialog-buttonset')
  143. .addClass('form-actions');
  144. };
  145. /**
  146. * Command to close a dialog.
  147. *
  148. * If no selector is given, it defaults to trying to close the modal.
  149. *
  150. * @param {Drupal.Ajax} [ajax]
  151. * The ajax object.
  152. * @param {object} response
  153. * Object holding the server response.
  154. * @param {string} response.selector
  155. * The selector of the dialog.
  156. * @param {bool} response.persist
  157. * Whether to persist the dialog element or not.
  158. * @param {number} [status]
  159. * The HTTP status code.
  160. */
  161. Drupal.AjaxCommands.prototype.closeDialog = function(ajax, response, status) {
  162. const $dialog = $(response.selector);
  163. if ($dialog.length) {
  164. Drupal.dialog($dialog.get(0)).close();
  165. if (!response.persist) {
  166. $dialog.remove();
  167. }
  168. }
  169. // Unbind dialogButtonsChange.
  170. $dialog.off('dialogButtonsChange');
  171. };
  172. /**
  173. * Command to set a dialog property.
  174. *
  175. * JQuery UI specific way of setting dialog options.
  176. *
  177. * @param {Drupal.Ajax} [ajax]
  178. * The Drupal Ajax object.
  179. * @param {object} response
  180. * Object holding the server response.
  181. * @param {string} response.selector
  182. * Selector for the dialog element.
  183. * @param {string} response.optionsName
  184. * Name of a key to set.
  185. * @param {string} response.optionValue
  186. * Value to set.
  187. * @param {number} [status]
  188. * The HTTP status code.
  189. */
  190. Drupal.AjaxCommands.prototype.setDialogOption = function(
  191. ajax,
  192. response,
  193. status,
  194. ) {
  195. const $dialog = $(response.selector);
  196. if ($dialog.length) {
  197. $dialog.dialog('option', response.optionName, response.optionValue);
  198. }
  199. };
  200. /**
  201. * Binds a listener on dialog creation to handle the cancel link.
  202. *
  203. * @param {jQuery.Event} e
  204. * The event triggered.
  205. * @param {Drupal.dialog~dialogDefinition} dialog
  206. * The dialog instance.
  207. * @param {jQuery} $element
  208. * The jQuery collection of the dialog element.
  209. * @param {object} [settings]
  210. * Dialog settings.
  211. */
  212. $(window).on('dialog:aftercreate', (e, dialog, $element, settings) => {
  213. $element.on('click.dialog', '.dialog-cancel', e => {
  214. dialog.close('cancel');
  215. e.preventDefault();
  216. e.stopPropagation();
  217. });
  218. });
  219. /**
  220. * Removes all 'dialog' listeners.
  221. *
  222. * @param {jQuery.Event} e
  223. * The event triggered.
  224. * @param {Drupal.dialog~dialogDefinition} dialog
  225. * The dialog instance.
  226. * @param {jQuery} $element
  227. * jQuery collection of the dialog element.
  228. */
  229. $(window).on('dialog:beforeclose', (e, dialog, $element) => {
  230. $element.off('.dialog');
  231. });
  232. })(jQuery, Drupal);