de1d3c921977e8460cc2a114046f29fd0ce8a889
[yaffs-website] / web / core / misc / dialog / dialog.ajax.js
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
8 (function ($, Drupal) {
9   Drupal.behaviors.dialog = {
10     attach: function attach(context, settings) {
11       var $context = $(context);
12
13       if (!$('#drupal-modal').length) {
14         $('<div id="drupal-modal" class="ui-front"/>').hide().appendTo('body');
15       }
16
17       var $dialog = $context.closest('.ui-dialog-content');
18       if ($dialog.length) {
19         if ($dialog.dialog('option', 'drupalAutoButtons')) {
20           $dialog.trigger('dialogButtonsChange');
21         }
22
23         $dialog.dialog('widget').trigger('focus');
24       }
25
26       var originalClose = settings.dialog.close;
27
28       settings.dialog.close = function (event) {
29         for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
30           args[_key - 1] = arguments[_key];
31         }
32
33         originalClose.apply(settings.dialog, [event].concat(args));
34         $(event.target).remove();
35       };
36     },
37     prepareDialogButtons: function prepareDialogButtons($dialog) {
38       var buttons = [];
39       var $buttons = $dialog.find('.form-actions input[type=submit], .form-actions a.button');
40       $buttons.each(function () {
41         var $originalButton = $(this).css({
42           display: 'block',
43           width: 0,
44           height: 0,
45           padding: 0,
46           border: 0,
47           overflow: 'hidden'
48         });
49         buttons.push({
50           text: $originalButton.html() || $originalButton.attr('value'),
51           class: $originalButton.attr('class'),
52           click: function click(e) {
53             if ($originalButton.is('a')) {
54               $originalButton[0].click();
55             } else {
56               $originalButton.trigger('mousedown').trigger('mouseup').trigger('click');
57               e.preventDefault();
58             }
59           }
60         });
61       });
62       return buttons;
63     }
64   };
65
66   Drupal.AjaxCommands.prototype.openDialog = function (ajax, response, status) {
67     if (!response.selector) {
68       return false;
69     }
70     var $dialog = $(response.selector);
71     if (!$dialog.length) {
72       $dialog = $('<div id="' + response.selector.replace(/^#/, '') + '" class="ui-front"/>').appendTo('body');
73     }
74
75     if (!ajax.wrapper) {
76       ajax.wrapper = $dialog.attr('id');
77     }
78
79     response.command = 'insert';
80     response.method = 'html';
81     ajax.commands.insert(ajax, response, status);
82
83     if (!response.dialogOptions.buttons) {
84       response.dialogOptions.drupalAutoButtons = true;
85       response.dialogOptions.buttons = Drupal.behaviors.dialog.prepareDialogButtons($dialog);
86     }
87
88     $dialog.on('dialogButtonsChange', function () {
89       var buttons = Drupal.behaviors.dialog.prepareDialogButtons($dialog);
90       $dialog.dialog('option', 'buttons', buttons);
91     });
92
93     response.dialogOptions = response.dialogOptions || {};
94     var dialog = Drupal.dialog($dialog.get(0), response.dialogOptions);
95     if (response.dialogOptions.modal) {
96       dialog.showModal();
97     } else {
98       dialog.show();
99     }
100
101     $dialog.parent().find('.ui-dialog-buttonset').addClass('form-actions');
102   };
103
104   Drupal.AjaxCommands.prototype.closeDialog = function (ajax, response, status) {
105     var $dialog = $(response.selector);
106     if ($dialog.length) {
107       Drupal.dialog($dialog.get(0)).close();
108       if (!response.persist) {
109         $dialog.remove();
110       }
111     }
112
113     $dialog.off('dialogButtonsChange');
114   };
115
116   Drupal.AjaxCommands.prototype.setDialogOption = function (ajax, response, status) {
117     var $dialog = $(response.selector);
118     if ($dialog.length) {
119       $dialog.dialog('option', response.optionName, response.optionValue);
120     }
121   };
122
123   $(window).on('dialog:aftercreate', function (e, dialog, $element, settings) {
124     $element.on('click.dialog', '.dialog-cancel', function (e) {
125       dialog.close('cancel');
126       e.preventDefault();
127       e.stopPropagation();
128     });
129   });
130
131   $(window).on('dialog:beforeclose', function (e, dialog, $element) {
132     $element.off('.dialog');
133   });
134 })(jQuery, Drupal);