Skip to content
Snippets Groups Projects

Use core DrupalDialogEvent instead of jQuery trigger

Merged Justin Toupin requested to merge issue/mercury_editor-3456410:3456410-me-dialog-events into 2.1.x
Files
3
+ 19
6
@@ -10,6 +10,16 @@
buttonPrimaryClass: 'button--primary',
};
function dispatchDialogEvent(eventType, dialog, element, settings) {
// Use the jQuery if the DrupalDialogEvent is not defined for BC.
if (typeof DrupalDialogEvent === 'undefined') {
$(window).trigger(`dialog:${eventType}`, [dialog, $(element), settings]);
} else {
const event = new DrupalDialogEvent(eventType, dialog, settings || {});
element.dispatchEvent(event);
}
}
Drupal.mercuryDialog = function (element, options) {
var undef;
var $element = $(element);
@@ -188,28 +198,31 @@
*/
function openDialog(settings) {
settings = {...drupalSettings.dialog, ...options, ...settings};
$(window).trigger('dialog:beforecreate', [dialog, $element, settings]);
dispatchDialogEvent('beforecreate', dialog, $element.get(0), settings);
init(settings);
dialogElement[settings.modal ? 'showModal' : 'show']();
$(window).trigger('dialog:aftercreate', [dialog, $element, settings]);
// Set autoResize to false to prevent Drupal core's jQuery dialog from
// attemping to resize, which would throw an error.
const originalResizeSetting = settings.autoResize;
settings.autoResize = false;
dispatchDialogEvent('aftercreate', dialog, $element.get(0), settings);
settings.autoResize = originalResizeSetting;
// Add a mutation observer to the dialog element.
dialogMutationObserver.observe(dialogElement, { childList: true, attributes: true });
dialogElement.addEventListener('close', function () {
closeDialog();
});
}
function closeDialog(value) {
$(window).trigger('dialog:beforeclose', [dialog, $element]);
dispatchDialogEvent('beforeclose', dialog, $element.get(0));
// Stop observing height and width changes.
dockObserver.disconnect();
dialogMutationObserver.disconnect();
Drupal.detachBehaviors(element, null, 'unload');
element.close();
dialog.returnValue = value;
$(window).trigger('dialog:afterclose', [dialog, $element]);
dispatchDialogEvent('afterclose', dialog, $element.get(0));
$element.remove();
}
Loading