|
| 1 | +/* eslint-disable no-unused-vars */ |
| 2 | +import { isMobile } from '../util/env'; |
| 3 | +import * as dom from '../util/dom'; |
| 4 | + |
| 5 | +const title = dom.$.title; |
| 6 | +/** |
| 7 | + * Toggle button |
| 8 | + * @param {Element} el Button to be toggled |
| 9 | + * @void |
| 10 | + */ |
| 11 | +export function btn(el) { |
| 12 | + const toggle = _ => dom.body.classList.toggle('close'); |
| 13 | + |
| 14 | + el = dom.getNode(el); |
| 15 | + if (el === null || el === undefined) { |
| 16 | + return; |
| 17 | + } |
| 18 | + |
| 19 | + dom.on(el, 'click', e => { |
| 20 | + e.stopPropagation(); |
| 21 | + toggle(); |
| 22 | + }); |
| 23 | + |
| 24 | + isMobile && |
| 25 | + dom.on( |
| 26 | + dom.body, |
| 27 | + 'click', |
| 28 | + _ => dom.body.classList.contains('close') && toggle() |
| 29 | + ); |
| 30 | +} |
| 31 | + |
| 32 | +export function collapse(el) { |
| 33 | + el = dom.getNode(el); |
| 34 | + if (el === null || el === undefined) { |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + dom.on(el, 'click', ({ target }) => { |
| 39 | + if ( |
| 40 | + target.nodeName === 'A' && |
| 41 | + target.nextSibling && |
| 42 | + target.nextSibling.classList && |
| 43 | + target.nextSibling.classList.contains('app-sub-sidebar') |
| 44 | + ) { |
| 45 | + dom.toggleClass(target.parentNode, 'collapse'); |
| 46 | + } |
| 47 | + }); |
| 48 | +} |
| 49 | + |
| 50 | +export function sticky() { |
| 51 | + const cover = dom.getNode('section.cover'); |
| 52 | + if (!cover) { |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + const coverHeight = cover.getBoundingClientRect().height; |
| 57 | + |
| 58 | + if (window.pageYOffset >= coverHeight || cover.classList.contains('hidden')) { |
| 59 | + dom.toggleClass(dom.body, 'add', 'sticky'); |
| 60 | + } else { |
| 61 | + dom.toggleClass(dom.body, 'remove', 'sticky'); |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +/** |
| 66 | + * Get and active link |
| 67 | + * @param {Object} router Router |
| 68 | + * @param {String|Element} el Target element |
| 69 | + * @param {Boolean} isParent Active parent |
| 70 | + * @param {Boolean} autoTitle Automatically set title |
| 71 | + * @return {Element} Active element |
| 72 | + */ |
| 73 | +export function getAndActive(router, el, isParent, autoTitle) { |
| 74 | + el = dom.getNode(el); |
| 75 | + let links = []; |
| 76 | + if (el !== null && el !== undefined) { |
| 77 | + links = dom.findAll(el, 'a'); |
| 78 | + } |
| 79 | + |
| 80 | + const hash = decodeURI(router.toURL(router.getCurrentPath())); |
| 81 | + let target; |
| 82 | + |
| 83 | + links |
| 84 | + .sort((a, b) => b.href.length - a.href.length) |
| 85 | + .forEach(a => { |
| 86 | + const href = a.getAttribute('href'); |
| 87 | + const node = isParent ? a.parentNode : a; |
| 88 | + |
| 89 | + if (hash.indexOf(href) === 0 && !target) { |
| 90 | + target = a; |
| 91 | + dom.toggleClass(node, 'add', 'active'); |
| 92 | + } else { |
| 93 | + dom.toggleClass(node, 'remove', 'active'); |
| 94 | + } |
| 95 | + }); |
| 96 | + |
| 97 | + if (autoTitle) { |
| 98 | + dom.$.title = target |
| 99 | + ? target.title || `${target.innerText} - ${title}` |
| 100 | + : title; |
| 101 | + } |
| 102 | + |
| 103 | + return target; |
| 104 | +} |
0 commit comments