Skip to content

Commit

Permalink
refactor: split DOM operations from logics in Accordion
Browse files Browse the repository at this point in the history
Split DOM operations from logics in Accordion "up" and "down" methods
  • Loading branch information
ncoden committed Aug 30, 2018
1 parent 314b4ee commit abbbdbf
Showing 1 changed file with 75 additions and 32 deletions.
107 changes: 75 additions & 32 deletions js/foundation.accordion.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,67 +207,110 @@ class Accordion extends Plugin {
console.info('Cannot call down on an accordion that is disabled.');
return;
}
$target
.attr('aria-hidden', false)
.parent('[data-tab-content]')
.addBack()
.parent().addClass('is-active');

if (!this.options.multiExpand && !firstTime) {
var $currentActive = this.$element.children('.is-active').children('[data-tab-content]');
if ($currentActive.length) {
this.up($currentActive.not($target));
var $activeContents = this.$element.children('.is-active').children('[data-tab-content]');
if ($activeContents.length) {
this._closeTab($activeContents.not($target));
}
}

this._openTab($target);
}

/**
* Closes the tab defined by `$target`.
* It may be ignored if the Accordion options don't allow it.
*
* @param {jQuery} $target - Accordion tab to close (`.accordion-content`).
* @fires Accordion#up
* @function
*/
up($target) {

if (this.$element.is('[disabled]')) {
console.info('Cannot call up on an accordion that is disabled.');
return;
}

// Don't close the item if it is already closed
const $targetItem = $target.parent();
if (!$targetItem.hasClass('is-active')) return;

// Don't close the item if there is no other active item (unless with `allowAllClosed`)
const $othersItems = $targetItem.siblings();
if (!this.options.allowAllClosed && !$othersItems.hasClass('is-active')) return;

this._closeTab($target);
}

/**
* Opens the tab defined by `$target`.
* @param {jQuery} $target - Accordion tab to open (`.accordion-content`).
* @fires Accordion#down
* @function
* @private
*/
_openTab($target) {
const $targetItem = $target.parent();
const targetContentId = $target.attr('aria-labelledby');

$target.attr('aria-hidden', false);
$targetItem.addClass('is-active');

$(`#${targetContentId}`).attr({
'aria-expanded': true,
'aria-selected': true
});

$target.slideDown(this.options.slideSpeed, () => {
/**
* Fires when the tab is done opening.
* @event Accordion#down
*/
this.$element.trigger('down.zf.accordion', [$target]);
});

$(`#${$target.attr('aria-labelledby')}`).attr({
'aria-expanded': true,
'aria-selected': true
});
}

/**
* Closes the tab defined by `$target`.
* @param {jQuery} $target - Accordion tab to close (`.accordion-content`).
* @fires Accordion#up
* @function
* @private
*/
up($target) {
if ($target.closest('[data-accordion]').is('[disabled]')) {
console.info('Cannot call up on an accordion that is disabled.');
return;
}
_closeTab($target) {
const $targetItem = $target.parent();
const targetContentId = $target.attr('aria-labelledby');

var $aunts = $target.parent().siblings(),
_this = this;
$target.attr('aria-hidden', true)
$targetItem.removeClass('is-active');

if((!this.options.allowAllClosed && !$aunts.hasClass('is-active')) || !$target.parent().hasClass('is-active')) {
return;
}
$(`#${targetContentId}`).attr({
'aria-expanded': false,
'aria-selected': false
});

$target.slideUp(_this.options.slideSpeed, function () {
$target.slideUp(this.options.slideSpeed, () => {
/**
* Fires when the tab is done collapsing up.
* @event Accordion#up
*/
_this.$element.trigger('up.zf.accordion', [$target]);
this.$element.trigger('up.zf.accordion', [$target]);
});
}

$target.attr('aria-hidden', true)
.parent().removeClass('is-active');

$(`#${$target.attr('aria-labelledby')}`).attr({
'aria-expanded': false,
'aria-selected': false
});
/**
* Closes all active tabs
* @fires Accordion#up
* @function
* @private
*/
_closeAllTabs() {
var $activeTabs = this.$element.children('.is-active').children('[data-tab-content]');
if ($activeTabs.length) {
this._closeTab($activeTabs);
}
}

/**
Expand Down

0 comments on commit abbbdbf

Please sign in to comment.