{
  "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": ";;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAMA,MAAM,KAAK;AAAA,EANX,OAMW;AAAA;AAAA;AAAA,EACP,UAAsB;AAAA,EACtB,eAAuD;AAAA,EACvD,SAAS,CAAC;AAAA,EACV,KAAqD;AAAA,EACrD,eAAmD;AAAA,EAEnD,YACI,IACA,OAGI,EAAE,WAAW,OAAU,GAC7B;AACE,SAAK,eAAgB,KAAK,cAAc,SACpC,KAAK,YACJ,OAAO,WAAW,OAAO,QAAQ;AAGtC,QAAI,OAAO,KAAK,iBAAiB,YAAY;AACzC,WAAK,eAAe,KAAK;AAAA,IAC7B;AAEA,SAAK,KAAK;AAAA,EACd;AAAA,EAEA,KAAM,MAAa,OAAO,EAAE,UAAU,MAAM,GAAG;AAC3C,WAAO,KAAK,QAAQ,QAAQ,GAAG;AAE/B,SAAK,WAAW;AAChB,SAAK,UAAU;AAEf,UAAM,SAAS,KAAK,OAAO,IAAI;AAC/B,SAAK,SAAS,IAAI;AAElB,SAAK,MAAM,KAAK,GAAG,MAAM;AAAA,MACrB,UAAU,KAAK;AAAA,MACf,SAAU,UAAU,OAAO,CAAC,KAAM;AAAA,MAClC,SAAU,UAA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