Skip to content

Commit

Permalink
Moved stuff into method
Browse files Browse the repository at this point in the history
  • Loading branch information
magicsunday committed Oct 2, 2021
1 parent 84029e3 commit cbe583c
Showing 1 changed file with 42 additions and 28 deletions.
70 changes: 42 additions & 28 deletions resources/views/modules/pedigree-chart/page.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ use MagicSunday\Webtrees\PedigreeChart\Configuration;
<div class="col-sm-12 wt-page-options-value text-center">
<div class="btn-toolbar justify-content-between">
<div class="btn-group btn-group-sm">
<button id="options" class="btn btn-primary"
<button id="options"
class="btn btn-primary"
type="button"
data-bs-toggle="collapse"
data-bs-target="#showMoreOptions"
Expand Down Expand Up @@ -172,10 +173,44 @@ function getUrl(baseUrl, generations)
return url.toString();
}

// Options container
const showMoreOptions = document.getElementById('showMoreOptions');
// Options toggle button
const optionsToggle = document.getElementById('options');
/**
* Set visibility of "Show more options" to previous state after page reload.
*
* @param {Storage} storage
*/
function toggleMoreOptions(storage)
{
// Options container
const showMoreOptions = document.getElementById('showMoreOptions');

// Options toggle button
const optionsToggle = document.getElementById('options');

showMoreOptions.addEventListener('shown.bs.collapse', () => {
storage.write("showMoreOptions", true);
});

showMoreOptions.addEventListener('hidden.bs.collapse', () => {
storage.write("showMoreOptions", false);
});

const toggleText = () => {
Array.from(optionsToggle.children).forEach((element, index) => {
element.classList.toggle('d-none');
});
};

optionsToggle.addEventListener('click', () => {
optionsToggle.blur(); // needed for colors theme (see https://github.com/fisharebest/webtrees/issues/4053)
toggleText();
});

if (storage.read("showMoreOptions")) {
showMoreOptions.classList.add('show');
toggleText();
}
}

// Set up storage object
const storage = new WebtreesPedigreeChart.Storage("webtrees-pedigree-chart");

Expand All @@ -184,29 +219,8 @@ storage.register("generations");
storage.register("showEmptyBoxes");
storage.register("layout");

showMoreOptions.addEventListener('shown.bs.collapse', () => {
storage.write("showMoreOptions", true);
});

showMoreOptions.addEventListener('hidden.bs.collapse', () => {
storage.write("showMoreOptions", false);
});

const toggleText = () => {
Array.from(optionsToggle.children).forEach((element, index) => {
element.classList.toggle('d-none');
});
};

optionsToggle.addEventListener('click', () => {
optionsToggle.blur(); // needed for colors theme (see https://github.com/fisharebest/webtrees/issues/4053)
toggleText();
});

if (storage.read("showMoreOptions")) {
showMoreOptions.classList.add('show');
toggleText();
}
// Handle option toggle button
toggleMoreOptions(storage);

// Set initial stored value for radio button group
let formElements = document.getElementById("webtrees-pedigree-chart-form").elements;
Expand Down

2 comments on commit cbe583c

@ddrury
Copy link
Contributor

@ddrury ddrury commented on cbe583c Oct 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just out of interest why have you done this?

@magicsunday
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because I'm thinking, its significantly increases the clarity and readability of the source code, if code parts that belong together are moved into a method.

Please sign in to comment.