File: javascript.php

package info (click to toggle)
horde3 3.1.3-4etch7
  • links: PTS
  • area: main
  • in suites: etch
  • size: 22,876 kB
  • ctags: 18,071
  • sloc: php: 75,151; xml: 2,979; sql: 1,069; makefile: 79; sh: 64
file content (187 lines) | stat: -rw-r--r-- 5,735 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php
/**
 * The Horde_Tree_javascript:: class extends the Horde_Tree class to provide
 * javascript specific rendering functions.
 *
 * Copyright 2003-2006 Marko Djukic <marko@oblo.com>
 *
 * See the enclosed file COPYING for license information (LGPL). If you
 * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
 *
 * $Horde: framework/Tree/Tree/javascript.php,v 1.34.2.9 2006/01/01 21:28:40 jan Exp $
 *
 * @author  Marko Djukic <marko@oblo.com>
 * @package Horde_Tree
 * @since   Horde 3.0
 */
class Horde_Tree_javascript extends Horde_Tree {

    /**
     * The name of the source for the tree data.
     *
     * @var string
     */
    var $_source_name = null;

    /**
     * The name of the target element to output the javascript tree.
     *
     * @var string
     */
    var $_options_name = null;

    /**
     * The name of the target element to output the javascript tree.
     *
     * @var string
     */
    var $_target_name = null;

    /**
     * Constructor
     */
    function Horde_Tree_javascript($tree_name, $params = array())
    {
        parent::Horde_Tree($tree_name, $params);

        if (!isset($GLOBALS['browser'])) {
            require_once 'Horde/Browser.php';
            $GLOBALS['browser'] = &Browser::singleton();
        }

        /* Check for a javascript session state. */
        if ($this->_usesession &&
            isset($_COOKIE[$this->_instance . '_expanded'])) {
            /* Remove "exp" prefix from cookie value. */
            $nodes = explode(',', substr($_COOKIE[$this->_instance . '_expanded'], 3));

            /* Make sure there are no previous nodes stored in the
             * session. */
            $_SESSION['horde_tree'][$this->_instance]['expanded'] = array();

            /* Save nodes to the session. */
            foreach ($nodes as $id) {
                $_SESSION['horde_tree'][$this->_instance]['expanded'][$id] = true;
            }
        }
    }

    /**
     * Returns the tree.
     *
     * @param boolean $static  If true the tree nodes can't be expanded and
     *                         collapsed and the tree gets rendered expanded.
     *
     * @return string  The HTML code of the rendered tree.
     */
    function getTree($static = false)
    {
        $this->_static = $static;
        $this->_source_name = 'n_' . $this->_instance;
        $this->_header_name = 'h_' . $this->_instance;
        $this->_options_name = 'o_' . $this->_instance;
        $this->_target_name = 't_' . $this->_instance;
        $this->_buildIndents($this->_root_nodes);

        $tree = $this->_getTreeSource() .
             '<div id="' . $this->_target_name . '"></div>' .
             $this->_getTreeInit();

        Horde::addScriptFile('tree.js', 'horde');
        return $tree;
    }

    /**
     * Check the current environment to see if we can render the HTML
     * tree. We check for DOM support in the browser.
     *
     * @static
     *
     * @return boolean  Whether or not this Tree:: backend will function.
     */
    function isSupported()
    {
        return $GLOBALS['browser']->hasFeature('dom');
    }

    /**
     * Returns just the JS node definitions as a string.
     *
     * @return string  The Javascript node array definitions.
     */
    function renderNodeDefinitions()
    {
        $this->_buildIndents($this->_root_nodes);

        $nodeJs = '';
        foreach ($this->_nodes as $node_id => $node) {
            $nodeJs .= $this->_getJsArrayElement(sprintf('%s[\'%s\']', 'n_' . $this->_instance, $node_id), $node);
        }

        return $nodeJs . $this->_instance . '.renderTree(root_nodes);';
    }

    /**
     * Outputs the data for the tree as a javascript array.
     *
     * @access private
     */
    function _getTreeSource()
    {
        global $browser;

        $js  = '<script type="text/javascript">' . "\n";
        $js .= 'var extraColsLeft = ' . $this->_extra_cols_left . ";\n";
        $js .= 'var extraColsRight = ' . $this->_extra_cols_right . ";\n";
        $js .= 'var ' . $this->_source_name . ' = new Array();' . "\n";

        foreach ($this->_nodes as $node_id => $node) {
            $js .= $this->_getJsArrayElement(sprintf('%s[\'%s\']', $this->_source_name,
                                                     $browser->escapeJSCode(addslashes($node_id))), $node);
        }
        $js .= $this->_getJsArrayElement($this->_header_name, $this->_header);
        $js .= $this->_getJsArrayElement($this->_options_name, $this->_options);
        $js .= '</script>' . "\n";

        return $js;
    }

    /**
     * Outputs the javascript to initialise the tree.
     *
     * @access private
     */
    function _getTreeInit()
    {
        $js  = '<script type="text/javascript">' . "\n";
        $js .= sprintf('%1$s = new Horde_Tree(\'%1$s\');' . "\n",
                       $this->_instance);

        $js .= $this->_getJsArrayElement('root_nodes', $this->_root_nodes);

        $js .= sprintf("%s.renderTree(root_nodes, %s);\n</script>\n",
                       $this->_instance,
                       $this->_static ? 'true' : 'false');

        return $js;
    }

    function _getJsArrayElement($var, $value)
    {
        if (is_array($value)) {
            $js = $var . ' = new Array();' . "\n";
            foreach ($value as $key => $val) {
                if (is_numeric($key)) {
                    $newVar = $var . '[' . $key . ']';
                } else {
                    $newVar = $var . '[\'' . $key . '\']';
                }
                $js .= $this->_getJsArrayElement($newVar, $val);
            }
            return $js;
        } else {
            return $var . " = '" . addslashes($value) . "';\n";
        }
    }

}