dialog.ajax.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /**
  2. * DO NOT EDIT THIS FILE.
  3. * See the following change record for more information,
  4. * https://www.drupal.org/node/2815083
  5. * @preserve
  6. **/
  7. (function ($, Drupal) {
  8. Drupal.behaviors.dialog = {
  9. attach: function attach(context, settings) {
  10. var $context = $(context);
  11. if (!$('#drupal-modal').length) {
  12. $('<div id="drupal-modal" class="ui-front"></div>').hide().appendTo('body');
  13. }
  14. var $dialog = $context.closest('.ui-dialog-content');
  15. if ($dialog.length) {
  16. if ($dialog.dialog('option', 'drupalAutoButtons')) {
  17. $dialog.trigger('dialogButtonsChange');
  18. }
  19. $dialog.dialog('widget').trigger('focus');
  20. }
  21. var originalClose = settings.dialog.close;
  22. settings.dialog.close = function (event) {
  23. for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
  24. args[_key - 1] = arguments[_key];
  25. }
  26. originalClose.apply(settings.dialog, [event].concat(args));
  27. $(event.target).remove();
  28. };
  29. },
  30. prepareDialogButtons: function prepareDialogButtons($dialog) {
  31. var buttons = [];
  32. var $buttons = $dialog.find('.form-actions input[type=submit], .form-actions a.button');
  33. $buttons.each(function () {
  34. var $originalButton = $(this).css({ display: 'none' });
  35. buttons.push({
  36. text: $originalButton.html() || $originalButton.attr('value'),
  37. class: $originalButton.attr('class'),
  38. click: function click(e) {
  39. if ($originalButton.is('a')) {
  40. $originalButton[0].click();
  41. } else {
  42. $originalButton.trigger('mousedown').trigger('mouseup').trigger('click');
  43. e.preventDefault();
  44. }
  45. }
  46. });
  47. });
  48. return buttons;
  49. }
  50. };
  51. Drupal.AjaxCommands.prototype.openDialog = function (ajax, response, status) {
  52. if (!response.selector) {
  53. return false;
  54. }
  55. var $dialog = $(response.selector);
  56. if (!$dialog.length) {
  57. $dialog = $('<div id="' + response.selector.replace(/^#/, '') + '" class="ui-front"></div>').appendTo('body');
  58. }
  59. if (!ajax.wrapper) {
  60. ajax.wrapper = $dialog.attr('id');
  61. }
  62. response.command = 'insert';
  63. response.method = 'html';
  64. ajax.commands.insert(ajax, response, status);
  65. if (!response.dialogOptions.buttons) {
  66. response.dialogOptions.drupalAutoButtons = true;
  67. response.dialogOptions.buttons = Drupal.behaviors.dialog.prepareDialogButtons($dialog);
  68. }
  69. $dialog.on('dialogButtonsChange', function () {
  70. var buttons = Drupal.behaviors.dialog.prepareDialogButtons($dialog);
  71. $dialog.dialog('option', 'buttons', buttons);
  72. });
  73. response.dialogOptions = response.dialogOptions || {};
  74. var dialog = Drupal.dialog($dialog.get(0), response.dialogOptions);
  75. if (response.dialogOptions.modal) {
  76. dialog.showModal();
  77. } else {
  78. dialog.show();
  79. }
  80. $dialog.parent().find('.ui-dialog-buttonset').addClass('form-actions');
  81. };
  82. Drupal.AjaxCommands.prototype.closeDialog = function (ajax, response, status) {
  83. var $dialog = $(response.selector);
  84. if ($dialog.length) {
  85. Drupal.dialog($dialog.get(0)).close();
  86. if (!response.persist) {
  87. $dialog.remove();
  88. }
  89. }
  90. $dialog.off('dialogButtonsChange');
  91. };
  92. Drupal.AjaxCommands.prototype.setDialogOption = function (ajax, response, status) {
  93. var $dialog = $(response.selector);
  94. if ($dialog.length) {
  95. $dialog.dialog('option', response.optionName, response.optionValue);
  96. }
  97. };
  98. $(window).on('dialog:aftercreate', function (e, dialog, $element, settings) {
  99. $element.on('click.dialog', '.dialog-cancel', function (e) {
  100. dialog.close('cancel');
  101. e.preventDefault();
  102. e.stopPropagation();
  103. });
  104. });
  105. $(window).on('dialog:beforeclose', function (e, dialog, $element) {
  106. $element.off('.dialog');
  107. });
  108. })(jQuery, Drupal);