1. New Router and Base Page Class architecture to avoid circular references and conform to module bundler requirements. \n 2. Relative path bug fix for lib/validation.js file to work using module bundler.
This commit is contained in:
@@ -1,20 +1,30 @@
|
||||
|
||||
// Pages
|
||||
// Core
|
||||
import PageAdminHome from './pages/core/admin_home.js';
|
||||
import PageHome from './pages/core/home.js';
|
||||
import PageContact from './pages/core/contact.js';
|
||||
import PageServices from './pages/core/services.js';
|
||||
// Legal
|
||||
import PageAccessibilityReport from './pages/legal/accessibility_report.js';
|
||||
import PageAccessibilityStatement from './pages/legal/accessibility_statement.js';
|
||||
import PageLicense from './pages/legal/license.js';
|
||||
// Store
|
||||
import PageStoreBasket from './pages/store/basket.js';
|
||||
import PageStoreHome from './pages/store/home.js';
|
||||
import PageStoreProductCategories from './pages/store/product_categories.js';
|
||||
import PageStoreProductPermutations from './pages/store/product_permutations.js';
|
||||
// import PageStoreProductPrices from './pages/store/product_prices.js';
|
||||
// import PageStoreProducts from './pages/store/products.js';
|
||||
// import PageStoreProductVariations from './pages/store/product_variations.js';
|
||||
import PageStoreStockItems from './pages/store/stock_items.js';
|
||||
// User
|
||||
// import PageUserLogin from './pages/user/login.js';
|
||||
// import PageUserLogout from './pages/user/logout.js';
|
||||
// import PageUserAccount from './pages/user/account.js';
|
||||
|
||||
|
||||
/*
|
||||
import { PageAdminHome } from './pages/core/admin_home.js';
|
||||
import { PageHome } from './pages/core/home.js';
|
||||
import { PageContact } from './pages/core/contact.js';
|
||||
import { PageAccessibilityStatement } from './pages/legal/accessibility_statement.js';
|
||||
import { PageLicense } from './pages/legal/license.js';
|
||||
import { PageServices } from './pages/core/services.js';
|
||||
import { PageStoreBasket } from './pages/store/basket.js';
|
||||
import { PageStoreHome } from './pages/store/home.js';
|
||||
import { PageStoreProductCategories } from './pages/store/product_categories.js';
|
||||
import { PageStoreProductPermutations } from './pages/store/product_permutations.js';
|
||||
// import { PageStoreProductPrices } from './pages/store/product_prices.js';
|
||||
// import { PageStoreProducts } from './pages/store/products.js';
|
||||
// import { PageStoreProductVariations } from './pages/store/product_variations.js';
|
||||
import { PageStoreStockItems } from './pages/store/stock_items.js';
|
||||
*/
|
||||
import "./lib/common.js";
|
||||
import "./lib/constants.js";
|
||||
import "./lib/events.js";
|
||||
@@ -23,11 +33,15 @@ import "./lib/extras.js";
|
||||
import "./lib/local_storage.js";
|
||||
import "./lib/utils.js";
|
||||
import "./lib/validation.js";
|
||||
*/
|
||||
|
||||
import API from './api.js';
|
||||
import DOM from './dom.js';
|
||||
import PagePrivacyPolicy from './pages/legal/privacy_policy.js';
|
||||
import PageRetentionSchedule from './pages/legal/retention_schedule.js';
|
||||
|
||||
// Create a context for the pages
|
||||
const pagesContext = require.context('./pages', true, /\.js$/);
|
||||
// const pagesContext = require.context('./pages', true, /\.js$/);
|
||||
|
||||
/*
|
||||
const pageModules = {
|
||||
@@ -53,23 +67,26 @@ export default class Router {
|
||||
// Pages
|
||||
this.pages = {};
|
||||
// Core
|
||||
this.pages[hashPageHome] = { name: 'PageHome', pathModule: './core/home.js' };
|
||||
this.pages[hashPageContact] = { name: 'PageContact', pathModule: './core/contact.js' };
|
||||
this.pages[hashPageServices] = { name: 'PageServices', pathModule: './core/services.js' };
|
||||
this.pages[hashPageAdminHome] = { name: 'PageAdminHome', pathModule: './core/admin_home.js' };
|
||||
this.pages[hashPageHome] = { name: 'PageHome', module: PageAdminHome }; // importModule: () => import(/* webpackChunkName: "page_core_home" */ './pages/core/home.js') , pathModule: './pages/core/home.js'
|
||||
this.pages[hashPageContact] = { name: 'PageContact', module: PageContact }; // pathModule: './pages/core/contact.js' };
|
||||
this.pages[hashPageServices] = { name: 'PageServices', module: PageServices }; // pathModule: './pages/core/services.js' };
|
||||
this.pages[hashPageAdminHome] = { name: 'PageAdminHome', module: PageAdminHome }; // pathModule: './pages/core/admin_home.js' };
|
||||
// Legal
|
||||
this.pages[hashPageAccessibilityStatement] = { name: 'PageAccessibilityStatement', pathModule: './legal/accessibility_statement.js' };
|
||||
this.pages[hashPageLicense] = { name: 'PageLicense', pathModule: './legal/license.js' };
|
||||
this.pages[hashPageAccessibilityStatement] = { name: 'PageAccessibilityStatement', module: PageAccessibilityStatement }; // pathModule: '../../static/js/pages/legal/accessibility_statement.js' }; // , page: PageAccessibilityStatement
|
||||
this.pages[hashPageDataRetentionSchedule] = { name: 'PageDataRetentionSchedule', module: PageRetentionSchedule }; // pathModule: './pages/legal/data_retention_schedule.js' };
|
||||
this.pages[hashPageLicense] = { name: 'PageLicense', module: PageLicense }; // pathModule: './pages/legal/license.js' };
|
||||
this.pages[hashPagePrivacyPolicy] = { name: 'PagePrivacyPolicy', module: PagePrivacyPolicy }; // pathModule: './pages/legal/privacy_policy.js' }; // importModule: () => {return import(/* webpackChunkName: "page_privacy_policy" */ './pages/legal/privacy_policy.js'); }
|
||||
// Store
|
||||
this.pages[hashPageStoreProductCategories] = { name: 'PageStoreProductCategories', pathModule: './store/product_categories.js' };
|
||||
this.pages[hashPageStoreProductPermutations] = { name: 'PageStoreProductPermutations', pathModule: './store/product_permutations.js' };
|
||||
// this.pages[hashPageStoreProductPrices] = { name: 'PageStoreProductPrices', pathModule: './store/product_prices.js' };
|
||||
this.pages[hashPageStoreProducts] = { name: 'PageStoreProducts', pathModule: './store/products.js' };
|
||||
// this.pages[hashPageStoreProductVariations] = { name: 'PageStoreProductVariations', pathModule: './store/product_variations.js' };
|
||||
this.pages[hashPageStoreProductCategories] = { name: 'PageStoreProductCategories', module: PageStoreProductCategories }; // pathModule: './pages/store/product_categories.js' };
|
||||
this.pages[hashPageStoreProductPermutations] = { name: 'PageStoreProductPermutations', module: PageStoreProductPermutations }; // pathModule: './pages/store/product_permutations.js' };
|
||||
// this.pages[hashPageStoreProductPrices] = { name: 'PageStoreProductPrices', module: PageStoreProductPrices }; // pathModule: './pages/store/product_prices.js' };
|
||||
// this.pages[hashPageStoreProducts] = { name: 'PageStoreProducts', module: PageStoreProducts }; // pathModule: './pages/store/products.js' };
|
||||
// this.pages[hashPageStoreProductVariations] = { name: 'PageStoreProductVariations', module: PageStoreProductVariations }; // pathModule: './pages/store/product_variations.js' };
|
||||
this.pages[hashPageStoreStockItems] = { name: 'PageStoreStockItems', module: PageStoreStockItems };
|
||||
// User
|
||||
// this.pages[hashPageUserLogin] = { name: 'PageUserLogin', pathModule: './user/login.js' };
|
||||
// this.pages[hashPageUserLogout] = { name: 'PageUserLogout', pathModule: './user/logout.js' };
|
||||
// this.pages[hashPageUserAccount] = { name: 'PageUserAccount', pathModule: './user/account.js' };
|
||||
// this.pages[hashPageUserLogin] = { name: 'PageUserLogin', module: PageUserLogin }; // pathModule: './pages/user/login.js' };
|
||||
// this.pages[hashPageUserLogout] = { name: 'PageUserLogout', module: PageUserLogout }; // pathModule: './pages/user/logout.js' };
|
||||
// this.pages[hashPageUserAccount] = { name: 'PageUserAccount', module: PageUserAccount }; // pathModule: './pages/user/account.js' };
|
||||
|
||||
// Routes
|
||||
this.routes = {};
|
||||
@@ -80,7 +97,9 @@ export default class Router {
|
||||
this.routes[hashPageAdminHome] = (isPopState = false) => this.navigateToHash(hashPageAdminHome, isPopState);
|
||||
// Legal
|
||||
this.routes[hashPageAccessibilityStatement] = (isPopState = false) => this.navigateToHash(hashPageAccessibilityStatement, isPopState);
|
||||
this.routes[hashPageDataRetentionSchedule] = (isPopState = false) => this.navigateToHash(hashPageDataRetentionSchedule, isPopState);
|
||||
this.routes[hashPageLicense] = (isPopState = false) => this.navigateToHash(hashPageLicense, isPopState);
|
||||
this.routes[hashPagePrivacyPolicy] = (isPopState = false) => this.navigateToHash(hashPagePrivacyPolicy, isPopState);
|
||||
// Store
|
||||
this.routes[hashPageStoreProductCategories] = (isPopState = false) => this.navigateToHash(hashPageStoreProductCategories, isPopState);
|
||||
this.routes[hashPageStoreProductPermutations] = (isPopState = false) => this.navigateToHash(hashPageStoreProductPermutations, isPopState);
|
||||
@@ -97,7 +116,7 @@ export default class Router {
|
||||
console.log("loadPage: " + hashPage);
|
||||
const PageClass = await this.getClassPageFromHash(hashPage);
|
||||
console.log("PageClass: ", PageClass);
|
||||
this.currentPage = new PageClass();
|
||||
this.currentPage = new PageClass(this);
|
||||
console.log("this.currentPage: ", this.currentPage);
|
||||
this.currentPage.initialize(isPopState);
|
||||
}
|
||||
@@ -106,11 +125,19 @@ export default class Router {
|
||||
let pageJson = this.pages[hashPage];
|
||||
console.log("pageJson: ", pageJson);
|
||||
try {
|
||||
/*
|
||||
const module = await pagesContext(pageJson.pathModule);
|
||||
console.log("module: ", module);
|
||||
return module[pageJson.name];
|
||||
*/
|
||||
// const module = await import(pageJson.pathModule); // pageJson.page;
|
||||
// const module = () => import(pageJson.pathModule);
|
||||
const module = pageJson.module; // importModule;
|
||||
console.log("module: ", module);
|
||||
return module; // [pageJson.name];
|
||||
}
|
||||
catch (error) {
|
||||
console.log("this.pages: ", this.pages);
|
||||
console.error('Page not found:', hashPage);
|
||||
throw error;
|
||||
}
|
||||
@@ -144,12 +171,12 @@ export default class Router {
|
||||
*/
|
||||
let url = API.getUrlFromHash(hash, params);
|
||||
// if (!isPopState)
|
||||
history.pushState(data, '', url);
|
||||
history.pushState({data: data, params: params}, '', hash);
|
||||
API.goToUrl(url, data);
|
||||
}
|
||||
async beforeLeave() {
|
||||
const ClassPageCurrent = await this.getClassPageFromHash(DOM.getHashPageCurrent());
|
||||
const pageCurrent = new ClassPageCurrent();
|
||||
const pageCurrent = new ClassPageCurrent(this);
|
||||
pageCurrent.leave();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user