{
  "version": 3,
  "sources": ["../src/single-page.ts"],
  "sourcesContent": ["export interface RouteEventData {\n    scrollX:number;\n    scrollY:number;\n    popstate:boolean;\n}\n\nclass Page {\n    current:string|null = null\n    hasPushState:boolean|typeof window.history.pushState = false\n    scroll = {}\n    cb:((href:string, data:RouteEventData)=>void)|null = null\n    handleAnchor:boolean|((newPath:string)=>boolean) = true\n\n    constructor (\n        cb:(href:string, data:RouteEventData) => void,\n        opts:{\n            pushState?: typeof history.pushState\n            handleAnchor?:boolean|((newPath:string)=>boolean)\n        } = { pushState: undefined }\n    ) {\n        this.hasPushState = (opts.pushState !== undefined ?\n            opts.pushState :\n            (window.history && window.history.pushState)\n        )\n\n        if (typeof opts.handleAnchor === 'function') {\n            this.handleAnchor = opts.handleAnchor\n        }\n\n        this.cb = cb\n    }\n\n    show (href:string, opts = { popstate: false }) {\n        href = href.replace(/^\\/+/, '/')\n\n        this.saveScroll()\n        this.current = href\n\n        const scroll = this.scroll[href]\n        this.pushHref(href)\n\n        this.cb && this.cb(href, {\n            popstate: opts.popstate,\n            scrollX: (scroll && scroll[0]) || 0,\n            scrollY: (scroll && scroll[1]) || 0\n        })\n    }\n\n    saveScroll () {\n        if (this.scroll && this.current) {\n            this.scroll[this.current] = [window.scrollX, window.scrollY]\n        }\n    };\n\n    pushHref (href:string) {\n        this.current = href\n        const mismatched = getPath() !== href\n        let handleThis = true\n\n        if (href.includes('#')) {\n            handleThis = (typeof this.handleAnchor === 'function' ?\n                this.handleAnchor(href) :\n                this.handleAnchor)\n        }\n\n        if (mismatched && handleThis) {\n            window.history.pushState(null, '', href)\n        }\n    }\n\n    push (href:string) {\n        href = href.replace(/^\\/+/, '/')\n        this.saveScroll()\n        this.pushHref(href)\n    }\n}\n\nfunction getPath () {\n    return window.location.pathname\n        + (window.location.search || '')\n        + (window.location.hash || '')\n}\n\nexport default singlePage\n\nexport interface PushFunction {\n    (href:string):void;\n    push:(href:string) => void;\n    show:(href:string) => void;\n    page:InstanceType<typeof Page>;\n}\n\nexport function singlePage (\n    cb:((href:string, data:RouteEventData)=>void),\n    opts?:{\n        pushState?:typeof history.pushState;\n        handleAnchor?:boolean|((newPath:string)=>boolean);\n        init?:boolean  // default true\n    }\n):PushFunction {\n    const page = new Page(cb, opts)\n    window.addEventListener('popstate', onpopstate)\n\n    const init = opts?.init === undefined ? true : opts.init\n\n    function onpopstate () {\n        const href = getPath()\n        page.show(href, { popstate: true })\n    }\n\n    if (init) {\n        setTimeout(onpopstate, 0)  // trigger an event right away, on page load\n    }\n\n    const setRoute:PushFunction = function (href:string) {\n        return page.show(href)\n    }\n    setRoute.push = function (href:string) {\n        return page.push(href)\n    }\n    setRoute.show = function (href) { return page.show(href) }\n    setRoute.page = page\n\n    return setRoute\n}\n"],
  "mappings": "4dAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,aAAAE,EAAA,eAAAC,IAAA,eAAAC,EAAAJ,GAMA,IAAMK,EAAN,KAAW,CANX,MAMW,CAAAC,EAAA,aACP,QAAsB,KACtB,aAAuD,GACvD,OAAS,CAAC,EACV,GAAqD,KACrD,aAAmD,GAEnD,YACIC,EACAC,EAGI,CAAE,UAAW,MAAU,EAC7B,CACE,KAAK,aAAgBA,EAAK,YAAc,OACpCA,EAAK,UACJ,OAAO,SAAW,OAAO,QAAQ,UAGlC,OAAOA,EAAK,cAAiB,aAC7B,KAAK,aAAeA,EAAK,cAG7B,KAAK,GAAKD,CACd,CAEA,KAAME,EAAaD,EAAO,CAAE,SAAU,EAAM,EAAG,CAC3CC,EAAOA,EAAK,QAAQ,OAAQ,GAAG,EAE/B,KAAK,WAAW,EAChB,KAAK,QAAUA,EAEf,IAAMC,EAAS,KAAK,OAAOD,CAAI,EAC/B,KAAK,SAASA,CAAI,EAElB,KAAK,IAAM,KAAK,GAAGA,EAAM,CACrB,SAAUD,EAAK,SACf,QAAUE,GAAUA,EAAO,CAAC,GAAM,EAClC,QAAUA,GAAUA,EAAO,CAAC,GAAM,CACtC,CAAC,CACL,CAE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"__export", "single_page_default", "singlePage", "__toCommonJS", "Page", "__name", "cb", "opts", "href", "scroll", "mismatched", "getPath", "handleThis", "single_page_default", "singlePage", "page", "onpopstate", "init", "setRoute"]
}
