Chris@0: /** Chris@0: * @file Chris@0: * Positioning extensions for dialogs. Chris@0: */ Chris@0: Chris@0: /** Chris@0: * Triggers when content inside a dialog changes. Chris@0: * Chris@0: * @event dialogContentResize Chris@0: */ Chris@0: Chris@4: (function($, Drupal, drupalSettings, debounce, displace) { Chris@0: // autoResize option will turn off resizable and draggable. Chris@4: drupalSettings.dialog = $.extend( Chris@4: { autoResize: true, maxHeight: '95%' }, Chris@4: drupalSettings.dialog, Chris@4: ); Chris@4: Chris@4: /** Chris@4: * Position the dialog's center at the center of displace.offsets boundaries. Chris@4: * Chris@4: * @function Drupal.dialog~resetPosition Chris@4: * Chris@4: * @param {object} options Chris@4: * Options object. Chris@4: * Chris@4: * @return {object} Chris@4: * Altered options object. Chris@4: */ Chris@4: function resetPosition(options) { Chris@4: const offsets = displace.offsets; Chris@4: const left = offsets.left - offsets.right; Chris@4: const top = offsets.top - offsets.bottom; Chris@4: Chris@4: const leftString = `${(left > 0 ? '+' : '-') + Chris@4: Math.abs(Math.round(left / 2))}px`; Chris@4: const topString = `${(top > 0 ? '+' : '-') + Chris@4: Math.abs(Math.round(top / 2))}px`; Chris@4: options.position = { Chris@4: my: `center${left !== 0 ? leftString : ''} center${ Chris@4: top !== 0 ? topString : '' Chris@4: }`, Chris@4: of: window, Chris@4: }; Chris@4: return options; Chris@4: } Chris@0: Chris@0: /** Chris@0: * Resets the current options for positioning. Chris@0: * Chris@0: * This is used as a window resize and scroll callback to reposition the Chris@0: * jQuery UI dialog. Although not a built-in jQuery UI option, this can Chris@0: * be disabled by setting autoResize: false in the options array when creating Chris@0: * a new {@link Drupal.dialog}. Chris@0: * Chris@0: * @function Drupal.dialog~resetSize Chris@0: * Chris@0: * @param {jQuery.Event} event Chris@0: * The event triggered. Chris@0: * Chris@0: * @fires event:dialogContentResize Chris@0: */ Chris@0: function resetSize(event) { Chris@4: const positionOptions = [ Chris@4: 'width', Chris@4: 'height', Chris@4: 'minWidth', Chris@4: 'minHeight', Chris@4: 'maxHeight', Chris@4: 'maxWidth', Chris@4: 'position', Chris@4: ]; Chris@0: let adjustedOptions = {}; Chris@0: let windowHeight = $(window).height(); Chris@0: let option; Chris@0: let optionValue; Chris@0: let adjustedValue; Chris@0: for (let n = 0; n < positionOptions.length; n++) { Chris@0: option = positionOptions[n]; Chris@0: optionValue = event.data.settings[option]; Chris@0: if (optionValue) { Chris@0: // jQuery UI does not support percentages on heights, convert to pixels. Chris@4: if ( Chris@4: typeof optionValue === 'string' && Chris@4: /%$/.test(optionValue) && Chris@4: /height/i.test(option) Chris@4: ) { Chris@0: // Take offsets in account. Chris@0: windowHeight -= displace.offsets.top + displace.offsets.bottom; Chris@4: adjustedValue = parseInt( Chris@4: 0.01 * parseInt(optionValue, 10) * windowHeight, Chris@4: 10, Chris@4: ); Chris@0: // Don't force the dialog to be bigger vertically than needed. Chris@4: if ( Chris@4: option === 'height' && Chris@4: event.data.$element.parent().outerHeight() < adjustedValue Chris@4: ) { Chris@0: adjustedValue = 'auto'; Chris@0: } Chris@0: adjustedOptions[option] = adjustedValue; Chris@0: } Chris@0: } Chris@0: } Chris@0: // Offset the dialog center to be at the center of Drupal.displace.offsets. Chris@0: if (!event.data.settings.modal) { Chris@0: adjustedOptions = resetPosition(adjustedOptions); Chris@0: } Chris@0: event.data.$element Chris@0: .dialog('option', adjustedOptions) Chris@0: .trigger('dialogContentResize'); Chris@0: } Chris@0: Chris@0: $(window).on({ Chris@4: 'dialog:aftercreate': function(event, dialog, $element, settings) { Chris@0: const autoResize = debounce(resetSize, 20); Chris@0: const eventData = { settings, $element }; Chris@0: if (settings.autoResize === true || settings.autoResize === 'true') { Chris@0: $element Chris@0: .dialog('option', { resizable: false, draggable: false }) Chris@4: .dialog('widget') Chris@4: .css('position', 'fixed'); Chris@0: $(window) Chris@0: .on('resize.dialogResize scroll.dialogResize', eventData, autoResize) Chris@0: .trigger('resize.dialogResize'); Chris@4: $(document).on( Chris@4: 'drupalViewportOffsetChange.dialogResize', Chris@4: eventData, Chris@4: autoResize, Chris@4: ); Chris@0: } Chris@0: }, Chris@4: 'dialog:beforeclose': function(event, dialog, $element) { Chris@0: $(window).off('.dialogResize'); Chris@0: $(document).off('.dialogResize'); Chris@0: }, Chris@0: }); Chris@4: })(jQuery, Drupal, drupalSettings, Drupal.debounce, Drupal.displace);