Updated the Bootstrap theme.
[yaffs-website] / web / themes / contrib / bootstrap / js / misc / dialog.ajax.js
index f5c853cb41b383ad4cbdcb9cb5ca3efc18f2626f..7bfdcd33a63032d28f768d219ab6883069432db8 100644 (file)
@@ -2,25 +2,34 @@
  * @file
  * dialog.ajax.js
  */
-(function ($, Drupal) {
+(function ($, Drupal, Bootstrap) {
 
-  var dialogAjaxCurrentButton;
-  var dialogAjaxOriginalButton;
+  Drupal.behaviors.dialog.ajaxCurrentButton = null;
+  Drupal.behaviors.dialog.ajaxOriginalButton = null;
+
+  /**
+   * Synchronizes a faux button with its original counterpart.
+   *
+   * @param {Boolean} [reset = false]
+   *   Whether to reset the current and original buttons after synchronizing.
+   */
+  Drupal.behaviors.dialog.ajaxUpdateButtons = function (reset) {
+    if (this.ajaxCurrentButton && this.ajaxOriginalButton) {
+      this.ajaxCurrentButton.html(this.ajaxOriginalButton.html());
+      this.ajaxCurrentButton.prop('disabled', this.ajaxOriginalButton.prop('disabled'));
+    }
+    if (reset) {
+      this.ajaxCurrentButton = null;
+      this.ajaxOriginalButton = null;
+    }
+  };
 
   $(document)
     .ajaxSend(function () {
-      if (dialogAjaxCurrentButton && dialogAjaxOriginalButton) {
-        dialogAjaxCurrentButton.html(dialogAjaxOriginalButton.html());
-        dialogAjaxCurrentButton.prop('disabled', dialogAjaxOriginalButton.prop('disabled'));
-      }
+      Drupal.behaviors.dialog.ajaxUpdateButtons();
     })
     .ajaxComplete(function () {
-      if (dialogAjaxCurrentButton && dialogAjaxOriginalButton) {
-        dialogAjaxCurrentButton.html(dialogAjaxOriginalButton.html());
-        dialogAjaxCurrentButton.prop('disabled', dialogAjaxOriginalButton.prop('disabled'));
-      }
-      dialogAjaxCurrentButton = null;
-      dialogAjaxOriginalButton = null;
+      Drupal.behaviors.dialog.ajaxUpdateButtons(true);
     })
   ;
 
    * {@inheritdoc}
    */
   Drupal.behaviors.dialog.prepareDialogButtons = function prepareDialogButtons($dialog) {
+    var _that = this;
     var buttons = [];
     var $buttons = $dialog.find('.form-actions').find('button, input[type=submit], .form-actions a.button');
     $buttons.each(function () {
-      var $originalButton = $(this).css({
-        display: 'block',
-        width: 0,
-        height: 0,
-        padding: 0,
-        border: 0,
-        overflow: 'hidden'
-      });
-      var classes = $originalButton.attr('class').replace('use-ajax-submit', '');
+      var $originalButton = $(this)
+        // Prevent original button from being tabbed to.
+        .attr('tabindex', -1)
+        // Visually make the original button invisible, but don't actually hide
+        // or remove it from the DOM because the click needs to be proxied from
+        // the faux button created in the footer to its original counterpart.
+        .css({
+          display: 'block',
+          width: 0,
+          height: 0,
+          padding: 0,
+          border: 0,
+          overflow: 'hidden'
+        });
+
       buttons.push({
-        text: $originalButton.html() || $originalButton.attr('value'),
-        class: classes,
+        // Strip all HTML from the actual text value. This value is escaped.
+        // It actual HTML value will be synced with the original button's HTML
+        // below in the "create" method.
+        text: Bootstrap.stripHtml($originalButton),
+        class: $originalButton.attr('class').replace('use-ajax-submit', ''),
         click: function click(e) {
-          dialogAjaxCurrentButton = $(e.target);
-          dialogAjaxOriginalButton = $originalButton;
-          if ($originalButton.is('a')) {
-            $originalButton[0].click();
-          }
-          else {
-            $originalButton.trigger('mousedown').trigger('mouseup').trigger('click');
-            e.preventDefault();
-          }
+          e.preventDefault();
+          e.stopPropagation();
+          _that.ajaxCurrentButton = $(e.target);
+          _that.ajaxOriginalButton = $originalButton;
+          Bootstrap.simulate($originalButton, 'click');
+        },
+        create: function () {
+          _that.ajaxCurrentButton = $(this);
+          _that.ajaxOriginalButton = $originalButton;
+          _that.ajaxUpdateButtons(true);
         }
       });
     });
+
     return buttons;
   };