From 18ff57a183ac141f70164be481ef703729bcdf0d Mon Sep 17 00:00:00 2001 From: Jacob Thornton Date: Tue, 18 Aug 2015 19:22:46 -0700 Subject: js tests passing + eslint --- js/src/alert.js | 5 +++-- js/src/button.js | 9 ++++----- js/src/carousel.js | 32 +++++++++++++++++--------------- js/src/collapse.js | 19 +++++++++---------- js/src/dropdown.js | 24 ++++++++++++++++-------- js/src/modal.js | 18 +++++++++++------- js/src/popover.js | 2 +- js/src/scrollspy.js | 2 +- js/src/tab.js | 4 ++-- js/src/tooltip.js | 42 +++++++++++++++++++++++------------------- js/src/util.js | 46 +++++++++++++++++++++++++--------------------- 11 files changed, 112 insertions(+), 91 deletions(-) (limited to 'js/src') diff --git a/js/src/alert.js b/js/src/alert.js index e58a31175..da4e5b6a5 100644 --- a/js/src/alert.js +++ b/js/src/alert.js @@ -86,8 +86,8 @@ const Alert = (($) => { // private _getRootElement(element) { - let parent = false let selector = Util.getSelectorFromElement(element) + let parent = false if (selector) { parent = $(selector)[0] @@ -101,7 +101,8 @@ const Alert = (($) => { } _triggerCloseEvent(element) { - var closeEvent = $.Event(Event.CLOSE) + let closeEvent = $.Event(Event.CLOSE) + $(element).trigger(closeEvent) return closeEvent } diff --git a/js/src/button.js b/js/src/button.js index 8210e8ae0..58ef66b8b 100644 --- a/js/src/button.js +++ b/js/src/button.js @@ -20,7 +20,6 @@ const Button = (($) => { const EVENT_KEY = `.${DATA_KEY}` const DATA_API_KEY = '.data-api' const JQUERY_NO_CONFLICT = $.fn[NAME] - const TRANSITION_DURATION = 150 const ClassName = { ACTIVE : 'active', @@ -67,7 +66,7 @@ const Button = (($) => { toggle() { let triggerChangeEvent = true - let rootElement = $(this._element).closest( + let rootElement = $(this._element).closest( Selector.DATA_TOGGLE )[0] @@ -137,7 +136,7 @@ const Button = (($) => { */ $(document) - .on(Event.CLICK_DATA_API, Selector.DATA_TOGGLE_CARROT, function (event) { + .on(Event.CLICK_DATA_API, Selector.DATA_TOGGLE_CARROT, (event) => { event.preventDefault() let button = event.target @@ -148,8 +147,8 @@ const Button = (($) => { Button._jQueryInterface.call($(button), 'toggle') }) - .on(Event.FOCUS_BLUR_DATA_API, Selector.DATA_TOGGLE_CARROT, function (event) { - var button = $(event.target).closest(Selector.BUTTON)[0] + .on(Event.FOCUS_BLUR_DATA_API, Selector.DATA_TOGGLE_CARROT, (event) => { + let button = $(event.target).closest(Selector.BUTTON)[0] $(button).toggleClass(ClassName.FOCUS, /^focus(in)?$/.test(event.type)) }) diff --git a/js/src/carousel.js b/js/src/carousel.js index c11f0a599..efacd9494 100644 --- a/js/src/carousel.js +++ b/js/src/carousel.js @@ -171,13 +171,13 @@ const Carousel = (($) => { return } - if (activeIndex == index) { + if (activeIndex === index) { this.pause() this.cycle() return } - var direction = index > activeIndex ? + let direction = index > activeIndex ? Direction.NEXT : Direction.PREVIOUS @@ -213,7 +213,7 @@ const Carousel = (($) => { .on(Event.KEYDOWN, $.proxy(this._keydown, this)) } - if (this._config.pause == 'hover' && + if (this._config.pause === 'hover' && !('ontouchstart' in document.documentElement)) { $(this._element) .on(Event.MOUSEENTER, $.proxy(this.pause, this)) @@ -224,7 +224,9 @@ const Carousel = (($) => { _keydown(event) { event.preventDefault() - if (/input|textarea/i.test(event.target.tagName)) return + if (/input|textarea/i.test(event.target.tagName)) { + return + } switch (event.which) { case 37: this.prev(); break @@ -244,13 +246,13 @@ const Carousel = (($) => { let activeIndex = this._getItemIndex(activeElement) let lastItemIndex = (this._items.length - 1) let isGoingToWrap = (isPrevDirection && activeIndex === 0) || - (isNextDirection && activeIndex == lastItemIndex) + (isNextDirection && activeIndex === lastItemIndex) if (isGoingToWrap && !this._config.wrap) { return activeElement } - let delta = direction == Direction.PREVIOUS ? -1 : 1 + let delta = direction === Direction.PREVIOUS ? -1 : 1 let itemIndex = (activeIndex + delta) % this._items.length return itemIndex === -1 ? @@ -260,7 +262,7 @@ const Carousel = (($) => { _triggerSlideEvent(relatedTarget, directionalClassname) { let slideEvent = $.Event(Event.SLIDE, { - relatedTarget: relatedTarget, + relatedTarget, direction: directionalClassname }) @@ -290,9 +292,9 @@ const Carousel = (($) => { let nextElement = element || activeElement && this._getItemByDirection(direction, activeElement) - let isCycling = !!this._interval + let isCycling = Boolean(this._interval) - let directionalClassName = direction == Direction.NEXT ? + let directionalClassName = direction === Direction.NEXT ? ClassName.LEFT : ClassName.RIGHT @@ -319,7 +321,7 @@ const Carousel = (($) => { this._setActiveIndicatorElement(nextElement) - var slidEvent = $.Event(Event.SLID, { + let slidEvent = $.Event(Event.SLID, { relatedTarget: nextElement, direction: directionalClassName }) @@ -372,7 +374,7 @@ const Carousel = (($) => { static _jQueryInterface(config) { return this.each(function () { - let data = $(this).data(DATA_KEY) + let data = $(this).data(DATA_KEY) let _config = $.extend({}, Default, $(this).data()) if (typeof config === 'object') { @@ -386,7 +388,7 @@ const Carousel = (($) => { $(this).data(DATA_KEY, data) } - if (typeof config == 'number') { + if (typeof config === 'number') { data.to(config) } else if (action) { @@ -412,9 +414,9 @@ const Carousel = (($) => { return } - let config = $.extend({}, $(target).data(), $(this).data()) - + let config = $.extend({}, $(target).data(), $(this).data()) let slideIndex = this.getAttribute('data-slide-to') + if (slideIndex) { config.interval = false } @@ -440,7 +442,7 @@ const Carousel = (($) => { $(document) .on(Event.CLICK_DATA_API, Selector.DATA_SLIDE, Carousel._dataApiClickHandler) - $(window).on(Event.LOAD_DATA_API, function () { + $(window).on(Event.LOAD_DATA_API, () => { $(Selector.DATA_RIDE).each(function () { let $carousel = $(this) Carousel._jQueryInterface.call($carousel, $carousel.data()) diff --git a/js/src/collapse.js b/js/src/collapse.js index e911c98d1..e46d3ec60 100644 --- a/js/src/collapse.js +++ b/js/src/collapse.js @@ -182,15 +182,14 @@ const Collapse = (($) => { return } - let scrollSize = 'scroll' - + (dimension[0].toUpperCase() - + dimension.slice(1)) + let capitalizedDimension = dimension[0].toUpperCase() + dimension.slice(1) + let scrollSize = `scroll${capitalizedDimension}` $(this._element) .one(Util.TRANSITION_END, complete) .emulateTransitionEnd(TRANSITION_DURATION) - this._element.style[dimension] = this._element[scrollSize] + 'px' + this._element.style[dimension] = `${this._element[scrollSize]}px` } hide() { @@ -205,11 +204,11 @@ const Collapse = (($) => { return } - let dimension = this._getDimension() + let dimension = this._getDimension() let offsetDimension = dimension === Dimension.WIDTH ? 'offsetWidth' : 'offsetHeight' - this._element.style[dimension] = this._element[offsetDimension] + 'px' + this._element.style[dimension] = `${this._element[offsetDimension]}px` Util.reflow(this._element) @@ -239,7 +238,8 @@ const Collapse = (($) => { this._element.style[dimension] = 0 if (!Util.supportsTransitionEnd()) { - return complete() + complete() + return } $(this._element) @@ -266,7 +266,7 @@ const Collapse = (($) => { _getConfig(config) { config = $.extend({}, Default, config) - config.toggle = !!config.toggle // coerce string values + config.toggle = Boolean(config.toggle) // coerce string values Util.typeCheckConfig(NAME, config, DefaultType) return config } @@ -351,8 +351,7 @@ const Collapse = (($) => { event.preventDefault() let target = Collapse._getTargetFromElement(this) - - let data = $(target).data(DATA_KEY) + let data = $(target).data(DATA_KEY) let config = data ? 'toggle' : $(this).data() Collapse._jQueryInterface.call($(target), config) diff --git a/js/src/dropdown.js b/js/src/dropdown.js index 1c4866dc2..734e64312 100644 --- a/js/src/dropdown.js +++ b/js/src/dropdown.js @@ -78,7 +78,7 @@ const Dropdown = (($) => { toggle() { if (this.disabled || $(this).hasClass(ClassName.DISABLED)) { - return + return false } let parent = Dropdown._getParentFromElement(this) @@ -106,7 +106,7 @@ const Dropdown = (($) => { $(parent).trigger(showEvent) if (showEvent.isDefaultPrevented()) { - return + return false } this.focus() @@ -239,9 +239,17 @@ const Dropdown = (($) => { let index = items.indexOf(event.target) - if (event.which === 38 && index > 0) index-- // up - if (event.which === 40 && index < items.length - 1) index++ // down - if (!~index) index = 0 + if (event.which === 38 && index > 0) { // up + index-- + } + + if (event.which === 40 && index < items.length - 1) { // down + index++ + } + + if (!~index) { + index = 0 + } items[index].focus() } @@ -261,9 +269,9 @@ const Dropdown = (($) => { .on(Event.KEYDOWN_DATA_API, Selector.ROLE_LISTBOX, Dropdown._dataApiKeydownHandler) .on(Event.CLICK_DATA_API, Dropdown._clearMenus) .on(Event.CLICK_DATA_API, Selector.DATA_TOGGLE, Dropdown.prototype.toggle) - .on(Event.CLICK_DATA_API, Selector.FORM_CHILD, function (e) { - e.stopPropagation() - }) + .on(Event.CLICK_DATA_API, Selector.FORM_CHILD, (e) => { + e.stopPropagation() + }) /** diff --git a/js/src/modal.js b/js/src/modal.js index df49c1a4c..128863273 100644 --- a/js/src/modal.js +++ b/js/src/modal.js @@ -110,7 +110,7 @@ const Modal = (($) => { show(relatedTarget) { let showEvent = $.Event(Event.SHOW, { - relatedTarget: relatedTarget + relatedTarget }) $(this._element).trigger(showEvent) @@ -231,14 +231,18 @@ const Modal = (($) => { $(this._element).addClass(ClassName.IN) - if (this._config.focus) this._enforceFocus() + if (this._config.focus) { + this._enforceFocus() + } let shownEvent = $.Event(Event.SHOWN, { - relatedTarget: relatedTarget + relatedTarget }) let transitionComplete = () => { - if (this._config.focus) this._element.focus() + if (this._config.focus) { + this._element.focus() + } $(this._element).trigger(shownEvent) } @@ -389,11 +393,11 @@ const Modal = (($) => { this._element.scrollHeight > document.documentElement.clientHeight if (!this._isBodyOverflowing && isModalOverflowing) { - this._element.style.paddingLeft = this._scrollbarWidth + 'px' + this._element.style.paddingLeft = `${this._scrollbarWidth}px` } if (this._isBodyOverflowing && !isModalOverflowing) { - this._element.style.paddingRight = this._scrollbarWidth + 'px' + this._element.style.paddingRight = `${this._scrollbarWidth}px~` } } @@ -423,7 +427,7 @@ const Modal = (($) => { if (this._isBodyOverflowing) { document.body.style.paddingRight = - bodyPadding + this._scrollbarWidth + 'px' + bodyPadding + `${this._scrollbarWidth}px` } } diff --git a/js/src/popover.js b/js/src/popover.js index 31c7a3ae1..30c0e4acb 100644 --- a/js/src/popover.js +++ b/js/src/popover.js @@ -141,7 +141,7 @@ const Popover = (($) => { _getContent() { return this.element.getAttribute('data-content') - || (typeof this.config.content == 'function' ? + || (typeof this.config.content === 'function' ? this.config.content.call(this.element) : this.config.content) } diff --git a/js/src/scrollspy.js b/js/src/scrollspy.js index a407511f6..df33f48de 100644 --- a/js/src/scrollspy.js +++ b/js/src/scrollspy.js @@ -293,7 +293,7 @@ const ScrollSpy = (($) => { * ------------------------------------------------------------------------ */ - $(window).on(Event.LOAD_DATA_API, function () { + $(window).on(Event.LOAD_DATA_API, () => { let scrollSpys = $.makeArray($(Selector.DATA_SPY)) for (let i = scrollSpys.length; i--;) { diff --git a/js/src/tab.js b/js/src/tab.js index 4d8d7dec8..9d793417a 100644 --- a/js/src/tab.js +++ b/js/src/tab.js @@ -77,7 +77,7 @@ const Tab = (($) => { show() { if (this._element.parentNode && - (this._element.parentNode.nodeType == Node.ELEMENT_NODE) && + (this._element.parentNode.nodeType === Node.ELEMENT_NODE) && ($(this._element).parent().hasClass(ClassName.ACTIVE))) { return } @@ -157,7 +157,7 @@ const Tab = (($) => { let isTransitioning = callback && Util.supportsTransitionEnd() && ((active && $(active).hasClass(ClassName.FADE)) - || !!$(container).find(Selector.FADE_CHILD)[0]) + || Boolean($(container).find(Selector.FADE_CHILD)[0])) let complete = $.proxy( this._transitionComplete, diff --git a/js/src/tooltip.js b/js/src/tooltip.js index 5d62e154a..a65caf26e 100644 --- a/js/src/tooltip.js +++ b/js/src/tooltip.js @@ -174,11 +174,9 @@ const Tooltip = (($) => { } toggle(event) { - let context = this - let dataKey = this.constructor.DATA_KEY - if (event) { - context = $(event.currentTarget).data(dataKey) + let dataKey = this.constructor.DATA_KEY + let context = $(event.currentTarget).data(dataKey) if (!context) { context = new this.constructor( @@ -197,9 +195,13 @@ const Tooltip = (($) => { } } else { - $(context.getTipElement()).hasClass(ClassName.IN) ? - context._leave(null, context) : - context._enter(null, context) + + if ($(this.getTipElement()).hasClass(ClassName.IN)) { + this._leave(null, this) + return + } + + this._enter(null, this) } } @@ -267,9 +269,9 @@ const Tooltip = (($) => { $(this.element).trigger(this.constructor.Event.INSERTED) this._tether = new Tether({ + attachment, element : tip, target : this.element, - attachment : attachment, classes : TetherClass, classPrefix : CLASS_PREFIX, offset : this.config.offset, @@ -292,11 +294,14 @@ const Tooltip = (($) => { } } - Util.supportsTransitionEnd() && $(this.tip).hasClass(ClassName.FADE) ? + if (Util.supportsTransitionEnd() && $(this.tip).hasClass(ClassName.FADE)) { $(this.tip) .one(Util.TRANSITION_END, complete) - .emulateTransitionEnd(Tooltip._TRANSITION_DURATION) : - complete() + .emulateTransitionEnd(Tooltip._TRANSITION_DURATION) + return + } + + complete() } } @@ -343,7 +348,7 @@ const Tooltip = (($) => { // protected isWithContent() { - return !!this.getTitle() + return Boolean(this.getTitle()) } getTipElement() { @@ -407,10 +412,10 @@ const Tooltip = (($) => { ) } else if (trigger !== Trigger.MANUAL) { - let eventIn = trigger == Trigger.HOVER ? + let eventIn = trigger === Trigger.HOVER ? this.constructor.Event.MOUSEENTER : this.constructor.Event.FOCUSIN - let eventOut = trigger == Trigger.HOVER ? + let eventOut = trigger === Trigger.HOVER ? this.constructor.Event.MOUSELEAVE : this.constructor.Event.FOCUSOUT @@ -471,7 +476,7 @@ const Tooltip = (($) => { if (event) { context._activeTrigger[ - event.type == 'focusin' ? Trigger.FOCUS : Trigger.HOVER + event.type === 'focusin' ? Trigger.FOCUS : Trigger.HOVER ] = true } @@ -512,7 +517,7 @@ const Tooltip = (($) => { if (event) { context._activeTrigger[ - event.type == 'focusout' ? Trigger.FOCUS : Trigger.HOVER + event.type === 'focusout' ? Trigger.FOCUS : Trigger.HOVER ] = false } @@ -575,9 +580,8 @@ const Tooltip = (($) => { if (this.config) { for (let key in this.config) { - let value = this.config[key] - if (this.constructor.Default[key] !== value) { - config[key] = value + if (this.constructor.Default[key] !== this.config[key]) { + config[key] = this.config[key] } } } diff --git a/js/src/util.js b/js/src/util.js index 86bea6578..f4a584125 100644 --- a/js/src/util.js +++ b/js/src/util.js @@ -29,14 +29,14 @@ const Util = (($) => { } function isElement(obj) { - return (obj[0] || obj).nodeType; + return (obj[0] || obj).nodeType } function getSpecialTransitionEndEvent() { return { bindType: transition.end, delegateType: transition.end, - handle: function (event) { + handle(event) { if ($(event.target).is(this)) { return event.handleObj.handler.apply(this, arguments) } @@ -51,7 +51,7 @@ const Util = (($) => { let el = document.createElement('bootstrap') - for (var name in TransitionEndEvent) { + for (let name in TransitionEndEvent) { if (el.style[name] !== undefined) { return { end: TransitionEndEvent[name] } } @@ -63,7 +63,7 @@ const Util = (($) => { function transitionEndEmulator(duration) { let called = false - $(this).one(Util.TRANSITION_END, function () { + $(this).one(Util.TRANSITION_END, () => { called = true }) @@ -98,8 +98,9 @@ const Util = (($) => { TRANSITION_END: 'bsTransitionEnd', getUID(prefix) { - do prefix += ~~(Math.random() * 1000000) - while (document.getElementById(prefix)) + do { + prefix += ~~(Math.random() * 1000000) + } while (document.getElementById(prefix)) return prefix }, @@ -123,28 +124,31 @@ const Util = (($) => { }, supportsTransitionEnd() { - return !!transition + return Boolean(transition) }, typeCheckConfig(componentName, config, configTypes) { - for (let property in configTypes) { - let expectedTypes = configTypes[property] - let value = config[property] - let valueType - - if (value && isElement(value)) valueType = 'element' - else valueType = toType(value) - - if (!new RegExp(expectedTypes).test(valueType)) { - throw new Error( - `${componentName.toUpperCase()}: ` + - `Option "${property}" provided type "${valueType}" ` + - `but expected type "${expectedTypes}".`) + if (configTypes.hasOwnProperty(property)) { + let expectedTypes = configTypes[property] + let value = config[property] + let valueType + + if (value && isElement(value)) { + valueType = 'element' + } else { + valueType = toType(value) + } + + if (!new RegExp(expectedTypes).test(valueType)) { + throw new Error( + `${componentName.toUpperCase()}: ` + + `Option "${property}" provided type "${valueType}" ` + + `but expected type "${expectedTypes}".`) + } } } } - } setTransitionEndSupport() -- cgit v1.2.3