diff --git a/WebContent/js/bootstrap.js b/WebContent/js/bootstrap.js index 8a2e99a..e4e17cc 100644 --- a/WebContent/js/bootstrap.js +++ b/WebContent/js/bootstrap.js @@ -4,13 +4,16 @@ * Licensed under the MIT license */ +// 检查是否加载了jQuery,如果没有,则抛出错误 if (typeof jQuery === 'undefined') { throw new Error('Bootstrap\'s JavaScript requires jQuery') } +function ($) { 'use strict'; + // 获取当前页面加载的jQuery版本 var version = $.fn.jquery.split(' ')[0].split('.') + // 如果jQuery版本不符合要求(小于1.9.1或大于3),则抛出错误 if ((version[0] < 2 && version[1] < 9) || (version[0] == 1 && version[1] == 9 && version[2] < 1) || (version[0] > 3)) { throw new Error('Bootstrap\'s JavaScript requires jQuery version 1.9.1 or higher, but lower than version 4') } @@ -24,47 +27,50 @@ if (typeof jQuery === 'undefined') { * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE) * ======================================================================== */ - +function ($) { 'use strict'; - // CSS TRANSITION SUPPORT (Shoutout: http://www.modernizr.com/) + // 检查浏览器是否支持CSS过渡 // ============================================================ function transitionEnd() { var el = document.createElement('bootstrap') var transEndEventNames = { - WebkitTransition : 'webkitTransitionEnd', - MozTransition : 'transitionend', - OTransition : 'oTransitionEnd otransitionend', - transition : 'transitionend' + WebkitTransition : 'webkitTransitionEnd', // Safari浏览器 + MozTransition : 'transitionend', // Firefox浏览器 + OTransition : 'oTransitionEnd otransitionend', // Opera浏览器 + transition : 'transitionend' // 标准浏览器 } + // 遍历不同的浏览器前缀,检查支持的过渡事件 for (var name in transEndEventNames) { if (el.style[name] !== undefined) { return { end: transEndEventNames[name] } } } - return false // explicit for ie8 ( ._.) + return false // 如果浏览器不支持过渡(例如IE8),返回false } - // http://blog.alexmaccaw.com/css-transitions + // 模拟过渡结束事件 $.fn.emulateTransitionEnd = function (duration) { var called = false var $el = this + // 绑定过渡结束事件 $(this).one('bsTransitionEnd', function () { called = true }) var callback = function () { if (!called) $($el).trigger($.support.transition.end) } - setTimeout(callback, duration) + setTimeout(callback, duration) // 设置超时时间 return this } $(function () { $.support.transition = transitionEnd() + // 如果不支持过渡,直接返回 if (!$.support.transition) return + // 定义过渡结束事件 $.event.special.bsTransitionEnd = { bindType: $.support.transition.end, delegateType: $.support.transition.end, @@ -84,59 +90,60 @@ if (typeof jQuery === 'undefined') { * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE) * ======================================================================== */ - +function ($) { 'use strict'; - // ALERT CLASS DEFINITION + // 定义ALERT类 // ====================== - var dismiss = '[data-dismiss="alert"]' + var dismiss = '[data-dismiss="alert"]' // 关闭按钮的选择器 var Alert = function (el) { - $(el).on('click', dismiss, this.close) + $(el).on('click', dismiss, this.close) // 点击关闭按钮时调用close方法 } Alert.VERSION = '3.3.7' - Alert.TRANSITION_DURATION = 150 + Alert.TRANSITION_DURATION = 150 // 过渡时间 + // 关闭alert的实现 Alert.prototype.close = function (e) { var $this = $(this) - var selector = $this.attr('data-target') + var selector = $this.attr('data-target') // 获取目标元素 + // 如果没有定义target,尝试通过href获取目标 if (!selector) { selector = $this.attr('href') - selector = selector && selector.replace(/.*(?=#[^\s]*$)/, '') // strip for ie7 + selector = selector && selector.replace(/.*(?=#[^\s]*$)/, '') // 为兼容IE7做的处理 } var $parent = $(selector === '#' ? [] : selector) - if (e) e.preventDefault() + if (e) e.preventDefault() // 防止默认事件 if (!$parent.length) { - $parent = $this.closest('.alert') + $parent = $this.closest('.alert') // 如果没有找到目标元素,查找最近的.alert元素 } + // 触发关闭事件 $parent.trigger(e = $.Event('close.bs.alert')) - if (e.isDefaultPrevented()) return + if (e.isDefaultPrevented()) return // 如果关闭事件被取消,则返回 - $parent.removeClass('in') + $parent.removeClass('in') // 移除'in'类,开始过渡效果 function removeElement() { - // detach from parent, fire event then clean up data - $parent.detach().trigger('closed.bs.alert').remove() + $parent.detach().trigger('closed.bs.alert').remove() // 从DOM中移除该alert元素 } + // 如果支持过渡效果,则使用过渡动画移除元素,否则直接移除 $.support.transition && $parent.hasClass('fade') ? - $parent - .one('bsTransitionEnd', removeElement) - .emulateTransitionEnd(Alert.TRANSITION_DURATION) : - removeElement() + $parent + .one('bsTransitionEnd', removeElement) + .emulateTransitionEnd(Alert.TRANSITION_DURATION) : + removeElement() } - - // ALERT PLUGIN DEFINITION + // 定义Alert插件 // ======================= function Plugin(option) { @@ -145,7 +152,7 @@ if (typeof jQuery === 'undefined') { var data = $this.data('bs.alert') if (!data) $this.data('bs.alert', (data = new Alert(this))) - if (typeof option == 'string') data[option].call($this) + if (typeof option == 'string') data[option].call($this) // 调用Alert的方法 }) } @@ -154,7 +161,6 @@ if (typeof jQuery === 'undefined') { $.fn.alert = Plugin $.fn.alert.Constructor = Alert - // ALERT NO CONFLICT // ================= @@ -163,11 +169,10 @@ if (typeof jQuery === 'undefined') { return this } - // ALERT DATA-API // ============== - $(document).on('click.bs.alert.data-api', dismiss, Alert.prototype.close) + $(document).on('click.bs.alert.data-api', dismiss, Alert.prototype.close) // 绑定关闭事件 }(jQuery); @@ -179,36 +184,36 @@ if (typeof jQuery === 'undefined') { * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE) * ======================================================================== */ - +function ($) { 'use strict'; - // BUTTON PUBLIC CLASS DEFINITION + // BUTTON公共类定义 // ============================== var Button = function (element, options) { this.$element = $(element) this.options = $.extend({}, Button.DEFAULTS, options) - this.isLoading = false + this.isLoading = false // 按钮的加载状态 } Button.VERSION = '3.3.7' Button.DEFAULTS = { - loadingText: 'loading...' + loadingText: 'loading...' // 加载中的文字 } + // 设置按钮的状态 Button.prototype.setState = function (state) { - var d = 'disabled' + var d = 'disabled' // 禁用状态 var $el = this.$element - var val = $el.is('input') ? 'val' : 'html' + var val = $el.is('input') ? 'val' : 'html' // 判断按钮类型 var data = $el.data() state += 'Text' - if (data.resetText == null) $el.data('resetText', $el[val]()) + if (data.resetText == null) $el.data('resetText', $el[val]()) // 记录初始文本 - // push to event loop to allow forms to submit + // 延时更新按钮文本,并根据状态启用或禁用按钮 setTimeout($.proxy(function () { $el[val](data[state] == null ? this.options[state] : data[state]) @@ -222,6 +227,7 @@ if (typeof jQuery === 'undefined') { }, this), 0) } + // 切换按钮的状态(激活/取消激活) Button.prototype.toggle = function () { var changed = true var $parent = this.$element.closest('[data-toggle="buttons"]') @@ -244,8 +250,7 @@ if (typeof jQuery === 'undefined') { } } - - // BUTTON PLUGIN DEFINITION + // BUTTON插件定义 // ======================== function Plugin(option) { @@ -256,8 +261,8 @@ if (typeof jQuery === 'undefined') { if (!data) $this.data('bs.button', (data = new Button(this, options))) - if (option == 'toggle') data.toggle() - else if (option) data.setState(option) + if (option == 'toggle') data.toggle() // 切换按钮状态 + else if (option) data.setState(option) // 设置按钮状态 }) } @@ -266,8 +271,7 @@ if (typeof jQuery === 'undefined') { $.fn.button = Plugin $.fn.button.Constructor = Button - - // BUTTON NO CONFLICT + // BUTTON无冲突模式 // ================== $.fn.button.noConflict = function () { @@ -275,25 +279,24 @@ if (typeof jQuery === 'undefined') { return this } - // BUTTON DATA-API // =============== $(document) - .on('click.bs.button.data-api', '[data-toggle^="button"]', function (e) { - var $btn = $(e.target).closest('.btn') - Plugin.call($btn, 'toggle') - if (!($(e.target).is('input[type="radio"], input[type="checkbox"]'))) { - // Prevent double click on radios, and the double selections (so cancellation) on checkboxes - e.preventDefault() - // The target component still receive the focus - if ($btn.is('input,button')) $btn.trigger('focus') - else $btn.find('input:visible,button:visible').first().trigger('focus') - } - }) - .on('focus.bs.button.data-api blur.bs.button.data-api', '[data-toggle^="button"]', function (e) { - $(e.target).closest('.btn').toggleClass('focus', /^focus(in)?$/.test(e.type)) - }) + .on('click.bs.button.data-api', '[data-toggle^="button"]', function (e) { + var $btn = $(e.target).closest('.btn') + Plugin.call($btn, 'toggle') + if (!($(e.target).is('input[type="radio"], input[type="checkbox"]'))) { + // 防止单选框和复选框的双击和多选(取消选择) + e.preventDefault() + // 让目标组件接收到焦点 + if ($btn.is('input,button')) $btn.trigger('focus') + else $btn.find('input:visible,button:visible').first().trigger('focus') + } + }) + .on('focus.bs.button.data-api blur.bs.button.data-api', '[data-toggle^="button"]', function (e) { + $(e.target).closest('.btn').toggleClass('focus', /^focus(in)?$/.test(e.type)) // 添加焦点样式 + }) }(jQuery); @@ -304,232 +307,242 @@ if (typeof jQuery === 'undefined') { * Copyright 2011-2016 Twitter, Inc. * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE) * ======================================================================== */ - - +function ($) { 'use strict'; - // CAROUSEL CLASS DEFINITION + // CAROUSEL 类定义 // ========================= var Carousel = function (element, options) { - this.$element = $(element) - this.$indicators = this.$element.find('.carousel-indicators') - this.options = options - this.paused = null - this.sliding = null - this.interval = null - this.$active = null - this.$items = null - + this.$element = $(element) // 获取carousel元素 + this.$indicators = this.$element.find('.carousel-indicators') // 获取carousel指示器(例如:小圆点) + this.options = options // 存储配置选项 + this.paused = null // 暂停状态 + this.sliding = null // 滑动状态 + this.interval = null // 自动切换间隔 + this.$active = null // 当前活动项 + this.$items = null // 所有的carousel项 + + // 如果启用了键盘控制,监听按键事件 this.options.keyboard && this.$element.on('keydown.bs.carousel', $.proxy(this.keydown, this)) + // 如果暂停选项为'hover',并且当前设备不支持触摸事件,设置鼠标悬停事件来暂停/恢复轮播 this.options.pause == 'hover' && !('ontouchstart' in document.documentElement) && this.$element - .on('mouseenter.bs.carousel', $.proxy(this.pause, this)) - .on('mouseleave.bs.carousel', $.proxy(this.cycle, this)) + .on('mouseenter.bs.carousel', $.proxy(this.pause, this)) // 鼠标进入时暂停 + .on('mouseleave.bs.carousel', $.proxy(this.cycle, this)) // 鼠标离开时恢复轮播 } - Carousel.VERSION = '3.3.7' + Carousel.VERSION = '3.3.7' // 版本信息 - Carousel.TRANSITION_DURATION = 600 + Carousel.TRANSITION_DURATION = 600 // 过渡持续时间(毫秒) Carousel.DEFAULTS = { - interval: 5000, - pause: 'hover', - wrap: true, - keyboard: true + interval: 5000, // 默认切换时间间隔(毫秒) + pause: 'hover', // 鼠标悬停时暂停 + wrap: true, // 是否循环轮播 + keyboard: true // 是否启用键盘控制 } + // 键盘事件处理 Carousel.prototype.keydown = function (e) { - if (/input|textarea/i.test(e.target.tagName)) return + if (/input|textarea/i.test(e.target.tagName)) return // 如果焦点在输入框或文本区域上,不处理 switch (e.which) { - case 37: this.prev(); break - case 39: this.next(); break + case 37: this.prev(); break // 左箭头 + case 39: this.next(); break // 右箭头 default: return } - e.preventDefault() + e.preventDefault() // 阻止默认行为 } + // 开始自动轮播 Carousel.prototype.cycle = function (e) { - e || (this.paused = false) + e || (this.paused = false) // 如果没有传入事件,恢复未暂停状态 + // 清除现有的interval this.interval && clearInterval(this.interval) + // 如果设置了自动切换并且没有暂停,重新启动自动轮播 this.options.interval - && !this.paused - && (this.interval = setInterval($.proxy(this.next, this), this.options.interval)) + && !this.paused + && (this.interval = setInterval($.proxy(this.next, this), this.options.interval)) return this } + // 获取当前项在所有项中的索引 Carousel.prototype.getItemIndex = function (item) { - this.$items = item.parent().children('.item') - return this.$items.index(item || this.$active) + this.$items = item.parent().children('.item') // 获取所有项 + return this.$items.index(item || this.$active) // 返回索引 } + // 获取用于切换方向的下一项 Carousel.prototype.getItemForDirection = function (direction, active) { - var activeIndex = this.getItemIndex(active) - var willWrap = (direction == 'prev' && activeIndex === 0) - || (direction == 'next' && activeIndex == (this.$items.length - 1)) - if (willWrap && !this.options.wrap) return active - var delta = direction == 'prev' ? -1 : 1 - var itemIndex = (activeIndex + delta) % this.$items.length - return this.$items.eq(itemIndex) + var activeIndex = this.getItemIndex(active) // 获取当前活动项的索引 + var willWrap = (direction == 'prev' && activeIndex === 0) // 是否回绕到最后一项 + || (direction == 'next' && activeIndex == (this.$items.length - 1)) // 是否回绕到第一项 + if (willWrap && !this.options.wrap) return active // 如果不循环,返回当前项 + var delta = direction == 'prev' ? -1 : 1 // 根据方向计算索引增量 + var itemIndex = (activeIndex + delta) % this.$items.length // 计算下一项的索引 + return this.$items.eq(itemIndex) // 返回下一项 } + // 跳转到指定位置 Carousel.prototype.to = function (pos) { var that = this - var activeIndex = this.getItemIndex(this.$active = this.$element.find('.item.active')) + var activeIndex = this.getItemIndex(this.$active = this.$element.find('.item.active')) // 获取当前活动项的索引 - if (pos > (this.$items.length - 1) || pos < 0) return + if (pos > (this.$items.length - 1) || pos < 0) return // 如果位置超出范围,返回 - if (this.sliding) return this.$element.one('slid.bs.carousel', function () { that.to(pos) }) // yes, "slid" - if (activeIndex == pos) return this.pause().cycle() + if (this.sliding) return this.$element.one('slid.bs.carousel', function () { that.to(pos) }) // 如果正在滑动,等待滑动完成后再跳转 + if (activeIndex == pos) return this.pause().cycle() // 如果目标位置是当前项,暂停并重新开始自动轮播 - return this.slide(pos > activeIndex ? 'next' : 'prev', this.$items.eq(pos)) + return this.slide(pos > activeIndex ? 'next' : 'prev', this.$items.eq(pos)) // 根据位置切换方向 } + // 暂停轮播 Carousel.prototype.pause = function (e) { - e || (this.paused = true) + e || (this.paused = true) // 如果没有传入事件,设置为暂停状态 + // 如果有过渡效果,触发过渡结束 if (this.$element.find('.next, .prev').length && $.support.transition) { this.$element.trigger($.support.transition.end) - this.cycle(true) + this.cycle(true) // 暂停后重新开始 } - this.interval = clearInterval(this.interval) + this.interval = clearInterval(this.interval) // 清除间隔 return this } + // 切换到下一项 Carousel.prototype.next = function () { - if (this.sliding) return - return this.slide('next') + if (this.sliding) return // 如果正在滑动,返回 + return this.slide('next') // 切换到下一项 } + // 切换到上一项 Carousel.prototype.prev = function () { - if (this.sliding) return - return this.slide('prev') + if (this.sliding) return // 如果正在滑动,返回 + return this.slide('prev') // 切换到上一项 } + // 执行滑动操作 Carousel.prototype.slide = function (type, next) { - var $active = this.$element.find('.item.active') - var $next = next || this.getItemForDirection(type, $active) - var isCycling = this.interval - var direction = type == 'next' ? 'left' : 'right' + var $active = this.$element.find('.item.active') // 当前活动项 + var $next = next || this.getItemForDirection(type, $active) // 获取下一项 + var isCycling = this.interval // 是否正在自动轮播 + var direction = type == 'next' ? 'left' : 'right' // 根据方向设置过渡方向 var that = this - if ($next.hasClass('active')) return (this.sliding = false) + if ($next.hasClass('active')) return (this.sliding = false) // 如果下一项已经是活动项,返回 var relatedTarget = $next[0] var slideEvent = $.Event('slide.bs.carousel', { relatedTarget: relatedTarget, direction: direction }) - this.$element.trigger(slideEvent) - if (slideEvent.isDefaultPrevented()) return + this.$element.trigger(slideEvent) // 触发slide事件 + if (slideEvent.isDefaultPrevented()) return // 如果事件被取消,返回 - this.sliding = true + this.sliding = true // 设置为滑动状态 - isCycling && this.pause() + isCycling && this.pause() // 如果正在轮播,先暂停 + // 更新指示器状态 if (this.$indicators.length) { this.$indicators.find('.active').removeClass('active') - var $nextIndicator = $(this.$indicators.children()[this.getItemIndex($next)]) + var $nextIndicator = $(this.$indicators.children()[this.getItemIndex($next)]) // 获取下一项的指示器 $nextIndicator && $nextIndicator.addClass('active') } - var slidEvent = $.Event('slid.bs.carousel', { relatedTarget: relatedTarget, direction: direction }) // yes, "slid" + var slidEvent = $.Event('slid.bs.carousel', { relatedTarget: relatedTarget, direction: direction }) // 滑动完成事件 if ($.support.transition && this.$element.hasClass('slide')) { - $next.addClass(type) - $next[0].offsetWidth // force reflow - $active.addClass(direction) - $next.addClass(direction) + $next.addClass(type) // 给下一项添加过渡类型 + $next[0].offsetWidth // 强制重排 + $active.addClass(direction) // 给当前项添加方向类 + $next.addClass(direction) // 给下一项添加方向类 $active - .one('bsTransitionEnd', function () { - $next.removeClass([type, direction].join(' ')).addClass('active') - $active.removeClass(['active', direction].join(' ')) - that.sliding = false - setTimeout(function () { - that.$element.trigger(slidEvent) - }, 0) - }) - .emulateTransitionEnd(Carousel.TRANSITION_DURATION) + .one('bsTransitionEnd', function () { + $next.removeClass([type, direction].join(' ')).addClass('active') // 移除过渡效果,设置为活动项 + $active.removeClass(['active', direction].join(' ')) // 移除活动状态 + that.sliding = false // 重置滑动状态 + setTimeout(function () { + that.$element.trigger(slidEvent) // 触发滑动完成事件 + }, 0) + }) + .emulateTransitionEnd(Carousel.TRANSITION_DURATION) // 模拟过渡结束 } else { - $active.removeClass('active') + $active.removeClass('active') // 直接切换,不使用过渡效果 $next.addClass('active') this.sliding = false - this.$element.trigger(slidEvent) + this.$element.trigger(slidEvent) // 触发滑动完成事件 } - isCycling && this.cycle() + isCycling && this.cycle() // 如果正在轮播,重新开始 return this } - - // CAROUSEL PLUGIN DEFINITION + // CAROUSEL 插件定义 // ========================== function Plugin(option) { return this.each(function () { var $this = $(this) - var data = $this.data('bs.carousel') - var options = $.extend({}, Carousel.DEFAULTS, $this.data(), typeof option == 'object' && option) - var action = typeof option == 'string' ? option : options.slide - - if (!data) $this.data('bs.carousel', (data = new Carousel(this, options))) - if (typeof option == 'number') data.to(option) - else if (action) data[action]() - else if (options.interval) data.pause().cycle() + var data = $this.data('bs.carousel') // 获取现有实例 + var options = $.extend({}, Carousel.DEFAULTS, $this.data(), typeof option == 'object' && option) // 合并配置 + var action = typeof option == 'string' ? option : options.slide // 获取操作类型 + + if (!data) $this.data('bs.carousel', (data = new Carousel(this, options))) // 如果没有实例,创建一个新的实例 + if (typeof option == 'number') data.to(option) // 如果是数字,跳转到指定位置 + else if (action) data[action]() // 如果是字符串,执行相应操作 + else if (options.interval) data.pause().cycle() // 如果有interval,暂停并启动自动轮播 }) } - var old = $.fn.carousel - - $.fn.carousel = Plugin - $.fn.carousel.Constructor = Carousel + var old = $.fn.carousel // 保存之前的插件定义 + $.fn.carousel = Plugin // 替换插件方法 + $.fn.carousel.Constructor = Carousel // 设置构造函数 - // CAROUSEL NO CONFLICT + // CAROUSEL 不冲突模式 // ==================== $.fn.carousel.noConflict = function () { - $.fn.carousel = old + $.fn.carousel = old // 恢复原来的插件定义 return this } - - // CAROUSEL DATA-API + // CAROUSEL 数据API // ================= var clickHandler = function (e) { var href var $this = $(this) - var $target = $($this.attr('data-target') || (href = $this.attr('href')) && href.replace(/.*(?=#[^\s]+$)/, '')) // strip for ie7 - if (!$target.hasClass('carousel')) return - var options = $.extend({}, $target.data(), $this.data()) - var slideIndex = $this.attr('data-slide-to') - if (slideIndex) options.interval = false + var $target = $($this.attr('data-target') || (href = $this.attr('href')) && href.replace(/.*(?=#[^\s]+$)/, '')) // 获取目标元素 + if (!$target.hasClass('carousel')) return // 如果目标不是carousel,返回 + var options = $.extend({}, $target.data(), $this.data()) // 合并配置选项 + var slideIndex = $this.attr('data-slide-to') // 获取数据属性中的slide索引 + if (slideIndex) options.interval = false // 禁用自动切换 - Plugin.call($target, options) + Plugin.call($target, options) // 调用插件 if (slideIndex) { - $target.data('bs.carousel').to(slideIndex) + $target.data('bs.carousel').to(slideIndex) // 跳转到指定位置 } - e.preventDefault() + e.preventDefault() // 阻止默认行为 } $(document) - .on('click.bs.carousel.data-api', '[data-slide]', clickHandler) - .on('click.bs.carousel.data-api', '[data-slide-to]', clickHandler) + .on('click.bs.carousel.data-api', '[data-slide]', clickHandler) // 监听点击事件 + .on('click.bs.carousel.data-api', '[data-slide-to]', clickHandler) // 监听点击事件 $(window).on('load', function () { $('[data-ride="carousel"]').each(function () { var $carousel = $(this) - Plugin.call($carousel, $carousel.data()) + Plugin.call($carousel, $carousel.data()) // 初始化所有的carousel元素 }) }) @@ -544,206 +557,215 @@ if (typeof jQuery === 'undefined') { * ======================================================================== */ /* jshint latedef: false */ - +function ($) { 'use strict'; - // COLLAPSE PUBLIC CLASS DEFINITION + // COLLAPSE 公共类定义 // ================================ var Collapse = function (element, options) { - this.$element = $(element) - this.options = $.extend({}, Collapse.DEFAULTS, options) - this.$trigger = $('[data-toggle="collapse"][href="#' + element.id + '"],' + - '[data-toggle="collapse"][data-target="#' + element.id + '"]') - this.transitioning = null + this.$element = $(element) // 获取目标元素 + this.options = $.extend({}, Collapse.DEFAULTS, options) // 合并默认配置和传入的配置 + this.$trigger = $('[data-toggle="collapse"][href="#' + element.id + '"],' + // 获取触发 collapse 的元素 + '[data-toggle="collapse"][data-target="#' + element.id + '"]') + this.transitioning = null // 初始化过渡状态 if (this.options.parent) { - this.$parent = this.getParent() + this.$parent = this.getParent() // 如果设置了 parent,获取父元素 } else { - this.addAriaAndCollapsedClass(this.$element, this.$trigger) + this.addAriaAndCollapsedClass(this.$element, this.$trigger) // 向元素和触发器添加 aria 属性和 collapsed 类 } - if (this.options.toggle) this.toggle() + if (this.options.toggle) this.toggle() // 如果 toggle 为 true,则自动调用 toggle 方法 } - Collapse.VERSION = '3.3.7' + Collapse.VERSION = '3.3.7' // 版本信息 - Collapse.TRANSITION_DURATION = 350 + Collapse.TRANSITION_DURATION = 350 // 过渡持续时间(毫秒) Collapse.DEFAULTS = { - toggle: true + toggle: true // 默认启用 toggle } + // 获取维度(宽度或高度) Collapse.prototype.dimension = function () { - var hasWidth = this.$element.hasClass('width') - return hasWidth ? 'width' : 'height' + var hasWidth = this.$element.hasClass('width') // 判断是否为宽度 + return hasWidth ? 'width' : 'height' // 返回宽度或高度 } + // 展开方法 Collapse.prototype.show = function () { - if (this.transitioning || this.$element.hasClass('in')) return + if (this.transitioning || this.$element.hasClass('in')) return // 如果正在过渡或已经展开,返回 var activesData - var actives = this.$parent && this.$parent.children('.panel').children('.in, .collapsing') + var actives = this.$parent && this.$parent.children('.panel').children('.in, .collapsing') // 获取所有正在展开或过渡中的元素 if (actives && actives.length) { - activesData = actives.data('bs.collapse') - if (activesData && activesData.transitioning) return + activesData = actives.data('bs.collapse') // 获取活动元素的 collapse 数据 + if (activesData && activesData.transitioning) return // 如果活动元素正在过渡,返回 } - var startEvent = $.Event('show.bs.collapse') + var startEvent = $.Event('show.bs.collapse') // 触发 show 事件 this.$element.trigger(startEvent) - if (startEvent.isDefaultPrevented()) return + if (startEvent.isDefaultPrevented()) return // 如果事件被取消,返回 + // 如果有其他活动项,先将它们隐藏 if (actives && actives.length) { Plugin.call(actives, 'hide') activesData || actives.data('bs.collapse', null) } - var dimension = this.dimension() + var dimension = this.dimension() // 获取维度(宽度或高度) this.$element - .removeClass('collapse') - .addClass('collapsing')[dimension](0) - .attr('aria-expanded', true) + .removeClass('collapse') // 移除 collapse 类 + .addClass('collapsing') // 添加 collapsing 类 + // 设置初始高度或宽度为 0 + .attr('aria-expanded', true) // 设置 aria-expanded 属性为 true this.$trigger - .removeClass('collapsed') - .attr('aria-expanded', true) + .removeClass('collapsed') // 移除 collapsed 类 + .attr('aria-expanded', true) // 设置 aria-expanded 属性为 true - this.transitioning = 1 + this.transitioning = 1 // 设置过渡状态为 1 var complete = function () { this.$element - .removeClass('collapsing') - .addClass('collapse in')[dimension]('') - this.transitioning = 0 + .removeClass('collapsing') // 移除 collapsing 类 + .addClass('collapse in') // 添加 collapse 和 in 类 + [dimension]('') // 恢复元素的原始维度 + this.transitioning = 0 // 重置过渡状态 this.$element - .trigger('shown.bs.collapse') + .trigger('shown.bs.collapse') // 触发 shown 事件 } - if (!$.support.transition) return complete.call(this) + if (!$.support.transition) return complete.call(this) // 如果不支持过渡,直接调用 complete - var scrollSize = $.camelCase(['scroll', dimension].join('-')) + var scrollSize = $.camelCase(['scroll', dimension].join('-')) // 获取滚动大小 this.$element - .one('bsTransitionEnd', $.proxy(complete, this)) - .emulateTransitionEnd(Collapse.TRANSITION_DURATION)[dimension](this.$element[0][scrollSize]) + .one('bsTransitionEnd', $.proxy(complete, this)) // 在过渡结束时触发 complete + .emulateTransitionEnd(Collapse.TRANSITION_DURATION) // 模拟过渡结束 + [dimension](this.$element[0][scrollSize]) // 设置滚动尺寸 } + // 收起方法 Collapse.prototype.hide = function () { - if (this.transitioning || !this.$element.hasClass('in')) return + if (this.transitioning || !this.$element.hasClass('in')) return // 如果正在过渡或没有展开,返回 - var startEvent = $.Event('hide.bs.collapse') + var startEvent = $.Event('hide.bs.collapse') // 触发 hide 事件 this.$element.trigger(startEvent) - if (startEvent.isDefaultPrevented()) return + if (startEvent.isDefaultPrevented()) return // 如果事件被取消,返回 - var dimension = this.dimension() + var dimension = this.dimension() // 获取维度(宽度或高度) - this.$element[dimension](this.$element[dimension]())[0].offsetHeight + this.$element[dimension](this.$element[dimension]())[0].offsetHeight // 强制重排 this.$element - .addClass('collapsing') - .removeClass('collapse in') - .attr('aria-expanded', false) + .addClass('collapsing') // 添加 collapsing 类 + .removeClass('collapse in') // 移除 collapse 和 in 类 + .attr('aria-expanded', false) // 设置 aria-expanded 属性为 false this.$trigger - .addClass('collapsed') - .attr('aria-expanded', false) + .addClass('collapsed') // 添加 collapsed 类 + .attr('aria-expanded', false) // 设置 aria-expanded 属性为 false - this.transitioning = 1 + this.transitioning = 1 // 设置过渡状态为 1 var complete = function () { - this.transitioning = 0 + this.transitioning = 0 // 重置过渡状态 this.$element - .removeClass('collapsing') - .addClass('collapse') - .trigger('hidden.bs.collapse') + .removeClass('collapsing') // 移除 collapsing 类 + .addClass('collapse') // 添加 collapse 类 + .trigger('hidden.bs.collapse') // 触发 hidden 事件 } - if (!$.support.transition) return complete.call(this) + if (!$.support.transition) return complete.call(this) // 如果不支持过渡,直接调用 complete this.$element - [dimension](0) - .one('bsTransitionEnd', $.proxy(complete, this)) - .emulateTransitionEnd(Collapse.TRANSITION_DURATION) + // 设置维度为 0 + .one('bsTransitionEnd', $.proxy(complete, this)) // 在过渡结束时触发 complete + .emulateTransitionEnd(Collapse.TRANSITION_DURATION) // 模拟过渡结束 } + // 切换展开和收起状态 Collapse.prototype.toggle = function () { - this[this.$element.hasClass('in') ? 'hide' : 'show']() + this[this.$element.hasClass('in') ? 'hide' : 'show']() // 如果已经展开则收起,否则展开 } + // 获取父元素(用于手风琴效果) Collapse.prototype.getParent = function () { - return $(this.options.parent) - .find('[data-toggle="collapse"][data-parent="' + this.options.parent + '"]') - .each($.proxy(function (i, element) { - var $element = $(element) - this.addAriaAndCollapsedClass(getTargetFromTrigger($element), $element) - }, this)) - .end() + return $(this.options.parent) // 获取父元素 + .find('[data-toggle="collapse"][data-parent="' + this.options.parent + '"]') // 查找子元素 + .each($.proxy(function (i, element) { + var $element = $(element) + this.addAriaAndCollapsedClass(getTargetFromTrigger($element), $element) // 为目标元素添加 aria 和 collapsed 类 + }, this)) + .end() } + // 向元素和触发器添加 aria 属性和 collapsed 类 Collapse.prototype.addAriaAndCollapsedClass = function ($element, $trigger) { - var isOpen = $element.hasClass('in') + var isOpen = $element.hasClass('in') // 判断元素是否展开 - $element.attr('aria-expanded', isOpen) + $element.attr('aria-expanded', isOpen) // 设置 aria-expanded 属性 $trigger - .toggleClass('collapsed', !isOpen) - .attr('aria-expanded', isOpen) + .toggleClass('collapsed', !isOpen) // 如果元素未展开,添加 collapsed 类 + .attr('aria-expanded', isOpen) // 设置 aria-expanded 属性 } + // 获取触发器对应的目标元素 function getTargetFromTrigger($trigger) { var href - var target = $trigger.attr('data-target') - || (href = $trigger.attr('href')) && href.replace(/.*(?=#[^\s]+$)/, '') // strip for ie7 - - return $(target) + var target = $trigger.attr('data-target') // 获取 data-target 属性 + || (href = $trigger.attr('href')) && href.replace(/.*(?=#[^\s]+$)/, '') // 如果没有 data-target,使用 href + return $(target) // 返回目标元素 } - // COLLAPSE PLUGIN DEFINITION + // COLLAPSE 插件定义 // ========================== function Plugin(option) { return this.each(function () { var $this = $(this) - var data = $this.data('bs.collapse') - var options = $.extend({}, Collapse.DEFAULTS, $this.data(), typeof option == 'object' && option) + var data = $this.data('bs.collapse') // 获取现有的 collapse 实例 + var options = $.extend({}, Collapse.DEFAULTS, $this.data(), typeof option == 'object' && option) // 合并默认配置和传入的配置 - if (!data && options.toggle && /show|hide/.test(option)) options.toggle = false - if (!data) $this.data('bs.collapse', (data = new Collapse(this, options))) - if (typeof option == 'string') data[option]() + if (!data && options.toggle && /show|hide/.test(option)) options.toggle = false // 如果没有实例且 option 是 'show' 或 'hide',则禁用 toggle + if (!data) $this.data('bs.collapse', (data = new Collapse(this, options))) // 如果没有实例,创建新实例 + if (typeof option == 'string') data[option]() // 如果 option 是字符串,调用对应的方法 }) } - var old = $.fn.collapse + var old = $.fn.collapse // 保存原始的 collapse 方法 - $.fn.collapse = Plugin - $.fn.collapse.Constructor = Collapse + $.fn.collapse = Plugin // 替换为新的 Plugin 方法 + $.fn.collapse.Constructor = Collapse // 设置构造函数为 Collapse - // COLLAPSE NO CONFLICT + // COLLAPSE 不冲突模式 // ==================== $.fn.collapse.noConflict = function () { - $.fn.collapse = old + $.fn.collapse = old // 恢复原来的 collapse 方法 return this } - // COLLAPSE DATA-API + // COLLAPSE 数据API // ================= $(document).on('click.bs.collapse.data-api', '[data-toggle="collapse"]', function (e) { var $this = $(this) - if (!$this.attr('data-target')) e.preventDefault() + if (!$this.attr('data-target')) e.preventDefault() // 如果没有 data-target,阻止默认行为 - var $target = getTargetFromTrigger($this) - var data = $target.data('bs.collapse') - var option = data ? 'toggle' : $this.data() + var $target = getTargetFromTrigger($this) // 获取目标元素 + var data = $target.data('bs.collapse') // 获取 collapse 数据 + var option = data ? 'toggle' : $this.data() // 如果已有实例,则执行 toggle,否则使用传入的配置 - Plugin.call($target, option) + Plugin.call($target, option) // 调用 Plugin }) }(jQuery); @@ -756,161 +778,164 @@ if (typeof jQuery === 'undefined') { * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE) * ======================================================================== */ - +function ($) { 'use strict'; - // DROPDOWN CLASS DEFINITION + // DROPDOWN 类定义 // ========================= - var backdrop = '.dropdown-backdrop' - var toggle = '[data-toggle="dropdown"]' + var backdrop = '.dropdown-backdrop' // 定义背景遮罩类名 + var toggle = '[data-toggle="dropdown"]' // 定义触发 dropdown 的元素选择器 var Dropdown = function (element) { - $(element).on('click.bs.dropdown', this.toggle) + $(element).on('click.bs.dropdown', this.toggle) // 为点击事件绑定 toggle 方法 } - Dropdown.VERSION = '3.3.7' + Dropdown.VERSION = '3.3.7' // 版本信息 + // 获取父元素 function getParent($this) { - var selector = $this.attr('data-target') + var selector = $this.attr('data-target') // 获取 data-target 属性 if (!selector) { - selector = $this.attr('href') - selector = selector && /#[A-Za-z]/.test(selector) && selector.replace(/.*(?=#[^\s]*$)/, '') // strip for ie7 + selector = $this.attr('href') // 如果没有 data-target,使用 href 属性 + selector = selector && /#[A-Za-z]/.test(selector) && selector.replace(/.*(?=#[^\s]*$)/, '') // 对 IE7 进行兼容处理 } - var $parent = selector && $(selector) + var $parent = selector && $(selector) // 获取目标元素 - return $parent && $parent.length ? $parent : $this.parent() + return $parent && $parent.length ? $parent : $this.parent() // 返回父元素,如果没有找到则返回当前元素的父级 } + // 清除菜单(关闭所有打开的下拉菜单) function clearMenus(e) { - if (e && e.which === 3) return - $(backdrop).remove() + if (e && e.which === 3) return // 如果点击的是右键,则不关闭菜单 + $(backdrop).remove() // 移除背景遮罩 $(toggle).each(function () { var $this = $(this) - var $parent = getParent($this) + var $parent = getParent($this) // 获取父元素 var relatedTarget = { relatedTarget: this } - if (!$parent.hasClass('open')) return + if (!$parent.hasClass('open')) return // 如果菜单没有展开,返回 - if (e && e.type == 'click' && /input|textarea/i.test(e.target.tagName) && $.contains($parent[0], e.target)) return + if (e && e.type == 'click' && /input|textarea/i.test(e.target.tagName) && $.contains($parent[0], e.target)) return // 点击在输入框或文本区内,则不关闭 - $parent.trigger(e = $.Event('hide.bs.dropdown', relatedTarget)) + $parent.trigger(e = $.Event('hide.bs.dropdown', relatedTarget)) // 触发隐藏事件 - if (e.isDefaultPrevented()) return + if (e.isDefaultPrevented()) return // 如果默认行为被阻止,则返回 - $this.attr('aria-expanded', 'false') - $parent.removeClass('open').trigger($.Event('hidden.bs.dropdown', relatedTarget)) + $this.attr('aria-expanded', 'false') // 更新 aria-expanded 属性 + $parent.removeClass('open').trigger($.Event('hidden.bs.dropdown', relatedTarget)) // 移除 open 类,并触发 hidden 事件 }) } + // 切换下拉菜单的显示状态 Dropdown.prototype.toggle = function (e) { var $this = $(this) - if ($this.is('.disabled, :disabled')) return + if ($this.is('.disabled, :disabled')) return // 如果元素被禁用,返回 - var $parent = getParent($this) - var isActive = $parent.hasClass('open') + var $parent = getParent($this) // 获取父元素 + var isActive = $parent.hasClass('open') // 判断当前菜单是否处于打开状态 - clearMenus() + clearMenus() // 关闭其他打开的菜单 if (!isActive) { if ('ontouchstart' in document.documentElement && !$parent.closest('.navbar-nav').length) { - // if mobile we use a backdrop because click events don't delegate + // 如果是移动端设备,则使用背景遮罩,因为点击事件无法委托 $(document.createElement('div')) - .addClass('dropdown-backdrop') - .insertAfter($(this)) - .on('click', clearMenus) + .addClass('dropdown-backdrop') // 创建背景遮罩 + .insertAfter($(this)) // 插入到当前元素后面 + .on('click', clearMenus) // 点击遮罩时关闭菜单 } var relatedTarget = { relatedTarget: this } - $parent.trigger(e = $.Event('show.bs.dropdown', relatedTarget)) + $parent.trigger(e = $.Event('show.bs.dropdown', relatedTarget)) // 触发显示事件 - if (e.isDefaultPrevented()) return + if (e.isDefaultPrevented()) return // 如果默认行为被阻止,返回 $this - .trigger('focus') - .attr('aria-expanded', 'true') + .trigger('focus') // 触发 focus 事件 + .attr('aria-expanded', 'true') // 设置 aria-expanded 属性为 true $parent - .toggleClass('open') - .trigger($.Event('shown.bs.dropdown', relatedTarget)) + .toggleClass('open') // 切换 open 类,控制下拉菜单的显示与隐藏 + .trigger($.Event('shown.bs.dropdown', relatedTarget)) // 触发 shown 事件 } - return false + return false // 阻止事件冒泡 } + // 处理键盘事件(上、下、Esc、空格) Dropdown.prototype.keydown = function (e) { - if (!/(38|40|27|32)/.test(e.which) || /input|textarea/i.test(e.target.tagName)) return + if (!/(38|40|27|32)/.test(e.which) || /input|textarea/i.test(e.target.tagName)) return // 只处理上、下箭头、Esc 和空格键,且不在输入框或文本区内处理 var $this = $(this) - e.preventDefault() - e.stopPropagation() + e.preventDefault() // 阻止默认事件 + e.stopPropagation() // 阻止事件冒泡 - if ($this.is('.disabled, :disabled')) return + if ($this.is('.disabled, :disabled')) return // 如果元素被禁用,返回 - var $parent = getParent($this) - var isActive = $parent.hasClass('open') + var $parent = getParent($this) // 获取父元素 + var isActive = $parent.hasClass('open') // 判断当前菜单是否处于打开状态 if (!isActive && e.which != 27 || isActive && e.which == 27) { - if (e.which == 27) $parent.find(toggle).trigger('focus') - return $this.trigger('click') + if (e.which == 27) $parent.find(toggle).trigger('focus') // 如果按下 Esc 键,聚焦触发器 + return $this.trigger('click') // 触发 click 事件 } - var desc = ' li:not(.disabled):visible a' - var $items = $parent.find('.dropdown-menu' + desc) + var desc = ' li:not(.disabled):visible a' // 查找可见且不被禁用的菜单项 + var $items = $parent.find('.dropdown-menu' + desc) // 获取所有有效的菜单项 - if (!$items.length) return + if (!$items.length) return // 如果没有菜单项,返回 - var index = $items.index(e.target) + var index = $items.index(e.target) // 获取当前选中的菜单项的索引 - if (e.which == 38 && index > 0) index-- // up - if (e.which == 40 && index < $items.length - 1) index++ // down - if (!~index) index = 0 + if (e.which == 38 && index > 0) index-- // 上箭头,索引减一 + if (e.which == 40 && index < $items.length - 1) index++ // 下箭头,索引加一 + if (!~index) index = 0 // 如果索引无效,则从第一个开始 - $items.eq(index).trigger('focus') + $items.eq(index).trigger('focus') // 聚焦到选中的菜单项 } - // DROPDOWN PLUGIN DEFINITION + // DROPDOWN 插件定义 // ========================== function Plugin(option) { return this.each(function () { var $this = $(this) - var data = $this.data('bs.dropdown') + var data = $this.data('bs.dropdown') // 获取现有的 dropdown 实例 - if (!data) $this.data('bs.dropdown', (data = new Dropdown(this))) - if (typeof option == 'string') data[option].call($this) + if (!data) $this.data('bs.dropdown', (data = new Dropdown(this))) // 如果没有实例,创建一个新的实例 + if (typeof option == 'string') data[option].call($this) // 如果传入的参数是字符串,则调用对应的方法 }) } - var old = $.fn.dropdown + var old = $.fn.dropdown // 保存原始的 dropdown 方法 - $.fn.dropdown = Plugin - $.fn.dropdown.Constructor = Dropdown + $.fn.dropdown = Plugin // 替换为新的 Plugin 方法 + $.fn.dropdown.Constructor = Dropdown // 设置构造函数为 Dropdown - // DROPDOWN NO CONFLICT + // DROPDOWN 不冲突模式 // ==================== $.fn.dropdown.noConflict = function () { - $.fn.dropdown = old + $.fn.dropdown = old // 恢复原来的 dropdown 方法 return this } - // APPLY TO STANDARD DROPDOWN ELEMENTS + // 应用到标准的 DROPDOWN 元素 // =================================== $(document) - .on('click.bs.dropdown.data-api', clearMenus) - .on('click.bs.dropdown.data-api', '.dropdown form', function (e) { e.stopPropagation() }) - .on('click.bs.dropdown.data-api', toggle, Dropdown.prototype.toggle) - .on('keydown.bs.dropdown.data-api', toggle, Dropdown.prototype.keydown) - .on('keydown.bs.dropdown.data-api', '.dropdown-menu', Dropdown.prototype.keydown) + .on('click.bs.dropdown.data-api', clearMenus) // 点击时清除菜单 + .on('click.bs.dropdown.data-api', '.dropdown form', function (e) { e.stopPropagation() }) // 阻止 form 元素点击时冒泡 + .on('click.bs.dropdown.data-api', toggle, Dropdown.prototype.toggle) // 点击触发下拉菜单的切换 + .on('keydown.bs.dropdown.data-api', toggle, Dropdown.prototype.keydown) // 键盘事件处理 + .on('keydown.bs.dropdown.data-api', '.dropdown-menu', Dropdown.prototype.keydown) // 键盘事件处理(在下拉菜单内) }(jQuery); @@ -922,192 +947,208 @@ if (typeof jQuery === 'undefined') { * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE) * ======================================================================== */ - +function ($) { 'use strict'; - // MODAL CLASS DEFINITION + // MODAL 类定义 // ====================== var Modal = function (element, options) { - this.options = options - this.$body = $(document.body) - this.$element = $(element) - this.$dialog = this.$element.find('.modal-dialog') - this.$backdrop = null - this.isShown = null - this.originalBodyPad = null - this.scrollbarWidth = 0 - this.ignoreBackdropClick = false - + this.options = options // 配置项 + this.$body = $(document.body) // 获取文档的 body 元素 + this.$element = $(element) // 当前 modal 元素 + this.$dialog = this.$element.find('.modal-dialog') // 获取 modal 对话框元素 + this.$backdrop = null // 背景遮罩 + this.isShown = null // 模态框是否已显示 + this.originalBodyPad = null // 原始 body padding + this.scrollbarWidth = 0 // 滚动条宽度 + this.ignoreBackdropClick = false // 是否忽略背景点击事件 + + // 如果有 remote 配置项,加载远程内容 if (this.options.remote) { this.$element - .find('.modal-content') - .load(this.options.remote, $.proxy(function () { - this.$element.trigger('loaded.bs.modal') - }, this)) + .find('.modal-content') + .load(this.options.remote, $.proxy(function () { + this.$element.trigger('loaded.bs.modal') // 内容加载完成后触发事件 + }, this)) } } - Modal.VERSION = '3.3.7' + Modal.VERSION = '3.3.7' // 版本号 + // 定义模态框过渡时间 Modal.TRANSITION_DURATION = 300 Modal.BACKDROP_TRANSITION_DURATION = 150 + // 默认配置项 Modal.DEFAULTS = { - backdrop: true, - keyboard: true, - show: true + backdrop: true, // 背景是否显示 + keyboard: true, // 是否响应键盘事件 + show: true // 是否显示模态框 } + // 切换显示或隐藏模态框 Modal.prototype.toggle = function (_relatedTarget) { - return this.isShown ? this.hide() : this.show(_relatedTarget) + return this.isShown ? this.hide() : this.show(_relatedTarget) // 如果模态框已显示,隐藏,否则显示 } + // 显示模态框 Modal.prototype.show = function (_relatedTarget) { var that = this - var e = $.Event('show.bs.modal', { relatedTarget: _relatedTarget }) + var e = $.Event('show.bs.modal', { relatedTarget: _relatedTarget }) // 触发 show 事件 - this.$element.trigger(e) + this.$element.trigger(e) // 触发模态框的 show 事件 - if (this.isShown || e.isDefaultPrevented()) return + if (this.isShown || e.isDefaultPrevented()) return // 如果已显示或事件被阻止,返回 - this.isShown = true + this.isShown = true // 标记为已显示 - this.checkScrollbar() - this.setScrollbar() - this.$body.addClass('modal-open') + this.checkScrollbar() // 检查是否有滚动条 + this.setScrollbar() // 设置滚动条 + this.$body.addClass('modal-open') // 在 body 上添加类 modal-open - this.escape() - this.resize() + this.escape() // 响应键盘 Escape 键 + this.resize() // 响应窗口大小变化 + // 绑定点击事件,点击关闭按钮时隐藏模态框 this.$element.on('click.dismiss.bs.modal', '[data-dismiss="modal"]', $.proxy(this.hide, this)) + // 处理模态框对话框的鼠标事件 this.$dialog.on('mousedown.dismiss.bs.modal', function () { that.$element.one('mouseup.dismiss.bs.modal', function (e) { - if ($(e.target).is(that.$element)) that.ignoreBackdropClick = true + if ($(e.target).is(that.$element)) that.ignoreBackdropClick = true // 如果点击了模态框,忽略背景点击 }) }) + // 处理背景遮罩 this.backdrop(function () { - var transition = $.support.transition && that.$element.hasClass('fade') + var transition = $.support.transition && that.$element.hasClass('fade') // 判断是否支持过渡动画 if (!that.$element.parent().length) { - that.$element.appendTo(that.$body) // don't move modals dom position + that.$element.appendTo(that.$body) // 不移动模态框的 DOM 位置 } that.$element - .show() - .scrollTop(0) + .show() // 显示模态框 + .scrollTop(0) // 滚动到顶部 - that.adjustDialog() + that.adjustDialog() // 调整模态框的位置 + // 如果需要过渡动画 if (transition) { - that.$element[0].offsetWidth // force reflow + that.$element[0].offsetWidth // 强制重新计算 DOM 样式 } - that.$element.addClass('in') + that.$element.addClass('in') // 添加显示类 in - that.enforceFocus() + that.enforceFocus() // 强制模态框聚焦 - var e = $.Event('shown.bs.modal', { relatedTarget: _relatedTarget }) + var e = $.Event('shown.bs.modal', { relatedTarget: _relatedTarget }) // 触发 shown 事件 transition ? - that.$dialog // wait for modal to slide in - .one('bsTransitionEnd', function () { - that.$element.trigger('focus').trigger(e) - }) - .emulateTransitionEnd(Modal.TRANSITION_DURATION) : - that.$element.trigger('focus').trigger(e) + that.$dialog // 等待模态框过渡完成 + .one('bsTransitionEnd', function () { + that.$element.trigger('focus').trigger(e) + }) + .emulateTransitionEnd(Modal.TRANSITION_DURATION) : + that.$element.trigger('focus').trigger(e) }) } + // 隐藏模态框 Modal.prototype.hide = function (e) { - if (e) e.preventDefault() + if (e) e.preventDefault() // 阻止默认事件 - e = $.Event('hide.bs.modal') + e = $.Event('hide.bs.modal') // 触发 hide 事件 - this.$element.trigger(e) + this.$element.trigger(e) // 触发模态框的 hide 事件 - if (!this.isShown || e.isDefaultPrevented()) return + if (!this.isShown || e.isDefaultPrevented()) return // 如果未显示或事件被阻止,返回 - this.isShown = false + this.isShown = false // 标记为未显示 - this.escape() - this.resize() + this.escape() // 取消响应 Escape 键 + this.resize() // 取消响应窗口大小变化 - $(document).off('focusin.bs.modal') + $(document).off('focusin.bs.modal') // 解除焦点事件监听 this.$element - .removeClass('in') - .off('click.dismiss.bs.modal') - .off('mouseup.dismiss.bs.modal') + .removeClass('in') // 移除显示类 in + .off('click.dismiss.bs.modal') + .off('mouseup.dismiss.bs.modal') this.$dialog.off('mousedown.dismiss.bs.modal') + // 如果需要过渡动画 $.support.transition && this.$element.hasClass('fade') ? - this.$element - .one('bsTransitionEnd', $.proxy(this.hideModal, this)) - .emulateTransitionEnd(Modal.TRANSITION_DURATION) : - this.hideModal() + this.$element + .one('bsTransitionEnd', $.proxy(this.hideModal, this)) + .emulateTransitionEnd(Modal.TRANSITION_DURATION) : + this.hideModal() // 隐藏模态框 } + // 强制模态框聚焦 Modal.prototype.enforceFocus = function () { $(document) - .off('focusin.bs.modal') // guard against infinite focus loop - .on('focusin.bs.modal', $.proxy(function (e) { - if (document !== e.target && - this.$element[0] !== e.target && - !this.$element.has(e.target).length) { - this.$element.trigger('focus') - } - }, this)) + .off('focusin.bs.modal') // 防止无限循环聚焦 + .on('focusin.bs.modal', $.proxy(function (e) { + if (document !== e.target && + this.$element[0] !== e.target && + !this.$element.has(e.target).length) { + this.$element.trigger('focus') // 如果点击的不是模态框内部元素,强制聚焦到模态框 + } + }, this)) } + // 响应键盘 Escape 键 Modal.prototype.escape = function () { if (this.isShown && this.options.keyboard) { this.$element.on('keydown.dismiss.bs.modal', $.proxy(function (e) { - e.which == 27 && this.hide() + e.which == 27 && this.hide() // 如果按下 Escape 键,隐藏模态框 }, this)) } else if (!this.isShown) { - this.$element.off('keydown.dismiss.bs.modal') + this.$element.off('keydown.dismiss.bs.modal') // 移除键盘事件监听 } } + // 响应窗口大小变化 Modal.prototype.resize = function () { if (this.isShown) { $(window).on('resize.bs.modal', $.proxy(this.handleUpdate, this)) } else { - $(window).off('resize.bs.modal') + $(window).off('resize.bs.modal') // 移除窗口大小变化事件监听 } } + // 隐藏模态框并移除背景 Modal.prototype.hideModal = function () { var that = this this.$element.hide() this.backdrop(function () { - that.$body.removeClass('modal-open') - that.resetAdjustments() - that.resetScrollbar() - that.$element.trigger('hidden.bs.modal') + that.$body.removeClass('modal-open') // 移除 modal-open 类 + that.resetAdjustments() // 重置样式 + that.resetScrollbar() // 重置滚动条 + that.$element.trigger('hidden.bs.modal') // 触发 hidden 事件 }) } + // 移除背景遮罩 Modal.prototype.removeBackdrop = function () { this.$backdrop && this.$backdrop.remove() this.$backdrop = null } + // 创建背景遮罩 Modal.prototype.backdrop = function (callback) { var that = this - var animate = this.$element.hasClass('fade') ? 'fade' : '' + var animate = this.$element.hasClass('fade') ? 'fade' : '' // 判断是否需要过渡效果 if (this.isShown && this.options.backdrop) { - var doAnimate = $.support.transition && animate + var doAnimate = $.support.transition && animate // 判断是否支持过渡动画 this.$backdrop = $(document.createElement('div')) - .addClass('modal-backdrop ' + animate) - .appendTo(this.$body) + .addClass('modal-backdrop ' + animate) + .appendTo(this.$body) this.$element.on('click.dismiss.bs.modal', $.proxy(function (e) { if (this.ignoreBackdropClick) { @@ -1116,55 +1157,57 @@ if (typeof jQuery === 'undefined') { } if (e.target !== e.currentTarget) return this.options.backdrop == 'static' - ? this.$element[0].focus() - : this.hide() + ? this.$element[0].focus() // 如果配置为 static,聚焦到模态框 + : this.hide() // 否则隐藏模态框 }, this)) - if (doAnimate) this.$backdrop[0].offsetWidth // force reflow + if (doAnimate) this.$backdrop[0].offsetWidth // 强制重新计算样式 - this.$backdrop.addClass('in') + this.$backdrop.addClass('in') // 显示背景 if (!callback) return doAnimate ? - this.$backdrop - .one('bsTransitionEnd', callback) - .emulateTransitionEnd(Modal.BACKDROP_TRANSITION_DURATION) : - callback() + this.$backdrop + .one('bsTransitionEnd', callback) + .emulateTransitionEnd(Modal.BACKDROP_TRANSITION_DURATION) : + callback() } else if (!this.isShown && this.$backdrop) { - this.$backdrop.removeClass('in') + this.$backdrop.removeClass('in') // 隐藏背景 var callbackRemove = function () { that.removeBackdrop() callback && callback() } $.support.transition && this.$element.hasClass('fade') ? - this.$backdrop - .one('bsTransitionEnd', callbackRemove) - .emulateTransitionEnd(Modal.BACKDROP_TRANSITION_DURATION) : - callbackRemove() + this.$backdrop + .one('bsTransitionEnd', callbackRemove) + .emulateTransitionEnd(Modal.BACKDROP_TRANSITION_DURATION) : + callbackRemove() } else if (callback) { callback() } } - // these following methods are used to handle overflowing modals + // 以下方法用于处理溢出的模态框 Modal.prototype.handleUpdate = function () { - this.adjustDialog() + this.adjustDialog() // 调整模态框位置 } + // 调整模态框的位置 Modal.prototype.adjustDialog = function () { - var modalIsOverflowing = this.$element[0].scrollHeight > document.documentElement.clientHeight + var modalIsOverflowing = this.$element[0].scrollHeight > document.documentElement.clientHeight // 判断模态框是否溢出 this.$element.css({ - paddingLeft: !this.bodyIsOverflowing && modalIsOverflowing ? this.scrollbarWidth : '', + paddingLeft: !this.bodyIsOverflowing && modalIsOverflowing ? this.scrollbarWidth : '', // 如果有滚动条,调整 padding paddingRight: this.bodyIsOverflowing && !modalIsOverflowing ? this.scrollbarWidth : '' }) } + // 重置模态框样式 Modal.prototype.resetAdjustments = function () { this.$element.css({ paddingLeft: '', @@ -1172,37 +1215,41 @@ if (typeof jQuery === 'undefined') { }) } + // 检查是否有滚动条 Modal.prototype.checkScrollbar = function () { var fullWindowWidth = window.innerWidth - if (!fullWindowWidth) { // workaround for missing window.innerWidth in IE8 + if (!fullWindowWidth) { // IE8 的兼容性处理 var documentElementRect = document.documentElement.getBoundingClientRect() fullWindowWidth = documentElementRect.right - Math.abs(documentElementRect.left) } - this.bodyIsOverflowing = document.body.clientWidth < fullWindowWidth - this.scrollbarWidth = this.measureScrollbar() + this.bodyIsOverflowing = document.body.clientWidth < fullWindowWidth // 判断 body 是否有溢出 + this.scrollbarWidth = this.measureScrollbar() // 获取滚动条宽度 } + // 设置滚动条样式 Modal.prototype.setScrollbar = function () { var bodyPad = parseInt((this.$body.css('padding-right') || 0), 10) this.originalBodyPad = document.body.style.paddingRight || '' - if (this.bodyIsOverflowing) this.$body.css('padding-right', bodyPad + this.scrollbarWidth) + if (this.bodyIsOverflowing) this.$body.css('padding-right', bodyPad + this.scrollbarWidth) // 如果有滚动条,设置 padding } + // 重置滚动条样式 Modal.prototype.resetScrollbar = function () { this.$body.css('padding-right', this.originalBodyPad) } + // 测量滚动条宽度 Modal.prototype.measureScrollbar = function () { // thx walsh var scrollDiv = document.createElement('div') scrollDiv.className = 'modal-scrollbar-measure' this.$body.append(scrollDiv) - var scrollbarWidth = scrollDiv.offsetWidth - scrollDiv.clientWidth + var scrollbarWidth = scrollDiv.offsetWidth - scrollDiv.clientWidth // 获取滚动条宽度 this.$body[0].removeChild(scrollDiv) return scrollbarWidth } - // MODAL PLUGIN DEFINITION + // MODAL 插件定义 // ======================= function Plugin(option, _relatedTarget) { @@ -1211,45 +1258,45 @@ if (typeof jQuery === 'undefined') { var data = $this.data('bs.modal') var options = $.extend({}, Modal.DEFAULTS, $this.data(), typeof option == 'object' && option) - if (!data) $this.data('bs.modal', (data = new Modal(this, options))) - if (typeof option == 'string') data[option](_relatedTarget) - else if (options.show) data.show(_relatedTarget) + if (!data) $this.data('bs.modal', (data = new Modal(this, options))) // 创建 Modal 实例 + if (typeof option == 'string') data[option](_relatedTarget) // 如果传入方法名,执行对应方法 + else if (options.show) data.show(_relatedTarget) // 默认显示模态框 }) } - var old = $.fn.modal + var old = $.fn.modal // 保存旧的插件定义 - $.fn.modal = Plugin - $.fn.modal.Constructor = Modal + $.fn.modal = Plugin // 定义 jQuery 插件 + $.fn.modal.Constructor = Modal // 导出构造函数 - // MODAL NO CONFLICT + // MODAL 无冲突处理 // ================= $.fn.modal.noConflict = function () { - $.fn.modal = old + $.fn.modal = old // 恢复旧的插件定义 return this } - // MODAL DATA-API + // MODAL 数据 API // ============== $(document).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 option = $target.data('bs.modal') ? 'toggle' : $.extend({ remote: !/#/.test(href) && href }, $target.data(), $this.data()) + var $target = $($this.attr('data-target') || (href && href.replace(/.*(?=#[^\s]+$)/, ''))) // 获取目标元素 + var option = $target.data('bs.modal') ? 'toggle' : $.extend({ remote: !/#/.test(href) && href }, $target.data(), $this.data()) // 获取选项 - if ($this.is('a')) e.preventDefault() + if ($this.is('a')) e.preventDefault() // 阻止默认行为 $target.one('show.bs.modal', function (showEvent) { - if (showEvent.isDefaultPrevented()) return // only register focus restorer if modal will actually get shown + if (showEvent.isDefaultPrevented()) return // 如果模态框不会显示,取消焦点恢复操作 $target.one('hidden.bs.modal', function () { - $this.is(':visible') && $this.trigger('focus') + $this.is(':visible') && $this.trigger('focus') // 隐藏时恢复焦点 }) }) - Plugin.call($target, option, this) + Plugin.call($target, option, this) // 调用插件 }) }(jQuery); @@ -1262,479 +1309,577 @@ if (typeof jQuery === 'undefined') { * Copyright 2011-2016 Twitter, Inc. * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE) * ======================================================================== */ - - +function ($) { 'use strict'; - // TOOLTIP PUBLIC CLASS DEFINITION + // TOOLTIP 公共类定义 // =============================== var Tooltip = function (element, options) { - this.type = null - this.options = null - this.enabled = null - this.timeout = null - this.hoverState = null - this.$element = null - this.inState = null + this.type = null; // Tooltip 类型 + this.options = null; // Tooltip 配置 + this.enabled = null; // Tooltip 是否启用 + this.timeout = null; // 延时变量 + this.hoverState = null; // 鼠标悬停状态 + this.$element = null; // 绑定的 DOM 元素 + this.inState = null; // 状态对象 {click, hover, focus} - this.init('tooltip', element, options) + this.init('tooltip', element, options); // 初始化 Tooltip 实例 } - Tooltip.VERSION = '3.3.7' - - Tooltip.TRANSITION_DURATION = 150 + Tooltip.VERSION = '3.3.7'; // Tooltip 版本 + Tooltip.TRANSITION_DURATION = 150; // 动画过渡时间 + // 默认配置 Tooltip.DEFAULTS = { - animation: true, - placement: 'top', - selector: false, - template: '