[ Index ] |
|
Code source de Horde 3.1.3 |
1 <?php 2 /** 3 * The Horde_Tree_javascript:: class extends the Horde_Tree class to provide 4 * javascript specific rendering functions. 5 * 6 * Copyright 2003-2006 Marko Djukic <marko@oblo.com> 7 * 8 * See the enclosed file COPYING for license information (LGPL). If you 9 * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html. 10 * 11 * $Horde: framework/Tree/Tree/javascript.php,v 1.34.2.9 2006/01/01 21:28:40 jan Exp $ 12 * 13 * @author Marko Djukic <marko@oblo.com> 14 * @package Horde_Tree 15 * @since Horde 3.0 16 */ 17 class Horde_Tree_javascript extends Horde_Tree { 18 19 /** 20 * The name of the source for the tree data. 21 * 22 * @var string 23 */ 24 var $_source_name = null; 25 26 /** 27 * The name of the target element to output the javascript tree. 28 * 29 * @var string 30 */ 31 var $_options_name = null; 32 33 /** 34 * The name of the target element to output the javascript tree. 35 * 36 * @var string 37 */ 38 var $_target_name = null; 39 40 /** 41 * Constructor 42 */ 43 function Horde_Tree_javascript($tree_name, $params = array()) 44 { 45 parent::Horde_Tree($tree_name, $params); 46 47 if (!isset($GLOBALS['browser'])) { 48 require_once 'Horde/Browser.php'; 49 $GLOBALS['browser'] = &Browser::singleton(); 50 } 51 52 /* Check for a javascript session state. */ 53 if ($this->_usesession && 54 isset($_COOKIE[$this->_instance . '_expanded'])) { 55 /* Remove "exp" prefix from cookie value. */ 56 $nodes = explode(',', substr($_COOKIE[$this->_instance . '_expanded'], 3)); 57 58 /* Make sure there are no previous nodes stored in the 59 * session. */ 60 $_SESSION['horde_tree'][$this->_instance]['expanded'] = array(); 61 62 /* Save nodes to the session. */ 63 foreach ($nodes as $id) { 64 $_SESSION['horde_tree'][$this->_instance]['expanded'][$id] = true; 65 } 66 } 67 } 68 69 /** 70 * Returns the tree. 71 * 72 * @param boolean $static If true the tree nodes can't be expanded and 73 * collapsed and the tree gets rendered expanded. 74 * 75 * @return string The HTML code of the rendered tree. 76 */ 77 function getTree($static = false) 78 { 79 $this->_static = $static; 80 $this->_source_name = 'n_' . $this->_instance; 81 $this->_header_name = 'h_' . $this->_instance; 82 $this->_options_name = 'o_' . $this->_instance; 83 $this->_target_name = 't_' . $this->_instance; 84 $this->_buildIndents($this->_root_nodes); 85 86 $tree = $this->_getTreeSource() . 87 '<div id="' . $this->_target_name . '"></div>' . 88 $this->_getTreeInit(); 89 90 Horde::addScriptFile('tree.js', 'horde'); 91 return $tree; 92 } 93 94 /** 95 * Check the current environment to see if we can render the HTML 96 * tree. We check for DOM support in the browser. 97 * 98 * @static 99 * 100 * @return boolean Whether or not this Tree:: backend will function. 101 */ 102 function isSupported() 103 { 104 return $GLOBALS['browser']->hasFeature('dom'); 105 } 106 107 /** 108 * Returns just the JS node definitions as a string. 109 * 110 * @return string The Javascript node array definitions. 111 */ 112 function renderNodeDefinitions() 113 { 114 $this->_buildIndents($this->_root_nodes); 115 116 $nodeJs = ''; 117 foreach ($this->_nodes as $node_id => $node) { 118 $nodeJs .= $this->_getJsArrayElement(sprintf('%s[\'%s\']', 'n_' . $this->_instance, $node_id), $node); 119 } 120 121 return $nodeJs . $this->_instance . '.renderTree(root_nodes);'; 122 } 123 124 /** 125 * Outputs the data for the tree as a javascript array. 126 * 127 * @access private 128 */ 129 function _getTreeSource() 130 { 131 global $browser; 132 133 $js = '<script type="text/javascript">' . "\n"; 134 $js .= 'var extraColsLeft = ' . $this->_extra_cols_left . ";\n"; 135 $js .= 'var extraColsRight = ' . $this->_extra_cols_right . ";\n"; 136 $js .= 'var ' . $this->_source_name . ' = new Array();' . "\n"; 137 138 foreach ($this->_nodes as $node_id => $node) { 139 $js .= $this->_getJsArrayElement(sprintf('%s[\'%s\']', $this->_source_name, 140 $browser->escapeJSCode(addslashes($node_id))), $node); 141 } 142 $js .= $this->_getJsArrayElement($this->_header_name, $this->_header); 143 $js .= $this->_getJsArrayElement($this->_options_name, $this->_options); 144 $js .= '</script>' . "\n"; 145 146 return $js; 147 } 148 149 /** 150 * Outputs the javascript to initialise the tree. 151 * 152 * @access private 153 */ 154 function _getTreeInit() 155 { 156 $js = '<script type="text/javascript">' . "\n"; 157 $js .= sprintf('%1$s = new Horde_Tree(\'%1$s\');' . "\n", 158 $this->_instance); 159 160 $js .= $this->_getJsArrayElement('root_nodes', $this->_root_nodes); 161 162 $js .= sprintf("%s.renderTree(root_nodes, %s);\n</script>\n", 163 $this->_instance, 164 $this->_static ? 'true' : 'false'); 165 166 return $js; 167 } 168 169 function _getJsArrayElement($var, $value) 170 { 171 if (is_array($value)) { 172 $js = $var . ' = new Array();' . "\n"; 173 foreach ($value as $key => $val) { 174 if (is_numeric($key)) { 175 $newVar = $var . '[' . $key . ']'; 176 } else { 177 $newVar = $var . '[\'' . $key . '\']'; 178 } 179 $js .= $this->_getJsArrayElement($newVar, $val); 180 } 181 return $js; 182 } else { 183 return $var . " = '" . addslashes($value) . "';\n"; 184 } 185 } 186 187 }
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sun Feb 25 18:01:28 2007 | par Balluche grâce à PHPXref 0.7 |