2011-08-10 16:36:31 +02:00
/ * *
2011-08-24 09:18:59 +02:00
* EGroupware eTemplate2 - JS Tabs object
2011-08-10 16:36:31 +02:00
*
* @ license http : //opensource.org/licenses/gpl-license.php GPL - GNU General Public License
* @ package etemplate
* @ subpackage api
* @ link http : //www.egroupware.org
* @ author Andreas Stöckel
* @ copyright Stylite 2011
* @ version $Id$
* /
"use strict" ;
/ * e g w : u s e s
2011-08-23 19:10:56 +02:00
jsapi . egw ;
2011-08-10 16:36:31 +02:00
jquery . jquery ;
2011-08-24 12:18:07 +02:00
et2 _core _widget ;
2011-08-10 16:36:31 +02:00
* /
/ * *
* Class which implements the tabbox - tag
2013-04-13 21:00:13 +02:00
*
* @ augments et2 _DOMWidget
2011-08-10 16:36:31 +02:00
* /
2013-05-10 22:39:12 +02:00
var et2 _tabbox = et2 _valueWidget . extend ( [ et2 _IInput ] ,
2013-04-13 21:00:13 +02:00
{
2012-07-09 23:26:24 +02:00
attributes : {
'tabs' : {
'name' : 'Tabs' ,
'default' : et2 _no _init ,
2013-05-06 19:17:52 +02:00
'description' : "Array of [extra] tabs. Each tab needs {label:..., template:...}. Additional optional keys are hidden and id, for access into content array"
2012-07-09 23:26:24 +02:00
}
} ,
2011-08-23 23:33:40 +02:00
/ * *
* Currently selected tab
* /
selected _index : 0 ,
2013-04-13 21:00:13 +02:00
/ * *
* Construtor
*
* @ memberOf et2 _tabbox
* /
2011-08-24 12:05:52 +02:00
init : function ( ) {
2011-08-10 16:36:31 +02:00
// Create the outer tabbox container
this . container = $j ( document . createElement ( "div" ) )
. addClass ( "et2_tabbox" ) ;
// Create the upper container for the tab flags
2011-08-17 11:07:09 +02:00
this . flagContainer = $j ( document . createElement ( "div" ) )
. addClass ( "et2_tabheader" )
2011-08-10 16:36:31 +02:00
. appendTo ( this . container ) ;
// Create the lower tab container
this . tabContainer = $j ( document . createElement ( "div" ) )
. addClass ( "et2_tabs" )
. appendTo ( this . container ) ;
this . _super . apply ( this , arguments ) ;
this . tabData = [ ] ;
} ,
2011-08-24 12:05:52 +02:00
destroy : function ( ) {
2011-08-10 16:36:31 +02:00
this . _super . apply ( this , arguments ) ;
this . container = null ;
this . flagContainer = null ;
this . tabData = [ ] ;
} ,
_readTabs : function ( tabData , tabs ) {
2011-08-23 23:33:40 +02:00
var selected = "" ;
this . selected _index = 0 ;
var hidden = { } ;
if ( this . id )
{
// Set the value for this element
var contentMgr = this . getArrayMgr ( "content" ) ;
if ( contentMgr != null ) {
2011-09-08 20:36:09 +02:00
var val = contentMgr . getEntry ( this . id ) ;
2011-08-23 23:33:40 +02:00
if ( val !== null )
{
selected = val ;
}
}
contentMgr = this . getArrayMgr ( "readonlys" ) ;
if ( contentMgr != null ) {
2011-09-08 20:36:09 +02:00
var val = contentMgr . getEntry ( this . id ) ;
2011-10-18 17:41:30 +02:00
if ( val !== null && typeof val !== 'undefined' )
2011-08-23 23:33:40 +02:00
{
hidden = val ;
}
}
}
var i = 0 ;
2011-08-10 16:36:31 +02:00
et2 _filteredNodeIterator ( tabs , function ( node , nodeName ) {
if ( nodeName == "tab" )
{
2011-08-24 09:18:59 +02:00
var index _name = et2 _readAttrWithDefault ( node , "id" ) ;
2011-08-23 23:33:40 +02:00
var hide = false ;
if ( index _name ) {
if ( selected == index _name ) this . selected _index = i ;
if ( hidden [ index _name ] ) {
hide = true ;
}
}
2011-08-10 16:36:31 +02:00
tabData . push ( {
2013-05-10 22:39:12 +02:00
"id" : index _name ,
2012-03-02 11:44:56 +01:00
"label" : this . egw ( ) . lang ( et2 _readAttrWithDefault ( node , "label" , "Tab" ) ) ,
2011-08-10 16:36:31 +02:00
"widget" : null ,
"contentDiv" : null ,
2011-08-23 23:33:40 +02:00
"flagDiv" : null ,
"hidden" : hide
2011-08-10 16:36:31 +02:00
} ) ;
}
else
{
throw ( "Error while parsing: Invalid tag '" + nodeName +
"' in tabs tag" ) ;
}
2011-08-23 23:33:40 +02:00
i ++ ;
2011-08-10 16:36:31 +02:00
} , this ) ;
} ,
_readTabPanels : function ( tabData , tabpanels ) {
var i = 0 ;
et2 _filteredNodeIterator ( tabpanels , function ( node , nodeName ) {
if ( i < tabData . length )
{
// Create the widget corresponding to the given node
tabData [ i ] . widget = this . createElementFromNode ( node ,
nodeName ) ;
}
else
{
throw ( "Error while reading tabpanels tag, too many widgets!" ) ;
}
i ++ ;
} , this ) ;
} ,
loadFromXML : function ( _node ) {
// Get the tabs and tabpanels tags
var tabsElems = et2 _directChildrenByTagName ( _node , "tabs" ) ;
var tabpanelsElems = et2 _directChildrenByTagName ( _node , "tabpanels" ) ;
2013-05-09 11:28:27 +02:00
var tabData = [ ] ;
2011-08-10 16:36:31 +02:00
2013-05-09 11:28:27 +02:00
// Check for a parent height, we'll apply it to tab panels
var height = et2 _readAttrWithDefault ( _node . parentNode , "height" , null ) ;
if ( height )
2011-08-10 16:36:31 +02:00
{
2013-05-09 11:28:27 +02:00
this . tabContainer . css ( "height" , height ) ;
2013-05-06 19:17:52 +02:00
}
2011-08-10 16:36:31 +02:00
2013-05-09 11:28:27 +02:00
// either use tabs defined via modification or xml (not both)
2013-05-09 06:05:03 +02:00
if ( this . options . tabs )
2013-05-06 19:17:52 +02:00
{
var readonly = this . getArrayMgr ( "readonlys" ) . getEntry ( this . id ) || { } ;
for ( var i = 0 ; i < this . options . tabs . length ; i ++ )
2012-07-09 23:26:24 +02:00
{
2013-05-06 19:17:52 +02:00
var tab = this . options . tabs [ i ] ;
var tab _id = tab . id || tab . template ;
var tab _options = { id : tab _id , template : tab . template } ;
if ( tab . id )
2012-07-09 23:26:24 +02:00
{
2013-05-06 19:17:52 +02:00
tab _options . content = tab . id ;
2012-07-09 23:26:24 +02:00
}
2013-05-06 19:17:52 +02:00
tabData . push ( {
2013-05-10 22:39:12 +02:00
"id" : tab . id ,
2013-05-06 19:17:52 +02:00
"label" : this . egw ( ) . lang ( tab . label ) ,
"widget" : et2 _createWidget ( 'template' , tab _options , this ) ,
"contentDiv" : null ,
"flagDiv" : null ,
"hidden" : typeof tab . hidden != "undefined" ? tab . hidden : readonly [ tab _id ] || false
} ) ;
2012-07-09 23:26:24 +02:00
}
2011-08-10 16:36:31 +02:00
}
2013-05-09 11:28:27 +02:00
else if ( tabsElems . length == 1 && tabpanelsElems . length == 1 )
{
var tabs = tabsElems [ 0 ] ;
var tabpanels = tabpanelsElems [ 0 ] ;
// Parse the "tabs" tag
this . _readTabs ( tabData , tabs ) ;
// Read and create the widgets defined in the "tabpanels"
this . _readTabPanels ( tabData , tabpanels ) ;
}
else
{
this . egw ( ) . debug ( "error" , "Error while parsing tabbox, none or multiple tabs or tabpanels tags!" , this ) ;
}
2013-05-06 19:17:52 +02:00
// Create the tab DOM-Nodes
this . createTabs ( tabData ) ;
2011-08-10 16:36:31 +02:00
} ,
2012-07-09 23:26:24 +02:00
/ * *
* Check for custom tabs
* /
transformAttributes : function ( _attrs ) {
this . _super . apply ( this , arguments ) ;
// Add in settings that are objects
var data = this . getArrayMgr ( "modifications" ) . getEntry ( this . id ) ;
for ( var key in data )
{
if ( typeof data [ key ] === 'object' && ! _attrs [ key ] ) _attrs [ key ] = data [ key ] ;
}
} ,
2011-08-10 16:36:31 +02:00
createTabs : function ( tabData ) {
this . tabData = tabData ;
this . tabContainer . empty ( ) ;
this . flagContainer . empty ( ) ;
for ( var i = 0 ; i < this . tabData . length ; i ++ )
{
var entry = this . tabData [ i ] ;
entry . flagDiv = $j ( document . createElement ( "span" ) )
. addClass ( "et2_tabflag" )
2013-05-10 22:39:12 +02:00
. appendTo ( this . flagContainer )
entry . flagDiv . text ( entry . label || "Tab" ) ;
2011-08-23 23:33:40 +02:00
if ( entry . hidden )
{
entry . flagDiv . hide ( ) ;
}
else
{
entry . flagDiv . click ( { "tabs" : this , "idx" : i } , function ( e ) {
2011-08-17 11:07:09 +02:00
e . data . tabs . setActiveTab ( e . data . idx ) ;
} ) ;
2011-08-23 23:33:40 +02:00
}
2011-08-10 16:36:31 +02:00
entry . contentDiv = $j ( document . createElement ( "div" ) )
. addClass ( "et2_tabcntr" )
. appendTo ( this . tabContainer ) ;
}
2011-08-17 11:07:09 +02:00
2011-08-23 23:33:40 +02:00
this . setActiveTab ( this . selected _index ) ;
2011-08-17 11:07:09 +02:00
} ,
setActiveTab : function ( _idx ) {
2011-08-23 23:33:40 +02:00
this . selected _index = _idx ;
2011-08-17 11:07:09 +02:00
// Remove the "active" flag from all tabs-flags
$j ( ".et2_tabflag" , this . flagContainer ) . removeClass ( "active" ) ;
// Hide all tab containers
this . tabContainer . children ( ) . hide ( ) ;
// Set the tab flag with the given index active and show the corresponding
// container
this . flagContainer . children ( ":eq(" + _idx + ")" ) . addClass ( "active" ) ;
this . tabContainer . children ( ":eq(" + _idx + ")" ) . show ( ) ;
2011-08-10 16:36:31 +02:00
} ,
getDOMNode : function ( _sender ) {
if ( _sender == this )
{
return this . container [ 0 ] ;
}
else
{
for ( var i = 0 ; i < this . tabData . length ; i ++ )
{
if ( this . tabData [ i ] . widget == _sender )
{
return this . tabData [ i ] . contentDiv [ 0 ] ;
}
}
return null ;
}
2011-08-19 18:00:44 +02:00
} ,
set _height : function ( _value ) {
this . height = _value ;
this . tabContainer . css ( "height" , _value ) ;
2013-05-10 22:39:12 +02:00
} ,
2011-08-10 16:36:31 +02:00
2013-05-10 22:39:12 +02:00
/ * *
* getValue has to return the value of the input widget
* /
getValue : function ( ) {
return this . tabData [ this . selected _index ] . id ;
} ,
/ * *
* Is dirty returns true if the value of the widget has changed since it
* was loaded .
* /
isDirty : function ( ) {
return this . selected _index != this . value ;
} ,
/ * *
* Causes the dirty flag to be reseted .
* /
resetDirty : function ( )
{
this . value = this . selected _index ;
}
2011-08-10 16:36:31 +02:00
} ) ;
et2 _register _widget ( et2 _tabbox , [ "tabbox" ] ) ;