UNPKG

lixin-web

Version:

vue and bootstrap

196 lines (154 loc) 6.46 kB
/* ======================================================================== * Bootstrap: dropdown.js v3.0.0 * http://twbs.github.com/bootstrap/javascript.html#dropdowns * ======================================================================== * Copyright 2012 Twitter, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ======================================================================== */ import {throttle} from 'lodash' export default function ($,hoverBreakpoint) { "use strict"; // DROPDOWN CLASS DEFINITION // ========================= // var backdrop = '.toggle-backdrop' var toggle = '[data-toggle="dropdown"]' var toggleHover = '[data-toggle=dropdown-hover]' var mouseEnterStart; var $doc = $(document) var Dropdown = function (element) { $(element).on('click.bs.dropdown', this.toggle); } Dropdown.hoverBreakpoint = hoverBreakpoint || 768; Dropdown.TRANSITION_DURATION = 300 Dropdown.prototype.toggle = function(e){ var $this = $(this) if ($this.is('.disabled, :disabled')) return var $parent = getParent($this) var $menu = $parent.find('.dropdown-menu') var isActive = $parent.hasClass('open') if ($this.data('toggle') == 'dropdown'){ //data-toggle="dropdown" click //data-toggle="dropdown-hover" mouseenter clearMenus(toggle) }else{ mouseEnterStart = true; $parent.one('mouseleave.bs.toggle.data-api',throttle ? throttle(function () { mouseleave($this) },100) : function () { mouseleave($this) }) } if (!isActive) { var relatedTarget = { relatedTarget: this } $parent.trigger(e = $.Event('show.bs.toggle',relatedTarget)) if (e.isDefaultPrevented()) return $this.trigger('focus') .attr('aria-expanded', 'true') $parent .addClass('open') .trigger($.Event('shown.bs.dropdown', relatedTarget)) $menu.hasClass('fade') && setTimeout(function () { $menu.addClass('in') }) } return false } Dropdown.prototype.keydown = function (e) { if (!/(38|40|27)/.test(e.keyCode)) return var $this = $(this) e.preventDefault() e.stopPropagation() if ($this.is('.disabled, :disabled')) return var $parent = getParent($this) var isActive = $parent.hasClass('open') if (!isActive || (isActive && e.keyCode == 27)) { if (e.which == 27) { $parent.find(toggle + ',' + toggleHover).trigger('focus') } return $this.trigger($this.data('toggle') === 'dropdown-hover' ? 'mouseenter':'click'); } var desc = ' li:not(.divider):visible a' var $items = $parent.find('[role="menu"]' + desc + ', [role="listbox"]' + desc) if (!$items.length) return var index = $items.index($items.filter(':focus')) if (e.keyCode == 38 && index > 0) index-- // up if (e.keyCode == 40 && index < $items.length - 1) index++ // down if (!~index) index = 0 $items.eq(index).trigger('focus') } function mouseleave ($ele) { clearMenus(toggleHover); mouseEnterStart = false // $ele.blur(); } Dropdown.prototype.mouseleave = mouseleave function clearMenus(toggle) { // $(backdrop).remove() $(toggle).each(function (e) { var $this = $(this) var $parent = getParent($this) var $menu = $parent.find('.dropdown-menu') var relatedTarget = { relatedTarget: this } if (!$parent.hasClass('open')) return $parent.trigger(e = $.Event('hide.bs.toggle',relatedTarget)) if (e.isDefaultPrevented()) return $this.attr('aria-expanded', 'false') if ($.support.transition && $menu.hasClass('fade')) { $menu.removeClass('in').one('bsTransitionEnd', function () { $parent.removeClass('open').trigger($.Event('hidden.bs.dropdown', relatedTarget)) }) .emulateTransitionEnd(Dropdown.TRANSITION_DURATION) }else{ $parent.removeClass('open').trigger($.Event('hidden.bs.dropdown', relatedTarget)) } }) } function getParent($this) { var selector = $this.attr('data-target') if (!selector) { selector = $this.attr('href') selector = selector && /#/.test(selector) && selector.replace(/.*(?=#[^\s]*$)/, '') //strip for ie7 } var $parent = selector && $(selector) return $parent && $parent.length ? $parent : $this.parent() } // dropdown PLUGIN DEFINITION // ========================== var old = $.fn.dropdown $.fn.dropdown = function (option) { return this.each(function () { var $this = $(this) var data = $this.data('dropdown') if (!data) $this.data('dropdown', (data = new Dropdown(this))) if (typeof option == 'string') data[option].call($this) }) } $.fn.dropdown.Constructor = Dropdown // dropdown NO CONFLICT // ==================== $.fn.dropdown.noConflict = function () { $.fn.toggle = old return this } // APPLY TO STANDARD dropdown ELEMENTS // =================================== $doc .on('click.bs.toggle.data-api', function(e){ !mouseEnterStart && clearMenus(toggle)//click only }) .on('click.bs.toggle.data-api', '.toggle form', function (e) { e.stopPropagation() }) .on('click.bs.toggle.data-api' , toggle, Dropdown.prototype.toggle) .on('keydown.bs.toggle.data-api', toggle + ', [role=menu] ,' + toggle, Dropdown.prototype.keydown); (screen ? screen.width : document.documentElement.clientWidth) > Dropdown.hoverBreakpoint && $doc.on('mouseenter.bs.dropdown.data-api', toggleHover, throttle ? throttle(Dropdown.prototype.toggle,100) : Dropdown.prototype.toggle) }