1/* Source and licensing information for the line(s) below can be found at https://www.nychiefs.org/core/misc/dialog/dialog.position.js. */ 2/** 3 * @file 4 * Positioning extensions for dialogs. 5 */ 6 7/** 8 * Triggers when content inside a dialog changes. 9 * 10 * @event dialogContentResize 11 */ 12 13(function ($, Drupal, drupalSettings, debounce, displace) { 14 // autoResize option will turn off resizable and draggable. 15 drupalSettings.dialog = $.extend( 16 { autoResize: true, maxHeight: '95%' }, 17 drupalSettings.dialog, 18 ); 19 20 /** 21 * Position the dialog's center at the center of displace.offsets boundaries. 22 * 23 * @function Drupal.dialog~resetPosition 24 * 25 * @param {object} options 26 * Options object. 27 * 28 * @return {object} 29 * Altered options object. 30 */ 31 function resetPosition(options) { 32 const offsets = displace.offsets; 33 const left = offsets.left - offsets.right; 34 const top = offsets.top - offsets.bottom; 35 36 const leftString = `${ 37 (left > 0 ? '+' : '-') + Math.abs(Math.round(left / 2)) 38 }px`; 39 const topString = `${ 40 (top > 0 ? '+' : '-') + Math.abs(Math.round(top / 2)) 41 }px`; 42 options.position = { 43 my: `center${left !== 0 ? leftString : ''} center${ 44 top !== 0 ? topString : '' 45 }`, 46 of: window, 47 }; 48 return options; 49 } 50 51 /** 52 * Resets the current options for positioning. 53 * 54 * This is used as a window resize and scroll callback to reposition the 55 * jQuery UI dialog. Although not a built-in jQuery UI option, this can 56 * be disabled by setting autoResize: false in the options array when creating 57 * a new {@link Drupal.dialog}. 58 * 59 * @function Drupal.dialog~resetSize 60 * 61 * @param {jQuery.Event} event 62 * The event triggered. 63 * 64 * @fires event:dialogContentResize 65 */ 66 function resetSize(event) { 67 const positionOptions = [ 68 'width', 69 'height', 70 'minWidth', 71 'minHeight', 72 'maxHeight', 73 'maxWidth', 74 'position', 75 ]; 76 let adjustedOptions = {}; 77 let windowHeight = $(window).height(); 78 let option; 79 let optionValue; 80 let adjustedValue; 81 for (let n = 0; n < positionOptions.length; n++) { 82 option = positionOptions[n]; 83 optionValue = event.data.settings[option]; 84 if (optionValue) { 85 // jQuery UI does not support percentages on heights, convert to pixels. 86 if ( 87 typeof optionValue === 'string' && 88 optionValue.endsWith('%') && 89 /height/i.test(option) 90 ) { 91 // Take offsets in account. 92 windowHeight -= displace.offsets.top + displace.offsets.bottom; 93 adjustedValue = parseInt( 94 0.01 * parseInt(optionValue, 10) * windowHeight, 95 10, 96 ); 97 // Don't force the dialog to be bigger vertically than needed. 98 if ( 99 option === 'height' && 100 Math.round(event.data.$element.parent().outerHeight()) < 101 adjustedValue 102 ) { 103 adjustedValue = 'auto'; 104 } 105 adjustedOptions[option] = adjustedValue; 106 } 107 } 108 } 109 // Offset the dialog center to be at the center of Drupal.displace.offsets. 110 if (!event.data.settings.modal) { 111 adjustedOptions = resetPosition(adjustedOptions); 112 } 113 event.data.$element.dialog('option', adjustedOptions); 114 115 event.data.$element 116 ?.get(0) 117 ?.dispatchEvent( 118 new CustomEvent('dialogContentResize', { bubbles: true }), 119 ); 120 } 121 122 window.addEventListener('dialog:aftercreate', (e) => { 123 const autoResize = debounce(resetSize, 20); 124 const $element = $(e.target); 125 const { settings } = e; 126 const eventData = { settings, $element }; 127 128 if (settings.autoResize === true || settings.autoResize === 'true') { 129 const uiDialog = $element 130 .dialog('option', { resizable: false, draggable: false }) 131 .dialog('widget'); 132 uiDialog[0].style.position = 'fixed'; 133 $(window) 134 .on('resize.dialogResize scroll.dialogResize', eventData, autoResize) 135 .trigger('resize.dialogResize'); 136 $(document).on( 137 'drupalViewportOffsetChange.dialogResize', 138 eventData, 139 autoResize, 140 ); 141 } 142 }); 143 144 window.addEventListener('dialog:beforeclose', () => { 145 $(window).off('.dialogResize'); 146 $(document).off('.dialogResize'); 147 }); 148})(jQuery, Drupal, drupalSettings, Drupal.debounce, Drupal.displace); 149 150/* Source and licensing information for the above line(s) can be found at https://www.nychiefs.org/core/misc/dialog/dialog.position.js. */
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.