Commit 69e4d4f3 authored by Johann-S's avatar Johann-S Committed by XhmikosR
Browse files

Rewrite Collapse without jQuery

parent 53ca76eb
main cleanup-floating-forms cssvar-function dependabot/npm_and_yarn/stylelint-and-stylelint-config-twbs-bootstrap-15.3.0 extend-snippets feat/data-target floating-always-visible floating-labels-icons fod-main-banner form-controls-with-icons github/fork/719media/patch-13 github/fork/719media/patch-14 github/fork/719media/patch-9 github/fork/ChellyAhmed/fix-typo-reboot.md github/fork/ChellyAhmed/offcanvas-scroll-back github/fork/CtrlAltLilith/main github/fork/Elysiome/offcanvas-optional-window-resizing github/fork/JanSargsyan/main github/fork/LunicLynx/support-different-line-height-for-buttons github/fork/Psixodelik/main github/fork/Ronid1/ronid1/offcanvas_static_backdrop github/fork/RyanBerliner/tooltip-accessibility github/fork/SantiagoPVazquez/Feature-default-border-bottom-to-dropdown-item github/fork/Sir-Genius/utils github/fork/Sumit-Singh-8/main github/fork/Viktor-VERA2020/offcanvas-slide github/fork/Zivangu9/input-group-for-form-control-plaintext github/fork/alpadev/alpadev/call-dispose-on-component-reinstantiation github/fork/astagi/fix/tree-shake-modules github/fork/compnerd/dark-accordion-icon github/fork/derSascha/dropdown-dont-close-on-input-click github/fork/dev-ph1l/main github/fork/donquixote/issue-33861-utl-mixin github/fork/florianlacreuse/mixin-make-row-gutter-y github/fork/gregorw/main github/fork/iteggmbh/transitionend-dispose-race github/fork/jdelStrother/patch-1 github/fork/jonnysp/form-floating github/fork/jonnysp/independent-offcanvas github/fork/jonnysp/theme-dark-on-card-and-modal-fix github/fork/josefdlange/floating-label-placeholder-opacity github/fork/julien-deramond/enhance-change-version.js github/fork/julien-deramond/main-jd-fix-offset-content github/fork/julien-deramond/main-jd-issue-with-utitlies github/fork/julien-deramond/main-xmr-pa11y-ci-jd-add-hideElements github/fork/kyletsang/fix-tooltip-padding github/fork/lacutah/CheckboxCenteringDocumentation github/fork/lekoala/patch-3 github/fork/louismaximepiton/main-kld-lmp-collapse-proposal github/fork/louismaximepiton/main-lmp-card-inner-border-radius-fix github/fork/louismaximepiton/main-lmp-carousel-multiple-images github/fork/louismaximepiton/main-lmp-css-var-init github/fork/louismaximepiton/main-lmp-disabled-floating-label-fix github/fork/louismaximepiton/main-lmp-input-range-fix github/fork/louismaximepiton/main-lmp-shift-color github/fork/louismaximepiton/main-lmp-table-active-tr-fix github/fork/maciek-szn/switch github/fork/michael-roth/feature/19964-multiple-tab-targets github/fork/mistic100/dom-utils github/fork/nkdas91/accordion github/fork/nstungcom/fix-missing-modal-open-class github/fork/oraliahdz/animation-utilities github/fork/pine3ree/patch-7 github/fork/pouwerkerk/unindent-scss-docs-shortcode github/fork/smares/smares-no-scolling-on-modal-close github/fork/tgm-git/patch-1 gs-forms gs-toasts-with-animated-progress-bar gs/add-history-helper gs/change-version-dir-on-docs gs/data-must-set-onlu-one-instance gs/docs/fix-drop-down-error gs/event-handler-2 gs/make-docs-js-build gs/make-simple-attribute-toggler gs/popover-fix-doc gs/provide-steConfig-method gs/scrollspy-smoothscroll-option-use-browser-history gs/streamline-jqueryInterface gs/support-drop-down-in-navbar gs/test-js-generic-trigger gs/try-web-components gs/tweak-collapse-js-selector gs/use-event-handler-in-cocmponent gs/use-rollup-replace-for-version jo-docs-thanks-page jo-ssr-friendly logical-props-spacing-utils main-fod-disabled-form-check-label main-fod-nested-accordion main-fod-simpler-table-structure main-fod-table-separator main-fod-utilities-contrast main-jd-abbr-title main-jd-add-chips main-jd-add-doc-for-sass-custom-colors main-jd-add-enable-host-to-handle-web-components main-jd-browserstack-fine-tune main-jd-browserstack-updates main-jd-docs-consistent-usage-of-css-sections-step-2 main-jd-fix-docs-headers-in-white main-jd-fix-highlight-docs-border-radius main-jd-fix-placeholder-color-background-params-for-img-markup main-jd-glossary-experiment main-jd-postcss-drop-empty-css-vars main-jd-proto-doc-astro main-jd-skip-navigation-component main-jd-stackblitz-for-examples main-jd-upgrade-browserlistrc main-jd-use-host main-lmp-dark-theme-customization main-lmp-handle-scroll-target main-lmp-tab-fix main-mc-opensearch main-xmr-bundlewatch-action main-xmr-eslint-plugin-compat main-xmr-hugo-docs-vendor main-xmr-hugo-rm-ver main-xmr-linkinator-prod main-xmr-min-mangle main-xmr-pa11y-ci more-darkmode-examples nested-dropdowns patrickhlauke-issue37428 patrickhlauke-use-of-color-tweaks pr/34102 pr/37590 previous-next-docs-links sticky-thead utilities-functions-mixin v530-dev v6-postcss-custom-media v6-spinner-dots v6/gs/use-floating-ui-in-place-of-popper xmr/dev xmr/docs-png xmr/docs-svgs xmr/hugo-reorg-files xmr/js-2 xmr/markdownlint xmr/prepare-530-alpha2 xmr/xo v5.3.0-alpha1 v5.2.3 v5.2.2 v5.2.1 v5.2.0 v5.2.0-beta1 v5.1.3 v5.1.2 v5.1.1 v5.1.0 v5.0.2 v5.0.1 v5.0.0 v5.0.0-beta3 v5.0.0-beta2 v5.0.0-beta1 v5.0.0-alpha3 v5.0.0-alpha2 v5.0.0-alpha1
5 merge requests!31948Examples/Floating-labels: fix bad behavior with autofill,!30064test,!29779Responsive sizing,!28882fix custom-select-indicator in IE10,!28721Hot test
Showing with 174 additions and 149 deletions
+174 -149
...@@ -5,7 +5,9 @@ ...@@ -5,7 +5,9 @@
* -------------------------------------------------------------------------- * --------------------------------------------------------------------------
*/ */
import $ from 'jquery' import Data from './dom/data'
import EventHandler from './dom/eventHandler'
import SelectorEngine from './dom/selectorEngine'
import Util from './util' import Util from './util'
/** /**
...@@ -19,7 +21,6 @@ const VERSION = '4.3.1' ...@@ -19,7 +21,6 @@ const VERSION = '4.3.1'
const DATA_KEY = 'bs.collapse' const DATA_KEY = 'bs.collapse'
const EVENT_KEY = `.${DATA_KEY}` const EVENT_KEY = `.${DATA_KEY}`
const DATA_API_KEY = '.data-api' const DATA_API_KEY = '.data-api'
const JQUERY_NO_CONFLICT = $.fn[NAME]
const Default = { const Default = {
toggle : true, toggle : true,
...@@ -67,19 +68,19 @@ class Collapse { ...@@ -67,19 +68,19 @@ class Collapse {
this._isTransitioning = false this._isTransitioning = false
this._element = element this._element = element
this._config = this._getConfig(config) this._config = this._getConfig(config)
this._triggerArray = [].slice.call(document.querySelectorAll( this._triggerArray = Util.makeArray(SelectorEngine.find(
`[data-toggle="collapse"][href="#${element.id}"],` + `[data-toggle="collapse"][href="#${element.id}"],` +
`[data-toggle="collapse"][data-target="#${element.id}"]` `[data-toggle="collapse"][data-target="#${element.id}"]`
)) ))
const toggleList = [].slice.call(document.querySelectorAll(Selector.DATA_TOGGLE)) const toggleList = Util.makeArray(document.querySelectorAll(Selector.DATA_TOGGLE))
for (let i = 0, len = toggleList.length; i < len; i++) { for (let i = 0, len = toggleList.length; i < len; i++) {
const elem = toggleList[i] const elem = toggleList[i]
const selector = Util.getSelectorFromElement(elem) const selector = Util.getSelectorFromElement(elem)
const filterElement = [].slice.call(document.querySelectorAll(selector)) const filterElement = Util.makeArray(document.querySelectorAll(selector))
.filter((foundElem) => foundElem === element) .filter((foundElem) => foundElem === element)
if (selector !== null && filterElement.length > 0) { if (selector !== null && filterElement.length) {
this._selector = selector this._selector = selector
this._triggerArray.push(elem) this._triggerArray.push(elem)
} }
...@@ -109,7 +110,7 @@ class Collapse { ...@@ -109,7 +110,7 @@ class Collapse {
// Public // Public
toggle() { toggle() {
if ($(this._element).hasClass(ClassName.SHOW)) { if (this._element.classList.contains(ClassName.SHOW)) {
this.hide() this.hide()
} else { } else {
this.show() this.show()
...@@ -118,7 +119,7 @@ class Collapse { ...@@ -118,7 +119,7 @@ class Collapse {
show() { show() {
if (this._isTransitioning || if (this._isTransitioning ||
$(this._element).hasClass(ClassName.SHOW)) { this._element.classList.contains(ClassName.SHOW)) {
return return
} }
...@@ -126,7 +127,7 @@ class Collapse { ...@@ -126,7 +127,7 @@ class Collapse {
let activesData let activesData
if (this._parent) { if (this._parent) {
actives = [].slice.call(this._parent.querySelectorAll(Selector.ACTIVES)) actives = Util.makeArray(this._parent.querySelectorAll(Selector.ACTIVES))
.filter((elem) => { .filter((elem) => {
if (typeof this._config.parent === 'string') { if (typeof this._config.parent === 'string') {
return elem.getAttribute('data-parent') === this._config.parent return elem.getAttribute('data-parent') === this._config.parent
...@@ -141,74 +142,70 @@ class Collapse { ...@@ -141,74 +142,70 @@ class Collapse {
} }
if (actives) { if (actives) {
activesData = $(actives).not(this._selector).data(DATA_KEY) activesData = Data.getData(actives[0], DATA_KEY)
if (activesData && activesData._isTransitioning) { if (activesData && activesData._isTransitioning) {
return return
} }
} }
const startEvent = $.Event(Event.SHOW) const startEvent = EventHandler.trigger(this._element, Event.SHOW)
$(this._element).trigger(startEvent) if (startEvent.defaultPrevented) {
if (startEvent.isDefaultPrevented()) {
return return
} }
if (actives) { if (actives) {
Collapse._jQueryInterface.call($(actives).not(this._selector), 'hide') actives.forEach((elemActive) => Collapse._collapseInterface(elemActive, 'hide'))
if (!activesData) { if (!activesData) {
$(actives).data(DATA_KEY, null) Data.setData(actives[0], DATA_KEY, null)
} }
} }
const dimension = this._getDimension() const dimension = this._getDimension()
$(this._element) this._element.classList.remove(ClassName.COLLAPSE)
.removeClass(ClassName.COLLAPSE) this._element.classList.add(ClassName.COLLAPSING)
.addClass(ClassName.COLLAPSING)
this._element.style[dimension] = 0 this._element.style[dimension] = 0
if (this._triggerArray.length) { if (this._triggerArray.length) {
$(this._triggerArray) this._triggerArray.forEach((element) => {
.removeClass(ClassName.COLLAPSED) element.classList.remove(ClassName.COLLAPSED)
.attr('aria-expanded', true) element.setAttribute('aria-expanded', true)
})
} }
this.setTransitioning(true) this.setTransitioning(true)
const complete = () => { const complete = () => {
$(this._element) this._element.classList.remove(ClassName.COLLAPSING)
.removeClass(ClassName.COLLAPSING) this._element.classList.add(ClassName.COLLAPSE)
.addClass(ClassName.COLLAPSE) this._element.classList.add(ClassName.SHOW)
.addClass(ClassName.SHOW)
this._element.style[dimension] = '' this._element.style[dimension] = ''
this.setTransitioning(false) this.setTransitioning(false)
$(this._element).trigger(Event.SHOWN) EventHandler.trigger(this._element, Event.SHOWN)
} }
const capitalizedDimension = dimension[0].toUpperCase() + dimension.slice(1) const capitalizedDimension = dimension[0].toUpperCase() + dimension.slice(1)
const scrollSize = `scroll${capitalizedDimension}` const scrollSize = `scroll${capitalizedDimension}`
const transitionDuration = Util.getTransitionDurationFromElement(this._element) const transitionDuration = Util.getTransitionDurationFromElement(this._element)
$(this._element) EventHandler.one(this._element, Util.TRANSITION_END, complete)
.one(Util.TRANSITION_END, complete)
Util.emulateTransitionEnd(transitionDuration) Util.emulateTransitionEnd(this._element, transitionDuration)
this._element.style[dimension] = `${this._element[scrollSize]}px` this._element.style[dimension] = `${this._element[scrollSize]}px`
} }
hide() { hide() {
if (this._isTransitioning || if (this._isTransitioning ||
!$(this._element).hasClass(ClassName.SHOW)) { !this._element.classList.contains(ClassName.SHOW)) {
return return
} }
const startEvent = $.Event(Event.HIDE) const startEvent = EventHandler.trigger(this._element, Event.HIDE)
$(this._element).trigger(startEvent) if (startEvent.defaultPrevented) {
if (startEvent.isDefaultPrevented()) {
return return
} }
...@@ -218,10 +215,9 @@ class Collapse { ...@@ -218,10 +215,9 @@ class Collapse {
Util.reflow(this._element) Util.reflow(this._element)
$(this._element) this._element.classList.add(ClassName.COLLAPSING)
.addClass(ClassName.COLLAPSING) this._element.classList.remove(ClassName.COLLAPSE)
.removeClass(ClassName.COLLAPSE) this._element.classList.remove(ClassName.SHOW)
.removeClass(ClassName.SHOW)
const triggerArrayLength = this._triggerArray.length const triggerArrayLength = this._triggerArray.length
if (triggerArrayLength > 0) { if (triggerArrayLength > 0) {
...@@ -230,10 +226,11 @@ class Collapse { ...@@ -230,10 +226,11 @@ class Collapse {
const selector = Util.getSelectorFromElement(trigger) const selector = Util.getSelectorFromElement(trigger)
if (selector !== null) { if (selector !== null) {
const $elem = $([].slice.call(document.querySelectorAll(selector))) const elem = SelectorEngine.findOne(selector)
if (!$elem.hasClass(ClassName.SHOW)) {
$(trigger).addClass(ClassName.COLLAPSED) if (!elem.classList.contains(ClassName.SHOW)) {
.attr('aria-expanded', false) trigger.classList.add(ClassName.COLLAPSED)
trigger.setAttribute('aria-expanded', false)
} }
} }
} }
...@@ -243,19 +240,16 @@ class Collapse { ...@@ -243,19 +240,16 @@ class Collapse {
const complete = () => { const complete = () => {
this.setTransitioning(false) this.setTransitioning(false)
$(this._element) this._element.classList.remove(ClassName.COLLAPSING)
.removeClass(ClassName.COLLAPSING) this._element.classList.add(ClassName.COLLAPSE)
.addClass(ClassName.COLLAPSE) EventHandler.trigger(this._element, Event.HIDDEN)
.trigger(Event.HIDDEN)
} }
this._element.style[dimension] = '' this._element.style[dimension] = ''
const transitionDuration = Util.getTransitionDurationFromElement(this._element) const transitionDuration = Util.getTransitionDurationFromElement(this._element)
$(this._element) EventHandler.one(this._element, Util.TRANSITION_END, complete)
.one(Util.TRANSITION_END, complete) Util.emulateTransitionEnd(this._element, transitionDuration)
Util.emulateTransitionEnd(transitionDuration)
} }
setTransitioning(isTransitioning) { setTransitioning(isTransitioning) {
...@@ -263,7 +257,7 @@ class Collapse { ...@@ -263,7 +257,7 @@ class Collapse {
} }
dispose() { dispose() {
$.removeData(this._element, DATA_KEY) Data.removeData(this._element, DATA_KEY)
this._config = null this._config = null
this._parent = null this._parent = null
...@@ -285,7 +279,7 @@ class Collapse { ...@@ -285,7 +279,7 @@ class Collapse {
} }
_getDimension() { _getDimension() {
const hasWidth = $(this._element).hasClass(Dimension.WIDTH) const hasWidth = this._element.classList.contains(Dimension.WIDTH)
return hasWidth ? Dimension.WIDTH : Dimension.HEIGHT return hasWidth ? Dimension.WIDTH : Dimension.HEIGHT
} }
...@@ -300,14 +294,14 @@ class Collapse { ...@@ -300,14 +294,14 @@ class Collapse {
parent = this._config.parent[0] parent = this._config.parent[0]
} }
} else { } else {
parent = document.querySelector(this._config.parent) parent = SelectorEngine.findOne(this._config.parent)
} }
const selector = const selector =
`[data-toggle="collapse"][data-parent="${this._config.parent}"]` `[data-toggle="collapse"][data-parent="${this._config.parent}"]`
const children = [].slice.call(parent.querySelectorAll(selector)) const elements = Util.makeArray(SelectorEngine.find(selector, parent))
$(children).each((i, element) => { elements.forEach((element) => {
this._addAriaAndCollapsedClass( this._addAriaAndCollapsedClass(
Collapse._getTargetFromElement(element), Collapse._getTargetFromElement(element),
[element] [element]
...@@ -318,12 +312,19 @@ class Collapse { ...@@ -318,12 +312,19 @@ class Collapse {
} }
_addAriaAndCollapsedClass(element, triggerArray) { _addAriaAndCollapsedClass(element, triggerArray) {
const isOpen = $(element).hasClass(ClassName.SHOW) if (element) {
const isOpen = element.classList.contains(ClassName.SHOW)
if (triggerArray.length) {
$(triggerArray) if (triggerArray.length) {
.toggleClass(ClassName.COLLAPSED, !isOpen) triggerArray.forEach((elem) => {
.attr('aria-expanded', isOpen) if (!isOpen) {
elem.classList.add(ClassName.COLLAPSED)
} else {
elem.classList.remove(ClassName.COLLAPSED)
}
elem.setAttribute('aria-expanded', isOpen)
})
}
} }
} }
...@@ -334,31 +335,34 @@ class Collapse { ...@@ -334,31 +335,34 @@ class Collapse {
return selector ? document.querySelector(selector) : null return selector ? document.querySelector(selector) : null
} }
static _jQueryInterface(config) { static _collapseInterface(element, config) {
return this.each(function () { let data = Data.getData(element, DATA_KEY)
const $this = $(this) const _config = {
let data = $this.data(DATA_KEY) ...Default,
const _config = { ...Util.getDataAttributes(element),
...Default, ...typeof config === 'object' && config ? config : {}
...$this.data(), }
...typeof config === 'object' && config ? config : {}
}
if (!data && _config.toggle && /show|hide/.test(config)) { if (!data && _config.toggle && /show|hide/.test(config)) {
_config.toggle = false _config.toggle = false
} }
if (!data) { if (!data) {
data = new Collapse(this, _config) data = new Collapse(element, _config)
$this.data(DATA_KEY, data) Data.setData(element, DATA_KEY, data)
} }
if (typeof config === 'string') { if (typeof config === 'string') {
if (typeof data[config] === 'undefined') { if (typeof data[config] === 'undefined') {
throw new TypeError(`No method named "${config}"`) throw new Error(`No method named "${config}"`)
}
data[config]()
} }
data[config]()
}
}
static _jQueryInterface(config) {
return this.each(function () {
Collapse._collapseInterface(this, config)
}) })
} }
} }
...@@ -369,21 +373,31 @@ class Collapse { ...@@ -369,21 +373,31 @@ class Collapse {
* ------------------------------------------------------------------------ * ------------------------------------------------------------------------
*/ */
$(document).on(Event.CLICK_DATA_API, Selector.DATA_TOGGLE, function (event) { EventHandler.on(document, Event.CLICK_DATA_API, Selector.DATA_TOGGLE, function (event) {
// preventDefault only for <a> elements (which change the URL) not inside the collapsible element // preventDefault only for <a> elements (which change the URL) not inside the collapsible element
if (event.currentTarget.tagName === 'A') { if (event.target.tagName === 'A') {
event.preventDefault() event.preventDefault()
} }
const $trigger = $(this) const triggerData = Util.getDataAttributes(this)
const selector = Util.getSelectorFromElement(this) const selector = Util.getSelectorFromElement(this)
const selectors = [].slice.call(document.querySelectorAll(selector)) const selectorElements = Util.makeArray(SelectorEngine.find(selector))
selectorElements.forEach((element) => {
const data = Data.getData(element, DATA_KEY)
let config
if (data) {
// update parent attribute
if (data._parent === null && typeof triggerData.parent === 'string') {
data._config.parent = triggerData.parent
data._parent = data._getParent()
}
config = 'toggle'
} else {
config = triggerData
}
$(selectors).each(function () { Collapse._collapseInterface(element, config)
const $target = $(this)
const data = $target.data(DATA_KEY)
const config = data ? 'toggle' : $trigger.data()
Collapse._jQueryInterface.call($target, config)
}) })
}) })
...@@ -391,13 +405,18 @@ $(document).on(Event.CLICK_DATA_API, Selector.DATA_TOGGLE, function (event) { ...@@ -391,13 +405,18 @@ $(document).on(Event.CLICK_DATA_API, Selector.DATA_TOGGLE, function (event) {
* ------------------------------------------------------------------------ * ------------------------------------------------------------------------
* jQuery * jQuery
* ------------------------------------------------------------------------ * ------------------------------------------------------------------------
* add .collapse to jQuery only if jQuery is present
*/ */
$.fn[NAME] = Collapse._jQueryInterface const $ = Util.jQuery
$.fn[NAME].Constructor = Collapse if (typeof $ !== 'undefined') {
$.fn[NAME].noConflict = () => { const JQUERY_NO_CONFLICT = $.fn[NAME]
$.fn[NAME] = JQUERY_NO_CONFLICT $.fn[NAME] = Collapse._jQueryInterface
return Collapse._jQueryInterface $.fn[NAME].Constructor = Collapse
$.fn[NAME].noConflict = () => {
$.fn[NAME] = JQUERY_NO_CONFLICT
return Collapse._jQueryInterface
}
} }
export default Collapse export default Collapse
...@@ -154,7 +154,14 @@ const Util = { ...@@ -154,7 +154,14 @@ const Util = {
if (typeof nodeList === 'undefined' || nodeList === null) { if (typeof nodeList === 'undefined' || nodeList === null) {
return [] return []
} }
return Array.prototype.slice.call(nodeList)
const strRepresentation = Object.prototype.toString.call(nodeList)
if (strRepresentation === '[object NodeList]' ||
strRepresentation === '[object HTMLCollection]' || strRepresentation === '[object Array]') {
return Array.prototype.slice.call(nodeList)
}
return [nodeList]
}, },
getDataAttributes(element) { getDataAttributes(element) {
......
...@@ -71,7 +71,7 @@ $(function () { ...@@ -71,7 +71,7 @@ $(function () {
assert.ok(!/height/i.test($el2.attr('style')), 'has height reset') assert.ok(!/height/i.test($el2.attr('style')), 'has height reset')
done() done()
}) })
$target.trigger('click') EventHandler.trigger($target[0], 'click')
}) })
QUnit.test('should collapse only the first collapse', function (assert) { QUnit.test('should collapse only the first collapse', function (assert) {
...@@ -165,7 +165,7 @@ $(function () { ...@@ -165,7 +165,7 @@ $(function () {
done() done()
}) })
$target.trigger('click') EventHandler.trigger($target[0], 'click')
}) })
QUnit.test('should add "collapsed" class to target when collapse is hidden', function (assert) { QUnit.test('should add "collapsed" class to target when collapse is hidden', function (assert) {
...@@ -181,7 +181,7 @@ $(function () { ...@@ -181,7 +181,7 @@ $(function () {
done() done()
}) })
$target.trigger('click') EventHandler.trigger($target[0], 'click')
}) })
QUnit.test('should remove "collapsed" class from all triggers targeting the collapse when the collapse is shown', function (assert) { QUnit.test('should remove "collapsed" class from all triggers targeting the collapse when the collapse is shown', function (assert) {
...@@ -199,7 +199,7 @@ $(function () { ...@@ -199,7 +199,7 @@ $(function () {
done() done()
}) })
$target.trigger('click') EventHandler.trigger($target[0], 'click')
}) })
QUnit.test('should add "collapsed" class to all triggers targeting the collapse when the collapse is hidden', function (assert) { QUnit.test('should add "collapsed" class to all triggers targeting the collapse when the collapse is hidden', function (assert) {
...@@ -217,7 +217,7 @@ $(function () { ...@@ -217,7 +217,7 @@ $(function () {
done() done()
}) })
$target.trigger('click') EventHandler.trigger($target[0], 'click')
}) })
QUnit.test('should not close a collapse when initialized with "show" option if already shown', function (assert) { QUnit.test('should not close a collapse when initialized with "show" option if already shown', function (assert) {
...@@ -309,7 +309,7 @@ $(function () { ...@@ -309,7 +309,7 @@ $(function () {
done() done()
}) })
$target3.trigger('click') EventHandler.trigger($target3[0], 'click')
}) })
QUnit.test('should allow dots in data-parent', function (assert) { QUnit.test('should allow dots in data-parent', function (assert) {
...@@ -343,7 +343,7 @@ $(function () { ...@@ -343,7 +343,7 @@ $(function () {
done() done()
}) })
$target3.trigger('click') EventHandler.trigger($target3[0], 'click')
}) })
QUnit.test('should set aria-expanded="true" on trigger/control when collapse is shown', function (assert) { QUnit.test('should set aria-expanded="true" on trigger/control when collapse is shown', function (assert) {
...@@ -359,7 +359,7 @@ $(function () { ...@@ -359,7 +359,7 @@ $(function () {
done() done()
}) })
$target.trigger('click') EventHandler.trigger($target[0], 'click')
}) })
QUnit.test('should set aria-expanded="false" on trigger/control when collapse is hidden', function (assert) { QUnit.test('should set aria-expanded="false" on trigger/control when collapse is hidden', function (assert) {
...@@ -375,7 +375,7 @@ $(function () { ...@@ -375,7 +375,7 @@ $(function () {
done() done()
}) })
$target.trigger('click') EventHandler.trigger($target[0], 'click')
}) })
QUnit.test('should set aria-expanded="true" on all triggers targeting the collapse when the collapse is shown', function (assert) { QUnit.test('should set aria-expanded="true" on all triggers targeting the collapse when the collapse is shown', function (assert) {
...@@ -393,7 +393,7 @@ $(function () { ...@@ -393,7 +393,7 @@ $(function () {
done() done()
}) })
$target.trigger('click') EventHandler.trigger($target[0], 'click')
}) })
QUnit.test('should set aria-expanded="false" on all triggers targeting the collapse when the collapse is hidden', function (assert) { QUnit.test('should set aria-expanded="false" on all triggers targeting the collapse when the collapse is hidden', function (assert) {
...@@ -411,7 +411,7 @@ $(function () { ...@@ -411,7 +411,7 @@ $(function () {
done() done()
}) })
$target.trigger('click') EventHandler.trigger($target[0], 'click')
}) })
QUnit.test('should change aria-expanded from active accordion trigger/control to "false" and set the trigger/control for the newly active one to "true"', function (assert) { QUnit.test('should change aria-expanded from active accordion trigger/control to "false" and set the trigger/control for the newly active one to "true"', function (assert) {
...@@ -445,7 +445,7 @@ $(function () { ...@@ -445,7 +445,7 @@ $(function () {
done() done()
}) })
$target3.trigger('click') EventHandler.trigger($target3[0], 'click')
}) })
QUnit.test('should not fire show event if show is prevented because other element is still transitioning', function (assert) { QUnit.test('should not fire show event if show is prevented because other element is still transitioning', function (assert) {
...@@ -470,13 +470,12 @@ $(function () { ...@@ -470,13 +470,12 @@ $(function () {
var $target2 = $('<a role="button" data-toggle="collapse" href="#body2"/>').appendTo($groups.eq(1)) var $target2 = $('<a role="button" data-toggle="collapse" href="#body2"/>').appendTo($groups.eq(1))
var $body2 = $('<div id="body2" class="collapse" data-parent="#accordion"/>').appendTo($groups.eq(1)) var $body2 = $('<div id="body2" class="collapse" data-parent="#accordion"/>').appendTo($groups.eq(1))
$target2.trigger('click') EventHandler.trigger($target2[0], 'click')
$body2 $body2.toggleClass('show collapsing')
.toggleClass('show collapsing') Data.getData($body2[0], 'bs.collapse')._isTransitioning = true
.data('bs.collapse')._isTransitioning = 1
$target1.trigger('click') EventHandler.trigger($target1[0], 'click')
setTimeout(function () { setTimeout(function () {
assert.ok(!showFired, 'show event did not fire') assert.ok(!showFired, 'show event did not fire')
...@@ -541,9 +540,9 @@ $(function () { ...@@ -541,9 +540,9 @@ $(function () {
assert.ok($collapseTwo.hasClass('show'), '#collapseTwo is shown') assert.ok($collapseTwo.hasClass('show'), '#collapseTwo is shown')
done() done()
}) })
$triggerTwo.trigger($.Event('click')) EventHandler.trigger($triggerTwo[0], 'click')
}) })
$trigger.trigger($.Event('click')) EventHandler.trigger($trigger[0], 'click')
}) })
QUnit.test('should allow accordion to contain nested elements', function (assert) { QUnit.test('should allow accordion to contain nested elements', function (assert) {
...@@ -687,40 +686,40 @@ $(function () { ...@@ -687,40 +686,40 @@ $(function () {
var $collapseTwo = $('#collapseTwo') var $collapseTwo = $('#collapseTwo')
var $nestedCollapseOne = $('#nestedCollapseOne') var $nestedCollapseOne = $('#nestedCollapseOne')
$collapseOne.one('shown.bs.collapse', function () { EventHandler.one($collapseOne[0], 'shown.bs.collapse', function () {
assert.ok($collapseOne.hasClass('show'), '#collapseOne is shown') assert.ok($collapseOne.hasClass('show'), '#collapseOne is shown')
assert.ok(!$collapseTwo.hasClass('show'), '#collapseTwo is not shown') assert.ok(!$collapseTwo.hasClass('show'), '#collapseTwo is not shown')
assert.ok(!$('#nestedCollapseOne').hasClass('show'), '#nestedCollapseOne is not shown') assert.ok(!$('#nestedCollapseOne').hasClass('show'), '#nestedCollapseOne is not shown')
$nestedCollapseOne.one('shown.bs.collapse', function () {
EventHandler.one($nestedCollapseOne[0], 'shown.bs.collapse', function () {
assert.ok($collapseOne.hasClass('show'), '#collapseOne is shown') assert.ok($collapseOne.hasClass('show'), '#collapseOne is shown')
assert.ok(!$collapseTwo.hasClass('show'), '#collapseTwo is not shown') assert.ok(!$collapseTwo.hasClass('show'), '#collapseTwo is not shown')
assert.ok($nestedCollapseOne.hasClass('show'), '#nestedCollapseOne is shown') assert.ok($nestedCollapseOne.hasClass('show'), '#nestedCollapseOne is shown')
$collapseTwo.one('shown.bs.collapse', function () { EventHandler.one($collapseTwo[0], 'shown.bs.collapse', function () {
assert.ok(!$collapseOne.hasClass('show'), '#collapseOne is not shown') assert.ok(!$collapseOne.hasClass('show'), '#collapseOne is not shown')
assert.ok($collapseTwo.hasClass('show'), '#collapseTwo is shown') assert.ok($collapseTwo.hasClass('show'), '#collapseTwo is shown')
assert.ok($nestedCollapseOne.hasClass('show'), '#nestedCollapseOne is shown') assert.ok($nestedCollapseOne.hasClass('show'), '#nestedCollapseOne is shown')
done() done()
}) })
$triggerTwo.trigger($.Event('click')) EventHandler.trigger($triggerTwo[0], 'click')
}) })
$nestedTrigger.trigger($.Event('click')) EventHandler.trigger($nestedTrigger[0], 'click')
}) })
$trigger.trigger($.Event('click')) EventHandler.trigger($trigger[0], 'click')
}) })
QUnit.test('should not prevent event for input', function (assert) { QUnit.test('should not prevent event for input', function (assert) {
assert.expect(3) assert.expect(3)
var done = assert.async() var done = assert.async()
var $target = $('<input type="checkbox" data-toggle="collapse" data-target="#collapsediv1" />').appendTo('#qunit-fixture') var $target = $('<input type="checkbox" data-toggle="collapse" data-target="#collapsediv1" />').appendTo('#qunit-fixture')
var $collapse = $('<div id="collapsediv1"/>').appendTo('#qunit-fixture')
$('<div id="collapsediv1"/>') EventHandler.one($collapse[0], 'shown.bs.collapse', function () {
.appendTo('#qunit-fixture') assert.ok($collapse.hasClass('show'))
.on('shown.bs.collapse', function () { assert.ok($target.attr('aria-expanded') === 'true')
assert.ok($(this).hasClass('show')) assert.ok($target.prop('checked'))
assert.ok($target.attr('aria-expanded') === 'true') done()
assert.ok($target.prop('checked')) })
done()
})
$target.trigger($.Event('click')) $target.trigger($.Event('click'))
}) })
...@@ -750,11 +749,11 @@ $(function () { ...@@ -750,11 +749,11 @@ $(function () {
assert.ok($trigger3.hasClass('collapsed'), 'trigger3 has collapsed class') assert.ok($trigger3.hasClass('collapsed'), 'trigger3 has collapsed class')
done() done()
}) })
$trigger1.trigger('click') EventHandler.trigger($trigger1[0], 'click')
}) })
$trigger2.trigger('click') EventHandler.trigger($trigger2[0], 'click')
}) })
$trigger3.trigger('click') EventHandler.trigger($trigger3[0], 'click')
}) })
QUnit.test('should set aria-expanded="true" to triggers targeting shown collaspe and aria-expanded="false" only when all the targeted collapses are shown', function (assert) { QUnit.test('should set aria-expanded="true" to triggers targeting shown collaspe and aria-expanded="false" only when all the targeted collapses are shown', function (assert) {
...@@ -782,11 +781,11 @@ $(function () { ...@@ -782,11 +781,11 @@ $(function () {
assert.strictEqual($trigger3.attr('aria-expanded'), 'false', 'aria-expanded on trigger3 is "false"') assert.strictEqual($trigger3.attr('aria-expanded'), 'false', 'aria-expanded on trigger3 is "false"')
done() done()
}) })
$trigger1.trigger('click') EventHandler.trigger($trigger1[0], 'click')
}) })
$trigger2.trigger('click') EventHandler.trigger($trigger2[0], 'click')
}) })
$trigger3.trigger('click') EventHandler.trigger($trigger3[0], 'click')
}) })
QUnit.test('should not prevent interactions inside the collapse element', function (assert) { QUnit.test('should not prevent interactions inside the collapse element', function (assert) {
...@@ -798,19 +797,17 @@ $(function () { ...@@ -798,19 +797,17 @@ $(function () {
'<div id="collapsediv1" class="collapse">' + '<div id="collapsediv1" class="collapse">' +
' <input type="checkbox" id="testCheckbox" />' + ' <input type="checkbox" id="testCheckbox" />' +
'</div>' '</div>'
var $collapse = $(htmlCollapse).appendTo('#qunit-fixture')
$(htmlCollapse)
.appendTo('#qunit-fixture') EventHandler.one($collapse[0], 'shown.bs.collapse', function () {
.on('shown.bs.collapse', function () { assert.ok($target.prop('checked'), '$trigger is checked')
assert.ok($target.prop('checked'), '$trigger is checked') var $testCheckbox = $('#testCheckbox')
var $testCheckbox = $('#testCheckbox') $testCheckbox.trigger($.Event('click'))
$testCheckbox.trigger($.Event('click')) setTimeout(function () {
setTimeout(function () { assert.ok($testCheckbox.prop('checked'), '$testCheckbox is checked too')
assert.ok($testCheckbox.prop('checked'), '$testCheckbox is checked too') done()
done() }, 5)
}, 5) })
})
$target.trigger($.Event('click')) $target.trigger($.Event('click'))
}) })
......
...@@ -73,6 +73,8 @@ ...@@ -73,6 +73,8 @@
<script src="../../../node_modules/jquery/dist/jquery.slim.min.js"></script> <script src="../../../node_modules/jquery/dist/jquery.slim.min.js"></script>
<script src="../../dist/dom/eventHandler.js"></script> <script src="../../dist/dom/eventHandler.js"></script>
<script src="../../dist/dom/selectorEngine.js"></script>
<script src="../../dist/dom/data.js"></script>
<script src="../../dist/util.js"></script> <script src="../../dist/util.js"></script>
<script src="../../dist/collapse.js"></script> <script src="../../dist/collapse.js"></script>
</body> </body>
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment