-
Notifications
You must be signed in to change notification settings - Fork 2
/
router.min.js
7 lines (7 loc) · 1.91 KB
/
router.min.js
1
2
3
4
5
6
7
/*!
* RouterJS v1.5.0 (https://www.interart.com/)
* Copyright 2018-2023 Silvio Delgado (https://github.com/silviodelgado)
* Licensed under MIT (https://opensource.org/licenses/MIT)
* https://github.com/silviodelgado/routerjs
*/
(function(e,t){"function"==typeof define&&define.amd?define([],t(e)):"object"==typeof exports?module.exports=t(e):e.Router=t(e)})("undefined"!=typeof global?global:this.window||this.global,function(e){"use strict";let t={current:null,routes:[],history:[],run_before:null,run_after:null};const r=e=>{"function"==typeof t.run_before&&t.run_before&&t.run_before.call(this),e&&e.run_before&&"function"==typeof e.run_before&&e.run_before&&e.run_before.call(this)},n=e=>{e&&e.run_after&&"function"==typeof e.run_after&&e.run_after&&e.run_after.call(this),"function"==typeof t.run_after&&t.run_after&&t.run_after.call(this)},o={getFragment:()=>window.location.hash.replace(/\/$/,""),add:(e,r,n,u)=>("function"==typeof e&&(r=e,e=""),t.routes.push({handler:r,route:e,run_before:n,run_after:u}),o),beforeAll:e=>(t.run_before=e,o),afterAll:e=>(t.run_after=e,o),apply:e=>{let u=e||o.getFragment();if(t.current)return o;for(let e=0;e<t.routes.length;e++){let l=u.match(t.routes[e].route);if(l)return l.shift(),t.current=u,t.history[u]||t.history.push(u),r(t.routes[e]),t.routes[e].handler.apply({},l),n(t.routes[e]),o}return o},start:()=>{let e=o.getFragment();return window.onhashchange=function(){let r=o.getFragment();r||(t.current=null,e=null),t.current!=r&&(t.current=null,o.apply(r))},e&&!t.current&&o.apply(),o},navigate:(e,t)=>(document.title=t||document.title,e=e.replace(/##/g,"#")||"",window.location.hash=e?"#"+e:"",o),clearHash:()=>{window.location.hash="#",history.pushState(null,document.title,window.location.pathname+window.location.search)},back:()=>{t.history.pop();let e=t.history.pop();return e=e||"",window.location.hash=e,o},checkFragment:e=>o.getFragment().indexOf(e)>=0,routes:()=>t.routes};return o});