dialog.es6.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /**
  2. * @file
  3. * Dialog API inspired by HTML5 dialog element.
  4. *
  5. * @see http://www.whatwg.org/specs/web-apps/current-work/multipage/commands.html#the-dialog-element
  6. */
  7. (function($, Drupal, drupalSettings) {
  8. /**
  9. * Default dialog options.
  10. *
  11. * @type {object}
  12. *
  13. * @prop {bool} [autoOpen=true]
  14. * @prop {string} [dialogClass='']
  15. * @prop {string} [buttonClass='button']
  16. * @prop {string} [buttonPrimaryClass='button--primary']
  17. * @prop {function} close
  18. */
  19. drupalSettings.dialog = {
  20. autoOpen: true,
  21. dialogClass: '',
  22. // Drupal-specific extensions: see dialog.jquery-ui.js.
  23. buttonClass: 'button',
  24. buttonPrimaryClass: 'button--primary',
  25. // When using this API directly (when generating dialogs on the client
  26. // side), you may want to override this method and do
  27. // `jQuery(event.target).remove()` as well, to remove the dialog on
  28. // closing.
  29. close(event) {
  30. Drupal.dialog(event.target).close();
  31. Drupal.detachBehaviors(event.target, null, 'unload');
  32. },
  33. };
  34. /**
  35. * @typedef {object} Drupal.dialog~dialogDefinition
  36. *
  37. * @prop {boolean} open
  38. * Is the dialog open or not.
  39. * @prop {*} returnValue
  40. * Return value of the dialog.
  41. * @prop {function} show
  42. * Method to display the dialog on the page.
  43. * @prop {function} showModal
  44. * Method to display the dialog as a modal on the page.
  45. * @prop {function} close
  46. * Method to hide the dialog from the page.
  47. */
  48. /**
  49. * Polyfill HTML5 dialog element with jQueryUI.
  50. *
  51. * @param {HTMLElement} element
  52. * The element that holds the dialog.
  53. * @param {object} options
  54. * jQuery UI options to be passed to the dialog.
  55. *
  56. * @return {Drupal.dialog~dialogDefinition}
  57. * The dialog instance.
  58. */
  59. Drupal.dialog = function(element, options) {
  60. let undef;
  61. const $element = $(element);
  62. const dialog = {
  63. open: false,
  64. returnValue: undef,
  65. };
  66. function openDialog(settings) {
  67. settings = $.extend({}, drupalSettings.dialog, options, settings);
  68. // Trigger a global event to allow scripts to bind events to the dialog.
  69. $(window).trigger('dialog:beforecreate', [dialog, $element, settings]);
  70. $element.dialog(settings);
  71. dialog.open = true;
  72. $(window).trigger('dialog:aftercreate', [dialog, $element, settings]);
  73. }
  74. function closeDialog(value) {
  75. $(window).trigger('dialog:beforeclose', [dialog, $element]);
  76. $element.dialog('close');
  77. dialog.returnValue = value;
  78. dialog.open = false;
  79. $(window).trigger('dialog:afterclose', [dialog, $element]);
  80. }
  81. dialog.show = () => {
  82. openDialog({ modal: false });
  83. };
  84. dialog.showModal = () => {
  85. openDialog({ modal: true });
  86. };
  87. dialog.close = closeDialog;
  88. return dialog;
  89. };
  90. })(jQuery, Drupal, drupalSettings);