Skip to content

Instantly share code, notes, and snippets.

@tokland
Created June 12, 2017 10:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tokland/3b4df5f29b437403809b845271269155 to your computer and use it in GitHub Desktop.
Save tokland/3b4df5f29b437403809b845271269155 to your computer and use it in GitHub Desktop.
Group subsections tabs for DHIS2 dataentry
/*
Group subsections tabs (with names "section@subsection1", "section@subsection2") into a single tab "section"
Save as a contentscript in DHIS2 app customJS/CSS (https://www.dhis2.org/download/appstore/customjscss.zip)
*/
var init = function(contentEl) {
var MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
var displayingTabs = contentEl.children().size() > 0;
var observer = new MutationObserver(function(mutations, observer) {
if (!displayingTabs && contentEl.children().size() > 0) {
groupSubsections({separator: "@"});
displayingTabs = true;
}
});
observer.observe(contentEl.get(0), {subtree: true, attributes: true});
};
var groupSubsections = function(options) {
var separator = options.separator || "@";
var groupsOfSections = _
.chain($("#tabs li").toArray())
.groupBy(el => $(el).text().split(separator)[0])
.select((elements, name) => elements.length > 1)
.value();
groupsOfSections.forEach(sections => {
var firstSection = $(sections[0]);
var firstSectionContents = $("#" + firstSection.attr("aria-controls"));
var title = $(firstSection).text().split(separator).slice(1).join(" ");
firstSectionContents
.wrapInner($("<div>"));
firstSectionContents.
.prepend($("<h2>").text(title));
sections.slice(1).forEach(section => {
var sectionContents = $("#" + $(section).attr("aria-controls"))
.wrapInner($("<div>"));
var title = $(section).text().split(separator).slice(1).join(" ");
firstSectionContents
.append($("<h2>").text(title))
.append(sectionContents.children());
section.remove();
});
var sectionName = firstSection.text().split(separator)[0];
firstSection.children("a").text(sectionName);
firstSectionContents.accordion();
});
};
$(() => init($("#contentDiv")));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment