bootstrap-source/bootstrap-3.0.3/js/dropdown.js
author stetrabby <info@trabucchi.de>
Fri, 20 Dec 2013 22:49:16 +0100
changeset 54 0ded9d7748b7
permissions -rwxr-xr-x
initial less based on the pymove3d.css
info@54
     1
/* ========================================================================
info@54
     2
 * Bootstrap: dropdown.js v3.0.3
info@54
     3
 * http://getbootstrap.com/javascript/#dropdowns
info@54
     4
 * ========================================================================
info@54
     5
 * Copyright 2013 Twitter, Inc.
info@54
     6
 *
info@54
     7
 * Licensed under the Apache License, Version 2.0 (the "License");
info@54
     8
 * you may not use this file except in compliance with the License.
info@54
     9
 * You may obtain a copy of the License at
info@54
    10
 *
info@54
    11
 * http://www.apache.org/licenses/LICENSE-2.0
info@54
    12
 *
info@54
    13
 * Unless required by applicable law or agreed to in writing, software
info@54
    14
 * distributed under the License is distributed on an "AS IS" BASIS,
info@54
    15
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
info@54
    16
 * See the License for the specific language governing permissions and
info@54
    17
 * limitations under the License.
info@54
    18
 * ======================================================================== */
info@54
    19
info@54
    20
info@54
    21
+function ($) { "use strict";
info@54
    22
info@54
    23
  // DROPDOWN CLASS DEFINITION
info@54
    24
  // =========================
info@54
    25
info@54
    26
  var backdrop = '.dropdown-backdrop'
info@54
    27
  var toggle   = '[data-toggle=dropdown]'
info@54
    28
  var Dropdown = function (element) {
info@54
    29
    $(element).on('click.bs.dropdown', this.toggle)
info@54
    30
  }
info@54
    31
info@54
    32
  Dropdown.prototype.toggle = function (e) {
info@54
    33
    var $this = $(this)
info@54
    34
info@54
    35
    if ($this.is('.disabled, :disabled')) return
info@54
    36
info@54
    37
    var $parent  = getParent($this)
info@54
    38
    var isActive = $parent.hasClass('open')
info@54
    39
info@54
    40
    clearMenus()
info@54
    41
info@54
    42
    if (!isActive) {
info@54
    43
      if ('ontouchstart' in document.documentElement && !$parent.closest('.navbar-nav').length) {
info@54
    44
        // if mobile we use a backdrop because click events don't delegate
info@54
    45
        $('<div class="dropdown-backdrop"/>').insertAfter($(this)).on('click', clearMenus)
info@54
    46
      }
info@54
    47
info@54
    48
      $parent.trigger(e = $.Event('show.bs.dropdown'))
info@54
    49
info@54
    50
      if (e.isDefaultPrevented()) return
info@54
    51
info@54
    52
      $parent
info@54
    53
        .toggleClass('open')
info@54
    54
        .trigger('shown.bs.dropdown')
info@54
    55
info@54
    56
      $this.focus()
info@54
    57
    }
info@54
    58
info@54
    59
    return false
info@54
    60
  }
info@54
    61
info@54
    62
  Dropdown.prototype.keydown = function (e) {
info@54
    63
    if (!/(38|40|27)/.test(e.keyCode)) return
info@54
    64
info@54
    65
    var $this = $(this)
info@54
    66
info@54
    67
    e.preventDefault()
info@54
    68
    e.stopPropagation()
info@54
    69
info@54
    70
    if ($this.is('.disabled, :disabled')) return
info@54
    71
info@54
    72
    var $parent  = getParent($this)
info@54
    73
    var isActive = $parent.hasClass('open')
info@54
    74
info@54
    75
    if (!isActive || (isActive && e.keyCode == 27)) {
info@54
    76
      if (e.which == 27) $parent.find(toggle).focus()
info@54
    77
      return $this.click()
info@54
    78
    }
info@54
    79
info@54
    80
    var $items = $('[role=menu] li:not(.divider):visible a', $parent)
info@54
    81
info@54
    82
    if (!$items.length) return
info@54
    83
info@54
    84
    var index = $items.index($items.filter(':focus'))
info@54
    85
info@54
    86
    if (e.keyCode == 38 && index > 0)                 index--                        // up
info@54
    87
    if (e.keyCode == 40 && index < $items.length - 1) index++                        // down
info@54
    88
    if (!~index)                                      index=0
info@54
    89
info@54
    90
    $items.eq(index).focus()
info@54
    91
  }
info@54
    92
info@54
    93
  function clearMenus() {
info@54
    94
    $(backdrop).remove()
info@54
    95
    $(toggle).each(function (e) {
info@54
    96
      var $parent = getParent($(this))
info@54
    97
      if (!$parent.hasClass('open')) return
info@54
    98
      $parent.trigger(e = $.Event('hide.bs.dropdown'))
info@54
    99
      if (e.isDefaultPrevented()) return
info@54
   100
      $parent.removeClass('open').trigger('hidden.bs.dropdown')
info@54
   101
    })
info@54
   102
  }
info@54
   103
info@54
   104
  function getParent($this) {
info@54
   105
    var selector = $this.attr('data-target')
info@54
   106
info@54
   107
    if (!selector) {
info@54
   108
      selector = $this.attr('href')
info@54
   109
      selector = selector && /#/.test(selector) && selector.replace(/.*(?=#[^\s]*$)/, '') //strip for ie7
info@54
   110
    }
info@54
   111
info@54
   112
    var $parent = selector && $(selector)
info@54
   113
info@54
   114
    return $parent && $parent.length ? $parent : $this.parent()
info@54
   115
  }
info@54
   116
info@54
   117
info@54
   118
  // DROPDOWN PLUGIN DEFINITION
info@54
   119
  // ==========================
info@54
   120
info@54
   121
  var old = $.fn.dropdown
info@54
   122
info@54
   123
  $.fn.dropdown = function (option) {
info@54
   124
    return this.each(function () {
info@54
   125
      var $this = $(this)
info@54
   126
      var data  = $this.data('bs.dropdown')
info@54
   127
info@54
   128
      if (!data) $this.data('bs.dropdown', (data = new Dropdown(this)))
info@54
   129
      if (typeof option == 'string') data[option].call($this)
info@54
   130
    })
info@54
   131
  }
info@54
   132
info@54
   133
  $.fn.dropdown.Constructor = Dropdown
info@54
   134
info@54
   135
info@54
   136
  // DROPDOWN NO CONFLICT
info@54
   137
  // ====================
info@54
   138
info@54
   139
  $.fn.dropdown.noConflict = function () {
info@54
   140
    $.fn.dropdown = old
info@54
   141
    return this
info@54
   142
  }
info@54
   143
info@54
   144
info@54
   145
  // APPLY TO STANDARD DROPDOWN ELEMENTS
info@54
   146
  // ===================================
info@54
   147
info@54
   148
  $(document)
info@54
   149
    .on('click.bs.dropdown.data-api', clearMenus)
info@54
   150
    .on('click.bs.dropdown.data-api', '.dropdown form', function (e) { e.stopPropagation() })
info@54
   151
    .on('click.bs.dropdown.data-api'  , toggle, Dropdown.prototype.toggle)
info@54
   152
    .on('keydown.bs.dropdown.data-api', toggle + ', [role=menu]' , Dropdown.prototype.keydown)
info@54
   153
info@54
   154
}(jQuery);
Impressum Datenschutzerklärung