dialog.position.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /**
  2. * DO NOT EDIT THIS FILE.
  3. * See the following change record for more information,
  4. * https://www.drupal.org/node/2815083
  5. * @preserve
  6. **/
  7. (function ($, Drupal, drupalSettings, debounce, displace) {
  8. drupalSettings.dialog = $.extend({ autoResize: true, maxHeight: '95%' }, drupalSettings.dialog);
  9. function resetSize(event) {
  10. var positionOptions = ['width', 'height', 'minWidth', 'minHeight', 'maxHeight', 'maxWidth', 'position'];
  11. var adjustedOptions = {};
  12. var windowHeight = $(window).height();
  13. var option = void 0;
  14. var optionValue = void 0;
  15. var adjustedValue = void 0;
  16. for (var n = 0; n < positionOptions.length; n++) {
  17. option = positionOptions[n];
  18. optionValue = event.data.settings[option];
  19. if (optionValue) {
  20. if (typeof optionValue === 'string' && /%$/.test(optionValue) && /height/i.test(option)) {
  21. windowHeight -= displace.offsets.top + displace.offsets.bottom;
  22. adjustedValue = parseInt(0.01 * parseInt(optionValue, 10) * windowHeight, 10);
  23. if (option === 'height' && event.data.$element.parent().outerHeight() < adjustedValue) {
  24. adjustedValue = 'auto';
  25. }
  26. adjustedOptions[option] = adjustedValue;
  27. }
  28. }
  29. }
  30. if (!event.data.settings.modal) {
  31. adjustedOptions = resetPosition(adjustedOptions);
  32. }
  33. event.data.$element.dialog('option', adjustedOptions).trigger('dialogContentResize');
  34. }
  35. function resetPosition(options) {
  36. var offsets = displace.offsets;
  37. var left = offsets.left - offsets.right;
  38. var top = offsets.top - offsets.bottom;
  39. var leftString = (left > 0 ? '+' : '-') + Math.abs(Math.round(left / 2)) + 'px';
  40. var topString = (top > 0 ? '+' : '-') + Math.abs(Math.round(top / 2)) + 'px';
  41. options.position = {
  42. my: 'center' + (left !== 0 ? leftString : '') + ' center' + (top !== 0 ? topString : ''),
  43. of: window
  44. };
  45. return options;
  46. }
  47. $(window).on({
  48. 'dialog:aftercreate': function dialogAftercreate(event, dialog, $element, settings) {
  49. var autoResize = debounce(resetSize, 20);
  50. var eventData = { settings: settings, $element: $element };
  51. if (settings.autoResize === true || settings.autoResize === 'true') {
  52. $element.dialog('option', { resizable: false, draggable: false }).dialog('widget').css('position', 'fixed');
  53. $(window).on('resize.dialogResize scroll.dialogResize', eventData, autoResize).trigger('resize.dialogResize');
  54. $(document).on('drupalViewportOffsetChange.dialogResize', eventData, autoResize);
  55. }
  56. },
  57. 'dialog:beforeclose': function dialogBeforeclose(event, dialog, $element) {
  58. $(window).off('.dialogResize');
  59. $(document).off('.dialogResize');
  60. }
  61. });
  62. })(jQuery, Drupal, drupalSettings, Drupal.debounce, Drupal.displace);