-
Notifications
You must be signed in to change notification settings - Fork 2
/
hafcaf.min.js
1 lines (1 loc) · 2.07 KB
/
hafcaf.min.js
1
var hafcaf={routes:[],config:{activeClass:"active",linkClass:null,linkTag:"li",linkTagClass:null,loadingHTML:"<p>Loading...</p>",mainID:"main-container",navID:"nav-list",pageClass:null,pageTag:"div"},exitFunctions:[],addRoute:function(e){let n=e.id;if(void 0!==this.routes[n])return console.error(`A route with the ID ${n}already exists.`),!1;if(this.routes[n]=e,e.linkLabel){var t=document.createElement(this.config.linkTag);(e.linkTagClass||this.config.linkTagClass)&&t.classList.add(e.linkTagClass||this.config.linkTagClass);var i=document.createElement("a");i.href=`#${n}`,i.innerHTML=e.linkLabel,(e.linkClass||this.config.linkClass)&&i.classList.add(e.linkClass||this.config.linkClass),t.appendChild(i),document.getElementById(this.config.navID).appendChild(t)}null===document.getElementById(n)&&((t=document.createElement(this.config.pageTag)).id=n,(e.pageClass||this.config.pageClass)&&t.classList.add(e.pageClass||this.config.pageClass),t.innerHTML=e.innerHTML||this.config.loadingHTML,document.getElementById(this.config.mainID).appendChild(t));n===location.hash.slice(1)&&this.routeChange()},defaultRouteID:"home",updateRoute:function(e){let n=e.id;const t=this.routes[n];if(!t)return console.error('A route with the ID "'+n+'" does not exist, cannot update it.'),!1;if(e.linkHTML){document.querySelector("a[href='#"+n+"']").innerHTML=e.linkHTML}if(e.innerHTML){document.getElementById(n).innerHTML=e.innerHTML}e.onRender&&(t.onRender=e.onRender),n===location.hash.slice(1)&&this.routeChange()},routeChange:function(){const e=location.hash.slice(1);let n=this.routes[e]||this.routes[this.defaultRouteID];if(!n)return;const{activeClass:t}=this.config;for(var i of document.getElementsByClassName(t))i.classList.remove(t);for(var a=this.exitFunctions.length;a>0;a--)this.exitFunctions.pop()();const s=document.querySelector("a[href='#"+n.id+"']");s&&s.classList.add(t),void 0!==n.onRender&&n.onRender()},init(e){e&&(this.config=e),window.addEventListener("hashchange",function(e){hafcaf.routeChange()}),window.location.hash||(window.location.hash=this.defaultRouteID),this.routeChange()}};export default hafcaf;