UNPKG

@osaedasia/oresume

Version:

A user-friendly library for generating complete Single Page Applications (SPAs)

2 lines (1 loc) 1.25 kB
import{Page as e}from"./Page.js";import{PageNotFoundError as i,AppInitializedError as t}from"../../domain/exceptions/App.js";class a{_pages;_urlBasedRouting;constructor(e){this._pages=[],this._urlBasedRouting=e}registerPages(i){for(const t of i){const i=new e(t.config,this._urlBasedRouting);this._initializePage(t,i),this._validateAndAddPage(i)}this._validatePagesExist()}findPageByOriginalUrl(e){const t=this._pages.find((i=>i.route.originalUrl===e));if(!t)throw new i(`No page found for URL: ${e}`);return t}findPageByNormalizedUrl(e){const t=this._pages.find((i=>i.route.normalizedUrl===e));if(!t)throw new i(`No page found for normalized URL: ${e}`);return t}_initializePage(e,i){e.instance=i,e.initCallbacks.length>0&&(e.initCallbacks.forEach((e=>e(i))),e.initCallbacks=[]),e.eventListeners.length>0&&(e.eventListeners.forEach((e=>i.fragment.addEventListener(e.selector,e.type,e.callback))),e.eventListeners=[])}_validateAndAddPage(e){if(this._pages.some((i=>i.route.normalizedUrl===e.route.normalizedUrl)))throw new t(`Page already registered with normalized url: '${e.route.normalizedUrl}'.`);this._pages.push(e)}_validatePagesExist(){if(0===this._pages.length)throw new t("No pages registered in the application.")}}export{a as PageManager};