open-nomad/ui/app/components/list-accordion.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

45 lines
1.2 KiB
JavaScript
Raw Normal View History

/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
import Component from '@ember/component';
import { computed, get } from '@ember/object';
import { computed as overridable } from 'ember-overridable-computed';
import { classNames } from '@ember-decorators/component';
import classic from 'ember-classic-decorator';
@classic
@classNames('accordion')
export default class ListAccordion extends Component {
key = 'id';
@overridable(() => []) source;
onToggle /* item, isOpen */() {}
startExpanded = false;
@computed('key', 'source.[]', 'startExpanded', 'stateCache')
get decoratedSource() {
2019-03-26 07:46:44 +00:00
const stateCache = this.stateCache;
const key = this.key;
const deepKey = `item.${key}`;
2019-03-26 07:46:44 +00:00
const startExpanded = this.startExpanded;
2021-12-28 14:45:20 +00:00
const decoratedSource = this.source.map((item) => {
const cacheItem = stateCache.findBy(deepKey, get(item, key));
return {
item,
isOpen: cacheItem ? !!cacheItem.isOpen : startExpanded,
};
});
// eslint-disable-next-line ember/no-side-effects
this.set('stateCache', decoratedSource);
return decoratedSource;
}
// When source updates come in, the state cache is used to preserve
// open/close state.
@overridable(() => []) stateCache;
}