/* 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 defaultObject from '../unify/defaultObject.js'; import tools from '../unify/tools.js'; import core from './core.js'; import themeImports from './imports/themeImports.js'; export default class themeLoader{ defaultObject = new defaultObject(); prepareThemeCore( themeCore, socketManager, os, device, tint ) { themeCore.addDOMToSelector = false; themeCore.parseEvents = false; themeCore.executeMethods = false; themeCore.initializeRenderLoop = false; themeCore.socketManager = socketManager; themeCore.os = os; themeCore.device = device; themeCore.tint = tint; if( os == this.os && device == this.device && tint == this.tint ) { themeCore.createStyleSheet = false; } } async loadCustomThemes( os, device, tint ) { var customThemes = new Array(); var applications = this.applicationManager.applications; for( var applicationID = 0; applicationID < applications.length; applicationID++ ) { var applicationFile = applications[ applicationID ]; //console.log(applicationFile.path, applicationID, os, device, tint); customThemes[ applicationID ] = await this.loadCustomTheme( applicationFile.path, applicationID, os, device, tint ); } return customThemes; } async createObject( applicationPath ) { if( document.mode == "production" ) { var importObject = themeImports[ applicationPath ]; return new importObject(); } else { var importObject = await import( "/" + applicationPath ); return new importObject.default(); } } createApplicationPath( path, os, device, tint ) { device = tools.CamelCase( device ); os = tools.CamelCase( os ); tint = tools.CamelCase( tint ); var applicationPath = 'framework/cache/platforms/' + os + '/' + device + '/' + tint + '/' + path + '/application.js'; return applicationPath; } async loadCustomTheme( path, applicationID, os, device, tint ) { //console.log(path, applicationID, os, device, tint); var applicationPath = this.createApplicationPath( path, os, device, tint ); //console.log( "load custom theme from url", applicationPath ); return await this.createObject( applicationPath ); } setThemeObjects( themeCore, os, device, tint ) { var applicationManager = this.applicationManager; if( !applicationManager.themeObjects ) { applicationManager.themeObjects = new Array(); } if( !applicationManager.themeObjects[os] ) { applicationManager.themeObjects[os] = new Array(); } if( !applicationManager.themeObjects[os][device] ) { applicationManager.themeObjects[os][device] = new Array(); } applicationManager.themeObjects[os][device][tint] = themeCore.classObjects; } async setupApplication( unifyCore, themeCore, application ) { this.defaultObject.agregateDefaultObject( application ); this.applicationManager.getDomElement( application ) themeCore.setTheme(); application.core = themeCore; await unifyCore.prepareObjects( application ); themeCore.parse( application, true ); themeCore.callAfterLoad( application ); } async parseTheme( unifyCore, os, device, tint ) { var themeApplications = await this.loadCustomThemes( os, device, tint ); var themeCore = new core(); var application = themeApplications[0]; this.prepareThemeCore( themeCore, unifyCore.socketManager, os, device, tint ); await this.setupApplication( unifyCore, themeCore, application ); console.log("setThemeObjects", themeCore, os, device, tint); this.setThemeObjects( themeCore, os, device, tint ) } async loadThemes( application, applicationID, core ) { if( application.loadThemes && applicationID == 0 ) { var osList = new Array("Windows", "Macos", "Android"); var deviceList = new Array("Pc"); var tintList = new Array("Light", "Dark"); for ( var i = 0; i < osList.length; i++ ) { var os = osList[i]; for ( var j = 0; j < deviceList.length; j++ ) { var device = deviceList[j]; for ( var k = 0; k < tintList.length; k++ ) { var tint = tintList[k]; await this.parseTheme( core, os, device, tint ); } } } } } }