/* 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 emptyObject from './emptyObject.js'; import purify from '../unify/purify.js'; import tools from '../unify/tools.js'; import shared from '../unify/shared.js'; import document from '../unify/document.js'; import Console from '../server/console.js'; import serialize from '../unify/serializer.js'; import queryManager from '../client/queryManager.js'; import userManager from '../server/userManager.js'; var globalTabIndex = 0; var queryManagerInstance = new queryManager(); class defaultObject{ id = 1; extend( object ) { if( this.exposeMethodsToObject( object ) ) { return true; } else { return false; } } exposeMethodsToObject( object, force = false ) { if( !force ) { if( object.constructor.name == "Object" && !object.tag ) { return false; } } // helper Methods if( object.__proto__ ) { object.__proto__.exposeMethodsToObject = this.exposeMethodsToObject; object.__proto__.agregateDefaultObject = this.agregateDefaultObject; // get object.__proto__.getClassName = this.getClassName; object.__proto__.getID = this.getID; object.__proto__.getFirstID = this.getFirstID; object.__proto__.getApplicationPath = this.getApplicationPath; object.__proto__.getChildren = this.getChildren; object.__proto__.getProperties = this.getProperties; object.__proto__.getParentWithTable = this.getParentWithTable; object.__proto__.getRoot = this.getRoot; object.__proto__.getCore = this.getCore; object.__proto__.getExtendedClass = this.getExtendedClass; object.__proto__.getExtendedClassName = this.getExtendedClassName; object.__proto__.parents = this.parents; object.__proto__.getAll = this.getAll; object.__proto__.getObjectsByClassName = this.getObjectsByClassName; //object.__proto__.get = this.get; // is object.__proto__.isTable = this.isTable; object.__proto__.extendsTable = this.extendsTable; object.__proto__.isRenderCollection = this.isRenderCollection; object.__proto__.extendsClass = this.extendsClass; object.__proto__.extend = this.extend; // set object.__proto__.setApplicationPath = this.setApplicationPath; object.__proto__.getApplicationPathString = this.getApplicationPathString; object.__proto__.setID = this.setID; object.__proto__.setApplicationID = this.setApplicationID; object.__proto__.setParent = this.setParent; object.__proto__.setUser = this.setUser; object.__proto__.setUserChildren = this.setUserChildren; object.__proto__.isHidden = this.isHidden; object.__proto__.syncAllChildren = this.syncAllChildren; // Actions if(object.type != "collection") { object.__proto__.prepend = this.prepend; object.__proto__.add = this.add; object.__proto__.remove = this.remove; } object.__proto__.serialize = this.serialize; object.__proto__.clean = this.clean; object.__proto__.empty = this.empty; object.__proto__.hide = this.hide; object.__proto__.show = this.show; object.__proto__.hideChildren = this.hideChildren; object.__proto__.showChildren = this.showChildren; object.__proto__.processGridElement = this.processGridElement; object.__proto__.allow = this.allow; object.__proto__.isAllowed = this.isAllowed; object.__proto__.updatePermissions = this.updatePermissions; object.__proto__.log = this.log; object.__proto__.updateElementContent = this.updateElementContent; object.__proto__.computePermissions = this.computePermissions; object.__proto__.showParents = this.showParents; object.__proto__.simpleClone = this.simpleClone; object.__proto__.updateProperties = this.updateProperties; object.__proto__.exposeMethodsToObject = this.exposeMethodsToObject; object.__proto__.getExtends = this.getExtends; object.__proto__.getExtendObjects = this.getExtendObjects; object.__proto__.isVisible = this.isVisible; object.__proto__.preloadImage = this.preloadImage; object.__proto__.getPreviousSibling = this.getPreviousSibling; object.__proto__.getNextSibling = this.getNextSibling; object.__proto__.query = function( query ) { var result = queryManagerInstance.query( query, this ); if( result[0] ) { return result[0]; } else { return false; } }; object.__proto__.queryAll = function( query ) { var result = queryManagerInstance.query( query, this ); if( result ) { return result; } else { return false; } }; this.overideRemainingMethods( object ); } return true; } findAll( className, object = this, found = new Array() ) { if( !object.getChildren ) { return false; } var children = object.getChildren(); for (var i = 0; i < children.length; i++) { var child = children[i]; if( !child.getClassName ) { continue; } var childClassName = child.getClassName(); if( childClassName == className ) { found.push( child ); } this.findAll( className, child, found ); } return found; } getByID( id, object = this, found = new Array() ) { if( !object.getChildren ) { return false; } var children = object.getChildren(); for (var i = 0; i < children.length; i++) { var child = children[i]; var childID = child.id; if( childID == id ) { found.push( child ); } this.getByID( id, child, found ); } return found; } getByPropertyName( propertyName, object = this, found = new Array() ) { if( !object.getChildren ) { return false; } var children = object.getChildren(); for (var i = 0; i < children.length; i++) { var child = children[i]; var childPropertyName = child.propertyName; if( childPropertyName == propertyName ) { found.push( child ); } this.getByPropertyName( propertyName, child, found ); } return found; } getChildByPropertyName( propertyName, object = this ) { var found = new Array(); var children = object.getChildren(); for (var i = 0; i < children.length; i++) { var child = children[i]; var childPropertyName = child.propertyName; if( childPropertyName == propertyName ) { found.push( child ); } } return found; } getChildById( id, object = this ) { var found = new Array(); var children = object.getChildren(); for (var i = 0; i < children.length; i++) { var child = children[i]; var childID = child.id; if( childID == id ) { found.push( child ); } } return found; } find( className ) { var results = this.findAll( className ); if( results[0] ) { return results[0]; } else { return false; } } overideRemainingMethod( object, method ) { const ommit = new Array( '', 'setJoinID', 'reset', 'isOrExtendsClass', 'hasClass', 'getTableName', 'delete', 'appendToSelector', 'agregateDefaultObject', '__defineGetter__', '__defineSetter__', '__lookupGetter__', '__lookupSetter__', 'valueOf', 'constructor', 'toLocaleString', 'toString' ); if( ommit.includes( method ) ) { return false; } if( !object.__proto__[ method ] ) { object.__proto__[ method ] = this[ method ]; } } overideRemainingMethods( object ) { if( this.constructor.name != "defaultObject" ) { return false; } const methods = tools.getAllFuncs( this ); for ( var i = 0; i < methods.length; i++ ) { var method = methods[i]; this.overideRemainingMethod( object, method ); } } propertyIsValid( property ) { const omit = new Array( "datatype", "validateType", "appendToSelector", "gridArea", "order", "customElement", "__sourcePath", "applicationID", "selector", "appendToSelector", "gridArea", "order", "display" ); if( property.name.slice(0, 2) == "__" ) { return false; } if( omit.includes( property.name ) ) { return false; } if( !tools.isCSSProperty( property.name ) ) { return false; } return true; } updateValidCssProperty( property ) { var core = this.getCore(); if( !this[ property.name ] ) { core.css.parseProperty( property, this ); } console.log("--", "update css property", property.name, property.value); this[ property.name ] = property.value; } updateCssProperty( property ) { if( !property.value ) { return false; } if( this.propertyIsValid( property ) ) { this.updateValidCssProperty( property ); } else { // console.log( "other properties?", property.name ); } } updateCssProperties( sourceProperties ) { for ( var i = 0; i < sourceProperties.length; i++ ) { var property = sourceProperties[i]; this.updateCssProperty( property ); } } updatePropertyText( property ) { if( property.name == "text" && property.value ) { //console.log("change text", property.value); this.label = property.value; } } getPreviousSibling() { var id = this.id; var children = this.parent.getChildren(); for (var i = 0; i < children.length; i++) { var child = children[i]; if( child.id == id - 1 ) { return child; } } } getNextSibling() { var id = this.id; var children = this.parent.getChildren(); for (var i = 0; i < children.length; i++) { var child = children[i]; if( child.id == id + 1 ) { return child; } } } updateEmptyCssProperty( property, sourceProperties ) { var omit = ["applicationID", "selector", "appendToSelector", "gridArea", "order", "display"]; let obj = sourceProperties.find( o => o.name === property.name ); if( obj ) { return false; } if( this.propertyIsValid( property ) ) { //console.log("property not found!!", property); this[ property.name ] = "none"; } } updateEmptyCssProperties( sourceProperties ) { var properties = this.getProperties() for ( var i = 0; i < properties.length; i++ ) { var property = properties[i]; this.updatePropertyText( property ); this.updateEmptyCssProperty( property, sourceProperties ); } } getAllDefinedMethods( fromObject ) { var allMethods = tools.getAllFuncs( fromObject ); var emptyInstance = new emptyObject(); this.exposeMethodsToObject( emptyInstance ); var basicMethods = tools.getAllFuncs( emptyInstance ); var methods = allMethods.filter( x => !basicMethods.includes( x ) ); return methods; } overideMethod( sourceObject, methodName, nodeMethods ) { if( !nodeMethods.includes( methodName ) ) { this[ methodName ] = sourceObject[ methodName ]; } } overrideMethods( sourceObject ) { var methods = this.getAllDefinedMethods( sourceObject ); var nodeMethods = this.__nodeMethods.split(","); for ( var i = 0; i < methods.length; i++ ) { var methodName = methods[i]; this.overideMethod( sourceObject, methodName, nodeMethods ); } } addNewMethods() { // Todo /* if( this.getClassName() == fromObject.getClassName() ) { // add new children var fromChildren = fromObject.getChildren(); var children = this.getChildren(); for (var i = 0; i < fromChildren.length; i++) { var child = fromChildren[i]; let obj = children.find(o => o.propertyName === child.propertyName); if( !obj ) { child.parent = this; this[child.propertyName] = child; core.parse( child ); } } // remove children for (var i = 0; i < children.length; i++) { var child = children[i]; let obj = fromChildren.find(o => o.propertyName === child.propertyName); if( !obj ) { this[child.propertyName].remove(); } } } */ } updateProperties( sourceObject ) { var sourceProperties = sourceObject.getProperties(); this.updateCssProperties( sourceProperties ); this.updateEmptyCssProperties( sourceProperties ); this.overrideMethods( sourceObject ); // this.addNewMethods(); } simpleClone( object ) { //let clone = Object.assign(Object.create(Object.getPrototypeOf(this)), this); var clone = new this.constructor(); this.exposeMethodsToObject( clone ) //console.log("clone", clone) clone.id = this.id; return clone; } appendToSelector() { var selectedElement = document.querySelector( object.selector ); if( selectedElement ) { var children = selectedElement.childNodes; if( children.length == 0 ) { selectedElement.appendChild( object.elements[0] ); } } } agregateDefaultObject( object ){ if( object.constructor.name != "Object" ) { this.exposeMethodsToObject( object ); var children = object.getChildren(); for( var c = 0; c < children.length; c++ ) { var child = children[ c ]; this.agregateDefaultObject( child ); } } } getObjectsByClassName( className ) { return this.getCore().getObjectsByClassName( className ); } isHidden() { function isVisible( e ) { return !!( e.offsetWidth || e.offsetHeight || e.getClientRects().length ); } if( this.customElement ) { return isVisible( this.customElement ); } } getAll( className ) { return this.getCore().getObjectsByClassName( className ); } getElement() { if( this.element ) { var element = this.element; } if( this.innerElement ) { var element = this.innerElement; } return element; } updateElementWithLabel( element ) { if( typeof this.label != "string" ) { return false; } if( this.label == "" ) { return false; } if( this.sanitize ) { element.innerHTML = purify.sanitize( this.label ); } else { element.innerHTML = this.label; } } updateInputWithValue( element ) { if( element.type == "file" ) { return false; } if( this.sanitize ) { element.value = purify.sanitize( this.value ); } else { element.value = this.value; } } updateInnerHTMLWithValue( element ) { if( this.sanitize ) { element.innerHTML = purify.sanitize( this.value ); } else { element.innerHTML = this.value; } } updateElementWithValue( element ) { if( typeof this.value != "string" ) { return false; } if( !element.tagName ) { return false; } switch( element.tagName.toLowerCase() ) { case "input": this.updateInputWithValue( element ); break; default: this.updateInnerHTMLWithValue( element ); } } updateElementWithText( element ) { if( typeof this.text != "string" ) { return false; } if( this.text == "" ) { return false; } if( this.sanitize ) { element.innerHTML = purify.sanitize( this.__text.toString() ); } else { element.innerHTML = this.__text.toString(); } } updateElementTries( element ) { if( !element ) { return false; } if( !this.showValue ) { return false; } this.updateElementWithLabel( element ); this.updateElementWithValue( element ); this.updateElementWithText( element ); } updateElementContent() { if( this.type == "file" ) { return true; } if( !this.preventUpdating && this.getElement ) { var element = this.getElement(); this.updateElementTries( element ); } if( this.update ) { this.update(); } this.processGridElement(); } setParent( parent ) { this.parent = parent; } updatePermissions( permissions ) { if( permissions ) { this.permissions = permissions; } else { var permissions = this.permissions; } if( permissions.WRITE ) { if( this.enableWRITE ) { this.enableWRITE(); } } else { if( this.disableWRITE ) { this.disableWRITE(); } } if( permissions.READ ) { if( this.enableREAD ) { this.enableREAD(); } } else { if( this.disableREAD ) { this.disableREAD(); } } if( permissions.DELETE ) { if( this.enableDELETE ) { this.enableDELETE(); } } else { if( this.disableDELETE ) { this.disableDELETE(); } } } allow( user, type ) { this.permissionManager.allow( user, type ); } logPermission( message ) { if( this.debug ) { console.log( "Log permission:", this.getClassName(), message ); } } isAllowed( user, type ) { if( this.permissionManager.isAllowed( user, type, this ) ) { this.logPermission( "Allowed to " + type.toLowerCase() ); return true; } else { this.logPermission( "Not allowed to " + type.toLowerCase() ); return false; } } logInfinite( caller ) { console.log( "Stuck in infinity loop, Reason: The method 'parents' -> defaultObject.js, and the object:", caller ); } getCompareFromSelector( selector, object ) { if( selector.slice( 0, 1 ) == "." ) { var compareA = object.getClassName(); var compareB = selector.slice( 1, selector.length ); } else { //var compareA = object.propertyName; //var compareB = selector; var compareA = object.getClassName(); var compareB = selector.slice( 0, selector.length ); } return { compareA, compareB }; } parents( selector, object = this, caller = this, depth = 0 ) { if( depth == 40 ) { this.logInfinite( caller ); } var { compareA, compareB } = this.getCompareFromSelector( selector, object ); if( compareA == compareB ) { return object; } else { if( !object.parent ) { return false; } return this.parents( selector, object.parent, caller, depth + 1 ); } } getCore() { var cores = shared.cores; var rootApplication = this.getRoot(); var applicationID = rootApplication.applicationID; if( document.body ) { //console.log("not nodejs", document); return rootApplication.core; } else { //console.log("running nodejs"); } if( !applicationID ) { applicationID = 0;//hack } return cores[ applicationID ]; } preloadImage( url ) { var imageElement = document.createElement("link"); imageElement.href = url; imageElement.rel = "preload" imageElement.as = "image"; document.head.appendChild( imageElement ); } getTemplateTerm( gridTemplate, i ) { gridTemplate[i] = gridTemplate[i].replaceAll(" ", ""); gridTemplate[i] = gridTemplate[i].replaceAll("\t", ""); gridTemplate[i] = gridTemplate[i].replaceAll("\"", ""); if( gridTemplate[i].includes("px") ) { gridTemplate[i] = ""; } } getTemplateTerms() { var gridTemplate = this.gridTemplate.replaceAll("\t", " ").split(" ") for (var i = 0; i < gridTemplate.length; i++) { this.getTemplateTerm( gridTemplate, i ); } gridTemplate = gridTemplate.filter( v => v ); return gridTemplate; } updateTabIndices( child, gridTemplate ) { let gridIndex = gridTemplate.indexOf( child.propertyName ); if( child.customElement ) { var tabIndex = gridIndex + globalTabIndex; if( !child.customElement.tabIndex ) { child.customElement.tabIndex = tabIndex; } } } hideChild( child ) { if( child.hide ){ child.hide(); } } updateTabIndexChild( child, gridTemplate ) { if( gridTemplate.includes( child.propertyName ) ) { this.updateTabIndices( child, gridTemplate ); } else { this.hideChild( child ); } } updateGridTemplates( gridTemplate ) { globalTabIndex += gridTemplate.length; var children = this.getChildren(); for ( var i = 0; i < children.length; i++ ) { var child = children[i]; this.updateTabIndexChild( child, gridTemplate ); } } setTabIndex( child, tabIndex ) { if( child.customElement ) { if( !child.customElement.tabIndex ) { child.customElement.tabIndex = tabIndex; } } } updateTabIndicesChildren() { var children = this.getChildren(); for (var i = 0; i < children.length; i++) { var child = children[i]; this.setTabIndex( child, i ); } } processGridElement() { if( this.display == "grid" && this.gridTemplate ) { var gridTemplate = this.getTemplateTerms(); if( gridTemplate.length > 0 ) { this.showChildren(); this.updateGridTemplates( gridTemplate ); } else { this.updateTabIndicesChildren(); } } } hideMultiLayer() { if( this.element ) { if( this.element.parentNode ) { this.handleMultiLayer(); } } } handleMultiLayer() { if( !this.defaultDisplayType ) { this.handleBoxDisplayValue(); } this.boxDisplay = "none"; this.element.parentNode.style.display = "none"; } handleBoxDisplayValue() { if( this.element.parentNode.style.display == "none" ) { this.saveDefaultBoxDisplayValue(); } else { this.saveOldBoxDisplayValue(); } } saveDefaultBoxDisplayValue() { this.defaultBoxDisplayType = "flex"; } saveOldBoxDisplayValue() { this.defaultBoxDisplayType = this.element.parentNode.style.display; } saveOldDisplayValue() { if( !this.defaultDisplayType ) { this.defaultDisplayType = this.display; } } saveDefaultDisplayValue() { if( !this.defaultDisplayType ) { this.defaultDisplayType = "flex"; } } hide() { if( !this.static ) { if( this.display ) { this.saveOldDisplayValue(); } else{ this.saveDefaultDisplayValue(); } this.display = "none"; if( this.layers == 2 ) { this.hideMultiLayer(); } } } communicateVisibleElements() { if( this.getCore() && this.getRoot().mode == "development" ) { this.getCore().communicateVisibleElements(); } } showMultiLayer() { if( this.element ) { if( this.element && this.element.parentNode ) { this.boxDisplay = this.defaultBoxDisplayType; this.element.parentNode.style.display = this.defaultBoxDisplayType; } } } show( type, parentType ) { if( type && type != "none" ) { this.defaultDisplayType = type; } else if( this.display && this.display != "none" ) { this.defaultDisplayType = this.display; } if( parentType ) { this.defaultBoxDisplayType = parentType; } if( !this.defaultBoxDisplayType ) { this.defaultBoxDisplayType = "flex"; } if( this.defaultDisplayType ) { this.display = this.defaultDisplayType; } else { this.display = "flex"; } if( this.layers > 1 ) { this.showMultiLayer(); } this.communicateVisibleElements(); } syncChild( child ) { if( child.type == "renderCollection" ) { //child.sync(); } if( child.getChildren ) { this.syncAllChildren( child ); } } syncAllChildren( object ) { var children = object.getChildren(); for ( var i = 0; i < children.length; i++ ) { var child = children[i]; this.syncChild( child ); } } findStaticChild( child ) { if( child.static ) { this.syncAllChildren( child ); } } findStaticChildren( object ) { var children = object.getChildren(); for (var i = 0; i < children.length; i++) { var child = children[i]; this.findStaticChild( child ); } } showParents( object = this ) { object.show(); this.findStaticChildren( object ); if( object.parent ) { this.showParents( object.parent ); } } hideChild( child ) { if( child.hide ) { child.hide(); } } hideChildren( object = this ) { var children = object.getChildren(); for( var c = 0; c < children.length; c++ ) { var child = children[ c ]; this.hideChild( child ); } } showChild( child ) { if( child.show ) { child.show(); } } showChildren( object = this ) { var children = object.getChildren(); for( var c = 0; c < children.length; c++ ) { var child = children[ c ]; this.showChild( child ); } } getRoot( object = this, depth = 0 ) { if( object.parent ) { //console.log( object.getClassName(), object.parent.getClassName() ); return this.getRoot( object.parent, depth++ ); } else { return object; } } getParentWithTable( object = this ){ if( object.type == "table" ) { return object; } else { return this.getParentWithTable( object.parent ); } } removeProperty() { var name = this.propertyName; if( name && this.parent[ name ] ) { delete this.parent[ name ]; } } removeElements() { if( this.element ) { this.element.remove(); } if( this.layers == 2 ) { this.elements[0].remove(); if( this.elements[1] ) { this.elements[1].remove(); } } if( this.defaultElement ) { this.defaultElement.remove(); } if( this.customElement ) { this.customElement.remove(); } } remove() { this.removeElements(); this.removeProperty(); } setUser( user ) { if( user ) { this.setUserChildren( this, user ); } } setUserProperty( property, user ) { if( property.value == "userplaceholder" && property.name != "user" ) { //user.userType = "currentUser"; //object[ property.name ] = user; } //object.user = } setUserProperties( object, user ) { if( !object.getProperties ) { return false; } var properties = object.getProperties(); for( var c = 0; c < properties.length; c++ ) { var property = properties[c]; this.setUserProperty( property, user ); } } setUserChildrenRecursive( object, user ) { var d = new defaultObject(); d.exposeMethodsToObject( object ); if( !object.getChildren ) { return false; } var children = object.getChildren( false ); for( var c = 0; c < children.length; c++ ) { var child = children[ c ]; if( !child.user ) { this.setUserChildren( child, user ); } } } setUserChildren( object, user ) { this.exposeMethodsToObject( object ); //this.setUserProperties( object, user ); this.setUserChildrenRecursive( object, user ); } setApplicationPath() { this.applicationPath = this.getApplicationPath(); this.applicationPathString = this.applicationPath.join("/"); } getApplicationPathString() { return this.getApplicationPath().join("/"); } async prepend( object, parse = true ) { return this.add( object, parse, true ); } ensureID( object ) { if( !object.id ) { object.id = this.getChildren().length; } } convertToUnifyObject( object ) { var defaultInstance = new defaultObject(); defaultInstance.agregateDefaultObject( object ); } prependPropertyName( object ) { object.propertyName = "__" + object.propertyName; } appendPropertyName( object ) { if( object.type == "renderCollection" ) { object.propertyName = object.getClassName(); } else { object.propertyName = object.getClassName() + object.id; } } createPropertyName( object, prepend ) { if( prepend ) { this.prependPropertyName( object ); } else { this.appendPropertyName( object ); } } prepareChildBeforeAdd( object, prepend ) { object.parent = this; object.dynamic = true; object.parse = false; this.createPropertyName( object, prepend ); } bindChildToParent( object ) { this[ object.propertyName ] = object; } parseAddedObject( object ) { object.getCore().createStyleSheet = true; object.getCore().parse( object ); } // Deprecated sendAddedObjectToServer( object ) { /* var data = new Object(); data.className = object.getClassName(); data.parentApplicationPath = object.parent.getApplicationPath(); data.id = object.id; data.propertyName = object.propertyName; var result = await this.socketManager.get( "object", "add", JSON.stringify( data ) ); */ } handleAddedChild( object ) { if( typeof document.body != undefined && this.socketManager ) { // this.sendAddedObjectToServer( object ); this.parseAddedObject( object ); } else { this.getRoot().client.addObjectMessage( 10, object ); } } ensureUnifyObject( object ) { if( tools.isUnifyObject( "object", object ) ) { return true; } else { console.log( "object is not a unify object", object ); return false; } } async add( child, parse = true, prepend = false ) { this.ensureID( child ); this.convertToUnifyObject( child ); var isUnifyObject = this.ensureUnifyObject( child ); if( !isUnifyObject ) { return false; } this.prepareChildBeforeAdd( child, prepend ) this.bindChildToParent( child ); this.handleAddedChild( child ); } delete() { } getClassName() { if( this.__className ) { return this.__className; } // Rollup fix name$1 or name$0 return this.constructor.name.split("$")[0]; } getID() { if( this.id ) { return this.id; } else { return false; } } getFirstID() { var object = this; if( object.id ) { return object.id; } if( object.parent.id ) { return object.parent.id; } if( object.parent.parent.id) { return object.parent.parent.id; } } getExtendedClass() { var object = this; if( !object.prototype ) { object = object.constructor; } var extendedClass = object.prototype.__proto__; if( !extendedClass ) { return false; } return extendedClass; } getExtendedClassName() { var extendedClass = tools.getExtendedClass( this ); return tools.getClassName( extendedClass ); } extendsTable( object = this ) { if( !object ) { return false; } if( !object.isTable ) { //console.error("object.isTable is not a function", object); return false; } if( object.isTable() ) { return true; } var extendedClass = tools.getExtendedClass( object ); if( extendedClass ) { return this.extendsTable( extendedClass ); } else { return false; } } isColumn() { if( this.datatype ) { return true; } else { return false; } } isTable() { var object = this; if( !object.prototype ) { object = object.constructor; } var extendedClass = tools.getExtendedClass( object ); if( !extendedClass ) { return false; } var className = tools.getClassName( extendedClass ); // this is a fix for webpack. if( className == "table" || className == "unify_table_table" || className == "server_table_table" ) { return true; } else { return false; } } // dont enable because rendercollection has this function and overriding destroys the framework getTableName() { var object = this; var table = tools.getTableFromObject( object ); var tableName = tools.getClassName( table ); return tableName; } getParentApplicationPath( object, array ) { if( tools.getClassName( object ) == tools.getClassName(object.parent) ) { return array; } array.push( object.propertyName ); return this.getApplicationPath( object.parent, array ); } getApplicationPath( object, array ) { if( !object ) { var object = this; } if( !array ) { array = new Array(); } if( object.parent ) { return this.getParentApplicationPath( object, array ); } else { return array; } } isRenderCollection( object ) { return this.extendsClass("renderCollection"); } hasClass( className ) { if( this.getClassName( this ) == className ) { return true; } else { return false; } } getExtend( extended, array, depth ) { this.exposeMethodsToObject( extended ); if( extended.getClassName ) { array.push( extended.getClassName() ); } this.getExtends( extended, array, depth + 1 ); } getExtendsRecursive( extendedClasses, depth, array ) { for ( var i = 0; i < extendedClasses.length; i++ ) { var extended = extendedClasses[i]; this.getExtend( extended, array, depth ); } } getExtends( object = this, array = new Array(), depth = 0 ) { var extendedClasses = tools.getExtendedClassesMulti( object ); if( !extendedClasses ) { return array; } else { this.getExtendsRecursive( extendedClasses, depth, array ); } return array; } getExtendObjectsRecursive( object, depth, array ) { if( depth > 0 ) { this.exposeMethodsToObject( object ); if( object.getClassName ) { array.push( object ); } } depth++; return this.getExtendObjects( extended, array, depth ); } getExtendObjects( object = this, array = new Array(), depth = 0 ) { var extended = tools.getExtendedClass( object ); if( !extended ) { return array; } else { return this.getExtendObjectsRecursive( object, depth, array ); } } isVisible( element = this.element ) { if( !element ) { return false; } if( !element.offsetWidth ) { return false; } return element.offsetWidth > 0 || element.offsetHeight > 0; } extendsClass( className ) { var extendedClass = this.getExtendedClass( this ); if( !extendedClass.getClassName ) { return false; } if( extendedClass.getClassName() == className ) { return true; } else { return false; } } isOrExtendsClass( className ) { if( this.isClass( className ) ) { return true; } if( this.extendsClass( className ) ) { return true; } return false; } /* extend( object ) { var table = tools.getTableFromObject( object ); var tableName = tools.getClassName( table ); object.tableName = tableName; object.table = object; if( object.table ) { var entries = Object.entries( object ); for( var c = 0; c= 0; } ) ) { const invalid = new Array( "parent", "table", "user", "objectToManipulate", "mouse", "mouseVelocity" ); if( !invalid.includes( name ) ) { children.push( object ); } } } getChildrenByEntry( entry, children, bindParent ) { var name = tools.getClassNameByEntry( entry ); var object = tools.getObjectByEntry( entry ); this.checkAndAddChild( object, name, children, bindParent ); } getChildren( bindParent = true ) { var children = new Array(); var entries = Object.entries( this ); for( var c = 0; c < entries.length; c++ ){ var entry = entries[c]; this.getChildrenByEntry( entry, children, bindParent ); } return children; } serializeAndAddProperty( object, name, properties ) { if( !tools.isUnifyObject( name, object ) ) { var property = new Object(); property.name = name; property.value = object; const invalid = new Array( "user" ); if( !invalid.includes( name ) ) { properties.push( property ); } } } getPropertyEntry( entry, properties ) { var object = tools.getObjectByEntry( entry ); var name = tools.getClassNameByEntry( entry ); this.serializeAndAddProperty( object, name, properties ); } getProperties() { var properties = new Array(); var entries = Object.entries( this ); for( var c = 0; c < entries.length; c++ ){ var entry = entries[c]; this.getPropertyEntry( entry, properties ); } return properties; } setApplicationID( id, object = this ) { object.applicationID = id; var children = object.getChildren(); for( var c = 0; c < children.length; c++ ) { var child = children[ c ]; //child.setApplicationID( id ); } } setChildJoinID( child, join_id ) { if( child.propertyName != "parent" && child.type != "table" ) { this.setJoinID( join_id, child ); } } setChildrenJoinID( object, join_id ) { var children = tools.getChildren( object ); for( var c = 0; c < children.length; c++ ) { var child = children[ c ]; this.setChildJoinID( child, join_id ); } } setJoinID( join_id, object = this ) { if( this.getClassName() != "user") { object.join_id = join_id; this.setChildJoinID( object, join_id ); } } setChildID( child, id ) { if( child.propertyName != "parent" && child.type != "table" ) { this.setID( id, child ); } } setChildrenID( object, id ) { var children = tools.getChildren( object ); for( var c = 0; c < children.length; c++ ) { var child = children[ c ]; this.setChildID( child, id ); } } setID( id, object = this ) { if( this.getClassName() != "user" ) { object.id = id; this.setChildrenID( object, id ); } } clean( method ) { var serializer = new serialize(); return serializer.clean( this, method ); } clearCollection( collection, client ) { //console.log("clearCollection", collection); if( collection ) { var rows = collection.rows; this.clearRows( rows, client ) collection.rows = new Array(); } } clearRow( object, client ) { if( object.remove ) { object.remove(); } this.getCore().removeObject( object.getClassName(), object.id, client ); } removeChildren() { var children = this.getChildren(); var reject = new Array("storageCollection", "filterObject") for (var i = 0; i < children.length; i++) { var propertyName = children[i].propertyName; if( !reject.includes( propertyName ) ) { console.log("remove ", propertyName) delete this[ propertyName ]; } } } clearRows( rows, client ) { for( var c = 0; c < rows.length; c++ ) { var row = rows[c]; this.clearRow( row, client ); delete this[row.propertyName]; } } clearOneLayer( child ) { if( child.defaultElement ) { child.remove(); } } clearTwoLayers( child ) { if( child.defaultElement && child.defaultElement.parentNode ) { child.defaultElement.parentNode.remove(); } } clearChild( child ) { this.exposeMethodsToObject( child ); if( child.layers == 2 ) { this.clearTwoLayers( child ); } else { this.clearOneLayer( child ); } } clearChildNode( child ) { child.id = 0; child.value = ""; } clearChildren() { var children = this.getChildren(); for( var c = 0; c < children.length; c++ ) { var child = children[c]; if( tools.getEnvironment() == "Node" ) { this.clearChildNode( child ); } else { this.clearChild( child ); } } } empty( client ) { if( this.getCollection ) { var collection = this.getCollection(); this.clearCollection( collection, client ); } this.clearChildren(); return true; } deserialize( object = this ) { if( object.getChildren ) { var children = object.getChildren(); for( var c = 0; c < children.length; c++ ) { var child = children[c]; child.id = 0; child.value = ""; this.deserialize( child ); } } if( this.rows ) { this.rows = new Array(); } } serialize( sourceObject, client ){ var targetObject = this; var serializer = new serialize(); return serializer.serialize( sourceObject, targetObject, client ); } getUserFromClient( client ) { return client.user; } computePermissions( client ) { var user = this.getUserFromClient( client ); this.permissions = new Object(); if( this.isAllowed( user, "WRITE" ) ) { this.permissions.WRITE = true; } else { this.permissions.WRITE = false; } if( this.isAllowed( user, "DELETE" ) ) { this.permissions.DELETE = true; } else { this.permissions.DELETE = false; } if( this.isAllowed( user, "READ" ) ) { this.permissions.READ = true; } else { this.permissions.READ = false; } } getColumnName() { var extendsArray = this.getExtends(); if( extendsArray[0] == "column" ) { var columnName = this.propertyName; } else { var columnIndex = 0; for (var i = 0; i < extendsArray.length; i++) { if( extendsArray[i] == "column" ) { columnIndex = i; } } var columnName = extendsArray[columnIndex-1]; } return columnName; } updateChildrenPermissions( object, user ) { if( object.permission ) { object.permissions = userManager.computePermissions( object, user ); } if( object.getChildren ) { var children = object.getChildren(); for (var i = 0; i < children.length; i++) { var child = children[i] this.updateChildrenPermissions( child, user ); } } } } export default defaultObject;