blob: 7af29e5032fe4235d8c4438c1b21fd38a97c1f8d [file] [log] [blame]
{
"version": 3,
"sources": ["unit.ts"],
"sourcesContent": ["/**\n * @license\n * Copyright 2021 The Go Authors. All rights reserved.\n * Use of this source code is governed by a BSD-style\n * license that can be found in the LICENSE file.\n */\n\nconst headerHeight = 3.5;\n\n/**\n * MainLayoutController calculates dynamic height values for header elements\n * to support variable size sticky positioned elements in the header so that\n * banners and breadcumbs may overflow to multiple lines.\n */\nexport class MainLayoutController {\n private headerObserver: IntersectionObserver;\n private navObserver: IntersectionObserver;\n private asideObserver: IntersectionObserver;\n\n constructor(\n private mainHeader?: Element | null,\n private mainNav?: Element | null,\n private mainAside?: Element | null\n ) {\n this.headerObserver = new IntersectionObserver(\n ([e]) => {\n if (e.intersectionRatio < 1) {\n for (const x of document.querySelectorAll('[class^=\"go-Main-header\"')) {\n x.setAttribute('data-fixed', 'true');\n }\n } else {\n for (const x of document.querySelectorAll('[class^=\"go-Main-header\"')) {\n x.removeAttribute('data-fixed');\n }\n this.handleResize();\n }\n },\n { threshold: 1, rootMargin: `${headerHeight * 16}px` }\n );\n this.navObserver = new IntersectionObserver(\n ([e]) => {\n if (e.intersectionRatio < 1) {\n this.mainNav?.classList.add('go-Main-nav--fixed');\n this.mainNav?.setAttribute('data-fixed', 'true');\n } else {\n this.mainNav?.classList.remove('go-Main-nav--fixed');\n this.mainNav?.removeAttribute('data-fixed');\n }\n },\n { threshold: 1, rootMargin: `-${headerHeight * 16 + 10}px` }\n );\n this.asideObserver = new IntersectionObserver(\n ([e]) => {\n if (e.intersectionRatio < 1) {\n this.mainHeader?.setAttribute('data-raised', 'true');\n } else {\n this.mainHeader?.removeAttribute('data-raised');\n }\n },\n { threshold: 1, rootMargin: `-${headerHeight * 16 + 20}px 0px 0px 0px` }\n );\n this.init();\n }\n\n private init() {\n this.handleResize();\n window.addEventListener('resize', this.handleResize);\n this.mainHeader?.addEventListener('dblclick', this.handleDoubleClick);\n const siteHeader = document.querySelector('.js-siteHeader');\n if (this.mainHeader?.hasChildNodes() && siteHeader) {\n const headerSentinel = document.createElement('div');\n siteHeader.prepend(headerSentinel);\n this.headerObserver.observe(headerSentinel);\n }\n if (this.mainNav?.hasChildNodes()) {\n const navSentinel = document.createElement('div');\n this.mainNav.prepend(navSentinel);\n this.navObserver.observe(navSentinel);\n }\n if (this.mainAside) {\n const asideSentinel = document.createElement('div');\n this.mainAside.prepend(asideSentinel);\n this.asideObserver.observe(asideSentinel);\n }\n }\n\n private handleDoubleClick: EventListener = e => {\n const target = e.target;\n if (target === this.mainHeader?.lastElementChild) {\n window.getSelection()?.removeAllRanges();\n window.scrollTo({ top: 0, behavior: 'smooth' });\n }\n };\n\n private handleResize = () => {\n const setProp = (name: string, value: string) =>\n document.documentElement.style.setProperty(name, value);\n setProp('--js-unit-header-height', '0');\n setTimeout(() => {\n const mainHeaderHeight = (this.mainHeader?.getBoundingClientRect().height ?? 0) / 16;\n setProp('--js-unit-header-height', `${mainHeaderHeight}rem`);\n setProp('--js-sticky-header-height', `${headerHeight}rem`);\n setProp('--js-unit-header-top', `${(mainHeaderHeight - headerHeight) * -1}rem`);\n });\n };\n}\n\nconst el = <T extends HTMLElement>(selector: string) => document.querySelector<T>(selector);\nnew MainLayoutController(el('.js-mainHeader'), el('.js-mainNav'), el('.js-mainAside'));\n"],
"mappings": "AAcO,IAAMA,EAAN,KAA2B,CAKhC,YACUC,EACAC,EACAC,EACR,CAHQ,gBAAAF,EACA,aAAAC,EACA,eAAAC,EAgEV,KAAQ,kBAAmCC,GAAK,CAC/BA,EAAE,SACF,KAAK,YAAY,mBAC9B,OAAO,aAAa,GAAG,gBAAgB,EACvC,OAAO,SAAS,CAAE,IAAK,EAAG,SAAU,QAAS,CAAC,EAElD,EAEA,KAAQ,aAAe,IAAM,CAC3B,IAAMC,EAAU,CAACC,EAAcC,IAC7B,SAAS,gBAAgB,MAAM,YAAYD,EAAMC,CAAK,EACxDF,EAAQ,0BAA2B,GAAG,EACtC,WAAW,I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AgB,EAC1D,GAAI,KAAK,YAAY,cAAc,GAAKA,EAAY,CAClD,IAAMC,EAAiB,SAAS,cAAc,KAAK,EACnDD,EAAW,QAAQC,CAAc,EACjC,KAAK,eAAe,QAAQA,CAAc,CAC5C,CACA,GAAI,KAAK,SAAS,cAAc,EAAG,CACjC,IAAMC,EAAc,SAAS,cAAc,KAAK,EAChD,KAAK,QAAQ,QAAQA,CAAW,EAChC,KAAK,YAAY,QAAQA,CAAW,CACtC,CACA,GAAI,KAAK,UAAW,CAClB,IAAMC,EAAgB,SAAS,cAAc,KAAK,EAClD,KAAK,UAAU,QAAQA,CAAa,EACpC,KAAK,cAAc,QAAQA,CAAa,CAC1C,CACF,CAqBF,EAEMC,EAA6BC,GAAqB,SAAS,cAAiBA,CAAQ,EAC1F,IAAIf,EAAqBc,EAAG,gBAAgB,EAAGA,EAAG,aAAa,EAAGA,EAAG,eAAe,CAAC",
"names": ["MainLayoutController", "mainHeader", "mainNav", "mainAside", "e", "setProp", "name", "value", "mainHeaderHeight", "x", "siteHeader", "headerSentinel", "navSentinel", "asideSentinel", "el", "selector"]
}