/* 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 tools from '../unify/tools.js'; import baseRenderCollection from '../server/renderCollection.js'; import shared from '../unify/shared.js'; import defaultObject from '../unify/defaultObject.js'; import collection from '../unify/collection.js'; import document from '../unify/document.js'; import userManager from '../server/userManager.js'; import searchable from '../unify/sql/searchable.js'; import unify from '../unify/unify.js'; import placeholder from '../unify/placeholder.js'; export default class renderCollection extends baseRenderCollection { object; collections = new Array(); collection = false; // Future storageCollection = false; type = "renderCollection"; rows = new Array(); filterObject = false; processAsync = false; handleRenderCollection( renderCollection ) { var currentCollection = renderCollection.getCollection(); if( !renderCollection.storageCollection ) { renderCollection.storageCollection = new collection( currentCollection.object ); renderCollection.storageCollection.enabled = false; } collection1 = renderCollection.storageCollection; } handleCollection( argument ) { this.storageCollection = new collection( argument.object ); this.storageCollection.name = "storageCollection"; var defaultObjectInstance = new defaultObject(); defaultObjectInstance.exposeMethodsToObject( this.storageCollection ); this.setCollection( argument ); } handleSecondArgument( argument ) { switch( argument.type ) { case "renderCollection": this.handleRenderCollection( argument ); break; case "collection": this.handleCollection( argument ); break; } } constructor( object, secondArgument ) { super(); if( secondArgument ) { this.handleSecondArgument( secondArgument ); } else { if( object ) { //return false; //this.logMissingArgument(); } } this.object = object; } logMissingArgument() { console.log("\n_______________________________________________\n\n\n\n"); console.error( '\x1b[31m%s\x1b[0m', 'Collection of renderCollection Not set:\n ' ); console.log("This renderCollection will not load anything, There is probably an typo in the second argument or no collection present as second argument. \n"); console.log( this ) console.log("\n_______________________________________________\n\n\n\n"); } createInstance() { var object = new this.object(); unify.extend( object ); return object; } setupObject( sourceObject, client, rowID ) { var object = this.createInstance(); // todo : caution : this wasn't commented and can cause problems, But this can probably be removed because of multi extend. //object.extend( this.getCollection().createInstance() ); object.id = sourceObject.id; object.parent = this; object.dynamic = true; object.propertyName = object.getClassName() + object.id; object.load = false; object.rowID = rowID; object.serialize( sourceObject, client ); return object; } callMethods( object, client ) { if( client ) { if( object.process ) { object.process( object ); } } } bindAsChild( object ) { this[ object.propertyName ] = object; } updatePermissionsClientAndServer( object, client ) { if( client ) { this.getPermissionForHierarchy( object, client.user ); } else { object.updatePermissions( object.permissions ); } return object; } addToCollection( object ) { var collection = this.getCollection(); collection.rows.push( object ); } addRow( sourceObject, client, rowID ) { var object = this.setupObject( sourceObject, client, rowID ); this.callMethods( object, client ) this.bindAsChild( object ); this.getCore().parse( object, client ); this.updatePermissionsClientAndServer( object, client ); this.addToCollection( object ); return object; } getPermissionForHierarchy( object, user ) { object.permissions = userManager.computePermissions( object, user ); var children = object.getChildren(); for (var i = 0; i < children.length; i++) { var child = children[i]; if( child.isAllowed ) { this.getPermissionForHierarchy( child, user ) } } } async addRowSync( sourceObject, client, rowID ) { var object = this.setupObject( sourceObject, client, rowID ); this.callMethods( object, client ) this.bindAsChild( object ); if( client ) { await this.getCore().parse( object, client ); } else { await this.getCore().parse( object, true ); } this.updatePermissionsClientAndServer( object, client ); this.addToCollection( object ); return object; } setCollection( collection ) { if( !collection ) { this.collections = new Array(); } else { this.collections = [ collection ]; } } setCollectionObject( collectionObject ) { var newCollection = new collection( collectionObject ); this.setCollection( newCollection ); this.object = collectionObject; } getCollection() { return this.collections[0]; } createFilterObject() { if( !this.filterObject ) { this.filterObject = new placeholder(); } this.filterObject.__className = "placeholder"; unify.extend( this.filterObject ); } createCollectionSearchable( collection, child ) { var tableName = collection.createInstance().getClassName(); //tableName var search = new searchable( "./" + collection.propertyName + "/" + child.propertyName ); this.filterObject[ collection.propertyName ][ child.propertyName ] = search; } handleFilterCollectionChildren( collectionObject, collection ) { var collectionChildren = collectionObject.getChildren(); for( var b = 0; b < collectionChildren.length; b++ ) { var child = collectionChildren[b]; this.createCollectionSearchable( collection, child ); } } handleCollectionSearchables( child ) { if( child.type == "collection") { var collectionObject = child.createInstance(); unify.extend( collectionObject ); this.filterObject[ child.propertyName ].tableName = collectionObject.getClassName() this.filterObject[ child.propertyName ].type = "collection"; this.handleFilterCollectionChildren( collectionObject, child ) } } createSearchable( child, parent ) { //console.log("get news from this", parent); var search = new searchable( parent.getClassName() + "/" + child.propertyName ); this.filterObject[ child.propertyName ] = search; } createSearchables( object ) { var children = object.getChildren(); for( var c = 0; c < children.length; c++ ) { var child = children[c]; this.createSearchable( child, object ); this.handleCollectionSearchables( child ); } } createSearchableID() { var search = new searchable( "./id" ); this.filterObject[ "id" ] = search; } getCollectionObject() { var collection = this.getCollection(); var realObject = new collection.object(); unify.extend( realObject ); return realObject; } getFilter() { /* var collection = this.getCollection(); return collection.getFilter(); */ this.createFilterObject(); this.createSearchableID(); var object = this.getCollectionObject(); this.createSearchables( object ); //console.log("this.filterObject", this.filterObject); return this.filterObject; } createInstance() { return new this.object(); } getChildrenRenderCollections2( object, client ) { var children = object.getChildren( ); for( var c = 0; c < children.length; c++ ) { var child = children[ c ]; if( child.type == "renderCollection" ) { child.id = object.id; child = this.get( child, client ); } } } getChildrenRenderCollections( client ) { var rows = this.rows; for( var b = 0; b < rows.length; b++ ) { var row = rows[b]; this.getChildrenRenderCollections2( row, client ); } } }