|
| 1 | +/** |
| 2 | + * Sections Plugin |
| 3 | + * |
| 4 | + * Copyright 2019 Marc Schreiber (@schrieveslaach) |
| 5 | + * Released under the MIT license. |
| 6 | + */ |
| 7 | +/* global document */ |
| 8 | + |
| 9 | +(function (document) { |
| 10 | + "use strict"; |
| 11 | + var root, api, gc; |
| 12 | + |
| 13 | + var indexedSteps; |
| 14 | + |
| 15 | + var agenda = document.querySelector("div.agenda ul"); |
| 16 | + var currentSection = document.querySelector("div.impress-section-overview .impress-current-section"); |
| 17 | + var sectionNumbers = document.querySelector("div.impress-section-overview .impress-section-numbers"); |
| 18 | + |
| 19 | + function indexSteps() { |
| 20 | + var slides = Array.prototype.slice.call(root.querySelectorAll(".step")) |
| 21 | + .map(function (step) { |
| 22 | + return { |
| 23 | + id: step.id, |
| 24 | + section: step.dataset.section, |
| 25 | + subSection: step.dataset.subSection |
| 26 | + }; |
| 27 | + }) |
| 28 | + .reduce(function (accumulatedSlides, slide, currentIndex, slides) { |
| 29 | + if (currentIndex === 0) { |
| 30 | + return [slide]; |
| 31 | + } |
| 32 | + |
| 33 | + let previousSlide = slides[currentIndex - 1]; |
| 34 | + if (slide.section == null) { |
| 35 | + var currentSection = previousSlide.section; |
| 36 | + slide.section = currentSection; |
| 37 | + } |
| 38 | + |
| 39 | + if (slide.subSection == null && previousSlide.section === slide.section) { |
| 40 | + var currentSubSection = previousSlide.subSection; |
| 41 | + slide.subSection = currentSubSection; |
| 42 | + } |
| 43 | + |
| 44 | + return accumulatedSlides.concat([slide]); |
| 45 | + }, []); |
| 46 | + |
| 47 | + return { |
| 48 | + sectionIndices: slides.reduce(function (sectionIndices, slide) { |
| 49 | + var section = sectionIndices[slide.section]; |
| 50 | + |
| 51 | + if (section == null) { |
| 52 | + section = { |
| 53 | + index: Object.keys(sectionIndices).length + 1, |
| 54 | + steps: [slide.id], |
| 55 | + subSections: [] |
| 56 | + }; |
| 57 | + sectionIndices[slide.section] = section; |
| 58 | + } else { |
| 59 | + section.steps = section.steps.concat([slide.id]); |
| 60 | + } |
| 61 | + |
| 62 | + if (slide.subSection != null && section.subSections.indexOf(slide.subSection) < 0) { |
| 63 | + section.subSections = section.subSections.concat([slide.subSection]); |
| 64 | + } |
| 65 | + |
| 66 | + return sectionIndices; |
| 67 | + }, {}), |
| 68 | + |
| 69 | + slideSectionMapping: slides.reduce(function (slideSectionMapping, slide) { |
| 70 | + slideSectionMapping[slide.id] = { |
| 71 | + section: slide.section, |
| 72 | + subSection: slide.subSection |
| 73 | + }; |
| 74 | + return slideSectionMapping; |
| 75 | + }, {}) |
| 76 | + }; |
| 77 | + } |
| 78 | + |
| 79 | + document.addEventListener("impress:init", function (event) { |
| 80 | + root = event.target; |
| 81 | + api = event.detail.api; |
| 82 | + gc = api.lib.gc; |
| 83 | + |
| 84 | + indexedSteps = indexSteps(); |
| 85 | + |
| 86 | + if (agenda != null) { |
| 87 | + Object.keys(indexedSteps.sectionIndices).forEach(function (section) { |
| 88 | + var li = document.createElement("li"); |
| 89 | + agenda.appendChild(li); |
| 90 | + li.innerText = section; |
| 91 | + |
| 92 | + var subSections = indexedSteps.sectionIndices[section].subSections; |
| 93 | + if (subSections.length > 0) { |
| 94 | + var ul = document.createElement("ul"); |
| 95 | + li.appendChild(ul); |
| 96 | + |
| 97 | + subSections.forEach(function (subSection) { |
| 98 | + var li = document.createElement("li"); |
| 99 | + ul.appendChild(li); |
| 100 | + li.innerText = subSection; |
| 101 | + }); |
| 102 | + } |
| 103 | + }); |
| 104 | + } |
| 105 | + }); |
| 106 | + |
| 107 | + |
| 108 | + document.addEventListener("impress:stepleave", function (event) { |
| 109 | + updateSectionOverview(event.detail.next.id); |
| 110 | + }); |
| 111 | + |
| 112 | + document.addEventListener("impress:steprefresh", function (event) { |
| 113 | + updateSectionOverview(event.target.id); |
| 114 | + }); |
| 115 | + |
| 116 | + function updateSectionOverview(slideId) { |
| 117 | + if (indexedSteps == null) { |
| 118 | + return; |
| 119 | + } |
| 120 | + |
| 121 | + var section = { |
| 122 | + section: indexedSteps.slideSectionMapping[slideId].section, |
| 123 | + subSection: indexedSteps.slideSectionMapping[slideId].subSection, |
| 124 | + indices: indexedSteps.sectionIndices[indexedSteps.slideSectionMapping[slideId].section] |
| 125 | + }; |
| 126 | + |
| 127 | + if (currentSection != null) { |
| 128 | + if (section.subSection == null) { |
| 129 | + currentSection.innerText = section.section; |
| 130 | + } else { |
| 131 | + currentSection.innerText = section.section + " \u2022 " + section.subSection; |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + if (sectionNumbers != null) { |
| 136 | + sectionNumbers.innerText = section.indices.index + "/" + Object.keys(indexedSteps.sectionIndices).length; |
| 137 | + } |
| 138 | + |
| 139 | + Object.keys(indexedSteps.slideSectionMapping).forEach(function (stepId) { |
| 140 | + var step = document.getElementById(stepId); |
| 141 | + |
| 142 | + var subSection = indexedSteps.slideSectionMapping[stepId].subSection; |
| 143 | + if (section.indices.steps.indexOf(stepId) >= 0 && section.subSection === subSection) { |
| 144 | + step.classList.add("active-section"); |
| 145 | + step.classList.remove("hidden-section"); |
| 146 | + } else { |
| 147 | + step.classList.remove("active-section"); |
| 148 | + step.classList.add("hidden-section"); |
| 149 | + } |
| 150 | + }); |
| 151 | + } |
| 152 | + |
| 153 | +})(document); |
0 commit comments