/* 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 tools from '../unify/tools.js'; import defaultObject from '../unify/defaultObject.js'; import omitMethods from "../unify/omitMethodNames.js"; import unify from '../unify/unify.js'; //import cacheString from '../../assets/cache/cache.js' var cache; async function loadCache() { var randomInt = Math.floor( Math.random() * 100000 ); var cacheString = await import('../../assets/cache/cache.js?random=' +randomInt ); cache = JSON.parse( cacheString.default ); } loadCache(); export default class renderCollection{ __className = "renderCollection"; methodCache = new Array(); //create() { // this.fetch(); //} synced = false; clearOnSync = true; cache = false; cacheLastModified = false; useCache = false; async updateCache() { var randomInt = Math.floor( Math.random() * 100000 ); var cacheString = await import('../../assets/cache/cache.js?random=' +randomInt ); cache = JSON.parse( cacheString.default ); } clearRenderCollection() { var collection = this.getCollection(); if( collection ) { collection.clear(); } var children = this.getChildren(); for( var c = 0; c < children.length; c++ ) { var child = children[ c ]; child.remove(); } this.defaultElement.innerHTML = ""; } async sync() { await this.fetch(); this.getCore().createDependencyMap(); return true; } hasURL() { var hasUrl = false if( this.parent.type != "table" ) { hasUrl = true; } if( this.parent.type == "renderCollection" ) { hasUrl = false; } if( this.title && this.title.value == "" ) { hasUrl = false; } if( !this.title ) { hasUrl = false; } return hasUrl; } composePath() { var urlPath = new Array(); urlPath.push( this.propertyName ); if( this.id ) { urlPath.push( this.id ); } if( this.title ) { if( this.title.value == "" ) { urlPath.push( this.propertyName.replaceAll(" ", "_") + ".html" ); } else { urlPath.push( this.title.value.replaceAll(" ", "_") + ".html" ); } } else { urlPath.push( this.propertyName.replaceAll(" ", "_") + ".html" ); } return urlPath.join("/"); } async createUrl( pass ) { var hasURL = this.hasURL(); if( hasURL || pass ) { var path = this.composePath(); var state = new Object(); state.id = this.id; state.applicationPath = this.applicationPath; var url = window.location; await history.pushState( state, "Unify", "/" + path ); } } cleanChildren( object ) { if( this.rows ) { this.rows = new Array(); } if( object.getChildren ) { var children = object.getChildren(); for (var i = 0; i < children.length; i++) { var child = children[i]; if( child.rowID ) { console.log("delete", child.propertyName); delete this[ child.propertyName ]; } this.cleanChildren( child ); } } } logRenderCollection() { if( this.debug || document.debugALL ) { console.log( "" ); console.log("%c renderCollection::fetch", "background: #a6e22e;", this.getClassName() ); console.log( this ); } } async recreateCache() { var cacheDateTime = await this.core.socketManager.get( "settings", "recreateCache", this, "recreateCache", false ); this.core.socketManager.get( "table", "get", this, "fetch", false ); } async fetch( ) { this.logRenderCollection(); this.rows = new Array(); if( this.core ) { //var result = await this.core.socketManager.get( "table", "get", this, "fetch" ); if( this.useCache && !this.id ) { var cacheDateTime = await this.core.socketManager.get( "settings", "getCacheDateTime", this, "fetch", false ); if( !this.cacheLastModified ) { this.cacheLastModified = cacheDateTime; } if( this.cacheLastModified != cacheDateTime ) { console.log("cache has been modified, reloading cache."); await this.updateCache(); this.cacheLastModified = cacheDateTime; } else { console.log("Use old cache because cache has not been modified."); } var currentCache = cache[ this.getClassName() ]; if( currentCache ) { this.core.socketManager.get( "table", "get", this, "fetch", false ); result = currentCache; } else { console.log("dont use cache, await fetch"); var result = await this.core.socketManager.get( "table", "get", this, "fetch" ); } } else { var result = await this.core.socketManager.get( "table", "get", this, "fetch" ); } this.synced = true; if( this.clearOnSync ) { this.empty(); console.log("empty renderCollection"); this.rows = new Array(); } await this.parseRows( result.rows ); this.getCore().communicateVisibleElements(); } return true; } remapCurrentMethod( object ) { var className = object.__className; var methods = this.methodCache[ className ]; if( methods ){ const keys = Object.keys(methods); for (var i = 0; i < keys.length; i++) { var methodName = keys[i] object[ methodName ] = this.methodCache[className][methodName]; } } } remapMethods( object ) { unify.extend( object, true ); this.remapCurrentMethod( object ); var children = object.getChildren(); for( var c = 0; c < children.length; c++ ) { var child = children[c]; this.remapMethods( child ); } } deleteProperties() { if( object.elements ) { for (var i = 0; i < object.elements.length; i++) { delete object.elements[i]; } } delete object.element; delete object.customElement; delete object.boxElement; delete object.defaultElement; delete object.user; delete object.fileLoader; } copyMethods() { var properties = Object.getOwnPropertyNames( object ) for (var j = 0; j < properties.length; j++) { var methodName = properties[ j ] if( typeof object[ methodName ] == "function" ) { this.methodCache[ className ][ methodName ] = object[ methodName ]; object[ methodName ] = false; } } var methods = tools.getAllFuncs( object ); for (var i = 0; i < methods.length; i++) { methodName = methods[ i ]; this.methodCache[ className ][ methodName ] = object[ methodName ]; } } removeDOM( object ) { var className = object.getClassName(); if( !this.methodCache[className] ) { this.methodCache[ className ] = new Array(); } this.deleteProperties(); this.copyMethods(); var children = object.getChildren(); for( var c = 0; c < children.length; c++ ) { var child = children[c]; this.removeDOM( child ); } } async parseRows( rows ) { var promises = new Array(); if( !rows || !Array.isArray(rows) ) { console.log("rows doesnt contain an array.", rows, this); return false; } for( var c = 0; c < rows.length; c++ ) { var object = rows[ c ]; object = this.addRowSync( object, false, c ); this.rows.push( object ); } await Promise.all( promises ); return true; } serializeRenderCollection( object ) { if( object.rows && tools.isArray( object.rows ) ) { var rows = object.rows; var rows = new Array(); for( var c = 0; c < rows.length; c++ ) { var object = rows[ c ]; var object = targetObject.addRow( object, false, c ); rows.push( object ); } } } }