3
* © Copyright 2009 IntraHealth International, Inc.
5
* This File is part of I2CE
7
* I2CE is free software; you can redistribute it and/or modify
8
* it under the terms of the GNU General Public License as published by
9
* the Free Software Foundation; either version 3 of the License, or
10
* (at your option) any later version.
12
* This program is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License
18
* along with this program. If not, see <http://www.gnu.org/licenses/>.
21
* @author Carl Leitner <litlfred@ibiblio.org>
27
* Class I2CE_Module_TreeSelect
33
class I2CE_Module_TreeSelect extends I2CE_Module{
36
public static function getMethods() {
38
'I2CE_Page->addAutoCompleteInputTreeById'=>'addAutoCompleteInputTreeById',
39
'I2CE_Template->addAutoCompleteInputTreeById'=>'addAutoCompleteInputTreeById',
40
'I2CE_Page->addAutoCompleteInputTree'=>'addAutoCompleteInputTree',
41
'I2CE_Template->addAutoCompleteInputTree'=>'addAutoCompleteInputTree'
45
public static function getHooks() {
47
'post_page_prepare_display_I2CE_Template'=> 'writeOutJS'
53
public function __construct() {
54
$this->trees = array();
58
* Adds a auto complete input tree
59
* @param string $tree_id The id of the tree
60
* @param array $tree_options. Defaults to empty array. Passed to the javascript formwork constructor.
61
* @param array $autocomplete_options. Defaults to empty array. Passed to the javascript formwork constructor.
63
public function addAutoCompleteInputTreeById($obj,$tree_id,$tree_options=array(),$autocomplete_options = array()) {
64
if (!array_key_exists('auto_input',$this->trees) || !is_array($this->trees['auto_input'])) {
65
$this->trees['auto_input'] = array();
67
if (is_array($tree_options) ) {
68
$tree_options = json_encode($tree_options);
70
if (!is_string($tree_options)) {
73
if (is_array($autocomplete_options)) {
74
$autocomplete_options = json_encode($autocomplete_options);
76
if (!is_string($autocomplete_options)) {
77
$autocomplete_options = '{}';
79
$this->trees['auto_input'][] = "new I2CE_TreeSelectAutoCompleter(new I2CE_TreeInputSelect('{$tree_id}',{$tree_options}),{$autocomplete_options});\n";
85
public function addAutoCompleteInputTree($template,$node,$hidden_name,$hidden_id,$selected=null,$data,
86
$tree_options=array(),$autocomplete_options=array()) {
87
if ($template instanceof I2CE_Page) {
88
$template = $template->getTemplate();
90
if (!$template instanceof I2CE_Template) {
93
if (!is_array($data)) {
96
if (!$node instanceof DOMElement) {
99
$display_id = $hidden_id . '_inputtree_display';
100
$tree_id = $hidden_id . '_inputtree_tree';
101
$toggle_class = $tree_id . '_toggle';
102
$hiddenNode = $template->createElement(
106
'name'=>$hidden_name,
109
if (is_array($selected) && array_key_exists('value', $selected)) {
110
$hiddenNode->setAttribute('value',$selected['value']);
113
$node->appendChild($hiddenNode);
114
$displayNode = $template->createElement(
120
if (is_array($selected) && array_key_exists('display', $selected)) {
121
$displayNode->setAttribute('value',$selected['display']);
123
$node->appendChild($displayNode);
124
$toggleNode = $template->createElement(
127
'class'=>"tree_main_toggle $toggle_class"
130
$node->appendChild($toggleNode);
131
$treeNode = $template->createElement(
135
'class'=>'tree_closed treeselect'
137
$template->setClassValues(
140
'inputSelectHidden'=>$hidden_id,
141
'inputSelectVisible'=>$display_id
143
$node->appendChild($treeNode);
144
$this->createTreeData($template,$treeNode, $data);
145
$this->addAutoCompleteInputTreeById($template,$tree_id, $tree_options,$autocomplete_options);
149
protected function createTreeData($template,$treeNode,$data) {
150
$has_selectable = false;
151
foreach ($data as $d) {
155
if (!array_key_exists('display',$d)) {
158
if (array_key_exists('children',$d) && is_array($d['children'])) {
159
$childNode = $template->createElement('span',array('class'=>'tree_children tree_closed'));
160
$has_selectable_children = $this->createTreeData($template,$childNode,$d['children']);
161
//we have children, so this is an expander
162
$expanderNode = $template->createElement('span',array('class'=>'tree_expandable'));
163
$treeNode->appendChild($expanderNode);
164
if (array_key_exists('value',$d)) {
166
$has_selectable = true;
167
$expanderNode->appendChild( //the toggler
168
$template->createElement('span',array('class'=>'tree_toggle'),' '));
170
$template->createElement(
172
array('class'=>'treeselect_selectable'),
174
if (array_key_exists('title',$d)) {
175
$selectorNode->setAttribute('title',$d['title']);
177
$template->setClassValue($selectorNode, 'treeselect_value',$d['value']);
178
$expanderNode->appendChild( $selectorNode);
180
//it is not selectable
181
$class = 'tree_toggle';
182
if ($has_selectable_children){
183
$has_selectable = true;
185
$class .= ' treeselect_notselectable';
188
$togglerNode = $template->createElement('span',array('class'=>$class),$d['display']);
189
if (array_key_exists('title',$d)) {
190
$togglerNode->setAttribute('title',$d['title']);
192
$expanderNode->appendChild( $togglerNode);
194
$expanderNode->appendChild($childNode);
196
//we have no children, so this is not an expander
197
$childNode = $template->createElement('span', array(),$d['display']);
198
if (array_key_exists('value',$d)) {
199
$has_selectable = true;
200
$childNode->setAttribute('class','treeselect_selectable');
201
$template->setClassValue($childNode,'treeselect_value', $d['value']);
203
$childNode->setAttribute('class','treeselect_notselectable');
206
if (array_key_exists('title',$d)) {
207
$childNode->setAttribute('title',$d['title']);
209
$treeNode->appendChild($childNode);
212
return $has_selectable;
219
public function writeOutJS($page) {
220
if (count($this->trees) == 0) {
223
if (!$page instanceof I2CE_Page) {
226
$template= $page->getTemplate();
227
if (!$template instanceof I2CE_Template) {
230
$template->addHeaderLink("mootools-core.js");
231
$template->addHeaderLink("mootools-more.js");
232
$template->addHeaderLink("getElementsByClassName-1.0.1.js");
233
$template->addHeaderLink("I2CE_ClassValues.js");
234
$template->addHeaderLink("I2CE_Window.js");
235
$template->addHeaderLink("I2CE_ToggableWindow.js");
236
$template->addHeaderLink("I2CE_TreeSelect.js");
237
$template->addHeaderLink("Tree.css");
238
if (array_key_exists('auto_input',$this->trees) && is_array($this->trees['auto_input']) && count($this->trees['auto_input'])> 0) {
239
$template->addHeaderLink('Observer.js');
240
$template->addHeaderLink('Autocompleter.js');
241
$template->addHeaderLink('Autocompleter.css');
242
$template->addHeaderLink('I2CE_TreeSelectAutoCompleter.js');
245
foreach ($this->trees as $type=>$trees) {
246
foreach ($trees as $tree_js) {
250
$js = "window.addEvent('domready',function() {\n" . $js . "\n});\n";
251
$template->addHeaderText($js,'script','treeselect');
259
# c-default-style: "bsd"
260
# indent-tabs-mode: nil