annotate core/misc/dialog/dialog.position.js @ 0:4c8ae668cc8c

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