assets/javascripts/bootstrap/dropdown.js
changeset 114 af15590802a4
equal deleted inserted replaced
113:d4ec02c51c91 114:af15590802a4
       
     1 /* ========================================================================
       
     2  * Bootstrap: dropdown.js v3.3.5
       
     3  * http://getbootstrap.com/javascript/#dropdowns
       
     4  * ========================================================================
       
     5  * Copyright 2011-2015 Twitter, Inc.
       
     6  * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
       
     7  * ======================================================================== */
       
     8 
       
     9 
       
    10 +function ($) {
       
    11   'use strict';
       
    12 
       
    13   // DROPDOWN CLASS DEFINITION
       
    14   // =========================
       
    15 
       
    16   var backdrop = '.dropdown-backdrop'
       
    17   var toggle   = '[data-toggle="dropdown"]'
       
    18   var Dropdown = function (element) {
       
    19     $(element).on('click.bs.dropdown', this.toggle)
       
    20   }
       
    21 
       
    22   Dropdown.VERSION = '3.3.5'
       
    23 
       
    24   function getParent($this) {
       
    25     var selector = $this.attr('data-target')
       
    26 
       
    27     if (!selector) {
       
    28       selector = $this.attr('href')
       
    29       selector = selector && /#[A-Za-z]/.test(selector) && selector.replace(/.*(?=#[^\s]*$)/, '') // strip for ie7
       
    30     }
       
    31 
       
    32     var $parent = selector && $(selector)
       
    33 
       
    34     return $parent && $parent.length ? $parent : $this.parent()
       
    35   }
       
    36 
       
    37   function clearMenus(e) {
       
    38     if (e && e.which === 3) return
       
    39     $(backdrop).remove()
       
    40     $(toggle).each(function () {
       
    41       var $this         = $(this)
       
    42       var $parent       = getParent($this)
       
    43       var relatedTarget = { relatedTarget: this }
       
    44 
       
    45       if (!$parent.hasClass('open')) return
       
    46 
       
    47       if (e && e.type == 'click' && /input|textarea/i.test(e.target.tagName) && $.contains($parent[0], e.target)) return
       
    48 
       
    49       $parent.trigger(e = $.Event('hide.bs.dropdown', relatedTarget))
       
    50 
       
    51       if (e.isDefaultPrevented()) return
       
    52 
       
    53       $this.attr('aria-expanded', 'false')
       
    54       $parent.removeClass('open').trigger('hidden.bs.dropdown', relatedTarget)
       
    55     })
       
    56   }
       
    57 
       
    58   Dropdown.prototype.toggle = function (e) {
       
    59     var $this = $(this)
       
    60 
       
    61     if ($this.is('.disabled, :disabled')) return
       
    62 
       
    63     var $parent  = getParent($this)
       
    64     var isActive = $parent.hasClass('open')
       
    65 
       
    66     clearMenus()
       
    67 
       
    68     if (!isActive) {
       
    69       if ('ontouchstart' in document.documentElement && !$parent.closest('.navbar-nav').length) {
       
    70         // if mobile we use a backdrop because click events don't delegate
       
    71         $(document.createElement('div'))
       
    72           .addClass('dropdown-backdrop')
       
    73           .insertAfter($(this))
       
    74           .on('click', clearMenus)
       
    75       }
       
    76 
       
    77       var relatedTarget = { relatedTarget: this }
       
    78       $parent.trigger(e = $.Event('show.bs.dropdown', relatedTarget))
       
    79 
       
    80       if (e.isDefaultPrevented()) return
       
    81 
       
    82       $this
       
    83         .trigger('focus')
       
    84         .attr('aria-expanded', 'true')
       
    85 
       
    86       $parent
       
    87         .toggleClass('open')
       
    88         .trigger('shown.bs.dropdown', relatedTarget)
       
    89     }
       
    90 
       
    91     return false
       
    92   }
       
    93 
       
    94   Dropdown.prototype.keydown = function (e) {
       
    95     if (!/(38|40|27|32)/.test(e.which) || /input|textarea/i.test(e.target.tagName)) return
       
    96 
       
    97     var $this = $(this)
       
    98 
       
    99     e.preventDefault()
       
   100     e.stopPropagation()
       
   101 
       
   102     if ($this.is('.disabled, :disabled')) return
       
   103 
       
   104     var $parent  = getParent($this)
       
   105     var isActive = $parent.hasClass('open')
       
   106 
       
   107     if (!isActive && e.which != 27 || isActive && e.which == 27) {
       
   108       if (e.which == 27) $parent.find(toggle).trigger('focus')
       
   109       return $this.trigger('click')
       
   110     }
       
   111 
       
   112     var desc = ' li:not(.disabled):visible a'
       
   113     var $items = $parent.find('.dropdown-menu' + desc)
       
   114 
       
   115     if (!$items.length) return
       
   116 
       
   117     var index = $items.index(e.target)
       
   118 
       
   119     if (e.which == 38 && index > 0)                 index--         // up
       
   120     if (e.which == 40 && index < $items.length - 1) index++         // down
       
   121     if (!~index)                                    index = 0
       
   122 
       
   123     $items.eq(index).trigger('focus')
       
   124   }
       
   125 
       
   126 
       
   127   // DROPDOWN PLUGIN DEFINITION
       
   128   // ==========================
       
   129 
       
   130   function Plugin(option) {
       
   131     return this.each(function () {
       
   132       var $this = $(this)
       
   133       var data  = $this.data('bs.dropdown')
       
   134 
       
   135       if (!data) $this.data('bs.dropdown', (data = new Dropdown(this)))
       
   136       if (typeof option == 'string') data[option].call($this)
       
   137     })
       
   138   }
       
   139 
       
   140   var old = $.fn.dropdown
       
   141 
       
   142   $.fn.dropdown             = Plugin
       
   143   $.fn.dropdown.Constructor = Dropdown
       
   144 
       
   145 
       
   146   // DROPDOWN NO CONFLICT
       
   147   // ====================
       
   148 
       
   149   $.fn.dropdown.noConflict = function () {
       
   150     $.fn.dropdown = old
       
   151     return this
       
   152   }
       
   153 
       
   154 
       
   155   // APPLY TO STANDARD DROPDOWN ELEMENTS
       
   156   // ===================================
       
   157 
       
   158   $(document)
       
   159     .on('click.bs.dropdown.data-api', clearMenus)
       
   160     .on('click.bs.dropdown.data-api', '.dropdown form', function (e) { e.stopPropagation() })
       
   161     .on('click.bs.dropdown.data-api', toggle, Dropdown.prototype.toggle)
       
   162     .on('keydown.bs.dropdown.data-api', toggle, Dropdown.prototype.keydown)
       
   163     .on('keydown.bs.dropdown.data-api', '.dropdown-menu', Dropdown.prototype.keydown)
       
   164 
       
   165 }(jQuery);