js/bootstrap-dropdown.js
changeset 0 ba8ab09f730e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/bootstrap-dropdown.js	Fri Jul 04 16:42:41 2014 +0400
     1.3 @@ -0,0 +1,165 @@
     1.4 +/* ============================================================
     1.5 + * bootstrap-dropdown.js v2.3.1
     1.6 + * http://twitter.github.com/bootstrap/javascript.html#dropdowns
     1.7 + * ============================================================
     1.8 + * Copyright 2012 Twitter, Inc.
     1.9 + *
    1.10 + * Licensed under the Apache License, Version 2.0 (the "License");
    1.11 + * you may not use this file except in compliance with the License.
    1.12 + * You may obtain a copy of the License at
    1.13 + *
    1.14 + * http://www.apache.org/licenses/LICENSE-2.0
    1.15 + *
    1.16 + * Unless required by applicable law or agreed to in writing, software
    1.17 + * distributed under the License is distributed on an "AS IS" BASIS,
    1.18 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    1.19 + * See the License for the specific language governing permissions and
    1.20 + * limitations under the License.
    1.21 + * ============================================================ */
    1.22 +
    1.23 +
    1.24 +!function ($) {
    1.25 +
    1.26 +  "use strict"; // jshint ;_;
    1.27 +
    1.28 +
    1.29 + /* DROPDOWN CLASS DEFINITION
    1.30 +  * ========================= */
    1.31 +
    1.32 +  var toggle = '[data-toggle=dropdown]'
    1.33 +    , Dropdown = function (element) {
    1.34 +        var $el = $(element).on('click.dropdown.data-api', this.toggle)
    1.35 +        $('html').on('click.dropdown.data-api', function () {
    1.36 +          $el.parent().removeClass('open')
    1.37 +        })
    1.38 +      }
    1.39 +
    1.40 +  Dropdown.prototype = {
    1.41 +
    1.42 +    constructor: Dropdown
    1.43 +
    1.44 +  , toggle: function (e) {
    1.45 +      var $this = $(this)
    1.46 +        , $parent
    1.47 +        , isActive
    1.48 +
    1.49 +      if ($this.is('.disabled, :disabled')) return
    1.50 +
    1.51 +      $parent = getParent($this)
    1.52 +
    1.53 +      isActive = $parent.hasClass('open')
    1.54 +
    1.55 +      clearMenus()
    1.56 +
    1.57 +      if (!isActive) {
    1.58 +        $parent.toggleClass('open')
    1.59 +      }
    1.60 +
    1.61 +      $this.focus()
    1.62 +
    1.63 +      return false
    1.64 +    }
    1.65 +
    1.66 +  , keydown: function (e) {
    1.67 +      var $this
    1.68 +        , $items
    1.69 +        , $active
    1.70 +        , $parent
    1.71 +        , isActive
    1.72 +        , index
    1.73 +
    1.74 +      if (!/(38|40|27)/.test(e.keyCode)) return
    1.75 +
    1.76 +      $this = $(this)
    1.77 +
    1.78 +      e.preventDefault()
    1.79 +      e.stopPropagation()
    1.80 +
    1.81 +      if ($this.is('.disabled, :disabled')) return
    1.82 +
    1.83 +      $parent = getParent($this)
    1.84 +
    1.85 +      isActive = $parent.hasClass('open')
    1.86 +
    1.87 +      if (!isActive || (isActive && e.keyCode == 27)) {
    1.88 +        if (e.which == 27) $parent.find(toggle).focus()
    1.89 +        return $this.click()
    1.90 +      }
    1.91 +
    1.92 +      $items = $('[role=menu] li:not(.divider):visible a', $parent)
    1.93 +
    1.94 +      if (!$items.length) return
    1.95 +
    1.96 +      index = $items.index($items.filter(':focus'))
    1.97 +
    1.98 +      if (e.keyCode == 38 && index > 0) index--                                        // up
    1.99 +      if (e.keyCode == 40 && index < $items.length - 1) index++                        // down
   1.100 +      if (!~index) index = 0
   1.101 +
   1.102 +      $items
   1.103 +        .eq(index)
   1.104 +        .focus()
   1.105 +    }
   1.106 +
   1.107 +  }
   1.108 +
   1.109 +  function clearMenus() {
   1.110 +    $(toggle).each(function () {
   1.111 +      getParent($(this)).removeClass('open')
   1.112 +    })
   1.113 +  }
   1.114 +
   1.115 +  function getParent($this) {
   1.116 +    var selector = $this.attr('data-target')
   1.117 +      , $parent
   1.118 +
   1.119 +    if (!selector) {
   1.120 +      selector = $this.attr('href')
   1.121 +      selector = selector && /#/.test(selector) && selector.replace(/.*(?=#[^\s]*$)/, '') //strip for ie7
   1.122 +    }
   1.123 +
   1.124 +    $parent = selector && $(selector)
   1.125 +
   1.126 +    if (!$parent || !$parent.length) $parent = $this.parent()
   1.127 +
   1.128 +    return $parent
   1.129 +  }
   1.130 +
   1.131 +
   1.132 +  /* DROPDOWN PLUGIN DEFINITION
   1.133 +   * ========================== */
   1.134 +
   1.135 +  var old = $.fn.dropdown
   1.136 +
   1.137 +  $.fn.dropdown = function (option) {
   1.138 +    return this.each(function () {
   1.139 +      var $this = $(this)
   1.140 +        , data = $this.data('dropdown')
   1.141 +      if (!data) $this.data('dropdown', (data = new Dropdown(this)))
   1.142 +      if (typeof option == 'string') data[option].call($this)
   1.143 +    })
   1.144 +  }
   1.145 +
   1.146 +  $.fn.dropdown.Constructor = Dropdown
   1.147 +
   1.148 +
   1.149 + /* DROPDOWN NO CONFLICT
   1.150 +  * ==================== */
   1.151 +
   1.152 +  $.fn.dropdown.noConflict = function () {
   1.153 +    $.fn.dropdown = old
   1.154 +    return this
   1.155 +  }
   1.156 +
   1.157 +
   1.158 +  /* APPLY TO STANDARD DROPDOWN ELEMENTS
   1.159 +   * =================================== */
   1.160 +
   1.161 +  $(document)
   1.162 +    .on('click.dropdown.data-api', clearMenus)
   1.163 +    .on('click.dropdown.data-api', '.dropdown form', function (e) { e.stopPropagation() })
   1.164 +    .on('click.dropdown-menu', function (e) { e.stopPropagation() })
   1.165 +    .on('click.dropdown.data-api'  , toggle, Dropdown.prototype.toggle)
   1.166 +    .on('keydown.dropdown.data-api', toggle + ', [role=menu]' , Dropdown.prototype.keydown)
   1.167 +
   1.168 +}(window.jQuery);