3 * Drupal's off-canvas library.
5 * @todo This functionality should extracted into a new core library or a part
6 * of the current drupal.dialog.ajax library.
7 * https://www.drupal.org/node/2784443
10 (function ($, Drupal, debounce, displace) {
14 // The minimum width to use body displace needs to match the width at which
15 // the tray will be %100 width. @see outside_in.module.css
16 var minDisplaceWidth = 768;
19 * The edge of the screen that the dialog should appear on.
23 var edge = document.documentElement.dir === 'rtl' ? 'left' : 'right';
25 var $mainCanvasWrapper = $('[data-off-canvas-main-canvas]');
28 * Resets the size of the dialog.
30 * @param {jQuery.Event} event
31 * The event triggered.
33 function resetSize(event) {
34 var offsets = displace.offsets;
35 var $element = event.data.$element;
36 var $widget = $element.dialog('widget');
38 var adjustedOptions = {
39 // @see http://api.jqueryui.com/position/
42 at: edge + ' top' + (offsets.top !== 0 ? '+' + offsets.top : ''),
49 height: ($(window).height() - (offsets.top + offsets.bottom)) + 'px'
53 .dialog('option', adjustedOptions)
54 .trigger('dialogContentResize.off-canvas');
58 * Adjusts the dialog on resize.
60 * @param {jQuery.Event} event
61 * The event triggered.
63 function handleDialogResize(event) {
64 var $element = event.data.$element;
65 var $widget = $element.dialog('widget');
67 var $offsets = $widget.find('> :not(#drupal-off-canvas, .ui-resizable-handle)');
71 // Let scroll element take all the height available.
72 $element.css({height: 'auto'});
73 modalHeight = $widget.height();
74 $offsets.each(function () { offset += $(this).outerHeight(); });
76 // Take internal padding into account.
77 var scrollOffset = $element.outerHeight() - $element.height();
78 $element.height(modalHeight - offset - scrollOffset);
82 * Adjusts the body padding when the dialog is resized.
84 * @param {jQuery.Event} event
85 * The event triggered.
87 function bodyPadding(event) {
88 if ($('body').outerWidth() < minDisplaceWidth) {
91 var $element = event.data.$element;
92 var $widget = $element.dialog('widget');
94 var width = $widget.outerWidth();
95 var mainCanvasPadding = $mainCanvasWrapper.css('padding-' + edge);
96 if (width !== mainCanvasPadding) {
97 $mainCanvasWrapper.css('padding-' + edge, width + 'px');
98 $widget.attr('data-offset-' + edge, width);
104 * Attaches off-canvas dialog behaviors.
106 * @type {Drupal~behavior}
108 * @prop {Drupal~behaviorAttach} attach
109 * Attaches event listeners for off-canvas dialogs.
111 Drupal.behaviors.offCanvasEvents = {
112 attach: function () {
113 $(window).once('off-canvas').on({
114 'dialog:aftercreate': function (event, dialog, $element, settings) {
115 if ($element.is('#drupal-off-canvas')) {
116 var eventData = {settings: settings, $element: $element};
117 $('.ui-dialog-off-canvas, .ui-dialog-off-canvas .ui-dialog-titlebar').toggleClass('ui-dialog-empty-title', !settings.title);
120 .on('dialogresize.off-canvas', eventData, debounce(bodyPadding, 100))
121 .on('dialogContentResize.off-canvas', eventData, handleDialogResize)
122 .on('dialogContentResize.off-canvas', eventData, debounce(bodyPadding, 100))
123 .trigger('dialogresize.off-canvas');
125 $element.dialog('widget').attr('data-offset-' + edge, '');
128 .on('resize.off-canvas scroll.off-canvas', eventData, debounce(resetSize, 100))
129 .trigger('resize.off-canvas');
132 'dialog:beforecreate': function (event, dialog, $element, settings) {
133 if ($element.is('#drupal-off-canvas')) {
134 $('body').addClass('js-tray-open');
135 // @see http://api.jqueryui.com/position/
136 settings.position = {
141 settings.dialogClass += ' ui-dialog-off-canvas';
142 // Applies initial height to dialog based on window height.
143 // See http://api.jqueryui.com/dialog for all dialog options.
144 settings.height = $(window).height();
147 'dialog:beforeclose': function (event, dialog, $element) {
148 if ($element.is('#drupal-off-canvas')) {
149 $('body').removeClass('js-tray-open');
150 // Remove all *.off-canvas events
151 $(document).off('.off-canvas');
152 $(window).off('.off-canvas');
153 $mainCanvasWrapper.css('padding-' + edge, 0);
160 })(jQuery, Drupal, Drupal.debounce, Drupal.displace);