/* 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 './tools.js'; import serverCollection from '../server/collection/collection.js'; import defaultObject from './defaultObject.js'; import unify from './unify.js'; export default class collection extends serverCollection { __className = "collection"; type = "collection"; rows = new Array(); filterObject = false; object; tableName; parentName; constructor( object ){ super(); if( object ) { this.object = object; } unify.extend( this ) } createInstance() { return new this.object(); } getParentName() { var parentTable = this.parent; //console.log("get table from this", this.parent); if( !tools.objectIsTable( this.parent ) ) { parentTable = tools.getTableFromObject( this.parent ); } var parentClassName = tools.getClassName( parentTable ); // todo: not good but works -> userB.friends.remove( userA ); selectuser.checkbox.js doesnt // work if this is disabled. userB is a collection but doesnt get an parentName // with this function if( !parentClassName ) { parentClassName = this.tableName; } return parentClassName; } getTableName() { var tableInstance = new this.object(); return tools.getClassName( tableInstance ); } getRight( update = true ) { if( update ) { this.update(); } var tableName = this.tableName; if( tableName == this.parentName ) { tableName += 0; } return tableName; } getLeft( update = true ) { if( update ) { this.update(); } if( this.parentName ) { var parentName = this.parentName; } else { var parentName = this.getParentName(); } if( this.tableName == parentName ) { parentName += 1; } return parentName; } update() { if( !this.enabled) { this.tableName = this.getTableName(); if( this.parent ) { this.parentName = this.getParentName(); } } } getColumnName() { var parentClassName = this.getParentName(); return parentClassName + "_" + this.propertyName + "_id"; } set( objects ){ this.rows = objects; } addObject( object ) { this.rows.push( object ); } filter( by, term ) { switch( by.toLowerCase() ) { case "custom": this.filterCustom( term ); break; case "class": this.filterByClassName( term ); break; case "name": this.filterByName( term ); break; case "id": this.filterByID( term ); break; case "parent": this.filterByParent( term ); break; case "parentName": this.filterByParentName( term ); break; case "type": this.filterByType( term ); break; } } filterCustom( func ) { var objects = this.rows; this.rows = new Array(); for( var c = 0; c < objects.length; c++ ) { var object = objects[c]; if( func( object ) ) { this.rows.push(object); } } } filterByType( type ) { var objects = this.rows; this.rows = new Array(); for( var c = 0; c < objects.length; c++ ) { var object = objects[c]; if( object.type == type ) { this.rows.push(object); } } } filterByCollection( collection_a ) { this.rows = new Array(); var objects = this.rows; for( var c = 0; c < objects.length; c++ ) { var renderCollection = objects[c]; var collection_b = renderCollection.getCollection(); if( collection_b.propertyName == collection_a.propertyName ) { this.rows.push( object ); } } } filterByClassName( className ) { var objects = this.rows; this.rows = new Array(); for( var c = 0; c < objects.length; c++ ) { var object = objects[c]; if( tools.getClassName( object ) == className ) { this.rows.push( object ); } } } filterByName( name ) { var objects = this.rows; this.rows = new Array(); for( var c = 0; c < objects.length; c++ ) { var object = objects[c]; if( object.propertyName == name ) { this.rows.push( object ); } } } filterByID( id ) { var objects = this.rows; this.rows = new Array(); for( var c = 0; c < objects.length; c++ ) { var object = objects[c]; if( object.id == id ) { this.rows.push( object ); } } } filterByParent( parent ) { this.filterByParentName( tools.getClassName( parent ) ); } filterByParentName( parentName ) { var objects = this.rows; this.rows = new Array(); for( var c = 0; c < objects.length; c++ ) { var object = objects[c]; if( tools.getClassName( object.parent ) == parentName ) { this.rows.push( object ); } } } getFirstRow() { return this.rows[0]; } }