/* 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 ESA Software Community License - Strong Copyleft LICENSE, as published by the ESA. See the ESA Software Community License - Strong Copyleft LICENSE, for more details. https://unifyjs.org */ import placeholder from './placeholder.js'; import objectSerializer from "./serializer.object.js"; import tools from './tools.js'; import cssManager from "../client/css.js"; class cleanManager{ css = new cssManager(); clean( sourceObject, method ) { if( tools.getEnvironment() == "Node" ) { var cleanObject = this.cleanObject( sourceObject ); } else { this.findColumns( sourceObject ); //this.preserveValues(); //console.log(sourceObject); var cleanObject = this.cleanObject( sourceObject, method ); } var name = sourceObject.getClassName(); var object = this.createOuterObject( name, cleanObject ); return object; } preserveValues() { } createOuterObject( classname, object ) { var outerObject = new placeholder(); outerObject[ classname ] = object; return outerObject; } findExtendedClass( child, extendedClass, table ) { if( !extendedClass.datatype ) { return false; } var column = tools.isUnifyColumn( extendedClass ); var columnName = tools.getClassName( extendedClass ); if( !column ) { return false; } table[ columnName ].value = child.value; } findExtendedClasses( child, parent, table ) { if( parent.type == "table" ) { return false; } if( !child.datatype ) { return false; } var extendedClasses = tools.getExtendedClassesMulti( child ); for ( var c = 0; c < extendedClasses.length; c++ ) { var extendedClass = extendedClasses[ c ]; this.findExtendedClass( child, extendedClass, table ); } } findChild( child, parent, table ) { this.findExtendedClasses( child, parent, table ); if( parent.type == "table" ) { table = parent; } this.findColumns( child, table ); } findColumns( parent, table = false ) { if( !parent.getChildren ) { return false; } var children = parent.getChildren(); for ( var i = 0; i < children.length; i++ ) { var child = children[i]; this.findChild( child, parent, table ); } } cleanColumn( sourceObject, targetObject ) { if( sourceObject.id ) { targetObject.id = tools.validateNumber( sourceObject.id ); } targetObject = objectSerializer.serializeValue( sourceObject, targetObject ); targetObject.type = "column"; } searchValues( sourceObject, targetObject ) { if( !sourceObject.getChildren ) { return false; } var children = sourceObject.getChildren(); var preserveParent = false; for ( var i = 0; i < children.length; i++ ) { var sourceChild = children[i]; if( sourceChild.type == "table" ) { continue; } var propertyName = tools.validateString( sourceChild.propertyName ); //console.log(sourceChild.getClassName(), sourceChild.getChildren(), sourceChild.value); if( sourceChild.value != "" && sourceChild.value ) { preserveParent = true; sourceChild.preserve = true; } var preserveChild = this.searchValues( sourceChild ) //targetChild if( preserveChild ) { sourceChild.preserve = true; preserveParent = true; } } return preserveParent; } preserveTree( sourceObject, targetObject ) { if( !sourceObject.getChildren ) { return false; } var children = sourceObject.getChildren(); for (var i = 0; i < children.length; i++) { var sourceChild = children[i]; var propertyName = tools.validateString( sourceChild.propertyName ); // target class is save if( propertyName == "groups") { continue; } if( propertyName == "group") { continue; } if( sourceChild.preserve && !targetObject[ propertyName ] ) { targetObject[ propertyName ] = this.cleanObject( sourceChild ); this.preserveTree( sourceChild, targetObject[ propertyName ] ) } } } cleanObject( sourceObject, method ) { if( sourceObject.scope == "private" ) { return false; } var targetObject = new placeholder(); var preserve = true; if( method == "get" && tools.getEnvironment() == "Browser" ) { preserve = false; } if( preserve ) { this.searchValues( sourceObject, targetObject); this.preserveTree( sourceObject, targetObject); } switch( sourceObject.type ) { case "column": this.cleanColumn( sourceObject, targetObject ); targetObject = objectSerializer.serializeProperties( sourceObject, targetObject, method ); break; case "table": targetObject = objectSerializer.serializeProperties( sourceObject, targetObject ); this.cleanTable( sourceObject, targetObject, method ); targetObject = objectSerializer.serializeProperties( sourceObject, targetObject, method ); break; case "renderCollection": if( tools.getEnvironment() == "Node" ) { this.cleanRenderCollection( sourceObject, targetObject ); } targetObject = objectSerializer.serializeProperties( sourceObject, targetObject, method ); break; default: this.cleanDefault( sourceObject, targetObject, method ); } if( tools.getEnvironment() == "Node" ) { this.propegateNewPropertiesOutside( sourceObject, targetObject ); } return targetObject; } cleanDefault( sourceObject, targetObject, method ) { targetObject = objectSerializer.serializeProperties( sourceObject, targetObject, method ); targetObject = this.cleanProperties( sourceObject, targetObject ); targetObject.serializationType = "object"; this.cleanChildren( sourceObject, targetObject ); } cleanColumn( sourceObject, targetObject ) { if( sourceObject.id ) { targetObject.id = tools.validateNumber( sourceObject.id ); } targetObject = objectSerializer.serializeValue( sourceObject, targetObject ); targetObject.type = "column"; } cleanChildren( sourceObject, targetObject, method ) { if( sourceObject.getChildren ) { var children = sourceObject.getChildren(); for( var c = 0; c < children.length; c++ ) { var child = children[ c ]; var propertyName = child.propertyName; //targetObject[ propertyName ] = this.cleanObject( child ); if( propertyName == "groups" ) { continue; } if( method == "get" && tools.getEnvironment() == "Browser" ) { continue; } if( child.type == "table" || child.value ) { targetObject[ propertyName ] = this.cleanObject( child ); } } } } cleanTable( sourceObject, targetObject, method ) { if( typeof sourceObject.value ) { targetObject = objectSerializer.serializeValue( sourceObject, targetObject ); } if( sourceObject.id ) { targetObject.id = tools.validateNumber( sourceObject.id ); } if( sourceObject.status ) { targetObject.status = tools.validateString( sourceObject.status ); } targetObject.type = "table"; targetObject.serializationType = "object"; //targetObject.collection = false; if( method != "get" || tools.getEnvironment() == "Node" ) { if( sourceObject.collections && sourceObject.collections.length > 0 ) { var collection = sourceObject.getCollection(); //this.cleanCollection( sourceObject, targetObject ) } this.cleanChildren( sourceObject, targetObject, method ); } } serializeCollection( sourceCollection, targetObject ) { var targetCollection = new placeholder(); targetObject.collections = new Array(); targetCollection.id = sourceCollection.id; targetObject.collections.push( targetCollection ); } cleanRenderCollection( sourceObject, targetObject ) { if( sourceObject.id ) { targetObject.id = tools.validateNumber( sourceObject.id ); } targetObject = objectSerializer.serializeValue( sourceObject, targetObject ); this.cleanRenderCollectionRows( sourceObject, targetObject ); //targetObject = this.cleanCollection( sourceObject, targetObject ); if( tools.getEnvironment() == "Browser" ) { //this.propegateNewProperties( sourceObject, targetObject ); } targetObject.type = "renderCollection"; targetObject.useCache = sourceObject.useCache; targetObject.performCacheUpdate = sourceObject.performCacheUpdate; } cleanRenderCollectionRows( sourceObject, targetObject ) { if( sourceObject.rows ) { var rows = sourceObject.rows; targetObject.rows = new Array(); for( var c = 0; c < rows.length; c++ ) { var row = rows[c]; if( row.type == "table" ) { var rowObject = this.cleanObject( row ); } targetObject.rows.push( rowObject ); } } return targetObject; } cleanProperties( sourceObject, targetObject ) { const allowedProperties = new Array( "permissionObjects", "childrenPermissions" ); for ( var i = 0; i < allowedProperties.length; i++ ) { var propertieName = allowedProperties[i]; if( sourceObject[ propertieName ] ) { targetObject[ propertieName ] = sourceObject[ propertieName ]; } } return targetObject; } cleanCollection( sourceObject, targetObject ) { if( sourceObject.getCollection && sourceObject.getCollection() ) { var collection = sourceObject.getCollection(); targetObject.collection = new placeholder(); targetObject.collection.objectName = tools.getClassName( new collection.object() ).split("$")[0]; targetObject.collection.propertyName = collection.propertyName; if( collection.parent ) { targetObject.collection.parentID = collection.parent.id; targetObject.collection.parentName = tools.getClassName( tools.getTableFromObject( collection.parent ) ); } } return targetObject; } propegateRenderCollection( sourceObject, targetObject, newProperies ) { var properties = sourceObject.getProperties(); for (var i = 0; i < properties.length; i++) { var property = properties[i]; this.propegateNewProperty( property, targetObject, newProperies ); } } propegateBrowserProperty( targetObject, property ) { if( typeof property.value == "string" ) { targetObject[ property.name ] = property.value; } } propegateBrowserProperties( sourceObject, targetObject ) { if( tools.getEnvironment() == "Node" ) { return false; } var properties = tools.getProperties( sourceObject ); for (var i = 0; i < properties.length; i++) { var property = properties[i]; this.propegateBrowserProperty( targetObject, property ); } } propegateProperty( targetObject, property ) { const preserved = new Array( "placeholder", "load", "permissions", "collections", "__sourcePath", "__sourcePath", "rowID", "tableName", "datatype", "_value", "__value", "__sourcePath", "synced", "permissionManager", "filterObject", "parent", "__nodeMethods", "__className", "join_id", "serializationType", "value", "clearOnSync","type","propertyName","showValue","propegateEvent","enabled","renderToDOM","isActive","parse","parseChildren","parseTable","layers", 'useCustomElement', 'editable', 'defaultDisplayType', 'scrollbarWidth', 'boxFlex', 'debug', "defaultBoxDisplayType"); if( !property.value ) { return false; } if( typeof property.name != "string" ) { return false; } if( this.css.propertyIsStyle( property.name.replace("__", "") ) ) { return false; } if( targetObject[ property.name ] ) { return false; } if( preserved.includes( property.name ) ) { return false; } targetObject[ property.name ] = property.value; } propegateNewPropertiesOutside( sourceObject, targetObject ) { if( sourceObject.permissions ) { targetObject.permissions = objectSerializer.serializePermission( sourceObject.permissions ); } if( sourceObject.permissionObjects ) { targetObject.permissionObjects = sourceObject.permissionObjects; } if( sourceObject.data ) { targetObject.data = sourceObject.data; } if( sourceObject.type == "renderCollection" ) { return false; } var properties = tools.getProperties( sourceObject ); for (var i = 0; i < properties.length; i++) { var property = properties[i]; this.propegateProperty( targetObject, property ); } } propegateNewProperties( sourceObject, targetObject ) { var newProperies = new Array(); if( sourceObject.getProperties ) { if( sourceObject.type == "renderCollection" ) { this.propegateRenderCollection( sourceObject, targetObject, newProperies ); } } else { this.propegateBrowserProperties( sourceObject, targetObject ); } return newProperies; } propegateNewProperty( property, targetObject, newProperies ) { this.propegateBoxProperty( property ); this.propegateValue( property, targetObject, newProperies ); } propegateBoxProperty( property ) { const preserved = new Array("serializationType", "value", "clearOnSync","type","propertyName","showValue","propegateEvent","enabled","renderToDOM","isActive","parse","parseChildren","parseTable","layers", 'useCustomElement', 'editable', 'defaultDisplayType', 'scrollbarWidth', 'boxFlex', 'debug', "defaultBoxDisplayType"); if( !property.name.toLowerCase().includes("box") ) { return false; } if( preserved.includes( property.name.replace( "box", "" ) ) ) { return false; } property.name = tools.firstLowerCase( property.name.replace( "box", "" ) ); } propegateValue( property, targetObject, newProperies ) { const preserved = new Array( "serializationType", "value", "clearOnSync","type","propertyName","showValue","propegateEvent","enabled","renderToDOM","isActive","parse","parseChildren","parseTable","layers", 'useCustomElement', 'editable', 'defaultDisplayType', 'scrollbarWidth', 'boxFlex', 'debug', "defaultBoxDisplayType" ); if( typeof property.name != "string" ) { return false; } if( this.css.propertyIsStyle( property.name.replace("box", "") ) ) { return false; } if( targetObject[ property.name ] ) { return false; } if( !tools.validateValue( property.value ) ) { return false; } if( preserved.includes( property.name ) ) { return false; } newProperies.push( property ); targetObject[ property.name ] = property.value; } } export default new cleanManager();