3 * Drupal's off-canvas library.
6 (($, Drupal, debounce, displace) => {
8 * Off-canvas dialog implementation using jQuery Dialog.
10 * Transforms the regular dialogs created using Drupal.dialog when the dialog
11 * element equals '#drupal-off-canvas' into an side-loading dialog.
17 * Storage for position information about the tray.
24 * The minimum height of the tray when opened at the top of the page.
31 * The minimum width to use body displace needs to match the width at which
32 * the tray will be 100% width. @see core/misc/dialog/off-canvas.css
36 minDisplaceWidth: 768,
39 * Wrapper used to position off-canvas dialog.
43 $mainCanvasWrapper: $('[data-off-canvas-main-canvas]'),
46 * Determines if an element is an off-canvas dialog.
48 * @param {jQuery} $element
52 * True this is currently an off-canvas dialog.
54 isOffCanvas($element) {
55 return $element.is('#drupal-off-canvas');
59 * Remove off-canvas dialog events.
61 * @param {jQuery} $element
64 removeOffCanvasEvents($element) {
65 $element.off('.off-canvas');
66 $(document).off('.off-canvas');
67 $(window).off('.off-canvas');
71 * Handler fired before an off-canvas dialog has been opened.
73 * @param {Object} settings
74 * Settings related to the composition of the dialog.
78 beforeCreate({ settings, $element }) {
79 // Clean up previous dialog event handlers.
80 Drupal.offCanvas.removeOffCanvasEvents($element);
82 $('body').addClass('js-off-canvas-dialog-open');
83 // @see http://api.jqueryui.com/position/
86 at: `${Drupal.offCanvas.getEdge()} top`,
91 * Applies initial height and with to dialog based depending on position.
92 * @see http://api.jqueryui.com/dialog for all dialog options.
94 const position = settings.drupalOffCanvasPosition;
95 const height = position === 'side' ? $(window).height() : settings.height;
96 const width = position === 'side' ? settings.width : '100%';
97 settings.height = height;
98 settings.width = width;
102 * Handler fired after an off-canvas dialog has been closed.
104 * @return {undefined}
106 beforeClose({ $element }) {
107 $('body').removeClass('js-off-canvas-dialog-open');
108 // Remove all *.off-canvas events
109 Drupal.offCanvas.removeOffCanvasEvents($element);
110 Drupal.offCanvas.resetPadding();
114 * Handler fired when an off-canvas dialog has been opened.
116 * @param {jQuery} $element
117 * The off-canvas dialog element.
118 * @param {Object} settings
119 * Settings related to the composition of the dialog.
121 * @return {undefined}
123 afterCreate({ $element, settings }) {
124 const eventData = { settings, $element, offCanvasDialog: this };
128 'dialogContentResize.off-canvas',
130 Drupal.offCanvas.handleDialogResize,
133 'dialogContentResize.off-canvas',
135 Drupal.offCanvas.bodyPadding,
139 .getContainer($element)
140 .attr(`data-offset-${Drupal.offCanvas.getEdge()}`, '');
146 debounce(Drupal.offCanvas.resetSize, 100),
148 .trigger('resize.off-canvas');
152 * Toggle classes based on title existence.
153 * Called with Drupal.offCanvas.afterCreate.
155 * @param {Object} settings
156 * Settings related to the composition of the dialog.
158 * @return {undefined}
160 render({ settings }) {
162 '.ui-dialog-off-canvas, .ui-dialog-off-canvas .ui-dialog-titlebar',
163 ).toggleClass('ui-dialog-empty-title', !settings.title);
167 * Adjusts the dialog on resize.
169 * @param {jQuery.Event} event
170 * The event triggered.
171 * @param {object} event.data
172 * Data attached to the event.
174 handleDialogResize(event) {
175 const $element = event.data.$element;
176 const $container = Drupal.offCanvas.getContainer($element);
178 const $offsets = $container.find(
179 '> :not(#drupal-off-canvas, .ui-resizable-handle)',
183 // Let scroll element take all the height available.
184 $element.css({ height: 'auto' });
185 const modalHeight = $container.height();
187 $offsets.each((i, e) => {
188 offset += $(e).outerHeight();
191 // Take internal padding into account.
192 const scrollOffset = $element.outerHeight() - $element.height();
193 $element.height(modalHeight - offset - scrollOffset);
197 * Resets the size of the dialog.
199 * @param {jQuery.Event} event
200 * The event triggered.
201 * @param {object} event.data
202 * Data attached to the event.
205 const $element = event.data.$element;
206 const container = Drupal.offCanvas.getContainer($element);
207 const position = event.data.settings.drupalOffCanvasPosition;
209 // Only remove the `data-offset-*` attribute if the value previously
210 // exists and the orientation is changing.
211 if (Drupal.offCanvas.position && Drupal.offCanvas.position !== position) {
212 container.removeAttr(`data-offset-${Drupal.offCanvas.position}`);
214 // Set a minimum height on $element
215 if (position === 'top') {
216 $element.css('min-height', `${Drupal.offCanvas.minimumHeight}px`);
221 const offsets = displace.offsets;
224 position === 'side' && offsets.top !== 0 ? `+${offsets.top}` : '';
225 const adjustedOptions = {
226 // @see http://api.jqueryui.com/position/
228 my: `${Drupal.offCanvas.getEdge()} top`,
229 at: `${Drupal.offCanvas.getEdge()} top${topPosition}`,
236 ? `${$(window).height() - (offsets.top + offsets.bottom)}px`
237 : event.data.settings.height;
244 .dialog('option', adjustedOptions)
245 .trigger('dialogContentResize.off-canvas');
247 Drupal.offCanvas.position = position;
251 * Adjusts the body padding when the dialog is resized.
253 * @param {jQuery.Event} event
254 * The event triggered.
255 * @param {object} event.data
256 * Data attached to the event.
259 const position = event.data.settings.drupalOffCanvasPosition;
261 position === 'side' &&
262 $('body').outerWidth() < Drupal.offCanvas.minDisplaceWidth
266 Drupal.offCanvas.resetPadding();
267 const $element = event.data.$element;
268 const $container = Drupal.offCanvas.getContainer($element);
269 const $mainCanvasWrapper = Drupal.offCanvas.$mainCanvasWrapper;
271 const width = $container.outerWidth();
272 const mainCanvasPadding = $mainCanvasWrapper.css(
273 `padding-${Drupal.offCanvas.getEdge()}`,
275 if (position === 'side' && width !== mainCanvasPadding) {
276 $mainCanvasWrapper.css(
277 `padding-${Drupal.offCanvas.getEdge()}`,
280 $container.attr(`data-offset-${Drupal.offCanvas.getEdge()}`, width);
284 const height = $container.outerHeight();
285 if (position === 'top') {
286 $mainCanvasWrapper.css('padding-top', `${height}px`);
287 $container.attr('data-offset-top', height);
293 * The HTML element that surrounds the dialog.
294 * @param {HTMLElement} $element
295 * The dialog element.
297 * @return {HTMLElement}
298 * The containing element.
300 getContainer($element) {
301 return $element.dialog('widget');
305 * The edge of the screen that the dialog should appear on.
308 * The edge the tray will be shown on, left or right.
311 return document.documentElement.dir === 'rtl' ? 'left' : 'right';
315 * Resets main canvas wrapper and toolbar padding / margin.
318 Drupal.offCanvas.$mainCanvasWrapper.css(
319 `padding-${Drupal.offCanvas.getEdge()}`,
322 Drupal.offCanvas.$mainCanvasWrapper.css('padding-top', 0);
328 * Attaches off-canvas dialog behaviors.
330 * @type {Drupal~behavior}
332 * @prop {Drupal~behaviorAttach} attach
333 * Attaches event listeners for off-canvas dialogs.
335 Drupal.behaviors.offCanvasEvents = {
340 'dialog:beforecreate': (event, dialog, $element, settings) => {
341 if (Drupal.offCanvas.isOffCanvas($element)) {
342 Drupal.offCanvas.beforeCreate({ dialog, $element, settings });
345 'dialog:aftercreate': (event, dialog, $element, settings) => {
346 if (Drupal.offCanvas.isOffCanvas($element)) {
347 Drupal.offCanvas.render({ dialog, $element, settings });
348 Drupal.offCanvas.afterCreate({ $element, settings });
351 'dialog:beforeclose': (event, dialog, $element) => {
352 if (Drupal.offCanvas.isOffCanvas($element)) {
353 Drupal.offCanvas.beforeClose({ dialog, $element });
359 })(jQuery, Drupal, Drupal.debounce, Drupal.displace);