First commit
This commit is contained in:
702
framework/client/applicationManager.js
Normal file
702
framework/client/applicationManager.js
Normal file
@@ -0,0 +1,702 @@
|
||||
/*
|
||||
|
||||
Copyright (c) 2020, 2023, The Unified Company.
|
||||
|
||||
This code is part of Unify.
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE,
|
||||
as published by the Free Software Foundation.
|
||||
See the GNU AFFERO GENERAL PUBLIC LICENSE, for more details.
|
||||
|
||||
https://unifyjs.org
|
||||
|
||||
*/
|
||||
|
||||
import core from './core.js';
|
||||
|
||||
import dom from './dom.js';
|
||||
|
||||
import css from './css.js';
|
||||
|
||||
import eventManager from './eventManager.js';
|
||||
|
||||
import socketManager from './socketManager.js';
|
||||
|
||||
import animationManager from './animation/animationManager.js';
|
||||
|
||||
import collection from './collection.js';
|
||||
|
||||
import codePreview from './codePreview.js';
|
||||
|
||||
import themeLoader from './themeLoader.js';
|
||||
|
||||
import tools from '../unify/tools.js';
|
||||
|
||||
import defaultObject from '../unify/defaultObject.js';
|
||||
|
||||
//import cookieManager from '../unify/cookieManager.js';
|
||||
|
||||
import cacheManager from "../unify/cacheManager.js";
|
||||
|
||||
import config from "../configs/config.js";
|
||||
|
||||
import applicationImport from './import.js';
|
||||
|
||||
import progressBar from './progressBar.js';
|
||||
|
||||
import applications from '../configs/applications.js';
|
||||
|
||||
import simplePath from "../unify/simplePath.js";
|
||||
|
||||
|
||||
// For Benchmarking
|
||||
//document.timer.lap("before");
|
||||
//document.timer.lap("after");
|
||||
|
||||
|
||||
document.promises = new Array();
|
||||
|
||||
document.cacheManager = new cacheManager();
|
||||
|
||||
document.cacheManager.useDB = false;
|
||||
|
||||
document.debugALL = false;
|
||||
|
||||
|
||||
|
||||
|
||||
export default class applicationManager{
|
||||
|
||||
socketPort = 5002;
|
||||
|
||||
defaultSocketPort = 5002;
|
||||
|
||||
loadEditor = true;
|
||||
|
||||
defaultObject = new defaultObject();
|
||||
|
||||
themeLoader = new themeLoader();
|
||||
|
||||
mode = "development";
|
||||
|
||||
serverAddress = "localhost";
|
||||
|
||||
serverPort = 8090;
|
||||
|
||||
application;
|
||||
|
||||
applications;
|
||||
|
||||
applicationInstances = new Array();
|
||||
|
||||
ssl = false;
|
||||
|
||||
themeObjects = new Array();
|
||||
|
||||
connectPromise;
|
||||
|
||||
signinPromise;
|
||||
|
||||
constructor() {
|
||||
|
||||
this.themeLoader.applicationManager = this;
|
||||
|
||||
}
|
||||
|
||||
setParameters( config ) {
|
||||
|
||||
this.mode = config.mode;
|
||||
|
||||
this.ssl = config.ssl;
|
||||
|
||||
this.socketPort = config.socketPort;
|
||||
|
||||
this.loadThemes = config.loadThemes;
|
||||
|
||||
this.os = tools.CamelCase( config.os );
|
||||
|
||||
this.tint = tools.CamelCase( config.tint );
|
||||
|
||||
this.device = tools.CamelCase( config.device );
|
||||
|
||||
if( config.serverAddress ) {
|
||||
|
||||
this.serverAddress = config.serverAddress;
|
||||
|
||||
} else {
|
||||
|
||||
this.serverAddress = "localhost";
|
||||
|
||||
}
|
||||
|
||||
if( config.port ) {
|
||||
|
||||
this.port = config.port;
|
||||
|
||||
} else {
|
||||
|
||||
this.port = 3000;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
setGlobal() {
|
||||
|
||||
document.mode = this.mode;
|
||||
|
||||
document.config = new Object();
|
||||
|
||||
document.config.os = this.os;
|
||||
|
||||
document.config.tint = this.tint;
|
||||
|
||||
document.config.device = this.device;
|
||||
|
||||
}
|
||||
|
||||
logConfig( config ) {
|
||||
|
||||
console.log( config );
|
||||
|
||||
console.log("set socket port", this.socketPort );
|
||||
|
||||
console.log( "running in ", config.mode, " mode.", "address : " + this.serverAddress );
|
||||
|
||||
}
|
||||
|
||||
parseConfig() {
|
||||
|
||||
this.setParameters( config );
|
||||
|
||||
this.setGlobal();
|
||||
|
||||
this.logConfig( config );
|
||||
|
||||
}
|
||||
|
||||
createApplicationPath( path ) {
|
||||
|
||||
var device = tools.CamelCase( this.device );
|
||||
|
||||
var os = tools.CamelCase( this.os );
|
||||
|
||||
var tint = tools.CamelCase( this.tint );
|
||||
|
||||
var applicationPath = '/framework/cache/platforms/' + os + '/' + device + '/' + tint + '/' + path + '/application.js';
|
||||
|
||||
return applicationPath;
|
||||
|
||||
}
|
||||
|
||||
async getApplicationInstance( path, applicationID ) {
|
||||
|
||||
if( this.mode == "development" ) {
|
||||
|
||||
var applicationPath = this.createApplicationPath( path );
|
||||
|
||||
var importObject = await import( applicationPath );
|
||||
|
||||
this.application = new importObject.default();
|
||||
|
||||
} else {
|
||||
|
||||
var currentBundle = applicationImport[ applicationID ];
|
||||
|
||||
//console.log("loading application with id", applicationID);
|
||||
|
||||
if( currentBundle ) {
|
||||
|
||||
this.application = new currentBundle();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return this.application;
|
||||
|
||||
}
|
||||
|
||||
getUserObject( core, application, username, sessionKey ) {
|
||||
|
||||
if( !username ) {
|
||||
|
||||
var userObject = core.getFirstByClassName( "signin", application );
|
||||
|
||||
} else {
|
||||
|
||||
var userObject = core.getFirstByClassName( "signin", application );
|
||||
|
||||
|
||||
if( !userObject ){
|
||||
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
|
||||
if( sessionKey ) {
|
||||
|
||||
userObject.sessionKey.value = sessionKey;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return userObject;
|
||||
|
||||
}
|
||||
|
||||
async singinUser( core, socketManager, userObject ) {
|
||||
|
||||
//var user = await socketManager.get( "settings", "signIn", userObject );
|
||||
|
||||
var user = await userObject.process();
|
||||
//await userObject.socketManager.get( "table", "process", userObject );
|
||||
|
||||
//var user = await userObject.process();
|
||||
|
||||
core.user = user;
|
||||
|
||||
await core.setUser( user, application );
|
||||
|
||||
console.log( "update user permissions", user );
|
||||
|
||||
if( !user.permissionObjects ) {
|
||||
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
core.updatePermissions( user.permissionObjects );
|
||||
|
||||
}
|
||||
|
||||
|
||||
configurePath() {
|
||||
|
||||
this.url = window.location;
|
||||
|
||||
this.pathName = this.url.pathname;
|
||||
|
||||
}
|
||||
|
||||
parseUrlByID( core, tableName, id ) {
|
||||
|
||||
var objects = core.getObjectsByPropertyName( tableName );
|
||||
|
||||
if( objects.length > 0 ) {
|
||||
|
||||
var object = objects[0];
|
||||
|
||||
if( object.parent.create ) {
|
||||
|
||||
object.parent.create();
|
||||
|
||||
}
|
||||
|
||||
object.id = parseFloat( id );
|
||||
|
||||
if( object.loadPage ) {
|
||||
|
||||
object.loadPage();
|
||||
|
||||
}
|
||||
|
||||
object.showParents();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
async parseUrl( core, application ) {
|
||||
|
||||
|
||||
|
||||
this.configurePath();
|
||||
|
||||
var pathName = this.pathName;
|
||||
|
||||
var pathParts = pathName.split("/");
|
||||
|
||||
pathParts.shift();
|
||||
|
||||
var tableName = pathParts[0];
|
||||
|
||||
var id = pathParts[1];
|
||||
|
||||
if( id ) {
|
||||
|
||||
this.parseUrlByID( core, tableName, id );
|
||||
|
||||
} else {
|
||||
|
||||
document.stateMachine.composeState()
|
||||
|
||||
core.runDefault( application );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
createBaseUrl() {
|
||||
|
||||
var base = document.createElement("base");
|
||||
|
||||
if( this.ssl ) {
|
||||
|
||||
base.href = "https://" + this.serverAddress;
|
||||
|
||||
} else {
|
||||
|
||||
base.href = "http://" + this.serverAddress + ":" + this.port;
|
||||
|
||||
}
|
||||
|
||||
document.head.appendChild( base );
|
||||
|
||||
}
|
||||
|
||||
createMetaDescription() {
|
||||
|
||||
if( this.application.description ) {
|
||||
|
||||
document.querySelector(".metaDescription").setAttribute("content", this.application.description );
|
||||
|
||||
} else {
|
||||
|
||||
document.querySelector(".metaDescription").setAttribute("content", "A unifyJS application." );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
async initializeSocketManager( core, application ) {
|
||||
|
||||
var socket = new socketManager( this.socketPort );
|
||||
|
||||
socket.ssl = this.ssl;
|
||||
|
||||
socket.serverAddress = this.serverAddress;
|
||||
|
||||
this.connectPromise = socket.connect();
|
||||
|
||||
|
||||
document.connectPromise = this.connectPromise;
|
||||
|
||||
await this.connectPromise;
|
||||
|
||||
socket.addClient();
|
||||
|
||||
|
||||
return socket;
|
||||
|
||||
}
|
||||
|
||||
async createApplicationInstances() {
|
||||
|
||||
for( var applicationID = 0; applicationID < this.applications.length; applicationID++ ) {
|
||||
|
||||
var applicationFile = this.applications[ applicationID ];
|
||||
|
||||
this.applicationInstances[ applicationID ] = await this.getApplicationInstance( applicationFile.path, applicationID );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
bindApplications() {
|
||||
|
||||
this.applications = applications;
|
||||
|
||||
document.applications = applications;
|
||||
|
||||
}
|
||||
|
||||
getApplication() {
|
||||
|
||||
var applicationFile = this.applications[ 0 ];
|
||||
|
||||
applicationFile.socket = socket;
|
||||
|
||||
}
|
||||
|
||||
async start() {
|
||||
|
||||
var unifyCore = new core();
|
||||
|
||||
this.parseConfig();
|
||||
|
||||
this.bindApplications();
|
||||
|
||||
this.createBaseUrl();
|
||||
|
||||
await this.createApplicationInstances();
|
||||
|
||||
var socket = this.initializeSocketManager( unifyCore, this.applicationInstances[0] );
|
||||
|
||||
await this.runApplication( socket, 0, unifyCore );
|
||||
|
||||
this.createMetaDescription();
|
||||
|
||||
document.timer.lap("Everything is loaded");
|
||||
|
||||
console.log( this.applicationInstances[0] );
|
||||
|
||||
}
|
||||
|
||||
async updateApplications() {
|
||||
|
||||
for( var applicationID = 1; applicationID < this.applications.length; applicationID++) {
|
||||
|
||||
var applicationFile = this.applications[ applicationID ];
|
||||
|
||||
var unifyCore = new core();
|
||||
|
||||
this.runApplication( applicationFile.socket, applicationID, unifyCore );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
async attachSocket( applicationFile, socket, applicationID ) {
|
||||
|
||||
if( !applicationFile.socket ) {
|
||||
|
||||
applicationFile.socket = await new socketManager( this.defaultSocketPort + applicationID );
|
||||
|
||||
if( this.serverAddress ) {
|
||||
|
||||
applicationFile.socket.serverAddress = this.serverAddress;
|
||||
|
||||
}
|
||||
|
||||
await applicationFile.socket.connect();
|
||||
|
||||
}
|
||||
|
||||
return applicationFile.socket;
|
||||
|
||||
}
|
||||
|
||||
preventDoubleLoading( element ) {
|
||||
|
||||
if( element && element.childElementCount != 0 ) {
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
createCodeEditor( app, applicationID ) {
|
||||
|
||||
if( applicationID != 0 ) {
|
||||
|
||||
new codePreview( app );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
getDomElement( application ) {
|
||||
|
||||
if( this.applications.length == 1) {
|
||||
|
||||
application.selector = "#application";
|
||||
|
||||
}
|
||||
|
||||
var element = document.querySelector( application.selector );
|
||||
|
||||
if( document.querySelectorAll( application.selector ).length == 0 ) {
|
||||
|
||||
return true;
|
||||
|
||||
} else {
|
||||
|
||||
if( this.preventDoubleLoading( element ) ) {
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
configureCore( core ) {
|
||||
|
||||
core.device = this.device;
|
||||
|
||||
core.os = this.os;
|
||||
|
||||
core.tint = this.tint;
|
||||
|
||||
}
|
||||
|
||||
setGlobalCore( unifyCore, applicationID ) {
|
||||
|
||||
if( !document.cores ) {
|
||||
|
||||
document.cores = new Array();
|
||||
|
||||
}
|
||||
|
||||
document.cores[ applicationID ] = unifyCore;
|
||||
|
||||
}
|
||||
|
||||
async parseApplication( core, application, applicationID ) {
|
||||
|
||||
this.defaultObject.agregateDefaultObject( application );
|
||||
|
||||
this.setGlobalCore( core, applicationID );
|
||||
|
||||
await core.prepareObjects( application );
|
||||
|
||||
await document.connectPromise;
|
||||
|
||||
this.configureCore( core );
|
||||
|
||||
core.setTheme();
|
||||
|
||||
application.core = core;
|
||||
|
||||
await core.parse( application, true );
|
||||
|
||||
core.createDependencyMap();
|
||||
|
||||
}
|
||||
|
||||
hidePreloader() {
|
||||
|
||||
if( document.querySelector(".loadingBarPanel, .preloader, #preloader" )) {
|
||||
|
||||
document.querySelector(".loadingBarPanel, .preloader, #preloader").style.opacity = "0%";
|
||||
|
||||
}
|
||||
|
||||
setTimeout(function(){
|
||||
|
||||
document.querySelector(".loadingBarPanel, .preloader, #preloader").style.display = "none";
|
||||
|
||||
}, 1000)
|
||||
|
||||
}
|
||||
|
||||
logApplication( application, unifyCore ) {
|
||||
|
||||
if( document.debugALL ) {
|
||||
|
||||
console.log( "" );
|
||||
console.log( "run application", application.selector );
|
||||
|
||||
console.log( "Core", unifyCore );
|
||||
console.log( "Application", application );
|
||||
console.log( "" );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
setServerPort( port ) {
|
||||
|
||||
if( application.port ) {
|
||||
|
||||
this.serverPort = application.port;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
prepareApplication(applicationID) {
|
||||
|
||||
var application = this.applicationInstances[ applicationID ];
|
||||
|
||||
application.applicationID = applicationID;
|
||||
|
||||
this.setServerPort( application.port );
|
||||
|
||||
return application;
|
||||
|
||||
}
|
||||
|
||||
bindObjectsToSocket( socket, application, unifyCore, applicationID ) {
|
||||
|
||||
if( applicationID == 0 ) {
|
||||
|
||||
socket.application = application;
|
||||
|
||||
socket.core = unifyCore;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
async runApplication( socket = false, applicationID, unifyCore ) {
|
||||
|
||||
if ( this.applicationInstances[ applicationID ] ) {
|
||||
|
||||
var application = this.prepareApplication( applicationID );
|
||||
|
||||
this.bindObjectsToSocket( socket, application, unifyCore, applicationID );
|
||||
|
||||
if( this.getDomElement( application ) ) {
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
unifyCore.socketManager = await socket;
|
||||
|
||||
this.hidePreloader();
|
||||
|
||||
this.createCodeEditor( application, applicationID );
|
||||
|
||||
await this.parseApplication( unifyCore, application, applicationID );
|
||||
|
||||
//this.signinPromise = this.checkSignIn( socket, unifyCore, application );
|
||||
//console.log("after signin");
|
||||
|
||||
if( applicationID == 0 ) {
|
||||
|
||||
//this.parseUrl( unifyCore, application );
|
||||
|
||||
}
|
||||
|
||||
//await Promise.all( document.promises );
|
||||
|
||||
await unifyCore.callAfterLoad( application );
|
||||
|
||||
this.logApplication( application, unifyCore );
|
||||
|
||||
if( this.loadThemes ) {
|
||||
|
||||
await this.themeLoader.loadThemes( application, applicationID, unifyCore );
|
||||
|
||||
}
|
||||
|
||||
this.setGlobals();
|
||||
|
||||
await unifyCore.callAfterThemeLoad( application );
|
||||
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
setGlobals() {
|
||||
|
||||
document.timer.initialising = false;
|
||||
|
||||
document.themeObjects = this.themeObjects;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user