dialog.ajax.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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"/>').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. originalClose.apply(settings.dialog, arguments);
  24. $(event.target).remove();
  25. };
  26. },
  27. prepareDialogButtons: function prepareDialogButtons($dialog) {
  28. var buttons = [];
  29. var $buttons = $dialog.find('.form-actions input[type=submit], .form-actions a.button');
  30. $buttons.each(function () {
  31. var $originalButton = $(this).css({
  32. display: 'block',
  33. width: 0,
  34. height: 0,
  35. padding: 0,
  36. border: 0,
  37. overflow: 'hidden'
  38. });
  39. buttons.push({
  40. text: $originalButton.html() || $originalButton.attr('value'),
  41. class: $originalButton.attr('class'),
  42. click: function click(e) {
  43. if ($originalButton.is('a')) {
  44. $originalButton[0].click();
  45. } else {
  46. $originalButton.trigger('mousedown').trigger('mouseup').trigger('click');
  47. e.preventDefault();
  48. }
  49. }
  50. });
  51. });
  52. return buttons;
  53. }
  54. };
  55. Drupal.AjaxCommands.prototype.openDialog = function (ajax, response, status) {
  56. if (!response.selector) {
  57. return false;
  58. }
  59. var $dialog = $(response.selector);
  60. if (!$dialog.length) {
  61. $dialog = $('<div id="' + response.selector.replace(/^#/, '') + '" class="ui-front"/>').appendTo('body');
  62. }
  63. if (!ajax.wrapper) {
  64. ajax.wrapper = $dialog.attr('id');
  65. }
  66. response.command = 'insert';
  67. response.method = 'html';
  68. ajax.commands.insert(ajax, response, status);
  69. if (!response.dialogOptions.buttons) {
  70. response.dialogOptions.drupalAutoButtons = true;
  71. response.dialogOptions.buttons = Drupal.behaviors.dialog.prepareDialogButtons($dialog);
  72. }
  73. $dialog.on('dialogButtonsChange', function () {
  74. var buttons = Drupal.behaviors.dialog.prepareDialogButtons($dialog);
  75. $dialog.dialog('option', 'buttons', buttons);
  76. });
  77. response.dialogOptions = response.dialogOptions || {};
  78. var dialog = Drupal.dialog($dialog.get(0), response.dialogOptions);
  79. if (response.dialogOptions.modal) {
  80. dialog.showModal();
  81. } else {
  82. dialog.show();
  83. }
  84. $dialog.parent().find('.ui-dialog-buttonset').addClass('form-actions');
  85. };
  86. Drupal.AjaxCommands.prototype.closeDialog = function (ajax, response, status) {
  87. var $dialog = $(response.selector);
  88. if ($dialog.length) {
  89. Drupal.dialog($dialog.get(0)).close();
  90. if (!response.persist) {
  91. $dialog.remove();
  92. }
  93. }
  94. $dialog.off('dialogButtonsChange');
  95. };
  96. Drupal.AjaxCommands.prototype.setDialogOption = function (ajax, response, status) {
  97. var $dialog = $(response.selector);
  98. if ($dialog.length) {
  99. $dialog.dialog('option', response.optionName, response.optionValue);
  100. }
  101. };
  102. $(window).on('dialog:aftercreate', function (e, dialog, $element, settings) {
  103. $element.on('click.dialog', '.dialog-cancel', function (e) {
  104. dialog.close('cancel');
  105. e.preventDefault();
  106. e.stopPropagation();
  107. });
  108. });
  109. $(window).on('dialog:beforeclose', function (e, dialog, $element) {
  110. $element.off('.dialog');
  111. });
  112. })(jQuery, Drupal);