123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242 |
- (function ($, Drupal) {
-
- Drupal.behaviors.dialog = {
- attach(context, settings) {
- const $context = $(context);
-
-
-
- if (!$('#drupal-modal').length) {
-
-
-
- $('<div id="drupal-modal" class="ui-front"/>').hide().appendTo('body');
- }
-
-
- const $dialog = $context.closest('.ui-dialog-content');
- if ($dialog.length) {
-
- if ($dialog.dialog('option', 'drupalAutoButtons')) {
-
- $dialog.trigger('dialogButtonsChange');
- }
-
- $dialog.dialog('widget').trigger('focus');
- }
- const originalClose = settings.dialog.close;
-
- settings.dialog.close = function (event, ...args) {
- originalClose.apply(settings.dialog, [event, ...args]);
- $(event.target).remove();
- };
- },
-
- prepareDialogButtons($dialog) {
- const buttons = [];
- const $buttons = $dialog.find('.form-actions input[type=submit], .form-actions a.button');
- $buttons.each(function () {
-
-
-
-
-
- const $originalButton = $(this).css({
- display: 'block',
- width: 0,
- height: 0,
- padding: 0,
- border: 0,
- overflow: 'hidden',
- });
- buttons.push({
- text: $originalButton.html() || $originalButton.attr('value'),
- class: $originalButton.attr('class'),
- click(e) {
-
-
- if ($originalButton.is('a')) {
- $originalButton[0].click();
- }
- else {
- $originalButton.trigger('mousedown').trigger('mouseup').trigger('click');
- e.preventDefault();
- }
- },
- });
- });
- return buttons;
- },
- };
-
- Drupal.AjaxCommands.prototype.openDialog = function (ajax, response, status) {
- if (!response.selector) {
- return false;
- }
- let $dialog = $(response.selector);
- if (!$dialog.length) {
-
- $dialog = $(`<div id="${response.selector.replace(/^#/, '')}" class="ui-front"/>`).appendTo('body');
- }
-
- if (!ajax.wrapper) {
- ajax.wrapper = $dialog.attr('id');
- }
-
- response.command = 'insert';
- response.method = 'html';
- ajax.commands.insert(ajax, response, status);
-
- if (!response.dialogOptions.buttons) {
- response.dialogOptions.drupalAutoButtons = true;
- response.dialogOptions.buttons = Drupal.behaviors.dialog.prepareDialogButtons($dialog);
- }
-
- $dialog.on('dialogButtonsChange', () => {
- const buttons = Drupal.behaviors.dialog.prepareDialogButtons($dialog);
- $dialog.dialog('option', 'buttons', buttons);
- });
-
- response.dialogOptions = response.dialogOptions || {};
- const dialog = Drupal.dialog($dialog.get(0), response.dialogOptions);
- if (response.dialogOptions.modal) {
- dialog.showModal();
- }
- else {
- dialog.show();
- }
-
- $dialog.parent().find('.ui-dialog-buttonset').addClass('form-actions');
- };
-
- Drupal.AjaxCommands.prototype.closeDialog = function (ajax, response, status) {
- const $dialog = $(response.selector);
- if ($dialog.length) {
- Drupal.dialog($dialog.get(0)).close();
- if (!response.persist) {
- $dialog.remove();
- }
- }
-
- $dialog.off('dialogButtonsChange');
- };
-
- Drupal.AjaxCommands.prototype.setDialogOption = function (ajax, response, status) {
- const $dialog = $(response.selector);
- if ($dialog.length) {
- $dialog.dialog('option', response.optionName, response.optionValue);
- }
- };
-
- $(window).on('dialog:aftercreate', (e, dialog, $element, settings) => {
- $element.on('click.dialog', '.dialog-cancel', (e) => {
- dialog.close('cancel');
- e.preventDefault();
- e.stopPropagation();
- });
- });
-
- $(window).on('dialog:beforeclose', (e, dialog, $element) => {
- $element.off('.dialog');
- });
- }(jQuery, Drupal));
|