dialog.position.es6.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /**
  2. * @file
  3. * Positioning extensions for dialogs.
  4. */
  5. /**
  6. * Triggers when content inside a dialog changes.
  7. *
  8. * @event dialogContentResize
  9. */
  10. (function ($, Drupal, drupalSettings, debounce, displace) {
  11. // autoResize option will turn off resizable and draggable.
  12. drupalSettings.dialog = $.extend({ autoResize: true, maxHeight: '95%' }, drupalSettings.dialog);
  13. /**
  14. * Resets the current options for positioning.
  15. *
  16. * This is used as a window resize and scroll callback to reposition the
  17. * jQuery UI dialog. Although not a built-in jQuery UI option, this can
  18. * be disabled by setting autoResize: false in the options array when creating
  19. * a new {@link Drupal.dialog}.
  20. *
  21. * @function Drupal.dialog~resetSize
  22. *
  23. * @param {jQuery.Event} event
  24. * The event triggered.
  25. *
  26. * @fires event:dialogContentResize
  27. */
  28. function resetSize(event) {
  29. const positionOptions = ['width', 'height', 'minWidth', 'minHeight', 'maxHeight', 'maxWidth', 'position'];
  30. let adjustedOptions = {};
  31. let windowHeight = $(window).height();
  32. let option;
  33. let optionValue;
  34. let adjustedValue;
  35. for (let n = 0; n < positionOptions.length; n++) {
  36. option = positionOptions[n];
  37. optionValue = event.data.settings[option];
  38. if (optionValue) {
  39. // jQuery UI does not support percentages on heights, convert to pixels.
  40. if (typeof optionValue === 'string' && /%$/.test(optionValue) && /height/i.test(option)) {
  41. // Take offsets in account.
  42. windowHeight -= displace.offsets.top + displace.offsets.bottom;
  43. adjustedValue = parseInt(0.01 * parseInt(optionValue, 10) * windowHeight, 10);
  44. // Don't force the dialog to be bigger vertically than needed.
  45. if (option === 'height' && event.data.$element.parent().outerHeight() < adjustedValue) {
  46. adjustedValue = 'auto';
  47. }
  48. adjustedOptions[option] = adjustedValue;
  49. }
  50. }
  51. }
  52. // Offset the dialog center to be at the center of Drupal.displace.offsets.
  53. if (!event.data.settings.modal) {
  54. adjustedOptions = resetPosition(adjustedOptions);
  55. }
  56. event.data.$element
  57. .dialog('option', adjustedOptions)
  58. .trigger('dialogContentResize');
  59. }
  60. /**
  61. * Position the dialog's center at the center of displace.offsets boundaries.
  62. *
  63. * @function Drupal.dialog~resetPosition
  64. *
  65. * @param {object} options
  66. * Options object.
  67. *
  68. * @return {object}
  69. * Altered options object.
  70. */
  71. function resetPosition(options) {
  72. const offsets = displace.offsets;
  73. const left = offsets.left - offsets.right;
  74. const top = offsets.top - offsets.bottom;
  75. const leftString = `${(left > 0 ? '+' : '-') + Math.abs(Math.round(left / 2))}px`;
  76. const topString = `${(top > 0 ? '+' : '-') + Math.abs(Math.round(top / 2))}px`;
  77. options.position = {
  78. my: `center${left !== 0 ? leftString : ''} center${top !== 0 ? topString : ''}`,
  79. of: window,
  80. };
  81. return options;
  82. }
  83. $(window).on({
  84. 'dialog:aftercreate': function (event, dialog, $element, settings) {
  85. const autoResize = debounce(resetSize, 20);
  86. const eventData = { settings, $element };
  87. if (settings.autoResize === true || settings.autoResize === 'true') {
  88. $element
  89. .dialog('option', { resizable: false, draggable: false })
  90. .dialog('widget').css('position', 'fixed');
  91. $(window)
  92. .on('resize.dialogResize scroll.dialogResize', eventData, autoResize)
  93. .trigger('resize.dialogResize');
  94. $(document).on('drupalViewportOffsetChange.dialogResize', eventData, autoResize);
  95. }
  96. },
  97. 'dialog:beforeclose': function (event, dialog, $element) {
  98. $(window).off('.dialogResize');
  99. $(document).off('.dialogResize');
  100. },
  101. });
  102. }(jQuery, Drupal, drupalSettings, Drupal.debounce, Drupal.displace));