[ Index ]
 

Code source de eZ Publish 3.9.0

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/design/standard/javascript/contentstructuremenu/ -> contentstructuremenu.js (source)

   1  //
   2  // Created on: <14-Jul-2004 14:18:58 dl>
   3  //
   4  // SOFTWARE NAME: eZ publish
   5  // SOFTWARE RELEASE: 3.9.0
   6  // BUILD VERSION: 17785
   7  // COPYRIGHT NOTICE: Copyright (C) 1999-2006 eZ systems AS
   8  // SOFTWARE LICENSE: GNU General Public License v2.0
   9  // NOTICE: >
  10  //   This program is free software; you can redistribute it and/or
  11  //   modify it under the terms of version 2.0  of the GNU General
  12  //   Public License as published by the Free Software Foundation.
  13  //
  14  //   This program is distributed in the hope that it will be useful,
  15  //   but WITHOUT ANY WARRANTY; without even the implied warranty of
  16  //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17  //   GNU General Public License for more details.
  18  //
  19  //   You should have received a copy of version 2.0 of the GNU General
  20  //   Public License along with this program; if not, write to the Free
  21  //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  22  //   MA 02110-1301, USA.
  23  //
  24  //
  25  
  26  /*! \file contentstructuremenu.js
  27  */
  28  
  29  /*!
  30      \brief
  31      This is a set of functions which helps to organize a work of
  32      tree-menu: fold/unfold nodes, save/restore state of menu
  33      to/from cookie, some helpers.
  34  
  35  
  36     Functions which works with cookie but adapted to tree menu:
  37          ezcst_cookie_restoreUnfoldedNodesList,
  38          ezcst_cookie_saveUnfoldedNodesList,
  39          ezcst_cookie_addNode,
  40          ezcst_cookie_addNodesList,
  41          ezcst_cookie_removeNode.
  42  
  43      Functions which change state of node(folded/unfolded):
  44          ezcst_changeState,
  45          ezcst_setFoldedState,
  46          ezcst_setUnfoldedState,
  47          ezcst_foldUnfold,
  48          ezcst_foldUnfoldSubtree,
  49          ezcst_unfoldNode.
  50  
  51      Functions which initializes menu:
  52          ezcst_createUnfoldedLabel,
  53          ezcst_createEmptyLabel,
  54          ezcst_setFoldUnfoldIcons,
  55          ezcst_initializeMenuState,
  56          ezcst_resetMenuState,
  57          ezcst_restoreMenuState.
  58  
  59      Event handlers:
  60          ezcst_onFoldClicked,
  61          ezcst_onItemClicked.
  62  
  63  */
  64  
  65  /*!
  66      Global array of unfolded nodes
  67  */
  68  var gUnfoldedNodesList                          = new Array(0);
  69  
  70  /*!
  71      Pathes to fold/unfold icons
  72  */
  73  var gUseFoldUnfoldIcons                         = false;
  74  var gFoldIcon                                   = '';
  75  var gUnfoldIcon                                 = '';
  76  var gEmptyIcon                                  = '';
  77  var gFoldUnfoldIconsWidth                       = 16;
  78  var gFoldUnfoldIconsHeight                      = 16;
  79  
  80  /*!
  81      Global identifier of the Root Node
  82  */
  83  var gRootNodeID                                 = 0;
  84  
  85  /*!
  86      CSS class names for current of node
  87  */
  88  var  EZCST_HIGHLIGHTED_NODE_CLASS_NAME          = "currentnode";
  89  
  90  /*!
  91      Cookie name where unfolded nodes list is stored
  92  */
  93  var  EZCST_UNFOLDED_LIST_COOKIE_NAME            = "ezcst_unfolded_node_list";
  94  var  EZCST_UNFOLDED_LIST_VALUES_DELIMITER       = ",";
  95  
  96  /*!
  97      Default url to redirect when user clicks on menu item.
  98  */
  99  var  gItemClickAction                           = "";
 100  
 101  /*!
 102      \return size of \a gUnfoldedNodesList.
 103  */
 104  function ezcst_getUnfoldedNodesListSize()
 105  {
 106      return gUnfoldedNodesList.length;
 107  }
 108  
 109  /*!
 110      Searches a node \a node_id in \a gUnfoldedNodesList
 111      \return if found nothing returns -1, else index of node.
 112  */
 113  function ezcst_findNodeIDInList( node_id )
 114  {
 115      var len = gUnfoldedNodesList.length;
 116      for ( var i = 0; i < len; ++i )
 117      {
 118          if( gUnfoldedNodesList[i] == node_id )
 119              return i;
 120      }
 121  
 122      return -1;
 123  }
 124  
 125  /*!
 126      cookie wrappers
 127  */
 128  /*!
 129       Initializes \a gUnfoldedNodesList with values stored in cookie
 130  */
 131  function ezcst_cookie_restoreUnfoldedNodesList()
 132  {
 133      gUnfoldedNodesList = ezjslib_getCookieToArray( EZCST_UNFOLDED_LIST_COOKIE_NAME, EZCST_UNFOLDED_LIST_VALUES_DELIMITER );
 134  }
 135  
 136  /*!
 137       Stores values from \a gUnfoldedNodesList to cookie
 138  */
 139  function ezcst_cookie_saveUnfoldedNodesList()
 140  {
 141      ezjslib_setCookieFromArray( EZCST_UNFOLDED_LIST_COOKIE_NAME, gUnfoldedNodesList, null, EZCST_UNFOLDED_LIST_VALUES_DELIMITER );
 142  }
 143  
 144  /*!
 145       Adds \a node_id to \a gUnfoldedNodesList and store it in cookie
 146  */
 147  function ezcst_cookie_addNode( node_id )
 148  {
 149      if ( node_id )
 150      {
 151          if ( ezcst_findNodeIDInList( node_id ) == -1 )
 152          {
 153              gUnfoldedNodesList[gUnfoldedNodesList.length] = node_id;
 154              ezcst_cookie_saveUnfoldedNodesList();
 155          }
 156      }
 157  }
 158  
 159  /*!
 160       Adds \a nodesList to \a gUnfoldedNodesList and store it in cookie
 161  */
 162  function ezcst_cookie_addNodesList( nodesList )
 163  {
 164      if ( nodesList )
 165      {
 166          var len = nodesList.length;
 167          for ( var i = 0; i < len; ++i )
 168          {
 169              ezcst_cookie_addNode( nodesList[i] );
 170          }
 171      }
 172  }
 173  
 174  /*!
 175       Removes \a node_id from \a gUnfoldedNodesList and cookie
 176  */
 177  function ezcst_cookie_removeNode( node_id )
 178  {
 179      if ( node_id && (node_id != null) )
 180      {
 181          var idx = ezcst_findNodeIDInList( node_id );
 182          if ( idx != -1 )
 183          {
 184              gUnfoldedNodesList.splice( idx, 1 );
 185              ezcst_cookie_saveUnfoldedNodesList();
 186          }
 187      }
 188  }
 189  
 190  /*!
 191      Changes state(folded/unfolded) of node.
 192      Saves \a node_id in cookie,
 193      changes display status of \a ul_node,
 194      changes text of \a link_node
 195  */
 196  function ezcst_changeState( node_id, ul_node, link_node, bForceFold, bForceUnfold )
 197  {
 198      // change display state of ul_node and label for link_node
 199      if ( ul_node )
 200      {
 201          if ( bForceFold )
 202          {
 203              ezcst_setFoldedState( node_id, ul_node, link_node );
 204              return;
 205          }
 206  
 207          if ( bForceUnfold )
 208          {
 209              ezcst_setUnfoldedState( node_id, ul_node, link_node );
 210              return;
 211          }
 212  
 213          if ( ul_node.style.display == "none" )
 214          {
 215              ezcst_setUnfoldedState( node_id, ul_node, link_node );
 216          }
 217          else
 218          {
 219              ezcst_setFoldedState( node_id, ul_node, link_node );
 220          }
 221      }
 222  }
 223  
 224  /*!
 225      Sets unfolded state of node.
 226      Saves \a node_id in cookie,
 227      changes display status of \a ul_node,
 228      changes text of \a link_node
 229  */
 230  function ezcst_setUnfoldedState( node_id, ul_node, link_node )
 231  {
 232      if ( ul_node && ul_node.style.display == "none" )
 233      {
 234          // fold state => make it unfold
 235              ul_node.style.display = "";
 236  
 237         // change label
 238              if ( gUseFoldUnfoldIcons )
 239                  ezjslib_setImageSourceToHTMLChildImageNode( link_node, gUnfoldIcon );
 240              else
 241                  ezjslib_setTextToHTMLChildTextNode( link_node, "[-]" );
 242  
 243          // update cookie
 244              ezcst_cookie_addNode( node_id );
 245      }
 246  }
 247  
 248  /*!
 249      Sets folded state of node.
 250      Saves \a node_id in cookie,
 251      changes display status of \a ul_node,
 252      changes text of \a link_node
 253  */
 254  function ezcst_setFoldedState( node_id, ul_node, link_node )
 255  {
 256      if ( ul_node && ul_node.style.display != "none" )
 257      {
 258          // unfold state => make it fold
 259              ul_node.style.display = "none";
 260  
 261         //Change label
 262              if ( gUseFoldUnfoldIcons )
 263                  ezjslib_setImageSourceToHTMLChildImageNode( link_node, gFoldIcon );
 264              else
 265                  ezjslib_setTextToHTMLChildTextNode( link_node, "[+]" );
 266  
 267          // update cookie
 268              ezcst_cookie_removeNode( node_id );
 269      }
 270  }
 271  
 272  /*!
 273      onClick handler for \a node
 274  */
 275  function ezcst_onFoldClicked( node )
 276  {
 277      ezcst_foldUnfold( node, true, false, false, false );
 278  }
 279  
 280  /*!
 281      onClick handler for menu item.
 282      \a ezpublish_nodeID is a id of the node
 283      \a defaultItemClickAction default redirect url
 284  */
 285  function ezcst_onItemClicked( ezpublish_nodeID, defaultItemClickAction )
 286  {
 287      var redirectURL = ( gItemClickAction != '' ) ? ( gItemClickAction + '/' + ezpublish_nodeID ) : defaultItemClickAction;
 288      location.href = redirectURL;
 289  }
 290  
 291  /*!
 292      Fold/unfold \a node. If \a bUpdateCookie sets to
 293      \a true then cookie will be updated.
 294      \a bInitFoldUnfoldLabels - initialize HTML nodes( e.g sets text [-]/[ ])
 295  */
 296  function ezcst_foldUnfold( node, bUpdateCookie, bInitFoldUnfoldLabels, bForceFold, bForceUnfold )
 297  {
 298      if( node )
 299      {
 300          for ( var i = 0; i < node.childNodes.length; ++i )
 301          {
 302              var child = node.childNodes[i];
 303  
 304              if ( child["tagName"] && child.tagName.toLowerCase() == "ul" )
 305              {
 306                  var node_id     = bUpdateCookie ? node.getAttribute( "id" ) : null;
 307                  var link_node   = ezjslib_getHTMLChildNodeByTag( node, "a" );
 308  
 309                  if( bInitFoldUnfoldLabels == true)
 310                      ezcst_createUnfoldedLabel( link_node );
 311  
 312                  ezcst_changeState( node_id, child, link_node, bForceFold, bForceUnfold );
 313                  break;
 314              }
 315              else if ( bInitFoldUnfoldLabels && child["tagName"] && child.tagName.toLowerCase() == "span" )
 316              {
 317                  ezcst_createEmptyLabel( child );
 318              }
 319          }
 320      }
 321  }
 322  
 323  /*!
 324      Fold/unfold subtree by recursive calls.
 325      \a root_node is a root node of subtree.
 326      \a bUpdateCookie - if sets to \a true, then cookie will be updated.
 327      \a bInitFoldUnfoldLabels - initialize HTML nodes( e.g sets text [-]/[ ])
 328      \a bForceFold - if node was folded then its state will be unchanged
 329      \a bForceUnfold - if node was unfolded then its state will be unchanged
 330      \a bExludeRootNode - If true then state of root node will be unchanged.
 331  */
 332  function ezcst_foldUnfoldSubtree( rootNode, bUpdateCookie, bInitFoldUnfoldLabels, bForceFold, bForceUnfold, bExcludeRootNode )
 333  {
 334      var root_ul_node = ezjslib_getHTMLChildNodeByTag( rootNode, "ul" );
 335  
 336      if ( root_ul_node != null )
 337      {
 338          // search subtrees by looping through child LI tags.
 339          for ( var i = 0; i < root_ul_node.childNodes.length; i++ )
 340          {
 341              var li_node = root_ul_node.childNodes[i];
 342              if ( li_node["tagName"] && li_node.tagName.toLowerCase() == "li" )
 343              {
 344                  ezcst_foldUnfoldSubtree( li_node, bUpdateCookie, bInitFoldUnfoldLabels, bForceFold, bForceUnfold, false );
 345              }
 346          }
 347      }
 348  
 349      // fold/unfold current subtree.
 350      if ( !bExcludeRootNode )
 351      {
 352          ezcst_foldUnfold( rootNode, bUpdateCookie, bInitFoldUnfoldLabels, bForceFold, bForceUnfold );
 353      }
 354  }
 355  
 356  /*!
 357      Just unfold node.
 358  */
 359  function ezcst_unfoldNode( li_node )
 360  {
 361      var ul_node = ezjslib_getHTMLChildNodeByTag( li_node, "ul" );
 362      ezcst_setUnfoldedState( null, ul_node, null );
 363  }
 364  
 365  /*!
 366      Expands subtree(node and all its children and children of their children
 367      and so on...) with root \a rootNodeID.
 368  */
 369  function ezcst_expandSubtree( rootNodeID )
 370  {
 371      ezcst_collapseExpandSubtree( rootNodeID, false, false );
 372  }
 373  
 374  /*!
 375      Collapses subtree(node and all its children and children of their children
 376      and so on...) with root \a rootNodeID.
 377  */
 378  function ezcst_collapseSubtree( rootNodeID )
 379  {
 380      ezcst_collapseExpandSubtree( rootNodeID, true, true );
 381  }
 382  
 383  /*!
 384      Collapses/expands subtree(node and all its children and children of their children
 385      and so on...) with root \a rootNodeID.
 386  */
 387  function ezcst_collapseExpandSubtree( rootNodeID, bCollapse, bExcludeRootNode )
 388  {
 389      if ( rootNodeID )
 390      {
 391          var liTagID = 'n' + rootNodeID;
 392          var liTag   = ezjslib_getHTMLNodeById( liTagID );
 393          if ( liTag )
 394          {
 395              ezcst_foldUnfoldSubtree( liTag, true, false, bCollapse, !bCollapse, ( bExcludeRootNode && ( gRootNodeID == liTagID ) ) );
 396          }
 397      }
 398  }
 399  
 400  /*!
 401      Default menu state: all 'container' nodes(except root node) are folded
 402  */
 403  function ezcst_resetMenuState( rootNode )
 404  {
 405      if ( rootNode != null )
 406      {
 407          // Since all nodes are folded, need to unfold root node.
 408          ezcst_foldUnfold( rootNode, true, false, false, false );
 409      }
 410  }
 411  
 412  /*!
 413      Restores menu state from \a gUnfoldedNodesList.
 414  */
 415  function ezcst_restoreMenuState( rootNode )
 416  {
 417      if ( rootNode != null )
 418      {
 419          // unfold nodes which were stored in cookies.
 420          for ( var i = 0; i < gUnfoldedNodesList.length; ++i )
 421          {
 422              var li_node = ezjslib_getHTMLNodeById( gUnfoldedNodesList[i] );
 423              if ( li_node )
 424              {
 425                  ezcst_foldUnfold( li_node, false, false, false, false );
 426              }
 427          }
 428  
 429          ezcst_unfoldNode( rootNode );
 430      }
 431  }
 432  
 433  /*!
 434      Creates initial text for [-] label.
 435  */
 436  function ezcst_createUnfoldedLabel( node )
 437  {
 438      if ( node )
 439      {
 440          if ( gUseFoldUnfoldIcons )
 441              ezjslib_createHTMLChildImageNode( node, gUnfoldIcon, gFoldUnfoldIconsWidth, gFoldUnfoldIconsHeight );
 442          else
 443              ezjslib_createHTMLChildTextNode( node, "[-]" );
 444      }
 445  }
 446  
 447  /*!
 448      Create an empty text( '[ ]' ) label
 449  */
 450  function ezcst_createEmptyLabel( node )
 451  {
 452      if ( node )
 453      {
 454          if ( gUseFoldUnfoldIcons )
 455              ezjslib_createHTMLChildImageNode( node, gEmptyIcon, gFoldUnfoldIconsWidth, gFoldUnfoldIconsHeight );
 456          else
 457              ezjslib_createHTMLChildTextNode( node, "[ ]" );
 458          //ezjslib_createHTMLChildTextNode( node, "" );
 459      }
 460  }
 461  
 462  /*!
 463      Sets icons instead of text labels [-]/[+]/[ ]
 464  */
 465  function ezcst_setFoldUnfoldIcons( foldIcon, unfoldIcon, emptyIcon )
 466  {
 467      if ( foldIcon && unfoldIcon )
 468      {
 469          gFoldIcon = foldIcon;
 470          gUnfoldIcon = unfoldIcon;
 471          gEmptyIcon = emptyIcon;
 472          gUseFoldUnfoldIcons = true;
 473      }
 474  }
 475  
 476  /*!
 477      Restores menu state from cookie, adds current location from
 478      \a additionalNodesList.
 479  */
 480  function ezcst_initializeMenuState( additionalNodesList, menuNodeID, autoopenCurrentNode )
 481  {
 482      var menu          = ezjslib_getHTMLNodeById( menuNodeID );
 483      if ( menu != null )
 484      {
 485          // restore unfolded nodes ids from cookies
 486          ezcst_cookie_restoreUnfoldedNodesList();
 487  
 488          // add current node's path to unfolded nodes list
 489          var currentNodeID = additionalNodesList.pop();           // remove current node from list.
 490          ezcst_cookie_addNodesList( additionalNodesList );
 491  
 492          var rootNode = ezjslib_getHTMLChildNodeByTag( menu, "li" );
 493          if ( rootNode != null )
 494          {
 495              // remember id of Root Node.
 496              gRootNodeID = rootNode.getAttribute( "id" );
 497  
 498              // Fold all 'container' nodes.
 499              ezcst_foldUnfoldSubtree( rootNode, false, true, false, false, false );
 500  
 501              // Remove [-]/[+] text of root node.
 502              var root_link_node = ezjslib_getHTMLChildNodeByTag( rootNode, "a" );
 503              if ( gUseFoldUnfoldIcons )
 504                  ezjslib_removeHTMLChildImageNode( root_link_node );
 505              else
 506                  ezjslib_removeHTMLChildTextNode( root_link_node );
 507  
 508              if ( ezcst_getUnfoldedNodesListSize() > 0 )
 509              {
 510                  // unfolde nodes which are stored in gUnfoldedNodesList
 511                  ezcst_restoreMenuState( rootNode );
 512              }
 513              else
 514              {
 515                  // reset to default view
 516                  // probably we never get into this "else".
 517                  ezcst_resetMenuState  ( rootNode );
 518              }
 519  
 520              // Highlight current node
 521              var currentNode = ezjslib_getHTMLNodeById( currentNodeID );
 522              while( !currentNode && currentNodeID )
 523              {
 524                  // if current viewing node is not in the tree menu(is invesible)
 525                  // then try to find the nearest visible parent node
 526                  currentNodeID = additionalNodesList.pop();
 527                  currentNode = ezjslib_getHTMLNodeById( currentNodeID );
 528              }
 529              ezjslib_appendHTMLNodeClassStyle( currentNode, EZCST_HIGHLIGHTED_NODE_CLASS_NAME );
 530  
 531              if( autoopenCurrentNode == "enabled" )
 532              {
 533                  // unfold current node.
 534                  ezcst_foldUnfold( currentNode, true, false, false, true );
 535              }
 536          }
 537  
 538          // show menu
 539          menu.style.display="";
 540      }
 541  }
 542  


Généré le : Sat Feb 24 10:30:04 2007 par Balluche grâce à PHPXref 0.7