Updated the Bootstrap theme.
[yaffs-website] / web / themes / contrib / bootstrap / js / modal.js
index 5bcce17846499037bd7ecb86c3403f81f160872d..009d630491c31969ac7a3b2b0ab4053d130014c8 100644 (file)
@@ -5,6 +5,49 @@
 (function ($, Drupal, Bootstrap, Attributes) {
   'use strict';
 
+  /**
+   * Document jQuery object.
+   *
+   * @type {jQuery}
+   */
+  var $document = $(document);
+
+  /**
+   * Finds the first available and visible focusable input element.
+   *
+   * This is abstracted from the main code below so sub-themes can override
+   * this method to return their own element if desired.
+   *
+   * @param {Modal} modal
+   *   The Bootstrap modal instance.
+   *
+   * @return {jQuery}
+   *   A jQuery object containing the element that should be focused. Note: if
+   *   this object contains multiple elements, only the first visible one will
+   *   be used.
+   */
+  Bootstrap.modalFindFocusableElement = function (modal) {
+    return modal.$dialogBody.find(':input,:button,.btn');
+  };
+
+  $document.on('shown.bs.modal', function (e) {
+    var $modal = $(e.target);
+    var modal = $modal.data('bs.modal');
+
+    // Focus the first input element found.
+    if (modal && modal.options.focusInput) {
+      var $focusable = Bootstrap.modalFindFocusableElement(modal);
+      if ($focusable && $focusable[0]) {
+        var $input = $focusable.filter(':visible:first').focus();
+
+        // Select text if input is text.
+        if (modal.options.selectText && $input.is(':text')) {
+          $input[0].setSelectionRange(0, $input[0].value.length)
+        }
+      }
+    }
+  });
+
   /**
    * Only process this once.
    */
@@ -40,6 +83,8 @@
       Modal.DEFAULTS = $.extend({}, BootstrapModal.DEFAULTS, {
         animation: !!settings.modal_animation,
         backdrop: settings.modal_backdrop === 'static' ? 'static' : !!settings.modal_backdrop,
+        focusInput: !!settings.modal_focus_input,
+        selectText: !!settings.modal_select_text,
         keyboard: !!settings.modal_keyboard,
         show: !!settings.modal_show,
         size: settings.modal_size
           var data    = $this.data('bs.modal');
           var initialize = false;
 
+          // Immediately return if there's no instance to invoke a valid method.
+          if (!data && method && method !== 'open') {
+            return;
+          }
+
           options = $.extend({}, Modal.DEFAULTS, data && data.options, $this.data(), options);
           if (!data) {
             // When initializing the Bootstrap Modal, only pass the "supported"
       // Replace the data API so that it calls $.fn.modal rather than Plugin.
       // This allows sub-themes to replace the jQuery Plugin if they like with
       // out having to redo all this boilerplate.
-      $(document)
+      $document
         .off('click.bs.modal.data-api')
         .on('click.bs.modal.data-api', '[data-toggle="modal"]', function (e) {
           var $this   = $(this);
           var href    = $this.attr('href');
-          var $target = $($this.attr('data-target') || (href && href.replace(/.*(?=#[^\s]+$)/, ''))); // strip for ie7
+          var target  = $this.attr('data-target') || (href && href.replace(/.*(?=#[^\s]+$)/, '')); // strip for ie7
+          var $target = $document.find(target);
           var option  = $target.data('bs.modal') ? 'toggle' : $.extend({ remote: !/#/.test(href) && href }, $target.data(), $this.data());
 
           if ($this.is('a')) e.preventDefault();