From ab39defe74914109df164c823af927de68320d55 Mon Sep 17 00:00:00 2001 From: Pierre Vanduynslager Date: Tue, 25 Apr 2017 03:32:14 -0400 Subject: [PATCH] Detach accordion from card without requiring 'data-children' --- js/src/collapse.js | 16 +++------ js/tests/unit/collapse.js | 76 ++++++++++++++++++++++++++++++++++----- 2 files changed, 71 insertions(+), 21 deletions(-) diff --git a/js/src/collapse.js b/js/src/collapse.js index dec272297b..bf1c738f56 100644 --- a/js/src/collapse.js +++ b/js/src/collapse.js @@ -56,9 +56,8 @@ const Collapse = (($) => { } const Selector = { - ACTIVES : '.card > .show, .card > .collapsing', - DATA_TOGGLE : '[data-toggle="collapse"]', - DATA_CHILDREN : 'data-children' + ACTIVES : '.show, .collapsing', + DATA_TOGGLE : '[data-toggle="collapse"]' } @@ -78,20 +77,13 @@ const Collapse = (($) => { `[data-toggle="collapse"][href="#${element.id}"],` + `[data-toggle="collapse"][data-target="#${element.id}"]` )) + this._parent = this._config.parent ? this._getParent() : null if (!this._config.parent) { this._addAriaAndCollapsedClass(this._element, this._triggerArray) } - this._selectorActives = Selector.ACTIVES - if (this._parent) { - const childrenSelector = this._parent.hasAttribute(Selector.DATA_CHILDREN) ? this._parent.getAttribute(Selector.DATA_CHILDREN) : null - if (childrenSelector !== null) { - this._selectorActives = `${childrenSelector} > .show, ${childrenSelector} > .collapsing` - } - } - if (this._config.toggle) { this.toggle() } @@ -129,7 +121,7 @@ const Collapse = (($) => { let activesData if (this._parent) { - actives = $.makeArray($(this._parent).find(this._selectorActives)) + actives = $.makeArray($(this._parent).children().children(Selector.ACTIVES)) if (!actives.length) { actives = null } diff --git a/js/tests/unit/collapse.js b/js/tests/unit/collapse.js index 35fcf2108e..2b9d0e58fc 100644 --- a/js/tests/unit/collapse.js +++ b/js/tests/unit/collapse.js @@ -491,27 +491,85 @@ $(function () { }) QUnit.test('should allow accordion to use children other than card', function (assert) { - assert.expect(2) + assert.expect(4) var done = assert.async() - var accordionHTML = '
' + var accordionHTML = '
' + '
' + '' + '
' + '
' + '
' - + '' + + '' + '
' + '
' + '
' $(accordionHTML).appendTo('#qunit-fixture') - var $target = $('#linkTrigger') - $('#collapseOne').on('shown.bs.collapse', function () { - assert.ok($(this).hasClass('show')) - assert.ok(!$('#collapseTwo').hasClass('show')) - done() + var $trigger = $('#linkTrigger') + var $triggerTwo = $('#linkTriggerTwo') + var $collapseOne = $('#collapseOne') + var $collapseTwo = $('#collapseTwo') + $collapseOne.on('shown.bs.collapse', function () { + assert.ok($collapseOne.hasClass('show'), '#collapseOne is shown') + assert.ok(!$collapseTwo.hasClass('show'), '#collapseTwo is not shown') + $collapseTwo.on('shown.bs.collapse', function () { + assert.ok(!$collapseOne.hasClass('show'), '#collapseOne is not shown') + assert.ok($collapseTwo.hasClass('show'), '#collapseTwo is shown') + done() + }) + $triggerTwo.trigger($.Event('click')) }) - $target.trigger($.Event('click')) + $trigger.trigger($.Event('click')) + }) + + QUnit.test('should collapse accordion children but not nested accordion children', function (assert) { + assert.expect(9) + var done = assert.async() + $('
' + + '
' + + '' + + '
' + + '
' + + '
' + + '' + + '
' + + '
' + + '
' + + '
' + + '
' + + '
' + + '
' + + '' + + '
' + + '
' + + '
').appendTo('#qunit-fixture') + var $trigger = $('#linkTrigger') + var $triggerTwo = $('#linkTriggerTwo') + var $nestedTrigger = $('#nestedLinkTrigger') + var $collapseOne = $('#collapseOne') + var $collapseTwo = $('#collapseTwo') + var $nestedCollapseOne = $('#nestedCollapseOne') + + + $collapseOne.one('shown.bs.collapse', function () { + assert.ok($collapseOne.hasClass('show'), '#collapseOne is shown') + assert.ok(!$collapseTwo.hasClass('show'), '#collapseTwo is not shown') + assert.ok(!$('#nestedCollapseOne').hasClass('show'), '#nestedCollapseOne is not shown') + $nestedCollapseOne.one('shown.bs.collapse', function () { + assert.ok($collapseOne.hasClass('show'), '#collapseOne is shown') + assert.ok(!$collapseTwo.hasClass('show'), '#collapseTwo is not shown') + assert.ok($nestedCollapseOne.hasClass('show'), '#nestedCollapseOne is shown') + $collapseTwo.one('shown.bs.collapse', function () { + assert.ok(!$collapseOne.hasClass('show'), '#collapseOne is not shown') + assert.ok($collapseTwo.hasClass('show'), '#collapseTwo is shown') + assert.ok($nestedCollapseOne.hasClass('show'), '#nestedCollapseOne is shown') + done() + }) + $triggerTwo.trigger($.Event('click')) + }) + $nestedTrigger.trigger($.Event('click')) + }) + $trigger.trigger($.Event('click')) }) QUnit.test('should not prevent event for input', function (assert) {