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
12 (($, Drupal, debounce, displace) => {
14 * Off-canvas dialog implementation using jQuery Dialog.
16 * Transforms the regular dialogs created using Drupal.dialog when the dialog
17 * element equals '#drupal-off-canvas' into an side-loading dialog.
24 * The minimum width to use body displace needs to match the width at which
25 * the tray will be %100 width. @see settings_tray.module.css
28 minDisplaceWidth: 768,
31 * Wrapper used to position off-canvas dialog.
34 $mainCanvasWrapper: $('[data-off-canvas-main-canvas]'),
37 * Determines if an element is an off-canvas dialog.
39 * @param {jQuery} $element
42 * True this is currently an off-canvas dialog.
44 isOffCanvas($element) {
45 return $element.is('#drupal-off-canvas');
49 * Remove off-canvas dialog events.
51 * @param {jQuery} $element
54 removeOffCanvasEvents($element) {
55 $element.off('.off-canvas');
56 $(document).off('.off-canvas');
57 $(window).off('.off-canvas');
61 * Handler fired before an off-canvas dialog has been opened.
62 * @param {Object} settings
63 * Settings related to the composition of the dialog.
66 beforeCreate({ settings, $element }) {
67 // Clean up previous dialog event handlers.
68 Drupal.offCanvas.removeOffCanvasEvents($element);
70 $('body').addClass('js-tray-open');
71 settings.dialogClass += ' ui-dialog-off-canvas';
72 // @see http://api.jqueryui.com/position/
75 at: `${Drupal.offCanvas.getEdge()} top`,
80 * Applies initial height to dialog based on window height.
81 * @see http://api.jqueryui.com/dialog for all dialog options.
83 settings.height = $(window).height();
87 * Handler fired after an off-canvas dialog has been closed.
90 beforeClose({ $element }) {
91 $('body').removeClass('js-tray-open');
92 // Remove all *.off-canvas events
93 Drupal.offCanvas.removeOffCanvasEvents($element);
95 Drupal.offCanvas.$mainCanvasWrapper.css(`padding-${Drupal.offCanvas.getEdge()}`, 0);
99 * Handler fired when an off-canvas dialog has been opened.
100 * @param {jQuery} $element
101 * The off-canvas dialog element.
102 * @param {Object} settings
103 * Settings related to the composition of the dialog.
104 * @return {undefined}
106 afterCreate({ $element, settings }) {
107 const eventData = { settings, $element, offCanvasDialog: this };
110 .on('dialogContentResize.off-canvas', eventData, Drupal.offCanvas.handleDialogResize)
111 .on('dialogContentResize.off-canvas', eventData, Drupal.offCanvas.bodyPadding);
113 Drupal.offCanvas.getContainer($element).attr(`data-offset-${Drupal.offCanvas.getEdge()}`, '');
116 .on('resize.off-canvas', eventData, debounce(Drupal.offCanvas.resetSize, 100))
117 .trigger('resize.off-canvas');
121 * Toggle classes based on title existence.
122 * Called with Drupal.offCanvas.afterCreate.
123 * @param {Object} settings
124 * Settings related to the composition of the dialog.
125 * @return {undefined}
127 render({ settings }) {
128 $('.ui-dialog-off-canvas, .ui-dialog-off-canvas .ui-dialog-titlebar').toggleClass('ui-dialog-empty-title', !settings.title);
132 * Adjusts the dialog on resize.
134 * @param {jQuery.Event} event
135 * The event triggered.
136 * @param {object} event.data
137 * Data attached to the event.
139 handleDialogResize(event) {
140 const $element = event.data.$element;
141 const $container = Drupal.offCanvas.getContainer($element);
143 const $offsets = $container.find('> :not(#drupal-off-canvas, .ui-resizable-handle)');
146 // Let scroll element take all the height available.
147 $element.css({ height: 'auto' });
148 const modalHeight = $container.height();
150 $offsets.each((i, e) => {
151 offset += $(e).outerHeight();
154 // Take internal padding into account.
155 const scrollOffset = $element.outerHeight() - $element.height();
156 $element.height(modalHeight - offset - scrollOffset);
160 * Resets the size of the dialog.
162 * @param {jQuery.Event} event
163 * The event triggered.
164 * @param {object} event.data
165 * Data attached to the event.
168 const offsets = displace.offsets;
169 const $element = event.data.$element;
170 const container = Drupal.offCanvas.getContainer($element);
172 const topPosition = (offsets.top !== 0 ? `+${offsets.top}` : '');
173 const adjustedOptions = {
174 // @see http://api.jqueryui.com/position/
176 my: `${Drupal.offCanvas.getEdge()} top`,
177 at: `${Drupal.offCanvas.getEdge()} top${topPosition}`,
184 height: `${$(window).height() - (offsets.top + offsets.bottom)}px`,
188 .dialog('option', adjustedOptions)
189 .trigger('dialogContentResize.off-canvas');
193 * Adjusts the body padding when the dialog is resized.
195 * @param {jQuery.Event} event
196 * The event triggered.
197 * @param {object} event.data
198 * Data attached to the event.
201 if ($('body').outerWidth() < Drupal.offCanvas.minDisplaceWidth) {
204 const $element = event.data.$element;
205 const $container = Drupal.offCanvas.getContainer($element);
206 const $mainCanvasWrapper = Drupal.offCanvas.$mainCanvasWrapper;
208 const width = $container.outerWidth();
209 const mainCanvasPadding = $mainCanvasWrapper.css(`padding-${Drupal.offCanvas.getEdge()}`);
210 if (width !== mainCanvasPadding) {
211 $mainCanvasWrapper.css(`padding-${Drupal.offCanvas.getEdge()}`, `${width}px`);
212 $container.attr(`data-offset-${Drupal.offCanvas.getEdge()}`, width);
218 * The HTML element that surrounds the dialog.
219 * @param {HTMLElement} $element
220 * The dialog element.
222 * @return {HTMLElement}
223 * The containing element.
225 getContainer($element) {
226 return $element.dialog('widget');
230 * The edge of the screen that the dialog should appear on.
233 * The edge the tray will be shown on, left or right.
236 return document.documentElement.dir === 'rtl' ? 'left' : 'right';
241 * Attaches off-canvas dialog behaviors.
243 * @type {Drupal~behavior}
245 * @prop {Drupal~behaviorAttach} attach
246 * Attaches event listeners for off-canvas dialogs.
248 Drupal.behaviors.offCanvasEvents = {
250 $(window).once('off-canvas').on({
251 'dialog:beforecreate': (event, dialog, $element, settings) => {
252 if (Drupal.offCanvas.isOffCanvas($element)) {
253 Drupal.offCanvas.beforeCreate({ dialog, $element, settings });
256 'dialog:aftercreate': (event, dialog, $element, settings) => {
257 if (Drupal.offCanvas.isOffCanvas($element)) {
258 Drupal.offCanvas.render({ dialog, $element, settings });
259 Drupal.offCanvas.afterCreate({ $element, settings });
262 'dialog:beforeclose': (event, dialog, $element) => {
263 if (Drupal.offCanvas.isOffCanvas($element)) {
264 Drupal.offCanvas.beforeClose({ dialog, $element });
270 })(jQuery, Drupal, Drupal.debounce, Drupal.displace);