|
1 | 1 | ;(function () { |
2 | 2 | 'use strict' |
3 | 3 |
|
| 4 | + var nav = document.querySelector('.nav-container-wrap') |
| 5 | + var navPanels = nav.querySelector('.panels') |
| 6 | + var pageUrl = document.head.querySelector('meta[name=page-url]').getAttribute('content') |
| 7 | + |
4 | 8 | var siteNavigationData = window.siteNavigationData.reduce(function (accum, entry) { |
5 | | - accum[entry.name] = entry |
6 | | - return accum |
| 9 | + return (accum[entry.name] = entry) && accum |
7 | 10 | }, {}) |
8 | 11 | window.siteNavigationGroups.forEach(function (group) { |
| 12 | + var groupName = document.createElement('div') |
| 13 | + groupName.className = 'group-name' |
| 14 | + groupName.appendChild(document.createTextNode(group.title)) |
| 15 | + navPanels.appendChild(groupName) |
9 | 16 | group.components.forEach(function (componentName) { |
10 | 17 | var componentNavData = siteNavigationData[componentName] |
| 18 | + var navTrees = document.createElement('div') |
| 19 | + navTrees.className = 'nav-trees' |
| 20 | + var versionBox = document.createElement('div') |
| 21 | + versionBox.className = 'version-box' |
| 22 | + var selectVersion = document.createElement('select') |
| 23 | + selectVersion.className = 'select-version' |
| 24 | + componentNavData.versions.forEach(function(version) { |
| 25 | + var versionOption = document.createElement('option') |
| 26 | + versionOption.value = version.version |
| 27 | + versionOption.appendChild(document.createTextNode(version.displayVersion || version.version)) |
| 28 | + selectVersion.appendChild(versionOption) |
| 29 | + }) |
| 30 | + versionBox.appendChild(selectVersion) |
| 31 | + navTrees.appendChild(versionBox) |
| 32 | + componentNavData.versions.forEach(function(version) { |
| 33 | + var navTree = document.createElement('div') |
| 34 | + var navItem = document.createElement('div') |
| 35 | + navItem.className = 'nav-item is-link nav-top' |
| 36 | + var navTitle = document.createElement('a') |
| 37 | + // FIXME: this should be component version url |
| 38 | + navTitle.href = relativize(pageUrl, '/' + componentNavData.name + '/current/index.html') |
| 39 | + navTitle.appendChild(document.createTextNode(componentNavData.title)) |
| 40 | + navItem.appendChild(navTitle) |
| 41 | + navTree.appendChild(navItem) |
| 42 | + navTrees.appendChild(navTree) |
| 43 | + }) |
| 44 | + navPanels.appendChild(navTrees) |
11 | 45 | }) |
12 | 46 | }) |
13 | 47 |
|
14 | | - var nav = document.querySelector('nav.nav') |
| 48 | + return |
15 | 49 | var menuExpandToggle = document.querySelector('.menu-expand-toggle') |
16 | 50 | var versionToggle = document.querySelector('.clickable') |
17 | 51 | var versionDropdownList = document.querySelector('.frame-dropdown') |
|
157 | 191 | } // if condition end |
158 | 192 |
|
159 | 193 | // clearTimeout(scrollCurrentPageMenu, 20000) |
| 194 | + |
| 195 | + function relativize (from, to) { |
| 196 | + if (!(from && to.charAt() === '/')) return to |
| 197 | + var hash = '' |
| 198 | + var hashIdx = to.indexOf('#') |
| 199 | + if (~hashIdx) { |
| 200 | + hash = to.substr(hashIdx) |
| 201 | + to = to.substr(0, hashIdx) |
| 202 | + } |
| 203 | + if (from === to) { |
| 204 | + return hash || (to.charAt(to.length - 1) === '/' ? './' : to.substr(to.lastIndexOf('/') + 1)) |
| 205 | + } else { |
| 206 | + return ( |
| 207 | + (computeRelativePath(from.slice(0, from.lastIndexOf('/')), to) || '.') + |
| 208 | + (to.charAt(to.length - 1) === '/' ? '/' + hash : hash) |
| 209 | + ) |
| 210 | + } |
| 211 | + } |
| 212 | + |
| 213 | + function computeRelativePath (from, to) { |
| 214 | + var fromParts = trimArray(from.split('/')) |
| 215 | + var toParts = trimArray(to.split('/')) |
| 216 | + for (var i = 0, l = Math.min(fromParts.length, toParts.length), sharedPathLength = l; i < l; i++) { |
| 217 | + if (fromParts[i] !== toParts[i]) { |
| 218 | + sharedPathLength = i |
| 219 | + break |
| 220 | + } |
| 221 | + } |
| 222 | + var outputParts = [] |
| 223 | + for (var remain = fromParts.length - sharedPathLength; remain > 0; remain--) outputParts.push('..') |
| 224 | + return outputParts.concat(toParts.slice(sharedPathLength)).join('/') |
| 225 | + } |
| 226 | + |
| 227 | + function trimArray (arr) { |
| 228 | + var start = 0 |
| 229 | + var length = arr.length |
| 230 | + for (; start < length; start++) { |
| 231 | + if (arr[start]) break |
| 232 | + } |
| 233 | + if (start === length) return [] |
| 234 | + for (var end = length; end > 0; end--) { |
| 235 | + if (arr[end - 1]) break |
| 236 | + } |
| 237 | + return arr.slice(start, end) |
| 238 | + } |
160 | 239 | })() |
0 commit comments