2020-01-22 18:12:56 +01:00
/ * *
* EGroupware eTemplate2 - JS Grid object
*
* @license http : //opensource.org/licenses/gpl-license.php GPL - GNU General Public License
* @package etemplate
* @subpackage api
2021-06-07 17:33:53 +02:00
* @link https : //www.egroupware.org
2020-01-22 18:12:56 +01:00
* @author Andreas Stöckel
2021-06-07 17:33:53 +02:00
* @copyright EGroupware GmbH 2011 - 2021
2020-01-22 18:12:56 +01:00
* /
/ * e g w : u s e s
/ v e n d o r / b o w e r - a s s e t / j q u e r y / d i s t / j q u e r y . j s ;
et2_core_DOMWidget ;
et2_core_xml ;
* /
2021-06-07 17:33:53 +02:00
import { et2_no_init } from "./et2_core_common" ;
import { et2_IAligned , et2_IDetachedDOM , et2_IResizeable } from "./et2_core_interfaces" ;
2020-02-27 19:54:55 +01:00
import { et2_register_widget , et2_widget , WidgetConfig } from "./et2_core_widget" ;
2020-01-22 18:12:56 +01:00
import { ClassWithAttributes } from "./et2_core_inheritance" ;
2020-02-27 19:54:55 +01:00
import { et2_action_object_impl , et2_DOMWidget } from "./et2_core_DOMWidget" ;
2023-07-10 16:02:30 +02:00
import { egw_getAppObjectManager , egwActionObject } from '../egw_action/egw_action' ;
2021-06-07 17:33:53 +02:00
import { et2_directChildrenByTagName , et2_filteredNodeIterator , et2_readAttrWithDefault } from "./et2_core_xml" ;
import { egw } from "../jsapi/egw_global" ;
2021-08-11 19:28:01 +02:00
import Sortable from 'sortablejs/modular/sortable.complete.esm.js' ;
2020-01-22 18:12:56 +01:00
/ * *
* Class which implements the "grid" XET - Tag
*
* This also includes repeating the last row in the grid and filling
* it with content data
*
* @augments et2_DOMWidget
* /
export class et2_grid extends et2_DOMWidget implements et2_IDetachedDOM , et2_IAligned , et2_IResizeable
{
static readonly _attributes : any = {
// Better to use CSS, no need to warn about it
"border" : {
"ignore" : true
} ,
"align" : {
"name" : "Align" ,
"type" : "string" ,
"default" : "left" ,
"description" : "Position of this element in the parent hbox"
} ,
"spacing" : {
"ignore" : true
} ,
"padding" : {
"ignore" : true
} ,
"sortable" : {
"name" : "Sortable callback" ,
"type" : "string" ,
"default" : et2_no_init ,
"description" : "PHP function called when user sorts the grid. Setting this enables sorting the grid rows. The callback will be passed the ID of the grid and the new order of the rows."
} ,
sortable_containment : {
name : "Sortable bounding area" ,
type : "string" ,
default : "" ,
description : "Defines bounding area for sortable items"
} ,
sortable_connectWith : {
name : "Sortable connectWith element" ,
type : "string" ,
default : "" ,
description : "Defines other sortable areas that should be connected to sort list"
} ,
sortable_placeholder : {
name : "Sortable placeholder" ,
type : "string" ,
default : "" ,
description : "Defines sortable placeholder"
} ,
sortable_cancel : {
name : "Sortable cancel class" ,
type : "string" ,
default : "" ,
description : "Defines sortable cancel which prevents sorting the matching element"
} ,
sortable_recieveCallback : {
name : "Sortable receive callback" ,
type : "js" ,
default : et2_no_init ,
description : "Defines sortable receive callback function"
} ,
sortable_startCallback : {
name : "Sortable start callback" ,
type : "js" ,
default : et2_no_init ,
description : "Defines sortable start callback function"
}
} ;
2021-08-19 01:41:23 +02:00
private table : JQuery ;
private thead : JQuery ;
private tfoot : JQuery ;
private tbody : JQuery ;
2020-01-22 18:12:56 +01:00
// Counters for rows and columns
2021-08-19 01:41:23 +02:00
private rowCount : number = 0 ;
private columnCount : number = 0 ;
2020-01-22 18:12:56 +01:00
// 2D-Array which holds references to the DOM td tags
private cells = [ ] ;
private rowData = [ ] ;
private colData = [ ] ;
private managementArray = [ ] ;
// Keep the template node for later regeneration
private template_node = null ;
// Wrapper div for height & overflow, if needed
private wrapper = null ;
2021-08-19 01:41:23 +02:00
private lastRowNode : null ;
2020-01-22 18:12:56 +01:00
2021-08-03 15:59:22 +02:00
private sortablejs : Sortable = null ;
2021-08-19 01:41:23 +02:00
2020-01-22 18:12:56 +01:00
/ * *
* Constructor
*
* @memberOf et2_grid
* /
constructor ( _parent , _attrs? : WidgetConfig , _child? : object )
{
// Call the parent constructor
super ( _parent , _attrs , ClassWithAttributes . extendAttributes ( et2_grid . _attributes , _child || { } ) ) ;
// Create the table body and the table
this . table = jQuery ( document . createElement ( "table" ) )
. addClass ( "et2_grid" ) ;
2024-02-11 13:41:30 +01:00
if ( _attrs . class ) this . table . addClass ( _attrs . class ) ;
2020-01-22 18:12:56 +01:00
this . thead = jQuery ( document . createElement ( "thead" ) )
. appendTo ( this . table ) ;
this . tfoot = jQuery ( document . createElement ( "tfoot" ) )
. appendTo ( this . table ) ;
this . tbody = jQuery ( document . createElement ( "tbody" ) )
. appendTo ( this . table ) ;
}
2021-08-19 01:41:23 +02:00
_initCells ( _colData : ColumnEntry [ ] , _rowData : RowEntry [ ] )
{
2020-01-22 18:12:56 +01:00
// Copy the width and height
const w = _colData . length ;
const h = _rowData . length ;
// Create the 2D-Cells array
const cells = new Array ( h ) ;
2021-08-19 01:41:23 +02:00
for ( let y = 0 ; y < h ; y ++ )
2020-01-22 18:12:56 +01:00
{
cells [ y ] = new Array ( w ) ;
// Initialize the cell description objects
2021-08-19 01:41:23 +02:00
for ( let x = 0 ; x < w ; x ++ )
2020-01-22 18:12:56 +01:00
{
// Some columns (nm) we do not parse into a boolean
const col_disabled = _colData [ x ] . disabled ;
cells [ y ] [ x ] = {
"td" : null ,
"widget" : null ,
"colData" : _colData [ x ] ,
"rowData" : _rowData [ y ] ,
"disabled" : col_disabled || _rowData [ y ] . disabled ,
"class" : _colData [ x ] [ "class" ] ,
"colSpan" : 1 ,
"autoColSpan" : false ,
"rowSpan" : 1 ,
"autoRowSpan" : false ,
"width" : _colData [ x ] . width ,
"x" : x ,
"y" : y
} ;
}
}
return cells ;
}
_getColDataEntry ( ) : ColumnEntry
{
2021-08-19 01:41:23 +02:00
return {
2020-01-22 18:12:56 +01:00
width : "auto" ,
class : "" ,
align : "" ,
span : "1" ,
disabled : false
} ;
}
_getRowDataEntry ( ) : RowEntry
{
return {
height : "auto" ,
class : "" ,
valign : "top" ,
span : "1" ,
disabled : false
} ;
}
_getCell ( _cells , _x , _y )
{
2021-08-19 01:41:23 +02:00
if ( ( 0 <= _y ) && ( _y < _cells . length ) )
2020-01-22 18:12:56 +01:00
{
const row = _cells [ _y ] ;
2021-08-19 01:41:23 +02:00
if ( ( 0 <= _x ) && ( _x < row . length ) )
2020-01-22 18:12:56 +01:00
{
return row [ _x ] ;
}
}
throw ( "Error while accessing grid cells, invalid element count or span value!" ) ;
}
_forceNumber ( _val )
{
2021-08-19 01:41:23 +02:00
if ( isNaN ( _val ) )
2020-01-22 18:12:56 +01:00
{
throw ( _val + " is not a number!" ) ;
}
return parseInt ( _val ) ;
}
_fetchRowColData ( columns , rows , colData : ColumnEntry [ ] , rowData : RowEntry [ ] )
{
// Some things cannot be done inside a nextmatch - nm will do the expansion later
var nm = false ;
2021-08-19 01:41:23 +02:00
let widget : et2_widget = this ;
2020-01-22 18:12:56 +01:00
while ( ! nm && widget != this . getRoot ( ) )
{
nm = ( widget . getType ( ) == 'nextmatch' ) ;
widget = widget . getParent ( ) ;
}
// Parse the columns tag
2021-08-19 01:41:23 +02:00
et2_filteredNodeIterator ( columns , function ( node , nodeName )
{
2020-01-22 18:12:56 +01:00
const colDataEntry = this . _getColDataEntry ( ) ;
// This cannot be done inside a nm, it will expand it later
colDataEntry [ "disabled" ] = nm ?
2021-08-19 01:41:23 +02:00
et2_readAttrWithDefault ( node , "disabled" , "" ) :
this . getArrayMgr ( "content" )
. parseBoolExpression ( et2_readAttrWithDefault ( node , "disabled" , "" ) ) ;
if ( nodeName == "column" )
2020-01-22 18:12:56 +01:00
{
colDataEntry [ "width" ] = et2_readAttrWithDefault ( node , "width" , "auto" ) ;
colDataEntry [ "class" ] = et2_readAttrWithDefault ( node , "class" , "" ) ;
colDataEntry [ "align" ] = et2_readAttrWithDefault ( node , "align" , "" ) ;
colDataEntry [ "span" ] = et2_readAttrWithDefault ( node , "span" , "1" ) ;
// Keep any others attributes set, there's no 'column' widget
for ( let i in node . attributes )
{
const attr = node . attributes [ i ] ;
if ( attr . nodeType == 2 && typeof colDataEntry [ attr . nodeName ] == 'undefined' )
{
colDataEntry [ attr . nodeName ] = attr . value ;
}
}
}
else
{
colDataEntry [ "span" ] = "all" ;
}
colData . push ( colDataEntry ) ;
} , this ) ;
// Parse the rows tag
2021-08-19 01:41:23 +02:00
et2_filteredNodeIterator ( rows , function ( node , nodeName )
{
2020-01-22 18:12:56 +01:00
const rowDataEntry = this . _getRowDataEntry ( ) ;
rowDataEntry [ "disabled" ] = this . getArrayMgr ( "content" )
2021-08-19 01:41:23 +02:00
. parseBoolExpression ( et2_readAttrWithDefault ( node , "disabled" , "" ) ) ;
if ( nodeName == "row" )
2020-01-22 18:12:56 +01:00
{
// Remember this row for auto-repeat - it'll eventually be the last one
this . lastRowNode = node ;
rowDataEntry [ "height" ] = et2_readAttrWithDefault ( node , "height" , "auto" ) ;
rowDataEntry [ "class" ] = et2_readAttrWithDefault ( node , "class" , "" ) ;
rowDataEntry [ "valign" ] = et2_readAttrWithDefault ( node , "valign" , "" ) ;
rowDataEntry [ "span" ] = et2_readAttrWithDefault ( node , "span" , "1" ) ;
rowDataEntry [ "part" ] = et2_readAttrWithDefault ( node , "part" , "body" ) ;
const id = et2_readAttrWithDefault ( node , "id" , "" ) ;
if ( id )
{
rowDataEntry [ "id" ] = id ;
}
}
else
{
rowDataEntry [ "span" ] = "all" ;
}
rowData . push ( rowDataEntry ) ;
} , this ) ;
// Add in repeated rows
// TODO: It would be nice if we could skip header (thead) & footer (tfoot) or treat them separately
let rowIndex = Infinity ;
if ( this . getArrayMgr ( "content" ) )
{
const content = this . getArrayMgr ( "content" ) ;
2021-08-19 01:41:23 +02:00
var rowDataEntry = rowData [ rowData . length - 1 ] ;
rowIndex = rowData . length - 1 ;
2020-01-22 18:12:56 +01:00
// Find out if we have any content rows, and how many
let cont = true ;
while ( cont )
{
if ( content . data [ rowIndex ] )
{
rowData [ rowIndex ] = jQuery . extend ( { } , rowDataEntry ) ;
rowIndex ++ ;
}
2021-08-19 01:41:23 +02:00
else if ( this . lastRowNode != null )
2020-01-22 18:12:56 +01:00
{
// Have to look through actual widgets to support const[$row]
// style names - should be avoided so we can remove this extra check
// Old etemplate checked first two widgets, or first two box children
// This cannot be done inside a nextmatch - nm will do the expansion later
var nm = false ;
if ( nm )
{
// No further checks for repeated rows
break ;
}
// Not in a nextmatch, so we can expand with abandon
const currentPerspective = jQuery . extend ( { } , content . perspectiveData ) ;
2021-08-19 01:41:23 +02:00
const check = function ( node , nodeName )
{
if ( nodeName == 'box' || nodeName == 'hbox' || nodeName == 'vbox' )
{
2020-01-22 18:12:56 +01:00
return et2_filteredNodeIterator ( node , check , this ) ;
}
content . perspectiveData . row = rowIndex ;
2021-08-19 01:41:23 +02:00
for ( let attr in node . attributes )
{
2020-01-22 18:12:56 +01:00
const value = et2_readAttrWithDefault ( node , node . attributes [ attr ] . name , "" ) ;
// Don't include first char, those should be handled by normal means
// and it would break nextmatch
2021-08-19 01:41:23 +02:00
if ( value . indexOf ( '@' ) > 0 || value . indexOf ( '$' ) > 0 )
{
2020-01-22 18:12:56 +01:00
// Ok, we found something. How many? Check for values.
let ident = content . expandName ( value ) ;
// expandName() handles index into content (@), but we have to look up
// regular values
2021-08-19 01:41:23 +02:00
if ( value [ 0 ] != '@' )
{
2020-01-22 18:12:56 +01:00
// Returns null if there isn't an actual value
ident = content . getEntry ( ident , false , true ) ;
}
2021-08-19 01:41:23 +02:00
while ( ident != null && rowIndex < 1000 )
{
2020-01-22 18:12:56 +01:00
rowData [ rowIndex ] = jQuery . extend ( { } , rowDataEntry ) ;
content . perspectiveData . row = ++ rowIndex ;
ident = content . expandName ( value ) ;
2021-08-19 01:41:23 +02:00
if ( value [ 0 ] != '@' )
{
2020-01-22 18:12:56 +01:00
// Returns null if there isn't an actual value
ident = content . getEntry ( ident , false , true ) ;
}
}
2021-08-19 01:41:23 +02:00
if ( rowIndex >= 1000 )
{
2020-01-22 18:12:56 +01:00
egw . debug ( "error" , "Problem in autorepeat fallback: too many rows for '%s'. Use a nextmatch, or start debugging." , value ) ;
}
return ;
}
}
} ;
2021-08-19 01:41:23 +02:00
et2_filteredNodeIterator ( this . lastRowNode , check , this ) ;
2020-01-22 18:12:56 +01:00
cont = false ;
content . perspectiveData = currentPerspective ;
}
else
{
// No more rows, stop
break ;
}
}
}
if ( rowIndex <= rowData . length - 1 )
{
// No auto-repeat
this . lastRowNode = null ;
}
}
2021-08-19 01:41:23 +02:00
_fillCells ( cells , columns : object [ ] , rows : object [ ] )
{
2020-01-22 18:12:56 +01:00
const h = cells . length ;
const w = ( h > 0 ) ? cells [ 0 ] . length : 0 ;
const currentPerspective = jQuery . extend ( { } , this . getArrayMgr ( "content" ) . perspectiveData ) ;
// Read the elements inside the columns
let x = 0 ;
2021-08-19 01:41:23 +02:00
et2_filteredNodeIterator ( columns , function ( node , nodeName )
{
2020-01-22 18:12:56 +01:00
2021-08-19 01:41:23 +02:00
function _readColNode ( node , nodeName )
{
if ( y >= h )
2020-01-22 18:12:56 +01:00
{
this . egw ( ) . debug ( "warn" , "Skipped grid cell in column, '" +
nodeName + "'" ) ;
return ;
}
const cell = this . _getCell ( cells , x , y ) ;
// Read the span value of the element
2021-08-19 01:41:23 +02:00
if ( node . getAttribute ( "span" ) )
2020-01-22 18:12:56 +01:00
{
cell . rowSpan = node . getAttribute ( "span" ) ;
}
else
{
cell . rowSpan = cell . colData [ "span" ] ;
cell . autoRowSpan = true ;
}
2021-08-19 01:41:23 +02:00
if ( cell . rowSpan == "all" )
2020-01-22 18:12:56 +01:00
{
cell . rowSpan = cells . length ;
}
const span = cell . rowSpan = this . _forceNumber ( cell . rowSpan ) ;
// Create the widget
const widget = this . createElementFromNode ( node , nodeName ) ;
// Fill all cells the widget is spanning
2021-08-19 01:41:23 +02:00
for ( let i = 0 ; i < span && y < cells . length ; i ++ , y ++ )
2020-01-22 18:12:56 +01:00
{
this . _getCell ( cells , x , y ) . widget = widget ;
}
}
// If the node is a column, create the widgets which belong into
// the column
var y = 0 ;
2021-08-19 01:41:23 +02:00
if ( nodeName == "column" )
2020-01-22 18:12:56 +01:00
{
et2_filteredNodeIterator ( node , _readColNode , this ) ;
}
else
{
_readColNode . call ( this , node , nodeName ) ;
}
x ++ ;
} , this ) ;
// Read the elements inside the rows
var y = 0 ;
x = 0 ;
let readRowNode ;
let nm = false ;
var widget : et2_widget = this ;
while ( ! nm && widget != this . getRoot ( ) )
{
nm = ( widget . getType ( ) == 'nextmatch' ) ;
widget = widget . getParent ( ) ;
}
2021-08-19 01:41:23 +02:00
et2_filteredNodeIterator ( rows , function ( node , nodeName )
{
2020-01-22 18:12:56 +01:00
2021-08-19 01:41:23 +02:00
readRowNode = function _readRowNode ( node , nodeName )
{
if ( x >= w )
2020-01-22 18:12:56 +01:00
{
if ( nodeName != "description" )
{
// Only notify it skipping other than description,
// description used to pad
this . egw ( ) . debug ( "warn" , "Skipped grid cell in row, '" +
nodeName + "'" ) ;
}
return ;
}
let cell = this . _getCell ( cells , x , y ) ;
// Read the span value of the element
2021-08-19 01:41:23 +02:00
if ( node . getAttribute ( "span" ) )
2020-01-22 18:12:56 +01:00
{
cell . colSpan = node . getAttribute ( "span" ) ;
}
else
{
cell . colSpan = cell . rowData [ "span" ] ;
cell . autoColSpan = true ;
}
2021-08-19 01:41:23 +02:00
if ( cell . colSpan == "all" )
2020-01-22 18:12:56 +01:00
{
cell . colSpan = cells [ y ] . length ;
}
const span = cell . colSpan = this . _forceNumber ( cell . colSpan ) ;
// Read the align value of the element
2021-08-19 01:41:23 +02:00
if ( node . getAttribute ( "align" ) )
2020-01-22 18:12:56 +01:00
{
cell . align = node . getAttribute ( "align" ) ;
}
// store id of nextmatch-*headers, so it is available for disabled widgets, which get not instanciated
2021-08-19 01:41:23 +02:00
if ( nodeName . substr ( 0 , 10 ) == 'nextmatch-' )
2020-01-22 18:12:56 +01:00
{
cell . nm_id = node . getAttribute ( 'id' ) ;
}
// Apply widget's class to td, for backward compatability
2021-08-19 10:46:34 +02:00
if ( node . getAttribute ( "class" ) )
2020-01-22 18:12:56 +01:00
{
2023-01-03 00:20:50 +01:00
let widget_class = ( this . getArrayMgr ( "content" ) . expandName ( node . getAttribute ( "class" ) ) || "" )
2022-09-23 16:51:57 +02:00
// Do not pass et2_required into parent cell, it looks bad
. replace ( "et2_required" , "" )
cell . class += ( cell . class ? " " : "" ) + widget_class ;
2020-01-22 18:12:56 +01:00
}
// Create the element
if ( ! cell . disabled || cell . disabled && typeof cell . disabled === 'string' )
{
//Skip if it is a nextmatch while the nextmatch handles row adjustment by itself
if ( ! nm )
{
// Adjust for the row
const mgrs = this . getArrayMgrs ( ) ;
for ( let name in mgrs )
{
this . getArrayMgr ( name ) . perspectiveData . row = y ;
}
if ( this . _getCell ( cells , x , y ) . rowData . id )
{
this . _getCell ( cells , x , y ) . rowData . id = this . getArrayMgr ( "content" ) . expandName ( this . _getCell ( cells , x , y ) . rowData . id ) ;
}
if ( this . _getCell ( cells , x , y ) . rowData . class )
{
this . _getCell ( cells , x , y ) . rowData . class = this . getArrayMgr ( "content" ) . expandName ( this . _getCell ( cells , x , y ) . rowData . class ) ;
}
}
if ( ! nm && typeof cell . disabled === 'string' )
{
cell . disabled = this . getArrayMgr ( "content" ) . parseBoolExpression ( cell . disabled ) ;
}
if ( nm || ! cell . disabled )
{
var widget = this . createElementFromNode ( node , nodeName ) ;
}
}
// Fill all cells the widget is spanning
2021-08-19 01:41:23 +02:00
for ( let i = 0 ; i < span && x < cells [ y ] . length ; i ++ , x ++ )
2020-01-22 18:12:56 +01:00
{
cell = this . _getCell ( cells , x , y ) ;
2021-08-19 01:41:23 +02:00
if ( cell . widget == null )
2020-01-22 18:12:56 +01:00
{
cell . widget = widget ;
}
else
{
throw ( "Grid cell collision, two elements " +
"defined for cell (" + x + "," + y + ")!" ) ;
}
}
} ;
// If the node is a row, create the widgets which belong into
// the row
x = 0 ;
if ( this . lastRowNode && node == this . lastRowNode )
{
return ;
}
2021-08-19 01:41:23 +02:00
if ( nodeName == "row" )
2020-01-22 18:12:56 +01:00
{
// Adjust for the row
for ( var name in this . getArrayMgrs ( ) )
{
//this.getArrayMgr(name).perspectiveData.row = y;
}
2021-08-19 01:41:23 +02:00
let cell = this . _getCell ( cells , x , y ) ;
2020-01-24 13:58:15 +01:00
if ( cell . rowData . id )
2020-01-22 18:12:56 +01:00
{
2020-01-24 13:58:15 +01:00
this . getArrayMgr ( "content" ) . expandName ( cell . rowData . id ) ;
2020-01-22 18:12:56 +01:00
}
// If row disabled, just skip it
let disabled = false ;
if ( node . getAttribute ( "disabled" ) == "1" )
{
disabled = true ;
}
if ( ! disabled )
{
et2_filteredNodeIterator ( node , readRowNode , this ) ;
}
}
else
{
readRowNode . call ( this , node , nodeName ) ;
}
y ++ ;
} , this ) ;
// Extra content rows
2021-08-19 01:41:23 +02:00
for ( y ; y < h ; y ++ )
{
2020-01-22 18:12:56 +01:00
x = 0 ;
et2_filteredNodeIterator ( this . lastRowNode , readRowNode , this ) ;
}
// Reset
for ( var name in this . getArrayMgrs ( ) )
{
this . getArrayMgr ( name ) . perspectiveData = currentPerspective ;
}
}
_expandLastCells ( _cells )
{
const h = _cells . length ;
const w = ( h > 0 ) ? _cells [ 0 ] . length : 0 ;
// Determine the last cell in each row and expand its span value if
// the span has not been explicitly set.
2021-08-19 01:41:23 +02:00
for ( var y = 0 ; y < h ; y ++ )
2020-01-22 18:12:56 +01:00
{
2021-08-19 01:41:23 +02:00
for ( var x = w - 1 ; x >= 0 ; x -- )
2020-01-22 18:12:56 +01:00
{
var cell = _cells [ y ] [ x ] ;
2021-08-19 01:41:23 +02:00
if ( cell . widget != null )
2020-01-22 18:12:56 +01:00
{
2021-08-19 01:41:23 +02:00
if ( cell . autoColSpan )
2020-01-22 18:12:56 +01:00
{
cell . colSpan = w - x ;
}
break ;
}
}
}
// Determine the last cell in each column and expand its span value if
// the span has not been explicitly set.
2021-08-19 01:41:23 +02:00
for ( var x = 0 ; x < w ; x ++ )
2020-01-22 18:12:56 +01:00
{
2021-08-19 01:41:23 +02:00
for ( var y = h - 1 ; y >= 0 ; y -- )
2020-01-22 18:12:56 +01:00
{
var cell = _cells [ y ] [ x ] ;
2021-08-19 01:41:23 +02:00
if ( cell . widget != null )
2020-01-22 18:12:56 +01:00
{
2021-08-19 01:41:23 +02:00
if ( cell . autoRowSpan )
2020-01-22 18:12:56 +01:00
{
cell . rowSpan = h - y ;
}
break ;
}
}
}
}
2020-01-29 22:29:06 +01:00
_createNamespace ( ) : boolean
{
return true ;
}
2021-08-19 01:41:23 +02:00
2020-01-22 18:12:56 +01:00
/ * *
* As the does not fit very well into the default widget structure , we ' re
* overwriting the loadFromXML function and doing a two - pass reading -
* in the first step the
*
* @param { object } _node xml node to process
* /
2021-08-19 01:41:23 +02:00
loadFromXML ( _node )
{
2020-01-22 18:12:56 +01:00
// Keep the node for later changing / reloading
this . template_node = _node ;
// Get the columns and rows tag
const rowsElems = et2_directChildrenByTagName ( _node , "rows" ) ;
const columnsElems = et2_directChildrenByTagName ( _node , "columns" ) ;
2021-08-19 01:41:23 +02:00
if ( rowsElems . length == 1 && columnsElems . length == 1 )
2020-01-22 18:12:56 +01:00
{
const columns = columnsElems [ 0 ] ;
const rows = rowsElems [ 0 ] ;
2021-08-19 01:41:23 +02:00
const colData : ColumnEntry [ ] = [ ] ;
const rowData : RowEntry [ ] = [ ] ;
2020-01-22 18:12:56 +01:00
// Fetch the column and row data
this . _fetchRowColData ( columns , rows , colData , rowData ) ;
// Initialize the cells
const cells = this . _initCells ( colData , rowData ) ;
// Create the widgets inside the cells and read the span values
this . _fillCells ( cells , columns , rows ) ;
// Expand the span values of the last cells
this . _expandLastCells ( cells ) ;
// Create the table rows
this . createTableFromCells ( cells , colData , rowData ) ;
}
else
{
throw ( "Error while parsing grid, none or multiple rows or columns tags!" ) ;
}
}
2021-08-19 01:41:23 +02:00
createTableFromCells ( _cells , _colData : any [ ] , _rowData : any [ ] )
2020-01-22 18:12:56 +01:00
{
this . managementArray = [ ] ;
this . cells = _cells ;
this . colData = _colData ;
this . rowData = _rowData ;
// Set the rowCount and columnCount variables
const h = this . rowCount = _cells . length ;
const w = this . columnCount = ( h > 0 ) ? _cells [ 0 ] . length : 0 ;
// Create the table rows.
2021-08-19 01:41:23 +02:00
for ( let y = 0 ; y < h ; y ++ )
2020-01-22 18:12:56 +01:00
{
let parent = this . tbody ;
switch ( this . rowData [ y ] [ "part" ] )
{
case 'header' :
2021-08-19 01:41:23 +02:00
if ( ! this . tbody . children ( ) . length && ! this . tfoot . children ( ) . length )
2020-01-22 18:12:56 +01:00
{
parent = this . thead ;
}
break ;
case 'footer' :
2021-08-19 01:41:23 +02:00
if ( ! this . tbody . children ( ) . length )
2020-01-22 18:12:56 +01:00
{
parent = this . tfoot ;
}
break ;
}
const tr = jQuery ( document . createElement ( "tr" ) ) . appendTo ( parent )
. addClass ( this . rowData [ y ] [ "class" ] ) ;
2021-08-19 01:41:23 +02:00
if ( this . rowData [ y ] . disabled )
2020-01-22 18:12:56 +01:00
{
tr . hide ( ) ;
}
2021-08-19 01:41:23 +02:00
if ( this . rowData [ y ] . height != "auto" )
2020-01-22 18:12:56 +01:00
{
tr . height ( this . rowData [ y ] . height ) ;
}
2021-08-19 01:41:23 +02:00
if ( this . rowData [ y ] . valign )
2020-01-22 18:12:56 +01:00
{
tr . attr ( "valign" , this . rowData [ y ] . valign ) ;
}
if ( this . rowData [ y ] . id )
{
tr . attr ( "id" , this . rowData [ y ] . id ) ;
}
// Create the cells. x is incremented by the colSpan value of the
// cell.
2021-08-19 01:41:23 +02:00
for ( let x = 0 ; x < w ; )
2020-01-22 18:12:56 +01:00
{
// Fetch a cell from the cells
const cell = this . _getCell ( _cells , x , y ) ;
2021-08-19 01:41:23 +02:00
if ( cell . td == null && cell . widget != null )
2020-01-22 18:12:56 +01:00
{
// Create the cell
const td = jQuery ( document . createElement ( "td" ) ) . appendTo ( tr )
. addClass ( cell [ "class" ] ) ;
2021-08-19 01:41:23 +02:00
if ( cell . disabled )
2020-01-22 18:12:56 +01:00
{
td . hide ( ) ;
2021-08-19 01:41:23 +02:00
// Need to do different things with webComponents
2022-01-19 21:37:40 +01:00
if ( typeof cell . widget . options === "object" )
2021-08-19 01:41:23 +02:00
{
cell . widget . options . disabled = cell . disabled ;
}
else if ( cell . widget . nodeName )
{
cell . widget . disabled = cell . disabled ;
}
2020-01-22 18:12:56 +01:00
}
2022-08-03 10:57:49 +02:00
// do NOT set cell width, if we have a colspan
if ( cell . width !== "auto" && cell . colSpan <= 1 )
2020-01-22 18:12:56 +01:00
{
td . width ( cell . width ) ;
}
2021-08-19 01:41:23 +02:00
if ( cell . align )
2020-01-22 18:12:56 +01:00
{
2021-08-19 01:41:23 +02:00
td . attr ( "align" , cell . align ) ;
2020-01-22 18:12:56 +01:00
}
// Add the entry for the widget to the management array
this . managementArray . push ( {
"cell" : td [ 0 ] ,
"widget" : cell . widget ,
"disabled" : cell . disabled
} ) ;
// Set the span values of the cell
const cs = ( x == w - 1 ) ? w - x : Math.min ( w - x , cell . colSpan ) ;
const rs = ( y == h - 1 ) ? h - y : Math.min ( h - y , cell . rowSpan ) ;
// Set the col and row span values
2021-08-19 01:41:23 +02:00
if ( cs > 1 )
{
2020-01-22 18:12:56 +01:00
td . attr ( "colspan" , cs ) ;
}
2021-08-19 01:41:23 +02:00
if ( rs > 1 )
{
2020-01-22 18:12:56 +01:00
td . attr ( "rowspan" , rs ) ;
}
// Assign the td to the cell
2021-08-19 01:41:23 +02:00
for ( let sx = x ; sx < x + cs ; sx ++ )
2020-01-22 18:12:56 +01:00
{
2021-08-19 01:41:23 +02:00
for ( let sy = y ; sy < y + rs ; sy ++ )
2020-01-22 18:12:56 +01:00
{
this . _getCell ( _cells , sx , sy ) . td = td ;
}
}
x += cell . colSpan ;
}
else
{
x ++ ;
}
}
}
}
getDOMNode ( _sender ? ) : HTMLElement | null
{
// If the parent class functions are asking for the DOM-Node, return the
// outer table.
2021-08-19 01:41:23 +02:00
if ( _sender == this || typeof _sender == 'undefined' )
2020-01-22 18:12:56 +01:00
{
return this . wrapper != null ? this . wrapper [ 0 ] : this . table [ 0 ] ;
}
// Check whether the _sender object exists inside the management array
2021-08-19 01:41:23 +02:00
for ( let i = 0 ; i < this . managementArray . length ; i ++ )
2020-01-22 18:12:56 +01:00
{
2021-08-19 01:41:23 +02:00
if ( this . managementArray [ i ] . widget == _sender )
2020-01-22 18:12:56 +01:00
{
return this . managementArray [ i ] . cell ;
}
}
return null ;
}
2021-08-19 01:41:23 +02:00
isInTree ( _sender? : et2_widget ) : boolean
2020-01-22 18:12:56 +01:00
{
let vis = true ;
2021-08-19 01:41:23 +02:00
if ( typeof _sender != "undefined" && _sender != this )
2020-01-22 18:12:56 +01:00
{
vis = false ;
// Check whether the _sender object exists inside the management array
2021-08-19 01:41:23 +02:00
for ( let i = 0 ; i < this . managementArray . length ; i ++ )
2020-01-22 18:12:56 +01:00
{
2021-08-19 01:41:23 +02:00
if ( this . managementArray [ i ] . widget == _sender )
2020-01-22 18:12:56 +01:00
{
vis = ! ( typeof this . managementArray [ i ] . disabled === 'boolean' ?
2021-08-19 01:41:23 +02:00
this . managementArray [ i ] . disabled :
this . getArrayMgr ( "content" ) . parseBoolExpression ( this . managementArray [ i ] . disabled )
2020-01-22 18:12:56 +01:00
) ;
break ;
}
}
}
return super . isInTree ( this , vis ) ;
}
/ * *
* Set the overflow attribute
*
* Grid needs special handling because HTML tables don ' t do overflow . We
* create a wrapper DIV to handle it .
* No value or default visible needs no wrapper , as table is always overflow visible .
*
* @param { string } _value Overflow value , must be a valid CSS overflow value , default 'visible'
* /
2021-08-19 01:41:23 +02:00
set_overflow ( _value : string )
2020-01-22 18:12:56 +01:00
{
let wrapper = this . wrapper || this . table . parent ( '[id$="_grid_wrapper"]' ) ;
this . overflow = _value ;
if ( wrapper . length == 0 && _value && _value !== 'visible' )
{
2021-08-19 01:41:23 +02:00
this . wrapper = wrapper = this . table . wrap ( '<div id="' + this . id + '_grid_wrapper"></div>' ) . parent ( ) ;
2020-01-22 18:12:56 +01:00
if ( this . height )
{
wrapper . css ( 'height' , this . height ) ;
}
}
wrapper . css ( 'overflow' , _value ) ;
2021-08-19 01:41:23 +02:00
if ( wrapper . length && ( ! _value || _value === 'visible' ) )
{
2020-01-22 18:12:56 +01:00
this . table . unwrap ( ) ;
}
}
/ * *
* Change the content for the grid , and re - generate its contents .
*
* Changing the content does not allow changing the structure of the grid ,
* as that is loaded from the template file . The rows and widgets inside
* will be re - created ( including auto - repeat ) .
*
* @param { Object } _value New data for the grid
* @param { Object } [ _value . content ] New content
* @param { Object } [ _value . sel_options ] New select options
* @param { Object } [ _value . readonlys ] New read - only values
* /
2021-08-19 01:41:23 +02:00
set_value ( _value : { content? : object , sel_options? : object , readonlys? : object } )
2020-01-22 18:12:56 +01:00
{
// Destroy children, empty grid
for ( let i = 0 ; i < this . managementArray . length ; i ++ )
{
const cell = this . managementArray [ i ] ;
if ( cell . widget )
{
2021-11-10 19:47:07 +01:00
this . removeChild ( cell . widget ) ;
2020-01-22 18:12:56 +01:00
cell . widget . destroy ( ) ;
}
}
this . managementArray = [ ] ;
this . thead . empty ( ) ;
this . tfoot . empty ( ) ;
this . tbody . empty ( ) ;
// Update array managers
for ( let key in _value )
{
this . getArrayMgr ( key ) . data = _value [ key ] ;
}
// Rebuild grid
this . loadFromXML ( this . template_node ) ;
// New widgets need to finish
let promises = [ ] ;
this . loadingFinished ( promises ) ;
}
/ * *
* Sortable allows you to reorder grid rows using the mouse .
* The new order is returned as part of the value of the
* grid , in 'sort_order' .
*
* @param { boolean | function } sortable Callback or false to disable
* /
2021-08-19 01:41:23 +02:00
set_sortable ( sortable : boolean | Function )
2020-01-22 18:12:56 +01:00
{
2021-08-03 15:59:22 +02:00
const self = this ;
let tbody = this . getDOMNode ( ) . getElementsByTagName ( 'tbody' ) [ 0 ] ;
if ( ! sortable && this . sortablejs )
2020-01-22 18:12:56 +01:00
{
2021-08-03 15:59:22 +02:00
this . sortablejs . destroy ( ) ;
2020-01-22 18:12:56 +01:00
return ;
}
2021-08-19 01:41:23 +02:00
for ( let i = 0 ; i < tbody . children . length ; i ++ )
2021-08-03 15:59:22 +02:00
{
2021-08-19 01:41:23 +02:00
if ( ! tbody . children [ i ] . classList . contains ( 'th' ) && ! tbody . children [ i ] . id )
2021-08-03 15:59:22 +02:00
{
tbody . children [ i ] . setAttribute ( 'id' , i . toString ( ) ) ;
}
}
2020-01-22 18:12:56 +01:00
2021-08-19 01:41:23 +02:00
this . sortablejs = new Sortable ( tbody , {
2021-08-11 19:28:01 +02:00
group : this.options.sortable_connectWith ,
draggable : "tr:not(.th)" ,
filter : this.options.sortable_cancel ,
ghostClass : this.options.sortable_placeholder ,
dataIdAttr : 'id' ,
2021-08-19 01:41:23 +02:00
onAdd : function ( event )
{
if ( typeof self . options . sortable_recieveCallback == 'function' )
{
2021-08-11 19:28:01 +02:00
self . options . sortable_recieveCallback . call ( self , event , this , self . id ) ;
}
} ,
2021-08-19 01:41:23 +02:00
onStart : function ( event , ui )
{
if ( typeof self . options . sortable_startCallback == 'function' )
{
2021-08-11 19:28:01 +02:00
self . options . sortable_startCallback . call ( self , event , this , self . id ) ;
}
} ,
2021-08-19 01:41:23 +02:00
onSort : function ( event )
{
self . egw ( ) . json ( sortable , [
2021-08-11 19:28:01 +02:00
self . getInstanceManager ( ) . etemplate_exec_id ,
self . sortablejs . toArray ( ) ,
self . id ] ,
null ,
self ,
true
) . sendRequest ( ) ;
} ,
} ) ;
2020-01-22 18:12:56 +01:00
}
/ * *
* Override parent to apply actions on each row
*
* @param { array } actions [ { ID : attributes.. } + ] as for set_actions
* /
_link_actions ( actions : object [ ] )
{
2021-08-19 01:41:23 +02:00
// Get the top level element for the tree
2021-04-19 14:00:38 +02:00
// get appObjectManager for the actual app, it might not always be the current app(e.g. running app content under admin tab)
2020-01-29 22:29:06 +01:00
// @ts-ignore
2021-06-09 14:28:29 +02:00
let objectManager = egw_getAppObjectManager ( true , this . getInstanceManager ( ) . app ) ;
2021-08-19 01:41:23 +02:00
objectManager = objectManager . getObjectById ( this . getInstanceManager ( ) . uniqueId , 2 ) || objectManager ;
2020-01-22 18:12:56 +01:00
let widget_object = objectManager . getObjectById ( this . id ) ;
2021-08-19 01:41:23 +02:00
if ( widget_object == null )
{
2020-01-22 18:12:56 +01:00
// Add a new container to the object manager which will hold the widget
// objects
widget_object = objectManager . insertObject ( false , new egwActionObject (
2020-02-27 19:54:55 +01:00
this . id , objectManager , new et2_action_object_impl ( this ) . getAOI ( ) ,
2020-01-22 18:12:56 +01:00
this . _actionManager || objectManager . manager . getActionById ( this . id ) || objectManager . manager
) ) ;
}
// Delete all old objects
widget_object . clear ( ) ;
// Go over the widget & add links - this is where we decide which actions are
// 'allowed' for this widget at this time
const action_links = this . _get_action_links ( actions ) ;
// Deal with each row in tbody, ignore action-wise rows in thead or tfooter for now
let i = 0 , r = 0 ;
for ( ; i < this . rowData . length ; i ++ )
{
2021-08-19 01:41:23 +02:00
if ( this . rowData [ i ] . part != 'body' ) continue ;
2020-01-22 18:12:56 +01:00
const content = this . getArrayMgr ( 'content' ) . getEntry ( i ) ;
if ( content )
{
// Add a new action object to the object manager
const row = jQuery ( 'tr' , this . tbody ) [ r ] ;
2020-02-27 19:54:55 +01:00
const aoi = new et2_action_object_impl ( this , row ) . getAOI ( ) ;
2020-01-22 18:12:56 +01:00
const obj = widget_object . addObject ( content . id || "row_" + r , aoi ) ;
// Set the data to the content so it's available for the action
obj . data = content ;
obj . updateActionLinks ( action_links ) ;
}
r ++ ;
}
}
/ * *
* Code for implementing et2_IDetachedDOM
* This doesn ' t need to be implemented .
* Individual widgets are detected and handled by the grid , but the interface is needed for this to happen
*
* @param { array } _attrs array to add further attributes to
* /
getDetachedAttributes ( _attrs )
{
}
getDetachedNodes ( )
{
return [ this . getDOMNode ( ) ] ;
}
setDetachedAttributes ( _nodes , _values )
{
}
/ * *
* Generates nextmatch column name for headers in a grid
*
* Implemented here as default implementation in et2_externsion_nextmatch
* only considers children , but grid does NOT instanciate disabled rows as children .
*
* @return { string }
* /
_getColumnName ( )
{
const ids = [ ] ;
2021-08-19 01:41:23 +02:00
for ( let r = 0 ; r < this . cells . length ; ++ r )
2020-01-22 18:12:56 +01:00
{
const cols = this . cells [ r ] ;
2021-08-19 01:41:23 +02:00
for ( let c = 0 ; c < cols . length ; ++ c )
2020-01-22 18:12:56 +01:00
{
2021-08-19 01:41:23 +02:00
if ( cols [ c ] . nm_id ) ids . push ( cols [ c ] . nm_id ) ;
2020-01-22 18:12:56 +01:00
}
}
return ids . join ( '_' ) ;
}
2021-08-19 01:41:23 +02:00
resize ( _height )
2020-01-22 18:12:56 +01:00
{
2021-08-19 01:41:23 +02:00
if ( typeof this . options != 'undefined' && _height
&& typeof this . options . resize_ratio != 'undefined' && this . options . resize_ratio )
2020-01-22 18:12:56 +01:00
{
// apply the ratio
2021-08-19 01:41:23 +02:00
_height = ( this . options . resize_ratio != '' ) ? _height * this . options.resize_ratio : _height ;
if ( _height != 0 )
2020-01-22 18:12:56 +01:00
{
2021-08-19 01:41:23 +02:00
if ( this . wrapper )
2020-01-22 18:12:56 +01:00
{
this . wrapper . height ( this . wrapper . height ( ) + _height ) ;
}
else
{
2021-08-19 01:41:23 +02:00
this . table . height ( this . table . height ( ) + _height ) ;
2020-01-22 18:12:56 +01:00
}
}
}
}
/ * *
* Get a dummy row object containing all widget of a row
*
* This is only a temp . solution until rows are implemented as eT2 containers and
* _sender . getParent ( ) will return a real row container .
*
* @deprecated Not used ? Remove this if you find something using it
*
* @param { et2_widget } _sender
* @returns { Array | undefined }
* /
getRow ( _sender : et2_widget ) : et2_widget
{
2021-08-19 01:41:23 +02:00
if ( ! _sender || ! this . cells ) return ;
2020-01-22 18:12:56 +01:00
2021-08-19 01:41:23 +02:00
for ( let r = 0 ; r < this . cells . length ; ++ r )
2020-01-22 18:12:56 +01:00
{
const row = this . cells [ r ] ;
2021-08-19 01:41:23 +02:00
for ( var c = 0 ; c < row . length ; ++ c )
2020-01-22 18:12:56 +01:00
{
2021-08-19 01:41:23 +02:00
if ( ! row [ c ] . widget ) continue ;
2020-01-22 18:12:56 +01:00
let found = row [ c ] . widget === _sender ;
2021-08-19 01:41:23 +02:00
if ( ! found ) row [ c ] . widget . iterateOver ( function ( _widget )
{
if ( _widget === _sender ) found = true ;
} ) ;
if ( found )
2020-01-22 18:12:56 +01:00
{
// return a fake row object allowing to iterate over it's children
2021-08-19 01:41:23 +02:00
const row_obj : et2_widget = new et2_widget ( this , { } ) ;
for ( var c = 0 ; c < row . length ; ++ c )
2020-01-22 18:12:56 +01:00
{
2021-08-19 01:41:23 +02:00
if ( row [ c ] . widget ) row_obj . addChild ( row [ c ] . widget ) ;
2020-01-22 18:12:56 +01:00
}
row_obj . isInTree = jQuery . proxy ( this . isInTree , this ) ;
// we must not free the children!
2021-08-19 01:41:23 +02:00
row_obj . destroy = function ( )
{
2020-01-22 18:12:56 +01:00
// @ts-ignore
delete row_obj . _children ;
} ;
return row_obj ;
}
}
}
}
/ * *
* Needed for the align interface , but we ' re not doing anything with it . . .
* /
2021-08-19 01:41:23 +02:00
get_align ( ) : string
{
2020-01-22 18:12:56 +01:00
return "" ;
}
}
2021-08-19 01:41:23 +02:00
2020-01-22 18:12:56 +01:00
et2_register_widget ( et2_grid , [ "grid" ] ) ;
interface ColumnEntry
{
id? : string ,
2021-08-19 01:41:23 +02:00
width : string | number , // 'auto'
class : string , // "",
align : string , // "",
span : string | number // "1",
disabled : boolean // false
2020-01-22 18:12:56 +01:00
}
interface RowEntry
{
id? : string
2021-08-19 01:41:23 +02:00
height : string | number , // "auto",
class : string , // "",
valign : string , // "top",
span : string | number , // "1",
disabled : boolean // false
2020-01-22 18:12:56 +01:00
}