Commit 925f92bb authored by Cottser's avatar Cottser

Revert "Issue #2830882 by tedbow, droplet, drpal, Adita, tim.plunkett,...

Revert "Issue #2830882 by tedbow, droplet, drpal, Adita, tim.plunkett, markcarver, Wim Leers, nod_: Introduce Drupal.offCanvas, mirrored after Drupal.dialog, to avoid Settings Tray using Drupal.Dialog in unintended ways"

This reverts commit aa76f967.
parent b5ec4f1d
...@@ -9,220 +9,101 @@ ...@@ -9,220 +9,101 @@
* @private * @private
*/ */
(($, Drupal, debounce, displace) => { (function ($, Drupal, debounce, displace) {
// The minimum width to use body displace needs to match the width at which
// the tray will be %100 width. @see outside_in.module.css
const minDisplaceWidth = 768;
/** /**
* Off-canvas dialog implementation using jQuery Dialog. * The edge of the screen that the dialog should appear on.
*
* Transforms the regular dialogs created using Drupal.dialog when the dialog
* element equals '#drupal-off-canvas' into an side-loading dialog.
* *
* @namespace * @type {string}
*/ */
Drupal.offCanvas = { const edge = document.documentElement.dir === 'rtl' ? 'left' : 'right';
/**
* The minimum width to use body displace needs to match the width at which
* the tray will be %100 width. @see outside_in.module.css
* @type {Number}
*/
minDisplaceWidth: 768,
/** const $mainCanvasWrapper = $('[data-off-canvas-main-canvas]');
* Wrapper used to position off-canvas dialog.
* @type {jQuery}
*/
$mainCanvasWrapper: $('[data-off-canvas-main-canvas]'),
/** /**
* Determines if an element is an off-canvas dialog. * Resets the size of the dialog.
* *
* @param {jQuery} $element * @param {jQuery.Event} event
* The dialog element. * The event triggered.
* @return {bool} */
* True this is currently an off-canvas dialog. function resetSize(event) {
*/ const offsets = displace.offsets;
isOffCanvas($element) { const $element = event.data.$element;
return $element.is('#drupal-off-canvas'); const $widget = $element.dialog('widget');
}, const $elementScroll = $element.scrollTop();
/** const adjustedOptions = {
* Handler fired before an off-canvas dialog has been opened.
* @param {Object} settings
* Settings related to the composition of the dialog.
* @return {undefined}
*/
beforeCreate({ settings }) {
$('body').addClass('js-tray-open');
settings.dialogClass += ' ui-dialog-off-canvas';
// @see http://api.jqueryui.com/position/ // @see http://api.jqueryui.com/position/
settings.position = { position: {
my: 'left top', my: `${edge} top`,
at: `${Drupal.offCanvas.getEdge()} top`, at: `${edge} top${offsets.top !== 0 ? `+${offsets.top}` : ''}`,
of: window, of: window,
}; },
};
/**
* Applies initial height to dialog based on window height.
* @see http://api.jqueryui.com/dialog for all dialog options.
*/
settings.height = $(window).height();
},
/**
* Handler fired after an off-canvas dialog has been closed.
* @return {undefined}
*/
beforeClose() {
$('body').removeClass('js-tray-open');
// Remove all *.off-canvas events
$(document).off('.off-canvas');
$(window).off('.off-canvas');
Drupal.offCanvas.$mainCanvasWrapper.css(`padding-${Drupal.offCanvas.getEdge()}`, 0);
},
/** $widget.css({
* Handler fired when an off-canvas dialog has been opened. position: 'fixed',
* @param {jQuery} $element height: `${$(window).height() - (offsets.top + offsets.bottom)}px`,
* The off-canvas dialog element. });
* @param {Object} settings
* Settings related to the composition of the dialog.
* @return {undefined}
*/
afterCreate({ $element, settings }) {
const eventData = { settings, $element, offCanvasDialog: this };
$element $element
.on('dialogresize.off-canvas', eventData, debounce(Drupal.offCanvas.bodyPadding, 100)) .dialog('option', adjustedOptions)
.on('dialogContentResize.off-canvas', eventData, Drupal.offCanvas.handleDialogResize) .trigger('dialogContentResize.off-canvas');
.on('dialogContentResize.off-canvas', eventData, debounce(Drupal.offCanvas.bodyPadding, 100))
.trigger('dialogresize.off-canvas');
Drupal.offCanvas.getContainer($element).attr(`data-offset-${Drupal.offCanvas.getEdge()}`, ''); // Reset the elements scroll position after being repositioned.
$element.scrollTop($elementScroll);
}
$(window) /**
.on('resize.off-canvas scroll.off-canvas', eventData, debounce(Drupal.offCanvas.resetSize, 100)) * Adjusts the dialog on resize.
.trigger('resize.off-canvas'); *
}, * @param {jQuery.Event} event
* The event triggered.
/** */
* Toggle classes based on title existence. function handleDialogResize(event) {
* Called with Drupal.offCanvas.afterCreate. const $element = event.data.$element;
* @param {Object} settings const $widget = $element.dialog('widget');
* Settings related to the composition of the dialog.
* @return {undefined} const $offsets = $widget.find('> :not(#drupal-off-canvas, .ui-resizable-handle)');
*/ let offset = 0;
render({ settings }) { let modalHeight;
$('.ui-dialog-off-canvas, .ui-dialog-off-canvas .ui-dialog-titlebar').toggleClass('ui-dialog-empty-title', !settings.title);
}, // Let scroll element take all the height available.
$element.css({ height: 'auto' });
/** modalHeight = $widget.height();
* Adjusts the dialog on resize. $offsets.each(function () {
* offset += $(this).outerHeight();
* @param {jQuery.Event} event });
* The event triggered.
* @param {object} event.data // Take internal padding into account.
* Data attached to the event. const scrollOffset = $element.outerHeight() - $element.height();
*/ $element.height(modalHeight - offset - scrollOffset);
handleDialogResize(event) { }
const $element = event.data.$element;
const $container = Drupal.offCanvas.getContainer($element);
const $offsets = $container.find('> :not(#drupal-off-canvas, .ui-resizable-handle)');
let offset = 0;
// Let scroll element take all the height available.
$element.css({ height: 'auto' });
const modalHeight = $container.height();
$offsets.each((i, e) => {
offset += $(e).outerHeight();
});
// Take internal padding into account.
const scrollOffset = $element.outerHeight() - $element.height();
$element.height(modalHeight - offset - scrollOffset);
},
/**
* Resets the size of the dialog.
*
* @param {jQuery.Event} event
* The event triggered.
* @param {object} event.data
* Data attached to the event.
*/
resetSize(event) {
const offsets = displace.offsets;
const $element = event.data.$element;
const container = Drupal.offCanvas.getContainer($element);
const topPosition = (offsets.top !== 0 ? `+${offsets.top}` : '');
const adjustedOptions = {
// @see http://api.jqueryui.com/position/
position: {
my: `${Drupal.offCanvas.getEdge()} top`,
at: `${Drupal.offCanvas.getEdge()} top${topPosition}`,
of: window,
},
};
container.css({
position: 'fixed',
height: `${$(window).height() - (offsets.top + offsets.bottom)}px`,
});
$element
.dialog('option', adjustedOptions)
.trigger('dialogContentResize.off-canvas');
},
/**
* Adjusts the body padding when the dialog is resized.
*
* @param {jQuery.Event} event
* The event triggered.
* @param {object} event.data
* Data attached to the event.
*/
bodyPadding(event) {
if ($('body').outerWidth() < Drupal.offCanvas.minDisplaceWidth) {
return;
}
const $element = event.data.$element;
const $container = Drupal.offCanvas.getContainer($element);
const $mainCanvasWrapper = Drupal.offCanvas.$mainCanvasWrapper;
const width = $container.outerWidth();
const mainCanvasPadding = $mainCanvasWrapper.css(`padding-${Drupal.offCanvas.getEdge()}`);
if (width !== mainCanvasPadding) {
$mainCanvasWrapper.css(`padding-${Drupal.offCanvas.getEdge()}`, `${width}px`);
$container.attr(`data-offset-${Drupal.offCanvas.getEdge()}`, width);
displace();
}
},
/**
* The HTML element that surrounds the dialog.
* @param {HTMLElement} $element
* The dialog element.
*
* @return {HTMLElement}
* The containing element.
*/
getContainer($element) {
return $element.dialog('widget');
},
/** /**
* The edge of the screen that the dialog should appear on. * Adjusts the body padding when the dialog is resized.
* *
* @return {string} * @param {jQuery.Event} event
* The edge the tray will be shown on, left or right. * The event triggered.
*/ */
getEdge() { function bodyPadding(event) {
return document.documentElement.dir === 'rtl' ? 'left' : 'right'; if ($('body').outerWidth() < minDisplaceWidth) {
}, return;
}; }
const $element = event.data.$element;
const $widget = $element.dialog('widget');
const width = $widget.outerWidth();
const mainCanvasPadding = $mainCanvasWrapper.css(`padding-${edge}`);
if (width !== mainCanvasPadding) {
$mainCanvasWrapper.css(`padding-${edge}`, `${width}px`);
$widget.attr(`data-offset-${edge}`, width);
displace();
}
}
/** /**
* Attaches off-canvas dialog behaviors. * Attaches off-canvas dialog behaviors.
...@@ -233,25 +114,51 @@ ...@@ -233,25 +114,51 @@
* Attaches event listeners for off-canvas dialogs. * Attaches event listeners for off-canvas dialogs.
*/ */
Drupal.behaviors.offCanvasEvents = { Drupal.behaviors.offCanvasEvents = {
attach: () => { attach() {
$(window).once('off-canvas').on({ $(window).once('off-canvas').on({
'dialog:beforecreate': (event, dialog, $element, settings) => { 'dialog:aftercreate': function (event, dialog, $element, settings) {
if (Drupal.offCanvas.isOffCanvas($element)) { if ($element.is('#drupal-off-canvas')) {
Drupal.offCanvas.beforeCreate({ dialog, $element, settings }); const eventData = { settings, $element };
$('.ui-dialog-off-canvas, .ui-dialog-off-canvas .ui-dialog-titlebar').toggleClass('ui-dialog-empty-title', !settings.title);
$element
.on('dialogresize.off-canvas', eventData, debounce(bodyPadding, 100))
.on('dialogContentResize.off-canvas', eventData, handleDialogResize)
.on('dialogContentResize.off-canvas', eventData, debounce(bodyPadding, 100))
.trigger('dialogresize.off-canvas');
$element.dialog('widget').attr(`data-offset-${edge}`, '');
$(window)
.on('resize.off-canvas scroll.off-canvas', eventData, debounce(resetSize, 100))
.trigger('resize.off-canvas');
} }
}, },
'dialog:aftercreate': (event, dialog, $element, settings) => { 'dialog:beforecreate': function (event, dialog, $element, settings) {
if (Drupal.offCanvas.isOffCanvas($element)) { if ($element.is('#drupal-off-canvas')) {
Drupal.offCanvas.render({ dialog, $element, settings }); $('body').addClass('js-tray-open');
Drupal.offCanvas.afterCreate({ $element, settings }); // @see http://api.jqueryui.com/position/
settings.position = {
my: 'left top',
at: `${edge} top`,
of: window,
};
settings.dialogClass += ' ui-dialog-off-canvas';
// Applies initial height to dialog based on window height.
// See http://api.jqueryui.com/dialog for all dialog options.
settings.height = $(window).height();
} }
}, },
'dialog:beforeclose': (event, dialog, $element) => { 'dialog:beforeclose': function (event, dialog, $element) {
if (Drupal.offCanvas.isOffCanvas($element)) { if ($element.is('#drupal-off-canvas')) {
Drupal.offCanvas.beforeClose({ dialog, $element }); $('body').removeClass('js-tray-open');
// Remove all *.off-canvas events
$(document).off('.off-canvas');
$(window).off('.off-canvas');
$mainCanvasWrapper.css(`padding-${edge}`, 0);
} }
}, },
}); });
}, },
}; };
})(jQuery, Drupal, Drupal.debounce, Drupal.displace); }(jQuery, Drupal, Drupal.debounce, Drupal.displace));
...@@ -6,131 +6,106 @@ ...@@ -6,131 +6,106 @@
**/ **/
(function ($, Drupal, debounce, displace) { (function ($, Drupal, debounce, displace) {
Drupal.offCanvas = { var minDisplaceWidth = 768;
minDisplaceWidth: 768,
$mainCanvasWrapper: $('[data-off-canvas-main-canvas]'), var edge = document.documentElement.dir === 'rtl' ? 'left' : 'right';
isOffCanvas: function isOffCanvas($element) { var $mainCanvasWrapper = $('[data-off-canvas-main-canvas]');
return $element.is('#drupal-off-canvas');
},
beforeCreate: function beforeCreate(_ref) {
var settings = _ref.settings;
$('body').addClass('js-tray-open'); function resetSize(event) {
settings.dialogClass += ' ui-dialog-off-canvas'; var offsets = displace.offsets;
var $element = event.data.$element;
var $widget = $element.dialog('widget');
var $elementScroll = $element.scrollTop();
settings.position = { var adjustedOptions = {
my: 'left top', position: {
at: Drupal.offCanvas.getEdge() + ' top', my: edge + ' top',
at: edge + ' top' + (offsets.top !== 0 ? '+' + offsets.top : ''),
of: window of: window
}; }
};
settings.height = $(window).height();
},
beforeClose: function beforeClose() {
$('body').removeClass('js-tray-open');
$(document).off('.off-canvas');
$(window).off('.off-canvas');
Drupal.offCanvas.$mainCanvasWrapper.css('padding-' + Drupal.offCanvas.getEdge(), 0);
},
afterCreate: function afterCreate(_ref2) {
var $element = _ref2.$element,
settings = _ref2.settings;
var eventData = { settings: settings, $element: $element, offCanvasDialog: this };
$element.on('dialogresize.off-canvas', eventData, debounce(Drupal.offCanvas.bodyPadding, 100)).on('dialogContentResize.off-canvas', eventData, Drupal.offCanvas.handleDialogResize).on('dialogContentResize.off-canvas', eventData, debounce(Drupal.offCanvas.bodyPadding, 100)).trigger('dialogresize.off-canvas');
Drupal.offCanvas.getContainer($element).attr('data-offset-' + Drupal.offCanvas.getEdge(), '');
$(window).on('resize.off-canvas scroll.off-canvas', eventData, debounce(Drupal.offCanvas.resetSize, 100)).trigger('resize.off-canvas'); $widget.css({
}, position: 'fixed',
render: function render(_ref3) { height: $(window).height() - (offsets.top + offsets.bottom) + 'px'
var settings = _ref3.settings; });
$('.ui-dialog-off-canvas, .ui-dialog-off-canvas .ui-dialog-titlebar').toggleClass('ui-dialog-empty-title', !settings.title); $element.dialog('option', adjustedOptions).trigger('dialogContentResize.off-canvas');
},
handleDialogResize: function handleDialogResize(event) {
var $element = event.data.$element;
var $container = Drupal.offCanvas.getContainer($element);
var $offsets = $container.find('> :not(#drupal-off-canvas, .ui-resizable-handle)'); $element.scrollTop($elementScroll);
var offset = 0; }
$element.css({ height: 'auto' }); function handleDialogResize(event) {
var modalHeight = $container.height(); var $element = event.data.$element;
var $widget = $element.dialog('widget');
$offsets.each(function (i, e) { var $offsets = $widget.find('> :not(#drupal-off-canvas, .ui-resizable-handle)');
offset += $(e).outerHeight(); var offset = 0;
}); var modalHeight = void 0;
var scrollOffset = $element.outerHeight() - $element.height(); $element.css({ height: 'auto' });
$element.height(modalHeight - offset - scrollOffset); modalHeight = $widget.height();
}, $offsets.each(function () {
resetSize: function resetSize(event) { offset += $(this).outerHeight();
var offsets = displace.offsets; });
var $element = event.data.$element;
var container = Drupal.offCanvas.getContainer($element);
var topPosition = offsets.top !== 0 ? '+' + offsets.top : '';
var adjustedOptions = {
position: {
my: Drupal.offCanvas.getEdge() + ' top',
at: Drupal.offCanvas.getEdge() + ' top' + topPosition,
of: window
}
};
container.css({ var scrollOffset = $element.outerHeight() - $element.height();
position: 'fixed', $element.height(modalHeight - offset - scrollOffset);
height: $(window).height() - (offsets.top + offsets.bottom) + 'px' }
});
$element.dialog('option', adjustedOptions).trigger('dialogContentResize.off-canvas'); function bodyPadding(event) {
}, if ($('body').outerWidth() < minDisplaceWidth) {
bodyPadding: function bodyPadding(event) { return;
if ($('body').outerWidth() < Drupal.offCanvas.minDisplaceWidth) {
return;
}
var $element = event.data.$element;
var $container = Drupal.offCanvas.getContainer($element);
var $mainCanvasWrapper = Drupal.offCanvas.$mainCanvasWrapper;
var width = $container.outerWidth();
var mainCanvasPadding = $mainCanvasWrapper.css('padding-' + Drupal.offCanvas.getEdge());
if (width !== mainCanvasPadding) {
$mainCanvasWrapper.css('padding-' + Drupal.offCanvas.getEdge(), width + 'px');
$container.attr('data-offset-' + Drupal.offCanvas.getEdge(), width);
displace();
}
},
getContainer: function getContainer($element) {
return $element.dialog('widget');
},
getEdge: function getEdge() {
return document.documentElement.dir === 'rtl' ? 'left' : 'right';
} }
}; var $element = event.data.$element;
var $widget = $element.dialog('widget');
var width = $widget.outerWidth();
var mainCanvasPadding = $mainCanvasWrapper.css('padding-' + edge);
if (width !== mainCanvasPadding) {
$mainCanvasWrapper.css('padding-' + edge, width + 'px');
$widget.attr('data-offset-' + edge, width);
displace();
}
}
Drupal.behaviors.offCanvasEvents = { Drupal.behaviors.offCanvasEvents = {
attach: function attach() { attach: function attach() {
$(window).once('off-canvas').on({ $(window).once('off-canvas').on({
'dialog:beforecreate': function dialogBeforecreate(event, dialog, $element, settings) { 'dialog:aftercreate': function dialogAftercreate(event, dialog, $element, settings) {
if (Drupal.offCanvas.isOffCanvas($element)) { if ($element.is('#drupal-off-canvas')) {
Drupal.offCanvas.beforeCreate({ dialog: dialog, $element: $element, settings: settings }); var eventData = { settings: settings, $element: $element };
$('.ui-dialog-off-canvas, .ui-dialog-off-canvas .ui-dialog-titlebar').toggleClass('ui-dialog-empty-title', !settings.title);
$element.on('dialogresize.off-canvas', eventData, debounce(bodyPadding, 100)).on('dialogContentResize.off-canvas', eventData, handleDialogResize).on('dialogContentResize.off-canvas', eventData, debounce(bodyPadding, 100)).trigger('dialogresize.off-canvas');
$element.dialog('widget').attr('data-offset-' + edge, '');
$(window).on('resize.off-canvas scroll.off-canvas', eventData, debounce(resetSize, 100)).trigger('resize.off-canvas');
} }
}, },
'dialog:aftercreate': function dialogAftercreate(event, dialog, $element, settings) { 'dialog:beforecreate': function dialogBeforecreate(event, dialog, $element, settings) {
if (Drupal.offCanvas.isOffCanvas($element)) { if ($element.is('#drupal-off-canvas')) {
Drupal.offCanvas.render({ dialog: dialog, $element: $element, settings: settings }); $('body').addClass('js-tray-open');
Drupal.offCanvas.afterCreate({ $element: $element, settings: settings });
settings.position = {
my: 'left top',
at: edge + ' top',
of: window
};
settings.dialogClass += ' ui-dialog-off-canvas';
settings.height = $(window).height();
} }
}, },
'dialog:beforeclose': function dialogBeforeclose(event, dialog, $element) { 'dialog:beforeclose': function dialogBeforeclose