dialog.ajax.js 4.0 KB

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