dialog.js 2.8 KB

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