212 lines
5.2 KiB
TypeScript
212 lines
5.2 KiB
TypeScript
![]() |
import { nextFrame } from "../animation/nextFrame";
|
||
|
import { OnAnimationFrame } from "../animation/OnAnimationFrame";
|
||
|
import { DateMath } from "../date/DateMath";
|
||
|
import { ActivityAnalyser } from "../dom/ActivityAnalyser";
|
||
|
import { Insight } from "../dom/Insight";
|
||
|
import { LandscapeScale } from "../dom/LandscapeScale";
|
||
|
import { PageHandler, PageHandlerMode } from "../dom/PageHandler";
|
||
|
import { UserAgentDeviceTypes } from "../dom/UserAgentDeviceType";
|
||
|
import { TemplatesManager, TemplatesManagerMode } from "../templates/TemplatesManager";
|
||
|
import { AppPathConverter } from "./AppPathConverter";
|
||
|
|
||
|
export class AppInitializerData
|
||
|
{
|
||
|
localAdress:string;
|
||
|
webAdress:string;
|
||
|
templatesList:string[];
|
||
|
addTemplateStyles:boolean = false;
|
||
|
pageRoot:string;
|
||
|
pagesPath?:string;
|
||
|
disableForceHTTPS?:boolean;
|
||
|
htmlMode?:boolean;
|
||
|
allowWWWSubdomain?:boolean;
|
||
|
|
||
|
}
|
||
|
|
||
|
export class ElectronAppInitializerData
|
||
|
{
|
||
|
appLocation:string;
|
||
|
templatesList:string[];
|
||
|
pageRoot:string;
|
||
|
disableForceHTTPS?:boolean;
|
||
|
htmlMode?:boolean;
|
||
|
allowWWWSubdomain:boolean;
|
||
|
}
|
||
|
|
||
|
export class App
|
||
|
{
|
||
|
readonly activityAnalyser = new ActivityAnalyser();
|
||
|
readonly onAnimationFrame = new OnAnimationFrame();
|
||
|
readonly templatesManager = new TemplatesManager();
|
||
|
readonly insight = new Insight();
|
||
|
readonly landscapeScale = new LandscapeScale();
|
||
|
readonly pathConverter = new AppPathConverter( this );
|
||
|
|
||
|
private _pageHandler:PageHandler;
|
||
|
private _initializerData:AppInitializerData;
|
||
|
private _electronInitializerData:ElectronAppInitializerData;
|
||
|
|
||
|
protected _loaded = false;
|
||
|
get loaded()
|
||
|
{
|
||
|
return this._loaded;
|
||
|
}
|
||
|
|
||
|
protected _loadedTime:Date = null;
|
||
|
|
||
|
get timeElapsedSinceLoaded()
|
||
|
{
|
||
|
return DateMath.getDifferenceMs( new Date(), this._loadedTime ) / 1000;
|
||
|
}
|
||
|
|
||
|
setLoaded()
|
||
|
{
|
||
|
this._loaded = true;
|
||
|
this._loadedTime = new Date();
|
||
|
}
|
||
|
|
||
|
get initializerData(){ return this._initializerData; }
|
||
|
|
||
|
get pageHandler(){ return this._pageHandler; }
|
||
|
|
||
|
|
||
|
get webAdress()
|
||
|
{
|
||
|
return this._initializerData.webAdress;
|
||
|
}
|
||
|
|
||
|
get isHTMLMode()
|
||
|
{
|
||
|
return this._initializerData.htmlMode === true;
|
||
|
}
|
||
|
|
||
|
grabHash()
|
||
|
{
|
||
|
let hash = window.location.hash;
|
||
|
return hash;
|
||
|
}
|
||
|
|
||
|
get isLocal()
|
||
|
{
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
async initializeApp( data:AppInitializerData|ElectronAppInitializerData ):Promise<void>
|
||
|
{
|
||
|
if ( document.readyState === "loading" )
|
||
|
{
|
||
|
document.addEventListener(
|
||
|
"DOMContentLoaded",
|
||
|
()=>
|
||
|
{
|
||
|
this.setLoaded();
|
||
|
}
|
||
|
);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
this.setLoaded();
|
||
|
}
|
||
|
|
||
|
setTimeout( ()=>{ this.setLoaded() }, 3000 )
|
||
|
|
||
|
if ( ! data.allowWWWSubdomain )
|
||
|
{
|
||
|
let url = window.location + "";
|
||
|
|
||
|
if ( url.startsWith( "https://www." ) )
|
||
|
{
|
||
|
let realURL = url.replace( /^https:\/\/www\./, "https://" );
|
||
|
|
||
|
window.location.assign( realURL );
|
||
|
|
||
|
return Promise.resolve();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
UserAgentDeviceTypes.setOnBody();
|
||
|
|
||
|
if ( ( data as ElectronAppInitializerData ).appLocation )
|
||
|
{
|
||
|
console.log( "ELECTRON APP" );
|
||
|
this._electronInitializerData = data as ElectronAppInitializerData;
|
||
|
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
this._initializerData = data as AppInitializerData;
|
||
|
}
|
||
|
|
||
|
|
||
|
if ( data.templatesList )
|
||
|
{
|
||
|
let templatesList = data.templatesList;
|
||
|
let templateStylesMode = this.initializerData.addTemplateStyles ?
|
||
|
TemplatesManagerMode.ADD_STYLES_TO_HEAD :
|
||
|
TemplatesManagerMode.IGNORE_STYLES;
|
||
|
|
||
|
this.templatesManager.setMode( templateStylesMode );
|
||
|
templatesList.forEach( c => this.templatesManager.addTemplateHTML( c ) );
|
||
|
}
|
||
|
|
||
|
if ( this._initializerData )
|
||
|
{
|
||
|
//console.log( "PAGE HANDLER FOR WEB" );
|
||
|
let appData = this._initializerData;
|
||
|
this._pageHandler = new PageHandler( appData.localAdress, appData.webAdress, data.disableForceHTTPS );
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
//console.log( "PAGE HANDLER FOR APP" );
|
||
|
let electronData = this._electronInitializerData;
|
||
|
this._pageHandler = new PageHandler( electronData.appLocation, null, electronData.disableForceHTTPS, PageHandlerMode.ELECTRON );
|
||
|
}
|
||
|
|
||
|
|
||
|
this.pageHandler.setPageRootTag( data.pageRoot );
|
||
|
|
||
|
if ( this._initializerData.pagesPath )
|
||
|
{
|
||
|
this.pageHandler.setPagesPath( this._initializerData.pagesPath );
|
||
|
}
|
||
|
|
||
|
|
||
|
this.onAnimationFrame.run();
|
||
|
|
||
|
this.onAnimationFrame.addListener(
|
||
|
()=>
|
||
|
{
|
||
|
this.landscapeScale.update();
|
||
|
this.insight.update();
|
||
|
|
||
|
}
|
||
|
)
|
||
|
|
||
|
this.activityAnalyser.start();
|
||
|
|
||
|
await this.pageHandler.initialize();
|
||
|
|
||
|
this.initializePage();
|
||
|
|
||
|
}
|
||
|
|
||
|
async initializePage()
|
||
|
{
|
||
|
while ( ! this.loaded )
|
||
|
{
|
||
|
await nextFrame();
|
||
|
}
|
||
|
|
||
|
//console.log( "App.initializePage" );
|
||
|
this.insight.grabElements();
|
||
|
this.templatesManager.processChildren( document.body );
|
||
|
this.pageHandler.replaceLinks();
|
||
|
|
||
|
return Promise.resolve();
|
||
|
}
|
||
|
|
||
|
static updateAll()
|
||
|
{
|
||
|
Insight.updateAll();
|
||
|
}
|
||
|
}
|