|
| 1 | +(function () { |
| 2 | + "use strict"; |
| 3 | + |
| 4 | + function getContainerElements() { |
| 5 | + return Array.prototype.concat.apply([], document.getElementsByClassName('component-selector')); |
| 6 | + } |
| 7 | + |
| 8 | + function getSelectElements() { |
| 9 | + return Array.prototype.concat.apply([], document.getElementsByClassName('component-selector__control')); |
| 10 | + } |
| 11 | + |
| 12 | + function prepareComponentList(components) { |
| 13 | + var componentList = { |
| 14 | + learn: { |
| 15 | + label: "Learn ZF", |
| 16 | + choices: [] |
| 17 | + }, |
| 18 | + middleware: { |
| 19 | + label: "Expressive and PSR-15 Middleware", |
| 20 | + choices: [] |
| 21 | + }, |
| 22 | + mvc: { |
| 23 | + label: "MVC Framework", |
| 24 | + choices: [] |
| 25 | + }, |
| 26 | + components: { |
| 27 | + label: "Components", |
| 28 | + choices: [] |
| 29 | + }, |
| 30 | + projects: { |
| 31 | + label: "Tooling and Composer Plugins", |
| 32 | + choices: [] |
| 33 | + }, |
| 34 | + }; |
| 35 | + var uncategorized = []; |
| 36 | + |
| 37 | + // eslint-disable-next-line no-use-before-define |
| 38 | + const matchActive = new RegExp('\/' + siteName + '(\/|$)'); |
| 39 | + |
| 40 | + components.forEach(function (component) { |
| 41 | + const selected = matchActive.test(component.url); |
| 42 | + const packageName = component.package.split('/'); |
| 43 | + const label = component.name + '<br/><span class="choices__packagename">' + packageName[1] + '</span>'; |
| 44 | + const choice = { |
| 45 | + value: component.url, |
| 46 | + label: label, |
| 47 | + selected: selected, |
| 48 | + customProperties: { |
| 49 | + description: component.description |
| 50 | + } |
| 51 | + }; |
| 52 | + |
| 53 | + componentList.hasOwnProperty(component.group) |
| 54 | + ? componentList[component.group].choices.push(choice) |
| 55 | + : uncategorized.push(choice); |
| 56 | + }); |
| 57 | + |
| 58 | + // Initialize the Choices selector using the component selector as its element |
| 59 | + getSelectElements().forEach(function(element) { |
| 60 | + const choices = new Choices(element, { |
| 61 | + itemSelectText: '', |
| 62 | + renderChoiceLimit: -1, |
| 63 | + searchChoices: true, |
| 64 | + searchEnabled: true, |
| 65 | + searchFields: ['label', 'customProperties.description'], |
| 66 | + searchPlaceholderValue: 'Jump to package…', |
| 67 | + searchResultLimit: 10, |
| 68 | + shouldSort: false |
| 69 | + }); |
| 70 | + |
| 71 | + choices.setChoices( |
| 72 | + Array.prototype.concat.apply(Object.values(componentList), uncategorized), |
| 73 | + 'value', |
| 74 | + 'label', |
| 75 | + true |
| 76 | + ); |
| 77 | + |
| 78 | + // On selection of a choice, redirect to its URL |
| 79 | + element.addEventListener('choice', function (event) { |
| 80 | + window.location.href = event.detail.choice.value; |
| 81 | + }, false); |
| 82 | + |
| 83 | + // Ensure the parent container expands to fit the list... |
| 84 | + element.addEventListener('showDropdown', function (event) { |
| 85 | + getContainerElements().forEach(function (container) { |
| 86 | + const choicesList = container.querySelector('.choices__list--dropdown'); |
| 87 | + container.parentElement.style.minHeight = container.clientHeight + choicesList.clientHeight + "px"; |
| 88 | + }); |
| 89 | + }, false); |
| 90 | + |
| 91 | + // ... and then shrinks back to size again. |
| 92 | + element.addEventListener('hideDropdown', function (event) { |
| 93 | + getContainerElements().forEach(function (container) { |
| 94 | + container.parentElement.style.minHeight = 'unset'; |
| 95 | + }); |
| 96 | + }, false); |
| 97 | + |
| 98 | + // Load all starting and ending points for the sticky scene. |
| 99 | + // After all of them have loaded, toggle the sticky styles accordingly. |
| 100 | + element.addEventListener('showDropdown', function (event) { |
| 101 | + const groups = choices.choiceList.element.querySelectorAll('.choices__group'); |
| 102 | + |
| 103 | + groups.forEach(function (group, index) { |
| 104 | + var stickyStart = group.offsetTop; |
| 105 | + var stickyEnd = null; |
| 106 | + |
| 107 | + if (groups.hasOwnProperty(index + 1)) { |
| 108 | + var nextGroup = groups[index + 1]; |
| 109 | + stickyEnd = nextGroup.offsetTop; |
| 110 | + } |
| 111 | + |
| 112 | + group.setAttribute('data-sticky-start', stickyStart); |
| 113 | + group.setAttribute('data-sticky-end', stickyEnd ? stickyEnd : ''); |
| 114 | + }); |
| 115 | + |
| 116 | + groups.forEach(function (group) { |
| 117 | + toggleStickyGroupStyles(group, choices.choiceList.element.scrollTop); |
| 118 | + }); |
| 119 | + }, false); |
| 120 | + |
| 121 | + // Remove all sticky styles when the drop-down hides |
| 122 | + element.addEventListener('hideDropdown', function (event) { |
| 123 | + const groups = choices.choiceList.element.querySelectorAll('.choices__group'); |
| 124 | + |
| 125 | + groups.forEach(function (group) { |
| 126 | + unsetStickyGroupStyles(group); |
| 127 | + }); |
| 128 | + }, false); |
| 129 | + |
| 130 | + // Track the scroll position and apply sticky styles to the opt-group elements. |
| 131 | + // The "touchmove" event is used for touch devices, because on some browsers |
| 132 | + // the scroll event is triggered only once after the scroll animation has completed. |
| 133 | + ['scroll', 'touchmove'].forEach(function (eventName) { |
| 134 | + choices.choiceList.element.addEventListener(eventName, function () { |
| 135 | + const groups = this.querySelectorAll('.choices__group'); |
| 136 | + const scrollTop = this.scrollTop; |
| 137 | + |
| 138 | + groups.forEach(function (group) { |
| 139 | + toggleStickyGroupStyles(group, scrollTop); |
| 140 | + }); |
| 141 | + }); |
| 142 | + }); |
| 143 | + }); |
| 144 | + } |
| 145 | + |
| 146 | + function parseComponentList(event) { |
| 147 | + var request = event.target; |
| 148 | + if (request.readyState === request.DONE && request.status === 200) { |
| 149 | + prepareComponentList(JSON.parse(request.responseText)); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + function toggleStickyGroupStyles(group, scrollTop) { |
| 154 | + var stickyStart = parseInt(group.getAttribute('data-sticky-start')); |
| 155 | + var stickyEnd = group.getAttribute('data-sticky-end') |
| 156 | + ? parseInt(group.getAttribute('data-sticky-end')) |
| 157 | + : null; |
| 158 | + |
| 159 | + if (scrollTop >= stickyStart && (scrollTop <= stickyEnd || !stickyEnd)) { |
| 160 | + setStickyGroupStyles(group); |
| 161 | + return; |
| 162 | + } |
| 163 | + |
| 164 | + unsetStickyGroupStyles(group); |
| 165 | + } |
| 166 | + |
| 167 | + function setStickyGroupStyles(group) { |
| 168 | + group.classList.add('is-sticky'); |
| 169 | + |
| 170 | + if (group.nextElementSibling) { |
| 171 | + // Group element's position becomes fixed, |
| 172 | + // causing the height of the scrollable element to decrease. |
| 173 | + // The "removed" height is added as a margin top |
| 174 | + // on the next element in order to compensate for the losses. |
| 175 | + // This gives a smooth, non-jumping feeling to the end-user. |
| 176 | + group.nextElementSibling.style.marginTop = group.clientHeight + 'px'; |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + function unsetStickyGroupStyles(group) { |
| 181 | + group.classList.remove('is-sticky'); |
| 182 | + |
| 183 | + if (group.nextElementSibling) { |
| 184 | + group.nextElementSibling.style.marginTop = null; |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + // When the window has finished loading the DOM, fetch the components and |
| 189 | + // populate the dropdown. |
| 190 | + window.addEventListener('load', function () { |
| 191 | + const request = new XMLHttpRequest(); |
| 192 | + request.onreadystatechange = parseComponentList; |
| 193 | + request.open('GET', '//docs.zendframework.com/js/zf-component-list.json'); |
| 194 | + request.send(); |
| 195 | + }); |
| 196 | +})(); |
0 commit comments