Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No easy way to add/remove folds after construction #16

Open
lBodia opened this issue Sep 9, 2019 · 3 comments
Open

No easy way to add/remove folds after construction #16

lBodia opened this issue Sep 9, 2019 · 3 comments

Comments

@lBodia
Copy link

lBodia commented Sep 9, 2019

Hi! Is it possible to fully destroy and init again accordion with the same container? (In case if items there have been changed)

When I call just update, it doesn't init recently added folds, and when I init a new accordion, I see the all previous are not deleted.

@Alhadis
Copy link
Owner

Alhadis commented Sep 12, 2019

When I call just update, it doesn't init recently added folds

When you say "init recently added folds", what are you referring to exactly?

@lBodia
Copy link
Author

lBodia commented Sep 12, 2019

@Alhadis, sorry, I explained it a bit unclear.
I didn't create any folds, I just dynamically change accordion items, by adding and deleting HTML items from it. And to make those items work, I had to init this accordion again.
I'm just wondering, is it possible to destroy the previous accordion object, because I saw that it was still kept in accordions array.

@Alhadis
Copy link
Owner

Alhadis commented Sep 12, 2019

Short answer: no it's not possible because it shouldn't be needed. Accordions can be disabled and reenabled programmatically after construction, which is (almost) the same as destroying and recreating it.

What you're experiencing is a limitation caused by a pretty stupid oversight on my behalf... one I'm surprised I never noticed earlier — there's no way of dynamically adding or removing accordion folds.

I'll need to fix this. In the meantime, you can use this ugly and illogical workaround:

Click to show
// Usage:
let el = document.createElement("li");
el.innerHTML = "<h3>Foo</h3><div>Bar</div>";
addFold(el, document.querySelector(".accordion"));

function addFold(element, accordion){
	if(!(accordion instanceof Accordion))
		accordion = Accordion.getAccordion(accordion);
	const prev = accordion.folds[accordion.folds.length - 1];
	const fold = new prev.constructor(accordion, element);
	accordion.folds.push(fold);
	prev.nextFold = fold;
	fold.previousFold = prev;
	if(element.parentElement !== accordion.el)
		accordion.el.appendChild(element);
	accordion.update();
	return fold;
}

function removeFold(fold){
	if(fold instanceof HTMLElement)
		fold = Accordion.getFold(fold);
	const {parentElement} = fold.el;
	parentElement && parentElement.removeChild(fold.el);

	const {folds} = fold.accordion;
	const index = folds.indexOf(fold);
	if(~index){
		const prev = folds[index - 1];
		const next = folds[index + 1];
		folds.splice(index, 1);
		prev.nextFold = next;
		if(next)
			next.previousFold = prev;
	}
	fold.accordion.update();
}

As for destroying an Accordion instance... well, as mentioned before, it shouldn't be necessary, but now that I think about it, it would benefit applications needing to perform a clean teardown of their state. That's unrelated to this issue, however.

@Alhadis Alhadis changed the title Reinit accordion No easy way to add/remove folds after construction Sep 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants