NCCOOS Trac Projects: Top | Web | Platforms | Processing | Viz | Sprints | Sandbox | (Wind)

root/Chameleon/trunk/Chameleon/JSTreeView/JSTreeView.widget.php

Revision 13 (checked in by jcleary, 17 years ago)

Latest Chameleon code checkout from previous repository

Line 
1 <?php
2 /**
3  * JavaScript TreeView Widget class
4  *
5  * @project     CWC2
6  * @revision    $Id: JSTreeView.widget.php,v 1.6 2004/12/03 03:31:54 pspencer Exp $
7  * @purpose     JS TreeView Widget class
8  * @author      DM Solutions Group (zjames@dmsolutions.ca)
9  * @copyright
10  * <b>Copyright (c) 2003, DM Solutions Group Inc.</b>
11  * Permission is hereby granted, free of charge, to any person obtaining a
12  * copy of this software and associated documentation files (the "Software"),
13  * to deal in the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15  * and/or sell copies of the Software, and to permit persons to whom the
16  * Software is furnished to do so, subject to the following conditions:
17  *
18  * The above copyright notice and this permission notice shall be included
19  * in all copies or substantial portions of the Software.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
24  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS IN THE SOFTWARE.
28  */
29 include_once(dirname(__FILE__)."/../Widget.php");
30
31 if (!defined( "WEBCOMMON" ))
32 {
33     define ("WEBCOMMON", $_SESSION['gszCoreWebPath']."/common/" );
34 }
35
36 /**
37  * creates a javascript treeview from a js enabled shared resource
38  */
39 class JSTreeView extends CWCWidget
40 {
41     /* is the treeview going to be valid (i.e. does it have data to display) */
42     var $mbJSEnabled = false;
43
44     /* the js shared resource to use */
45     var $mszJSSharedResource = "";
46
47     /* the onclick event for nodes, just the name of the javascript function */
48     var $mszNodeOnClick = "";
49
50     /* the field to get optional data to pass to the onclick function */
51     var $mszNodeDataField = "";
52
53     /* the field to get the branch label from */
54     var $mszNodeLabelField = "";
55
56     /* the field to get the branch closed icon from */
57     var $mszNodeIconField = "";
58
59     /* the field to get the branch opened icon from */
60     var $mszNodeIconOpenField = "";
61
62     /* the CSS class to use for tree text */
63     var $mszNodeClass = "treeItem";
64
65     /* the onclick event for leaves, just the name of the javascript function */
66     var $mszLeafOnClick = "void";
67
68     /* the field to display in the tree */
69     var $mszLeafLabelField = "";
70
71     /* the field to get the name of the image from */
72     var $mszLeafIconField = "";
73
74     /* the field to get optional data to pass to the onclick function */
75     var $mszLeafDataField = "";
76
77     /* the CSS class to use for links */
78     var $mszLinkClass = "treeLink";
79
80     /* the CSS style to use for selected links */
81     var $mszNodeSelectedStyle = "border-width:1px;border-color:#b2c4c4;font-weight:bold:background-color:#e3f0f0";
82
83     /* the CSS style to use for unselected links */
84     var $mszNodeNormalStyle = "border-width:1px;border-color:#ffffff;font-weight:normal:background-color:#ffffff";
85
86     /**
87      * construct a new tree view widget
88      */
89     function JSTreeView()
90     {
91         // invoke constructor of parent
92         parent::CWCWidget();
93
94         // set the description for this widget
95         $this->szWidgetDescription = <<<EOT
96 The Javascript TreeView displays a client side tree view of the data in a
97 Shared Resource Widget that has the JSEnabled attribute set to true.
98 EOT;
99
100         $this->maAttributes["JSSHAREDRESOURCE"] = new StringAttribute( "JSSHAREDRESOURCE", true );
101         $this->maAttributes["NODEONCLICK"] = new StringAttribute( "NODEONCLICK", false );
102         $this->maAttributes["NODEDATAFIELD"] = new StringAttribute( "NODEDATAFIELD", false );
103         $this->maAttributes["NODELABELFIELD"] = new StringAttribute( "NODELABELFIELD", false );
104         $this->maAttributes["NODEICONFIELD"] = new StringAttribute( "NODEICONFIELD", false );       
105         $this->maAttributes["NODEICONOPENFIELD"] = new StringAttribute( "NODEICONOPENFIELD", false );
106         $this->maAttributes["NODEICONOPENFIELD"] = new StringAttribute( "NODEICONOPENFIELD", false );
107         $this->maAttributes["NODECLASS"] = new StringAttribute( "NODECLASS", false );
108         $this->maAttributes["NODESELECTEDSTYLE"] = new StringAttribute( "LEAFHIGHLIGHTSTYLE", false );
109         $this->maAttributes["NODENORMALSTYLE"] = new StringAttribute( "LEAFHIGHLIGHTSTYLE", false );
110         $this->maAttributes["LEAFONCLICK"] = new StringAttribute( "LEAFONCLICK", false );
111         $this->maAttributes["LEAFLABELFIELD"] = new StringAttribute( "LEAFLABELFIELD", false );
112         $this->maAttributes["LEAFICONFIELD"] = new StringAttribute( "LEAFLABELFIELD", false );
113         $this->maAttributes["LEAFDATAFIELD"] = new StringAttribute( "LEAFDATAFIELD", false );
114         $this->maAttributes["LINKCLASS"] = new StringAttribute( "LINKCLASS", false );
115         $this->mnMaturityLevel = MATURITY_BETA;
116     }
117
118     /**
119      * initialize default values
120      */
121     function InitDefaults()
122     {
123         parent::InitDefaults();
124         if (isset($this->maParams["JSSHAREDRESOURCE"]))
125         {
126             $this->mszJSSharedResource = $this->maParams["JSSHAREDRESOURCE"];
127         }
128
129         if (isset($this->maParams["NODEONCLICK"]))
130         {
131             $this->mszNodeOnClick = $this->maParams["NODEONCLICK"];
132         }
133
134         if (isset($this->maParams["NODEDATAFIELD"]))
135         {
136             $this->mszNodeDataField = $this->maParams["NODEDATAFIELD"];
137         }
138        
139         if (isset($this->maParams["NODELABELFIELD"]))
140         {
141             $this->mszNodeLabelField = $this->maParams["NODELABELFIELD"];
142         }
143
144         if (isset($this->maParams["NODEICONFIELD"]))
145         {
146             $this->mszNodeIconField = $this->maParams["NODEICONFIELD"];
147         }
148
149         if (isset($this->maParams["NODEICONOPENFIELD"]))
150         {
151             $this->mszNodeIconOpenField = $this->maParams["NODEICONOPENFIELD"];
152             if (strlen($this->mszNodeIconField) == 0)
153             {
154                 $this->mszNodeIconField = $this->mszNodeIconOpenField;
155             }
156         }
157         else if (strlen($this->mszNodeIconField) > 0)
158         {
159             $this->mszNodeIconOpenField = $this->mszNodeIconField;
160         }
161        
162         if (isset($this->maParams["NODECLASS"]))
163         {
164             $this->mszNodeClass = $this->maParams["NODECLASS"];
165         }
166         if (isset($this->maParams["LINKCLASS"]))
167         {
168             $this->mszLinkClass = $this->maParams["LINKCLASS"];
169         }
170         if (isset($this->maParams["NODESELECTEDSTYLE"]))
171         {
172             $this->mszNodeSelectedStyle = $this->maParams["NODESELECTEDSTYLE"];
173         }
174         if (isset($this->maParams["NODENORMALSTYLE"]))
175         {
176             $this->mszNodeNormalStyle = $this->maParams["NODENORMALSTYLE"];
177         }
178        
179         if (isset($this->maParams["LEAFONCLICK"]))
180         {
181             $this->mszLeafOnClick = $this->maParams["LEAFONCLICK"];
182         }
183         if (isset($this->maParams["LEAFLABELFIELD"]))
184         {
185             $this->mszLeafLabelField = $this->maParams["LEAFLABELFIELD"];
186         }
187
188         if (isset($this->maParams["LEAFICONFIELD"]))
189         {
190             $this->mszLeafIconField = $this->maParams["LEAFICONFIELD"];
191         }
192        
193         if (isset($this->maParams["LEAFDATAFIELD"]))
194         {
195             $this->mszLeafDataField = $this->maParams["LEAFDATAFIELD"];
196         }
197        
198        
199         return true;
200     }
201
202     /**
203      * we aren't actually expecting anything from the URL, but we can check to see if the
204      * sharedresource is valid now
205      */
206     function ParseURL()
207     {
208         $this->mbJSEnabled = 0;
209            
210         if (isset($this->maSharedResourceWidgets[$this->mszJSSharedResource]))
211         {
212             $oSR = $this->maSharedResourceWidgets[$this->mszJSSharedResource];
213             if (isset($oSR->maParams["ENABLEJS"]))
214             {
215                 $this->mbJSEnabled = (strcasecmp($oSR->maParams["ENABLEJS"],
216                                                  "true") == 0) ? true : false;
217             }
218         }
219         else
220         {
221             //ERROR:
222             //echo "SharedResource ".$this->mszJSSharedResource." not found<BR>";
223         }
224            
225         if (!$this->mbJSEnabled)
226         {
227             //ERROR:
228             //echo "error, JSSHAREDRESOURCE ".$this->mszJSSharedResource." is not JS enabled<BR>";
229         }
230
231         return true;
232     }
233
234     /**
235      * return javascript functions
236      */
237     function GetJavascriptFunctions()
238     {
239         $aReturn = parent::GetJavascriptFunctions();
240        
241         //check if there is a js resource to make tree from
242         if (!$this->mbJSEnabled)
243         {
244             return $aReturn;
245         }
246         /* the problem here is that in the treeview code, the name of the
247          * tree is hard coded instead of passed in.  This could be fixed
248          * but will make our copy unmaintainable through upgrading
249          */
250         $szTreeName = "foldersTree"; //"goJSTreeView".$this->mnId;
251
252         $bNodeIcon = (strlen(trim($this->mszNodeIconField)) > 0) ? "true" : "false";
253         $bNodeIconOpen = (strlen(trim($this->mszNodeIconOpenField)) > 0) ? "true" : "false";
254         $bLeafIcon = (strlen(trim($this->mszLeafIconField)) > 0) ? "true" : "false";
255        
256         $bLeafLink = (strlen(trim($this->mszLeafOnClick)) > 0 ) ? "true" : "false";
257         $bNodeLink = (strlen(trim($this->mszNodeOnClick)) > 0 ) ? "true" : "false";
258        
259         $szJsFunctionName = "JSTreeViewInit_".$this->mnId;
260         $szFunction = <<<EOT
261 /**
262  * {$szJsFunctionName}
263  * called to set up the tree.
264  */
265 function {$szJsFunctionName}()
266 {
267     //server entry is first child of shared resource
268     var sr = false;
269     if (goJSSR.GetValue( "{$this->mszJSSharedResource}" ).maoChildren.length > 0)
270         sr = goJSSR.GetValue( "{$this->mszJSSharedResource}" ).maoChildren[0];
271
272     var _icon = "";
273
274     var szNodeOpen = "<span class=\"{$this->mszNodeClass}\">";
275     var szNodeClose = "</span>";
276
277    
278     if (sr != false)
279     {
280         if ({$bNodeLink})
281         {
282             _js = "javascript:{$this->mszNodeOnClick}(";
283             if ( '{$this->mszNodeDataField}' != "")
284             {
285                 _js = _js + "\\"" + sr.GetValue( "{$this->mszNodeDataField}" ) + "\\"";
286             }
287             _js = _js + ");";
288             {$szTreeName} = gFld( szNodeOpen + sr.GetValue("{$this->mszNodeLabelField}" ) + szNodeClose, _js);
289         }
290         else
291         {
292             {$szTreeName} = gFld( szNodeOpen + sr.GetValue("{$this->mszNodeLabelField}" ) + szNodeClose);
293         }
294    
295         if ({$bNodeIconOpen})
296         {
297             _icon = sr.GetValue( '{$this->mszNodeIconOpenField}' );
298             if (_icon.length > 0)
299             {
300                 JSTreeViewImage.src = _icon;
301                 {$szTreeName}.iconSrc = _icon;
302             }
303         }
304         if ({$bNodeIcon})
305         {
306             _icon = sr.GetValue( '{$this->mszNodeIconField}');
307             if (_icon.length > 0)
308             {
309                 JSTreeViewImage.src = _icon;
310                 {$szTreeName}.iconSrcClosed = _icon;
311             }
312         }
313
314         JSTreeView_AddRecursive( sr, {$szTreeName} );
315     }
316
317     return true;
318 }
319 EOT;
320
321         $aReturn[$szJsFunctionName] = $szFunction;
322
323         $szJSOpen = "javascript:".$this->mszLeafOnClick."(" ;
324         $szJSClose = ");";
325        
326         if ($this->mszLeafDataField != "")
327         {
328             $szJSOpen .= "\\\\\\\\\'";
329             $szJSClose = "\\\\\\\\\'" . $szJSClose;
330         }
331        
332         $szJsFunctionName = "JSTreeView_AddRecursive";
333         $szFunction = <<<EOT
334 /**
335  * {$szJsFunctionName}
336  * add children recursively from a JS SharedResource
337  */
338 function {$szJsFunctionName}( sr, obj )
339 {
340     var _item, _child, i, _js, _icon, szLink;
341     var szNodeOpen = "";
342     var szNodeClose = "";
343
344     for (i=0; i<sr.maoChildren.length; i++)
345     {
346         _child = sr.maoChildren[i];
347
348         if (_child.maoChildren.length > 0)
349         {
350             if ({$bNodeLink})
351             {
352                 _js = "javascript:{$this->mszNodeOnClick}(";
353                 if ( '{$this->mszNodeDataField}' != "")
354                 {
355                     _js = _js + "\\"" + _child.GetValue( "{$this->mszNodeDataField}" ) + "\\"";
356                 }
357                 _js = _js + ");";
358                 _item = gFld( szNodeOpen + _child.GetValue("{$this->mszNodeLabelField}" ) + szNodeClose, _js);
359             }
360             else
361             {
362                 _item = gFld( szNodeOpen + _child.GetValue("{$this->mszNodeLabelField}" ) + szNodeClose);
363             }
364             if ({$bNodeIconOpen})
365             {
366                 _icon = _child.GetValue( '{$this->mszNodeIconOpenField}' );
367                 if (_icon.length > 0)
368                 {
369                     JSTreeViewImage.src = _icon;
370                     _item.iconSrc = _icon;
371                 }
372             }   
373             if ({$bNodeIcon})
374             {
375                 _icon = _child.GetValue('{$this->mszNodeIconField}');
376                 if (_icon.length > 0)
377                 {
378                     JSTreeViewImage.src = _icon;
379                     _item.iconSrcClosed = _icon;
380                     if (_item.iconSrc.length == 0)
381                     {
382                         _item.iconSrc = _icon;
383                     }
384                 }
385                 else if (_item.iconSrc.length != 0)
386                 {
387                     _item.iconSrcClosed = _item.iconSrc;
388                 }
389             }
390             JSTreeView_AddRecursive( _child, _item );
391         }
392         else
393         {
394             if ({$bLeafLink})
395             {
396                 _js = "{$szJSOpen}";
397                 if ("{$this->mszLeafDataField}" != "")
398                 {
399                     _js = _js + _child.GetValue( "{$this->mszLeafDataField}");
400                 }
401                 _js = _js + "{$szJSClose}";
402            
403                 _item = gLnk("S", szNodeOpen + _child.GetValue("{$this->mszLeafLabelField}") + szNodeClose, _js);
404             }
405             else
406             {
407                 _item = gLnk("S", szNodeOpen + _child.GetValue("{$this->mszLeafLabelField}") + szNodeClose );           
408             }
409             if ({$bLeafIcon})
410             {
411                 _icon = _child.GetValue( '{$this->mszLeafIconField}' );
412                 if (_icon.length > 0)
413                 {
414                     JSTreeViewImage.src = _icon;
415                     _item.iconSrc = _icon;
416                 }
417             }
418         }
419
420         obj.addChild( _item );
421     }
422     return true;
423 }
424 EOT;
425
426         $aReturn[$szJsFunctionName] = $szFunction;       
427    
428         return $aReturn;
429     }       
430
431     function GetJavascriptVariables()
432     {
433         $aReturn = parent::GetJavascriptVariables();
434
435         $szVar = "foldersTree"; //"goJSTreeView".$this->mnId;
436         $aReturn[$szVar] = "var $szVar = '';\n";
437         $szVar = "HIGHLIGHT";
438         $aReturn[$szVar] = "var $szVar = 1;\n";
439         $szVar = "ICONPATH";
440         $aReturn[$szVar] = "var $szVar = \"".$_SESSION['gszCoreWebPath']."/skins/default/images/\";\n";
441         $szVar = "USETEXTLINKS";
442         $aReturn[$szVar] = "var $szVar = 1;\n";
443         $szVar = "USEFRAMES";
444         $aReturn[$szVar] = "var $szVar = 0;\n";
445         $szVar = "STARTALLOPEN";
446         $aReturn[$szVar] = "var $szVar = 0;\n";
447         $szVar = "PRESERVESTATE";
448         $aReturn[$szVar] = "var $szVar = 0;\n";
449         $szVar = "JSTreeViewImage";
450         $aReturn[$szVar] = "var $szVar = new Image();\n";
451         $szVar = "NODECLASS";
452         $aReturn[$szVar] = "var $szVar = \"".$this->mszNodeClass."\";\n";
453         $szVar = "LINKCLASS";
454         $aReturn[$szVar] = "var $szVar = \"".$this->mszLinkClass."\";\n";
455         $szVar = "NODESELECTEDSTYLE";
456         $aReturn[$szVar] = "var $szVar = \"".$this->mszNodeSelectedStyle."\";\n";
457         $szVar = "NODENORMALSTYLE";
458         $aReturn[$szVar] = "var $szVar = \"".$this->mszNodeNormalStyle."\";\n";
459        
460
461         return $aReturn;
462     }
463
464     /**
465      * javascript include files
466      */
467     function GetJavascriptIncludeFunctions()
468     {
469         $aReturn = parent::GetJavascriptIncludeFunctions();
470         if ($this->mbJSEnabled)
471         {
472             $szJsIncludeName = $_SESSION["gszCoreWebPath"]."/widgets/js/cwc_sr.js";
473             $szInclude = "<script language=\"JavaScript\" src=\"".$_SESSION["gszCoreWebPath"]."/widgets/js/cwc_sr.js\" type=\"text/javascript\"></script>";
474             $aReturn[$szJsIncludeName] = $szInclude;
475
476             //load the treeview javascript - see http://treeview.net
477             $szJsIncludeName = WEBCOMMON."/jtree/ua.js";
478             $szInclude = "<script language=\"JavaScript\" src=\"".WEBCOMMON."/jtree/ua.js\" type=\"text/javascript\"></script>";
479             $aReturn[$szJsIncludeName] = $szInclude;
480
481             $szJsIncludeName = WEBCOMMON."/jtree/ftiens4.js";
482             $szInclude = "<script language=\"JavaScript\" src=\"".WEBCOMMON."/jtree/ftiens4.js\" type=\"text/javascript\"></script>";
483             $aReturn[$szJsIncludeName] = $szInclude;
484         }
485         return $aReturn;
486     }
487
488     /**
489      * draw the TreeView widget
490      */
491     function DrawPublish()
492     {
493         $szReturn = "";
494         if ($this->mbJSEnabled)
495         {
496             $szReturn .= "<script language='JavaScript' type='text/javascript'>JSTreeViewInit_".$this->mnId."();initializeDocument();</script>";
497         }
498         if (!$this->mbVisible)
499             return "<!-- JSTreeView is hidden -->";
500
501        
502         return $szReturn;
503     }
504 }
505 ?>
Note: See TracBrowser for help on using the browser.