@frehner/apphistory
Advanced tools
Comparing version 0.0.3 to 0.0.4
@@ -481,2 +481,3 @@ function _defineProperty(obj, key, value) { | ||
if (linkTag) { | ||
evt.preventDefault(); | ||
window.appHistory.push(linkTag.href); | ||
@@ -483,0 +484,0 @@ } |
@@ -1,2 +0,2 @@ | ||
function t(t,e,n){return e in t?Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[e]=n,t}class e{constructor(){t(this,"current",void 0),t(this,"entries",void 0),t(this,"canGoBack",void 0),t(this,"canGoForward",void 0),t(this,"eventListeners",{navigate:[],currentchange:[],navigatesuccess:[],navigateerror:[]}),t(this,"onEventListeners",{navigate:null,currentchange:null,navigatesuccess:null,navigateerror:null}),this.current=new n({url:"TODO FIX DEFAULT URL"}),this.current.__updateEntry(void 0,0),this.entries=[this.current],this.canGoBack=!1,this.canGoForward=!1}getOptionsFromParams(t,e){let n;switch(typeof t){case"string":e&&"object"==typeof e?(n=e,n.url=t):n={url:t};break;case"object":t&&(n=t)}return n}async update(t,e){const n=performance.now(),i=this.getOptionsFromParams(t,e);this.current.__updateEntry(i??{}),this.current.finished=!1;const s=this.sendNavigateEvent(this.current,i?.navigateInfo);return this.sendCurrentChangeEvent(n),Promise.all(s).then((()=>{this.current.finished=!0,this.current.__fireEventListenersForEvent("finish"),this.sendNavigateSuccessEvent()})).catch((t=>{throw this.current.finished=!0,this.current.__fireEventListenersForEvent("finish"),this.sendNavigateErrorEvent(t),t}))}async push(t,e){const i=performance.now(),s=this.getOptionsFromParams(t,e),r=new n(s,this.current),a=this.sendNavigateEvent(r,s?.navigateInfo);this.current.__fireEventListenersForEvent("navigatefrom");const o=this.current,h=this.entries.findIndex((t=>t.key===o.key));return new URL(r.url,window.location.origin+window.location.pathname).origin===window.location.origin?window.history.pushState(null,"",r.url):window.location.assign(r.url),this.current=r,this.canGoBack=!0,this.canGoForward=!1,this.sendCurrentChangeEvent(i),this.current.__fireEventListenersForEvent("navigateto"),this.entries.slice(h+1).forEach((t=>{t.__updateEntry(void 0,-1),t.__fireEventListenersForEvent("dispose")})),this.entries=[...this.entries.slice(0,h+1),this.current].map(((t,e)=>(t.__updateEntry(void 0,e),t))),Promise.all(a).then((()=>{r.finished=!0,r.__fireEventListenersForEvent("finish"),this.sendNavigateSuccessEvent()})).catch((t=>{throw r.finished=!0,r.__fireEventListenersForEvent("finish"),this.sendNavigateErrorEvent(t),t}))}onnavigate(t){this.addOnEventListener("navigate",t)}oncurrentchange(t){this.addOnEventListener("currentchange",t)}onnavigatesuccess(t){this.addOnEventListener("navigatesuccess",t)}onnavigateerror(t){this.addOnEventListener("navigateerror",t)}addOnEventListener(t,e){this.onEventListeners[t]&&("navigate"===t?this.eventListeners.navigate=this.eventListeners.navigate.filter((t=>t!==this.onEventListeners.navigate)):this.eventListeners[t]=this.eventListeners[t].filter((e=>e!==this.onEventListeners[t]))),this.onEventListeners[t]=e,this.addEventListener(t,e)}addEventListener(t,e){if("navigate"!==t&&"currentchange"!==t&&"navigatesuccess"!==t&&"navigateerror"!==t)throw new Error("appHistory does not listen for that event at this time");!function(t,e){return"navigate"===t}(t)?this.eventListeners[t].includes(e)||this.eventListeners[t].push(e):this.eventListeners.navigate.includes(e)||this.eventListeners.navigate.push(e)}async navigateTo(t,e){const n=this.entries.findIndex((e=>e.key===t));if(-1===n)throw new DOMException("InvalidStateError");const i=this.entries[n];await this.changeCurrentEntry(i,e)}async back(t){const e=this.entries.findIndex((t=>t.key===this.current.key));if(0===e)throw new DOMException("InvalidStateError");const n=this.entries[e-1];await this.changeCurrentEntry(n,t)}async forward(t){const e=this.entries.findIndex((t=>t.key===this.current.key));if(e===this.entries.length-1)throw new DOMException("InvalidStateError");const n=this.entries[e+1];await this.changeCurrentEntry(n,t)}async changeCurrentEntry(t,e){await this.sendNavigateEvent(t,e?.navigateInfo),this.current.__fireEventListenersForEvent("navigatefrom"),this.current=t,this.current.__fireEventListenersForEvent("navigateto"),this.canGoBack=this.current.index>0,this.canGoForward=this.current.index<this.entries.length-1}sendNavigateEvent(t,e){const n=[],s=new URL(t.url,window.location.origin+window.location.pathname),r=s.origin===window.location.origin,a=new i({cancelable:!0,userInitiated:!0,hashChange:t.sameDocument&&s.hash!==window.location.hash,destination:t,info:e,canRespond:r,respondWith:t=>{if(!r)throw new Error("You cannot respond to this this event. Check event.canRespond before using respondWith");n.push(t)}});if(this.eventListeners.navigate.forEach((t=>{try{t.call(this,a)}catch(t){}})),a.defaultPrevented)throw new DOMException("AbortError");return n}sendCurrentChangeEvent(t){this.eventListeners.currentchange.forEach((e=>{try{e.call(this,new s({startTime:t}))}catch(t){}}))}sendNavigateSuccessEvent(){this.eventListeners.navigatesuccess.forEach((t=>{try{t(new CustomEvent("TODO figure out the correct event"))}catch(t){}}))}sendNavigateErrorEvent(t){this.eventListeners.navigateerror.forEach((e=>{try{e(new CustomEvent("TODO figure out the correct event",{detail:{error:t}}))}catch(t){}}))}}class n{constructor(e,n){t(this,"key",void 0),t(this,"url",void 0),t(this,"sameDocument",void 0),t(this,"index",void 0),t(this,"_state",void 0),t(this,"finished",void 0),t(this,"eventListeners",{navigateto:[],navigatefrom:[],dispose:[],finish:[]}),this._state=null,e?.state&&(this._state=e.state),this.key=Math.random().toString(36).substr(2,10),this.index=-1,this.finished=!1;const i=e?.url??n?.url??"";this.url=i;const s=new URL(i,window.location.origin+window.location.pathname);this.sameDocument=s.origin===window.location.origin&&s.pathname===window.location.pathname}getState(){return JSON.parse(JSON.stringify(this._state))}addEventListener(t,e){this.eventListeners[t].includes(e)||this.eventListeners[t].push(e)}__updateEntry(t,e){void 0!==t?.state&&(this._state=t.state),t?.url&&(this.url=t.url),"number"==typeof e&&(this.index=e)}__fireEventListenersForEvent(t){const e=new r({detail:{target:this}},t);this.eventListeners[t].map((t=>{try{t(e)}catch(t){}}))}}class i extends Event{constructor(e){super("AppHistoryNavigateEvent",e),t(this,"userInitiated",void 0),t(this,"hashChange",void 0),t(this,"destination",void 0),t(this,"formData",void 0),t(this,"info",void 0),t(this,"canRespond",void 0),t(this,"respondWith",void 0),this.userInitiated=e.userInitiated??!1,this.hashChange=e.hashChange??!1,this.destination=e.destination,this.formData=e.formData,this.canRespond=e.canRespond,this.respondWith=e.respondWith,this.info=e.info}}class s extends Event{constructor(e){super("AppHistoryCurrentChangeEvent",e),t(this,"startTime",void 0),this.startTime=e.startTime}}class r extends CustomEvent{constructor(t,e){super(e,t)}}function a(t){"appHistory"in window||(Object.defineProperty(window,"appHistory",{value:new e,enumerable:!0,configurable:t?.configurable??!1}),window.addEventListener("click",(t=>{if(t.target&&t.target instanceof HTMLElement){const e="A"===t.target.nodeName||"AREA"===t.target.nodeName?t.target:t.target.closest("a")??t.target.closest("area");e&&window.appHistory.push(e.href)}})))}export{e as AppHistory,a as useBrowserPolyfill}; | ||
function t(t,e,n){return e in t?Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[e]=n,t}class e{constructor(){t(this,"current",void 0),t(this,"entries",void 0),t(this,"canGoBack",void 0),t(this,"canGoForward",void 0),t(this,"eventListeners",{navigate:[],currentchange:[],navigatesuccess:[],navigateerror:[]}),t(this,"onEventListeners",{navigate:null,currentchange:null,navigatesuccess:null,navigateerror:null}),this.current=new n({url:"TODO FIX DEFAULT URL"}),this.current.__updateEntry(void 0,0),this.entries=[this.current],this.canGoBack=!1,this.canGoForward=!1}getOptionsFromParams(t,e){let n;switch(typeof t){case"string":e&&"object"==typeof e?(n=e,n.url=t):n={url:t};break;case"object":t&&(n=t)}return n}async update(t,e){const n=performance.now(),i=this.getOptionsFromParams(t,e);this.current.__updateEntry(i??{}),this.current.finished=!1;const s=this.sendNavigateEvent(this.current,i?.navigateInfo);return this.sendCurrentChangeEvent(n),Promise.all(s).then((()=>{this.current.finished=!0,this.current.__fireEventListenersForEvent("finish"),this.sendNavigateSuccessEvent()})).catch((t=>{throw this.current.finished=!0,this.current.__fireEventListenersForEvent("finish"),this.sendNavigateErrorEvent(t),t}))}async push(t,e){const i=performance.now(),s=this.getOptionsFromParams(t,e),r=new n(s,this.current),a=this.sendNavigateEvent(r,s?.navigateInfo);this.current.__fireEventListenersForEvent("navigatefrom");const o=this.current,h=this.entries.findIndex((t=>t.key===o.key));return new URL(r.url,window.location.origin+window.location.pathname).origin===window.location.origin?window.history.pushState(null,"",r.url):window.location.assign(r.url),this.current=r,this.canGoBack=!0,this.canGoForward=!1,this.sendCurrentChangeEvent(i),this.current.__fireEventListenersForEvent("navigateto"),this.entries.slice(h+1).forEach((t=>{t.__updateEntry(void 0,-1),t.__fireEventListenersForEvent("dispose")})),this.entries=[...this.entries.slice(0,h+1),this.current].map(((t,e)=>(t.__updateEntry(void 0,e),t))),Promise.all(a).then((()=>{r.finished=!0,r.__fireEventListenersForEvent("finish"),this.sendNavigateSuccessEvent()})).catch((t=>{throw r.finished=!0,r.__fireEventListenersForEvent("finish"),this.sendNavigateErrorEvent(t),t}))}onnavigate(t){this.addOnEventListener("navigate",t)}oncurrentchange(t){this.addOnEventListener("currentchange",t)}onnavigatesuccess(t){this.addOnEventListener("navigatesuccess",t)}onnavigateerror(t){this.addOnEventListener("navigateerror",t)}addOnEventListener(t,e){this.onEventListeners[t]&&("navigate"===t?this.eventListeners.navigate=this.eventListeners.navigate.filter((t=>t!==this.onEventListeners.navigate)):this.eventListeners[t]=this.eventListeners[t].filter((e=>e!==this.onEventListeners[t]))),this.onEventListeners[t]=e,this.addEventListener(t,e)}addEventListener(t,e){if("navigate"!==t&&"currentchange"!==t&&"navigatesuccess"!==t&&"navigateerror"!==t)throw new Error("appHistory does not listen for that event at this time");!function(t,e){return"navigate"===t}(t)?this.eventListeners[t].includes(e)||this.eventListeners[t].push(e):this.eventListeners.navigate.includes(e)||this.eventListeners.navigate.push(e)}async navigateTo(t,e){const n=this.entries.findIndex((e=>e.key===t));if(-1===n)throw new DOMException("InvalidStateError");const i=this.entries[n];await this.changeCurrentEntry(i,e)}async back(t){const e=this.entries.findIndex((t=>t.key===this.current.key));if(0===e)throw new DOMException("InvalidStateError");const n=this.entries[e-1];await this.changeCurrentEntry(n,t)}async forward(t){const e=this.entries.findIndex((t=>t.key===this.current.key));if(e===this.entries.length-1)throw new DOMException("InvalidStateError");const n=this.entries[e+1];await this.changeCurrentEntry(n,t)}async changeCurrentEntry(t,e){await this.sendNavigateEvent(t,e?.navigateInfo),this.current.__fireEventListenersForEvent("navigatefrom"),this.current=t,this.current.__fireEventListenersForEvent("navigateto"),this.canGoBack=this.current.index>0,this.canGoForward=this.current.index<this.entries.length-1}sendNavigateEvent(t,e){const n=[],s=new URL(t.url,window.location.origin+window.location.pathname),r=s.origin===window.location.origin,a=new i({cancelable:!0,userInitiated:!0,hashChange:t.sameDocument&&s.hash!==window.location.hash,destination:t,info:e,canRespond:r,respondWith:t=>{if(!r)throw new Error("You cannot respond to this this event. Check event.canRespond before using respondWith");n.push(t)}});if(this.eventListeners.navigate.forEach((t=>{try{t.call(this,a)}catch(t){}})),a.defaultPrevented)throw new DOMException("AbortError");return n}sendCurrentChangeEvent(t){this.eventListeners.currentchange.forEach((e=>{try{e.call(this,new s({startTime:t}))}catch(t){}}))}sendNavigateSuccessEvent(){this.eventListeners.navigatesuccess.forEach((t=>{try{t(new CustomEvent("TODO figure out the correct event"))}catch(t){}}))}sendNavigateErrorEvent(t){this.eventListeners.navigateerror.forEach((e=>{try{e(new CustomEvent("TODO figure out the correct event",{detail:{error:t}}))}catch(t){}}))}}class n{constructor(e,n){t(this,"key",void 0),t(this,"url",void 0),t(this,"sameDocument",void 0),t(this,"index",void 0),t(this,"_state",void 0),t(this,"finished",void 0),t(this,"eventListeners",{navigateto:[],navigatefrom:[],dispose:[],finish:[]}),this._state=null,e?.state&&(this._state=e.state),this.key=Math.random().toString(36).substr(2,10),this.index=-1,this.finished=!1;const i=e?.url??n?.url??"";this.url=i;const s=new URL(i,window.location.origin+window.location.pathname);this.sameDocument=s.origin===window.location.origin&&s.pathname===window.location.pathname}getState(){return JSON.parse(JSON.stringify(this._state))}addEventListener(t,e){this.eventListeners[t].includes(e)||this.eventListeners[t].push(e)}__updateEntry(t,e){void 0!==t?.state&&(this._state=t.state),t?.url&&(this.url=t.url),"number"==typeof e&&(this.index=e)}__fireEventListenersForEvent(t){const e=new r({detail:{target:this}},t);this.eventListeners[t].map((t=>{try{t(e)}catch(t){}}))}}class i extends Event{constructor(e){super("AppHistoryNavigateEvent",e),t(this,"userInitiated",void 0),t(this,"hashChange",void 0),t(this,"destination",void 0),t(this,"formData",void 0),t(this,"info",void 0),t(this,"canRespond",void 0),t(this,"respondWith",void 0),this.userInitiated=e.userInitiated??!1,this.hashChange=e.hashChange??!1,this.destination=e.destination,this.formData=e.formData,this.canRespond=e.canRespond,this.respondWith=e.respondWith,this.info=e.info}}class s extends Event{constructor(e){super("AppHistoryCurrentChangeEvent",e),t(this,"startTime",void 0),this.startTime=e.startTime}}class r extends CustomEvent{constructor(t,e){super(e,t)}}function a(t){"appHistory"in window||(Object.defineProperty(window,"appHistory",{value:new e,enumerable:!0,configurable:t?.configurable??!1}),window.addEventListener("click",(t=>{if(t.target&&t.target instanceof HTMLElement){const e="A"===t.target.nodeName||"AREA"===t.target.nodeName?t.target:t.target.closest("a")??t.target.closest("area");e&&(t.preventDefault(),window.appHistory.push(e.href))}})))}export{e as AppHistory,a as useBrowserPolyfill}; | ||
//# sourceMappingURL=appHistory.min.js.map |
# AppHistory | ||
## 0.0.4 | ||
Fixing bugs with useBrowserPolyfill() | ||
## 0.0.1 | ||
Initial release. |
{ | ||
"name": "@frehner/apphistory", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "A polyfill for the appHistory proposal. Not ready for production", | ||
@@ -5,0 +5,0 @@ "main": "build/esm/appHistory.min.js", |
@@ -56,2 +56,14 @@ import { useBrowserPolyfill } from "./polyfill"; | ||
it("should prevent default on anchor/area clicks so that navigation doesn't happen", async (done) => { | ||
useBrowserPolyfill({ configurable: true }); | ||
window.addEventListener("click", (evt) => { | ||
expect(evt.defaultPrevented).toBe(true); | ||
done(); | ||
}); | ||
document.body.innerHTML = `<div><a href="/page">Page</a></div>`; | ||
document.querySelector("a").click(); | ||
}); | ||
it("should not error out if no param passed", () => { | ||
@@ -58,0 +70,0 @@ useBrowserPolyfill(); |
@@ -25,2 +25,3 @@ import { AppHistory } from "./appHistory"; | ||
if (linkTag) { | ||
evt.preventDefault(); | ||
window.appHistory.push(linkTag.href); | ||
@@ -27,0 +28,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
155854
2234