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 resetPosition(options) {
  10. var offsets = displace.offsets;
  11. var left = offsets.left - offsets.right;
  12. var top = offsets.top - offsets.bottom;
  13. var leftString = (left > 0 ? '+' : '-') + Math.abs(Math.round(left / 2)) + 'px';
  14. var topString = (top > 0 ? '+' : '-') + Math.abs(Math.round(top / 2)) + 'px';
  15. options.position = {
  16. my: 'center' + (left !== 0 ? leftString : '') + ' center' + (top !== 0 ? topString : ''),
  17. of: window
  18. };
  19. return options;
  20. }
  21. function resetSize(event) {
  22. var positionOptions = ['width', 'height', 'minWidth', 'minHeight', 'maxHeight', 'maxWidth', 'position'];
  23. var adjustedOptions = {};
  24. var windowHeight = $(window).height();
  25. var option = void 0;
  26. var optionValue = void 0;
  27. var adjustedValue = void 0;
  28. for (var n = 0; n < positionOptions.length; n++) {
  29. option = positionOptions[n];
  30. optionValue = event.data.settings[option];
  31. if (optionValue) {
  32. if (typeof optionValue === 'string' && /%$/.test(optionValue) && /height/i.test(option)) {
  33. windowHeight -= displace.offsets.top + displace.offsets.bottom;
  34. adjustedValue = parseInt(0.01 * parseInt(optionValue, 10) * windowHeight, 10);
  35. if (option === 'height' && event.data.$element.parent().outerHeight() < adjustedValue) {
  36. adjustedValue = 'auto';
  37. }
  38. adjustedOptions[option] = adjustedValue;
  39. }
  40. }
  41. }
  42. if (!event.data.settings.modal) {
  43. adjustedOptions = resetPosition(adjustedOptions);
  44. }
  45. event.data.$element.dialog('option', adjustedOptions).trigger('dialogContentResize');
  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);