Updated the Bootstrap theme.
[yaffs-website] / web / themes / contrib / bootstrap / js / misc / dialog.ajax.js
1 /**
2  * @file
3  * dialog.ajax.js
4  */
5 (function ($, Drupal, Bootstrap) {
6
7   Drupal.behaviors.dialog.ajaxCurrentButton = null;
8   Drupal.behaviors.dialog.ajaxOriginalButton = null;
9
10   /**
11    * Synchronizes a faux button with its original counterpart.
12    *
13    * @param {Boolean} [reset = false]
14    *   Whether to reset the current and original buttons after synchronizing.
15    */
16   Drupal.behaviors.dialog.ajaxUpdateButtons = function (reset) {
17     if (this.ajaxCurrentButton && this.ajaxOriginalButton) {
18       this.ajaxCurrentButton.html(this.ajaxOriginalButton.html());
19       this.ajaxCurrentButton.prop('disabled', this.ajaxOriginalButton.prop('disabled'));
20     }
21     if (reset) {
22       this.ajaxCurrentButton = null;
23       this.ajaxOriginalButton = null;
24     }
25   };
26
27   $(document)
28     .ajaxSend(function () {
29       Drupal.behaviors.dialog.ajaxUpdateButtons();
30     })
31     .ajaxComplete(function () {
32       Drupal.behaviors.dialog.ajaxUpdateButtons(true);
33     })
34   ;
35
36   /**
37    * {@inheritdoc}
38    */
39   Drupal.behaviors.dialog.prepareDialogButtons = function prepareDialogButtons($dialog) {
40     var _that = this;
41     var buttons = [];
42     var $buttons = $dialog.find('.form-actions').find('button, input[type=submit], .form-actions a.button');
43     $buttons.each(function () {
44       var $originalButton = $(this)
45         // Prevent original button from being tabbed to.
46         .attr('tabindex', -1)
47         // Visually make the original button invisible, but don't actually hide
48         // or remove it from the DOM because the click needs to be proxied from
49         // the faux button created in the footer to its original counterpart.
50         .css({
51           display: 'block',
52           width: 0,
53           height: 0,
54           padding: 0,
55           border: 0,
56           overflow: 'hidden'
57         });
58
59       buttons.push({
60         // Strip all HTML from the actual text value. This value is escaped.
61         // It actual HTML value will be synced with the original button's HTML
62         // below in the "create" method.
63         text: Bootstrap.stripHtml($originalButton),
64         class: $originalButton.attr('class').replace('use-ajax-submit', ''),
65         click: function click(e) {
66           e.preventDefault();
67           e.stopPropagation();
68           _that.ajaxCurrentButton = $(e.target);
69           _that.ajaxOriginalButton = $originalButton;
70           Bootstrap.simulate($originalButton, 'click');
71         },
72         create: function () {
73           _that.ajaxCurrentButton = $(this);
74           _that.ajaxOriginalButton = $originalButton;
75           _that.ajaxUpdateButtons(true);
76         }
77       });
78     });
79
80     return buttons;
81   };
82
83 })(window.jQuery, window.Drupal, window.Drupal.bootstrap);