comparison core/misc/dialog/dialog.position.es6.js @ 4:a9cd425dd02b

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:11:55 +0000
parents c75dbcec494b
children 12f9dff5fda9
comparison
equal deleted inserted replaced
3:307d7a7fd348 4:a9cd425dd02b
7 * Triggers when content inside a dialog changes. 7 * Triggers when content inside a dialog changes.
8 * 8 *
9 * @event dialogContentResize 9 * @event dialogContentResize
10 */ 10 */
11 11
12 (function ($, Drupal, drupalSettings, debounce, displace) { 12 (function($, Drupal, drupalSettings, debounce, displace) {
13 // autoResize option will turn off resizable and draggable. 13 // autoResize option will turn off resizable and draggable.
14 drupalSettings.dialog = $.extend({ autoResize: true, maxHeight: '95%' }, drupalSettings.dialog); 14 drupalSettings.dialog = $.extend(
15 { autoResize: true, maxHeight: '95%' },
16 drupalSettings.dialog,
17 );
18
19 /**
20 * Position the dialog's center at the center of displace.offsets boundaries.
21 *
22 * @function Drupal.dialog~resetPosition
23 *
24 * @param {object} options
25 * Options object.
26 *
27 * @return {object}
28 * Altered options object.
29 */
30 function resetPosition(options) {
31 const offsets = displace.offsets;
32 const left = offsets.left - offsets.right;
33 const top = offsets.top - offsets.bottom;
34
35 const leftString = `${(left > 0 ? '+' : '-') +
36 Math.abs(Math.round(left / 2))}px`;
37 const topString = `${(top > 0 ? '+' : '-') +
38 Math.abs(Math.round(top / 2))}px`;
39 options.position = {
40 my: `center${left !== 0 ? leftString : ''} center${
41 top !== 0 ? topString : ''
42 }`,
43 of: window,
44 };
45 return options;
46 }
15 47
16 /** 48 /**
17 * Resets the current options for positioning. 49 * Resets the current options for positioning.
18 * 50 *
19 * This is used as a window resize and scroll callback to reposition the 51 * This is used as a window resize and scroll callback to reposition the
27 * The event triggered. 59 * The event triggered.
28 * 60 *
29 * @fires event:dialogContentResize 61 * @fires event:dialogContentResize
30 */ 62 */
31 function resetSize(event) { 63 function resetSize(event) {
32 const positionOptions = ['width', 'height', 'minWidth', 'minHeight', 'maxHeight', 'maxWidth', 'position']; 64 const positionOptions = [
65 'width',
66 'height',
67 'minWidth',
68 'minHeight',
69 'maxHeight',
70 'maxWidth',
71 'position',
72 ];
33 let adjustedOptions = {}; 73 let adjustedOptions = {};
34 let windowHeight = $(window).height(); 74 let windowHeight = $(window).height();
35 let option; 75 let option;
36 let optionValue; 76 let optionValue;
37 let adjustedValue; 77 let adjustedValue;
38 for (let n = 0; n < positionOptions.length; n++) { 78 for (let n = 0; n < positionOptions.length; n++) {
39 option = positionOptions[n]; 79 option = positionOptions[n];
40 optionValue = event.data.settings[option]; 80 optionValue = event.data.settings[option];
41 if (optionValue) { 81 if (optionValue) {
42 // jQuery UI does not support percentages on heights, convert to pixels. 82 // jQuery UI does not support percentages on heights, convert to pixels.
43 if (typeof optionValue === 'string' && /%$/.test(optionValue) && /height/i.test(option)) { 83 if (
84 typeof optionValue === 'string' &&
85 /%$/.test(optionValue) &&
86 /height/i.test(option)
87 ) {
44 // Take offsets in account. 88 // Take offsets in account.
45 windowHeight -= displace.offsets.top + displace.offsets.bottom; 89 windowHeight -= displace.offsets.top + displace.offsets.bottom;
46 adjustedValue = parseInt(0.01 * parseInt(optionValue, 10) * windowHeight, 10); 90 adjustedValue = parseInt(
91 0.01 * parseInt(optionValue, 10) * windowHeight,
92 10,
93 );
47 // Don't force the dialog to be bigger vertically than needed. 94 // Don't force the dialog to be bigger vertically than needed.
48 if (option === 'height' && event.data.$element.parent().outerHeight() < adjustedValue) { 95 if (
96 option === 'height' &&
97 event.data.$element.parent().outerHeight() < adjustedValue
98 ) {
49 adjustedValue = 'auto'; 99 adjustedValue = 'auto';
50 } 100 }
51 adjustedOptions[option] = adjustedValue; 101 adjustedOptions[option] = adjustedValue;
52 } 102 }
53 } 103 }
59 event.data.$element 109 event.data.$element
60 .dialog('option', adjustedOptions) 110 .dialog('option', adjustedOptions)
61 .trigger('dialogContentResize'); 111 .trigger('dialogContentResize');
62 } 112 }
63 113
64 /**
65 * Position the dialog's center at the center of displace.offsets boundaries.
66 *
67 * @function Drupal.dialog~resetPosition
68 *
69 * @param {object} options
70 * Options object.
71 *
72 * @return {object}
73 * Altered options object.
74 */
75 function resetPosition(options) {
76 const offsets = displace.offsets;
77 const left = offsets.left - offsets.right;
78 const top = offsets.top - offsets.bottom;
79
80 const leftString = `${(left > 0 ? '+' : '-') + Math.abs(Math.round(left / 2))}px`;
81 const topString = `${(top > 0 ? '+' : '-') + Math.abs(Math.round(top / 2))}px`;
82 options.position = {
83 my: `center${left !== 0 ? leftString : ''} center${top !== 0 ? topString : ''}`,
84 of: window,
85 };
86 return options;
87 }
88
89 $(window).on({ 114 $(window).on({
90 'dialog:aftercreate': function (event, dialog, $element, settings) { 115 'dialog:aftercreate': function(event, dialog, $element, settings) {
91 const autoResize = debounce(resetSize, 20); 116 const autoResize = debounce(resetSize, 20);
92 const eventData = { settings, $element }; 117 const eventData = { settings, $element };
93 if (settings.autoResize === true || settings.autoResize === 'true') { 118 if (settings.autoResize === true || settings.autoResize === 'true') {
94 $element 119 $element
95 .dialog('option', { resizable: false, draggable: false }) 120 .dialog('option', { resizable: false, draggable: false })
96 .dialog('widget').css('position', 'fixed'); 121 .dialog('widget')
122 .css('position', 'fixed');
97 $(window) 123 $(window)
98 .on('resize.dialogResize scroll.dialogResize', eventData, autoResize) 124 .on('resize.dialogResize scroll.dialogResize', eventData, autoResize)
99 .trigger('resize.dialogResize'); 125 .trigger('resize.dialogResize');
100 $(document).on('drupalViewportOffsetChange.dialogResize', eventData, autoResize); 126 $(document).on(
127 'drupalViewportOffsetChange.dialogResize',
128 eventData,
129 autoResize,
130 );
101 } 131 }
102 }, 132 },
103 'dialog:beforeclose': function (event, dialog, $element) { 133 'dialog:beforeclose': function(event, dialog, $element) {
104 $(window).off('.dialogResize'); 134 $(window).off('.dialogResize');
105 $(document).off('.dialogResize'); 135 $(document).off('.dialogResize');
106 }, 136 },
107 }); 137 });
108 }(jQuery, Drupal, drupalSettings, Drupal.debounce, Drupal.displace)); 138 })(jQuery, Drupal, drupalSettings, Drupal.debounce, Drupal.displace);