428 lines
6.3 KiB
JavaScript
428 lines
6.3 KiB
JavaScript
/*
|
|
|
|
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";
|
|
|
|
export default class table{
|
|
|
|
__className = "table";
|
|
|
|
type = "table";
|
|
|
|
id = 0;
|
|
|
|
collection = false;
|
|
|
|
collections = new Array();
|
|
|
|
load = true;
|
|
|
|
synced = false;
|
|
|
|
useCustomElement = false;
|
|
|
|
useCache = false;
|
|
|
|
createOnSync = true;
|
|
|
|
deleteProperty( child ) {
|
|
|
|
if( child.rowID ) {
|
|
|
|
delete this[ child.propertyName ];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cleanChildren( object ) {
|
|
|
|
if( object.getChildren ) {
|
|
|
|
var children = object.getChildren();
|
|
|
|
for (var i = 0; i < children.length; i++) {
|
|
|
|
var child = children[i];
|
|
|
|
if( child.rows ) {
|
|
|
|
child.rows = new Array();
|
|
|
|
}
|
|
|
|
this.deleteProperty( child );
|
|
|
|
this.cleanChildren( child );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
destroyEventListener( object ) {
|
|
|
|
if( object.render ) {
|
|
|
|
// this could be enabled to destory the render functions still running.
|
|
// But this causes problems when loading a new page, and then
|
|
// loading the old one again, The render method is not called.
|
|
if( object.autoDestroyRenderLoop ) {
|
|
|
|
window.cancelAnimationFrame( object.requestAnimationFrameID );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if( object.destroy ) {
|
|
|
|
object.destroy();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
destroyEventListeners( element ) {
|
|
|
|
if( element.object ) {
|
|
|
|
var object = element.object;
|
|
|
|
this.destroyEventListener( object );
|
|
|
|
}
|
|
|
|
var children = element.childNodes;
|
|
|
|
for (var i = 0; i < children.length; i++) {
|
|
|
|
var child = children[i];
|
|
|
|
this.destroyEventListeners(child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
logSync() {
|
|
|
|
if( this.debug | document.debugALL ) {
|
|
|
|
console.log( "" );
|
|
console.log("%ctable::sync", "background: #5dc2d6;" );
|
|
|
|
|
|
console.log( "className: ", this.getClassName() );
|
|
console.log( "Path: ", this.applicationPathString );
|
|
console.log( this );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
async loadObject( sign ) {
|
|
|
|
if( this.load ) {
|
|
|
|
//this.cleanChildren( this );
|
|
|
|
//this.destroyEventListeners( this.element );
|
|
|
|
if( !this.id ) {
|
|
|
|
await this.createObject( sign );
|
|
|
|
} else {
|
|
|
|
await this.updateObject();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
scrollPage( show ) {
|
|
|
|
if( show ) {
|
|
|
|
this.show();
|
|
|
|
if( this.element ) {
|
|
|
|
this.element.scrollTo( 0, 0 );
|
|
|
|
}
|
|
|
|
if( this.boxElement ) {
|
|
|
|
this.boxElement.scrollTo( 0, 0 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
state.table = this;
|
|
|
|
document.states.push( state );
|
|
|
|
var url = window.location;
|
|
|
|
await history.pushState( { id: document.states.length-1 }, "Unify", "/" + path );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
async sync( sign = false, show = true, createUrl = true ) {
|
|
|
|
var sign = true;
|
|
|
|
this.setApplicationPath();
|
|
|
|
this.logSync();
|
|
|
|
await this.loadObject( sign );
|
|
|
|
if( createUrl ) {
|
|
|
|
//this.createUrl();
|
|
|
|
}
|
|
|
|
this.scrollPage( show );
|
|
|
|
this.getCore().communicateVisibleElements();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
async createObject( sign ) {
|
|
|
|
if( sign ) {
|
|
|
|
var table = await this.socketManager.get( "table", "create", this, "sign" );
|
|
|
|
} else {
|
|
|
|
var table = await this.socketManager.get( "table", "create", this );
|
|
|
|
}
|
|
|
|
this.setID( table.id );
|
|
|
|
this.updatePermissions( table.permissions );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
async getObject() {
|
|
|
|
if( this.useCache ) {
|
|
|
|
var object = document.cacheManager.getLocalObject( this, this.id, tools.getTableName( this ) );
|
|
|
|
} else {
|
|
|
|
var object = false;
|
|
|
|
}
|
|
|
|
if( !object ) {
|
|
|
|
var object = await this.socketManager.get( "table", "get", this );
|
|
|
|
//document.cacheManager.addObject( object, this.id, tools.getTableName( this ) );
|
|
|
|
}
|
|
|
|
|
|
return object;
|
|
|
|
}
|
|
|
|
updateChildrenPermissionsClient( object ) {
|
|
|
|
if( object.permissions && object.updatePermissions ) {
|
|
|
|
|
|
|
|
object.updatePermissions( object.permissions );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if( object.getChildren ) {
|
|
|
|
var children = object.getChildren();
|
|
|
|
for (var i = 0; i < children.length; i++) {
|
|
|
|
var child = children[i]
|
|
|
|
console.log( "updateChildrenPermissions", child.propertyName);
|
|
|
|
this.updateChildrenPermissionsClient( child );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
async prepareObject( object ) {
|
|
|
|
this.setID( object.id );
|
|
|
|
this.serialize( object );
|
|
|
|
console.log("after serialize", this);
|
|
|
|
//this.updatePermissions( object.permissions );
|
|
|
|
this.updateChildrenPermissionsClient( this );
|
|
|
|
await this.setupChildren();
|
|
|
|
if( this.create && this.createOnSync ) {
|
|
|
|
await this.create();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
async updateObject() {
|
|
|
|
var object = await this.getObject();
|
|
|
|
await this.prepareObject( object );
|
|
|
|
this.getCore().appendAllToSelector();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
async setupChildren() {
|
|
|
|
var children = this.getChildren();
|
|
|
|
for( var c = 0; c < children.length; c++ ) {
|
|
|
|
var child = children[c];
|
|
|
|
if( child.setup ) {
|
|
|
|
await child.setup();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|