comparison core/misc/dialog/off-canvas.es6.js @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents 1fec387a4317
children
comparison
equal deleted inserted replaced
16:c2387f117808 17:129ea1e6d783
11 * element equals '#drupal-off-canvas' into an side-loading dialog. 11 * element equals '#drupal-off-canvas' into an side-loading dialog.
12 * 12 *
13 * @namespace 13 * @namespace
14 */ 14 */
15 Drupal.offCanvas = { 15 Drupal.offCanvas = {
16 /**
17 * Storage for position information about the tray.
18 *
19 * @type {?String}
20 */
21 position: null,
22
23 /**
24 * The minimum height of the tray when opened at the top of the page.
25 *
26 * @type {Number}
27 */
28 minimumHeight: 30,
16 29
17 /** 30 /**
18 * The minimum width to use body displace needs to match the width at which 31 * The minimum width to use body displace needs to match the width at which
19 * the tray will be 100% width. @see core/misc/dialog/off-canvas.css 32 * the tray will be 100% width. @see core/misc/dialog/off-canvas.css
20 * 33 *
73 at: `${Drupal.offCanvas.getEdge()} top`, 86 at: `${Drupal.offCanvas.getEdge()} top`,
74 of: window, 87 of: window,
75 }; 88 };
76 89
77 /** 90 /**
78 * Applies initial height to dialog based on window height. 91 * Applies initial height and with to dialog based depending on position.
79 * @see http://api.jqueryui.com/dialog for all dialog options. 92 * @see http://api.jqueryui.com/dialog for all dialog options.
80 */ 93 */
81 settings.height = $(window).height(); 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;
82 }, 99 },
83 100
84 /** 101 /**
85 * Handler fired after an off-canvas dialog has been closed. 102 * Handler fired after an off-canvas dialog has been closed.
86 * 103 *
88 */ 105 */
89 beforeClose({ $element }) { 106 beforeClose({ $element }) {
90 $('body').removeClass('js-off-canvas-dialog-open'); 107 $('body').removeClass('js-off-canvas-dialog-open');
91 // Remove all *.off-canvas events 108 // Remove all *.off-canvas events
92 Drupal.offCanvas.removeOffCanvasEvents($element); 109 Drupal.offCanvas.removeOffCanvasEvents($element);
93 110 Drupal.offCanvas.resetPadding();
94 Drupal.offCanvas.$mainCanvasWrapper.css(`padding-${Drupal.offCanvas.getEdge()}`, 0);
95 }, 111 },
96 112
97 /** 113 /**
98 * Handler fired when an off-canvas dialog has been opened. 114 * Handler fired when an off-canvas dialog has been opened.
99 * 115 *
106 */ 122 */
107 afterCreate({ $element, settings }) { 123 afterCreate({ $element, settings }) {
108 const eventData = { settings, $element, offCanvasDialog: this }; 124 const eventData = { settings, $element, offCanvasDialog: this };
109 125
110 $element 126 $element
111 .on('dialogContentResize.off-canvas', eventData, Drupal.offCanvas.handleDialogResize) 127 .on(
112 .on('dialogContentResize.off-canvas', eventData, Drupal.offCanvas.bodyPadding); 128 'dialogContentResize.off-canvas',
113 129 eventData,
114 Drupal.offCanvas.getContainer($element).attr(`data-offset-${Drupal.offCanvas.getEdge()}`, ''); 130 Drupal.offCanvas.handleDialogResize,
131 )
132 .on(
133 'dialogContentResize.off-canvas',
134 eventData,
135 Drupal.offCanvas.bodyPadding,
136 );
137
138 Drupal.offCanvas
139 .getContainer($element)
140 .attr(`data-offset-${Drupal.offCanvas.getEdge()}`, '');
115 141
116 $(window) 142 $(window)
117 .on('resize.off-canvas', eventData, debounce(Drupal.offCanvas.resetSize, 100)) 143 .on(
144 'resize.off-canvas',
145 eventData,
146 debounce(Drupal.offCanvas.resetSize, 100),
147 )
118 .trigger('resize.off-canvas'); 148 .trigger('resize.off-canvas');
119 }, 149 },
120 150
121 /** 151 /**
122 * Toggle classes based on title existence. 152 * Toggle classes based on title existence.
126 * Settings related to the composition of the dialog. 156 * Settings related to the composition of the dialog.
127 * 157 *
128 * @return {undefined} 158 * @return {undefined}
129 */ 159 */
130 render({ settings }) { 160 render({ settings }) {
131 $('.ui-dialog-off-canvas, .ui-dialog-off-canvas .ui-dialog-titlebar').toggleClass('ui-dialog-empty-title', !settings.title); 161 $(
162 '.ui-dialog-off-canvas, .ui-dialog-off-canvas .ui-dialog-titlebar',
163 ).toggleClass('ui-dialog-empty-title', !settings.title);
132 }, 164 },
133 165
134 /** 166 /**
135 * Adjusts the dialog on resize. 167 * Adjusts the dialog on resize.
136 * 168 *
141 */ 173 */
142 handleDialogResize(event) { 174 handleDialogResize(event) {
143 const $element = event.data.$element; 175 const $element = event.data.$element;
144 const $container = Drupal.offCanvas.getContainer($element); 176 const $container = Drupal.offCanvas.getContainer($element);
145 177
146 const $offsets = $container.find('> :not(#drupal-off-canvas, .ui-resizable-handle)'); 178 const $offsets = $container.find(
179 '> :not(#drupal-off-canvas, .ui-resizable-handle)',
180 );
147 let offset = 0; 181 let offset = 0;
148 182
149 // Let scroll element take all the height available. 183 // Let scroll element take all the height available.
150 $element.css({ height: 'auto' }); 184 $element.css({ height: 'auto' });
151 const modalHeight = $container.height(); 185 const modalHeight = $container.height();
166 * The event triggered. 200 * The event triggered.
167 * @param {object} event.data 201 * @param {object} event.data
168 * Data attached to the event. 202 * Data attached to the event.
169 */ 203 */
170 resetSize(event) { 204 resetSize(event) {
171 const offsets = displace.offsets;
172 const $element = event.data.$element; 205 const $element = event.data.$element;
173 const container = Drupal.offCanvas.getContainer($element); 206 const container = Drupal.offCanvas.getContainer($element);
174 207 const position = event.data.settings.drupalOffCanvasPosition;
175 const topPosition = (offsets.top !== 0 ? `+${offsets.top}` : ''); 208
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}`);
213 }
214 // Set a minimum height on $element
215 if (position === 'top') {
216 $element.css('min-height', `${Drupal.offCanvas.minimumHeight}px`);
217 }
218
219 displace();
220
221 const offsets = displace.offsets;
222
223 const topPosition =
224 position === 'side' && offsets.top !== 0 ? `+${offsets.top}` : '';
176 const adjustedOptions = { 225 const adjustedOptions = {
177 // @see http://api.jqueryui.com/position/ 226 // @see http://api.jqueryui.com/position/
178 position: { 227 position: {
179 my: `${Drupal.offCanvas.getEdge()} top`, 228 my: `${Drupal.offCanvas.getEdge()} top`,
180 at: `${Drupal.offCanvas.getEdge()} top${topPosition}`, 229 at: `${Drupal.offCanvas.getEdge()} top${topPosition}`,
181 of: window, 230 of: window,
182 }, 231 },
183 }; 232 };
184 233
234 const height =
235 position === 'side'
236 ? `${$(window).height() - (offsets.top + offsets.bottom)}px`
237 : event.data.settings.height;
185 container.css({ 238 container.css({
186 position: 'fixed', 239 position: 'fixed',
187 height: `${$(window).height() - (offsets.top + offsets.bottom)}px`, 240 height,
188 }); 241 });
189 242
190 $element 243 $element
191 .dialog('option', adjustedOptions) 244 .dialog('option', adjustedOptions)
192 .trigger('dialogContentResize.off-canvas'); 245 .trigger('dialogContentResize.off-canvas');
246
247 Drupal.offCanvas.position = position;
193 }, 248 },
194 249
195 /** 250 /**
196 * Adjusts the body padding when the dialog is resized. 251 * Adjusts the body padding when the dialog is resized.
197 * 252 *
199 * The event triggered. 254 * The event triggered.
200 * @param {object} event.data 255 * @param {object} event.data
201 * Data attached to the event. 256 * Data attached to the event.
202 */ 257 */
203 bodyPadding(event) { 258 bodyPadding(event) {
204 if ($('body').outerWidth() < Drupal.offCanvas.minDisplaceWidth) { 259 const position = event.data.settings.drupalOffCanvasPosition;
260 if (
261 position === 'side' &&
262 $('body').outerWidth() < Drupal.offCanvas.minDisplaceWidth
263 ) {
205 return; 264 return;
206 } 265 }
266 Drupal.offCanvas.resetPadding();
207 const $element = event.data.$element; 267 const $element = event.data.$element;
208 const $container = Drupal.offCanvas.getContainer($element); 268 const $container = Drupal.offCanvas.getContainer($element);
209 const $mainCanvasWrapper = Drupal.offCanvas.$mainCanvasWrapper; 269 const $mainCanvasWrapper = Drupal.offCanvas.$mainCanvasWrapper;
210 270
211 const width = $container.outerWidth(); 271 const width = $container.outerWidth();
212 const mainCanvasPadding = $mainCanvasWrapper.css(`padding-${Drupal.offCanvas.getEdge()}`); 272 const mainCanvasPadding = $mainCanvasWrapper.css(
213 if (width !== mainCanvasPadding) { 273 `padding-${Drupal.offCanvas.getEdge()}`,
214 $mainCanvasWrapper.css(`padding-${Drupal.offCanvas.getEdge()}`, `${width}px`); 274 );
275 if (position === 'side' && width !== mainCanvasPadding) {
276 $mainCanvasWrapper.css(
277 `padding-${Drupal.offCanvas.getEdge()}`,
278 `${width}px`,
279 );
215 $container.attr(`data-offset-${Drupal.offCanvas.getEdge()}`, width); 280 $container.attr(`data-offset-${Drupal.offCanvas.getEdge()}`, width);
281 displace();
282 }
283
284 const height = $container.outerHeight();
285 if (position === 'top') {
286 $mainCanvasWrapper.css('padding-top', `${height}px`);
287 $container.attr('data-offset-top', height);
216 displace(); 288 displace();
217 } 289 }
218 }, 290 },
219 291
220 /** 292 /**
235 * @return {string} 307 * @return {string}
236 * The edge the tray will be shown on, left or right. 308 * The edge the tray will be shown on, left or right.
237 */ 309 */
238 getEdge() { 310 getEdge() {
239 return document.documentElement.dir === 'rtl' ? 'left' : 'right'; 311 return document.documentElement.dir === 'rtl' ? 'left' : 'right';
312 },
313
314 /**
315 * Resets main canvas wrapper and toolbar padding / margin.
316 */
317 resetPadding() {
318 Drupal.offCanvas.$mainCanvasWrapper.css(
319 `padding-${Drupal.offCanvas.getEdge()}`,
320 0,
321 );
322 Drupal.offCanvas.$mainCanvasWrapper.css('padding-top', 0);
323 displace();
240 }, 324 },
241 }; 325 };
242 326
243 /** 327 /**
244 * Attaches off-canvas dialog behaviors. 328 * Attaches off-canvas dialog behaviors.
248 * @prop {Drupal~behaviorAttach} attach 332 * @prop {Drupal~behaviorAttach} attach
249 * Attaches event listeners for off-canvas dialogs. 333 * Attaches event listeners for off-canvas dialogs.
250 */ 334 */
251 Drupal.behaviors.offCanvasEvents = { 335 Drupal.behaviors.offCanvasEvents = {
252 attach: () => { 336 attach: () => {
253 $(window).once('off-canvas').on({ 337 $(window)
254 'dialog:beforecreate': (event, dialog, $element, settings) => { 338 .once('off-canvas')
255 if (Drupal.offCanvas.isOffCanvas($element)) { 339 .on({
256 Drupal.offCanvas.beforeCreate({ dialog, $element, settings }); 340 'dialog:beforecreate': (event, dialog, $element, settings) => {
257 } 341 if (Drupal.offCanvas.isOffCanvas($element)) {
258 }, 342 Drupal.offCanvas.beforeCreate({ dialog, $element, settings });
259 'dialog:aftercreate': (event, dialog, $element, settings) => { 343 }
260 if (Drupal.offCanvas.isOffCanvas($element)) { 344 },
261 Drupal.offCanvas.render({ dialog, $element, settings }); 345 'dialog:aftercreate': (event, dialog, $element, settings) => {
262 Drupal.offCanvas.afterCreate({ $element, settings }); 346 if (Drupal.offCanvas.isOffCanvas($element)) {
263 } 347 Drupal.offCanvas.render({ dialog, $element, settings });
264 }, 348 Drupal.offCanvas.afterCreate({ $element, settings });
265 'dialog:beforeclose': (event, dialog, $element) => { 349 }
266 if (Drupal.offCanvas.isOffCanvas($element)) { 350 },
267 Drupal.offCanvas.beforeClose({ dialog, $element }); 351 'dialog:beforeclose': (event, dialog, $element) => {
268 } 352 if (Drupal.offCanvas.isOffCanvas($element)) {
269 }, 353 Drupal.offCanvas.beforeClose({ dialog, $element });
270 }); 354 }
355 },
356 });
271 }, 357 },
272 }; 358 };
273 })(jQuery, Drupal, Drupal.debounce, Drupal.displace); 359 })(jQuery, Drupal, Drupal.debounce, Drupal.displace);