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

root/Chameleon/trunk/Chameleon/JSList/JSList.widget.php

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

Latest Chameleon code checkout from previous repository

Line 
1 <?php
2 /**
3  * JSList Widget class
4  *
5  * @project     GeoInnovations 2002 - MapLab enhancements
6  * @revision    $Id: JSList.widget.php,v 1.6 2004/12/20 20:15:32 pspencer Exp $
7  * @purpose     JSList creates a list box from the contents of a javascript
8  *              shared resource
9  * @author      DM Solutions Group (spencer@dmsolutions.ca)
10  * @copyright
11  * <b>Copyright (c) 2003, DM Solutions Group Inc.</b>
12  * Permission is hereby granted, free of charge, to any person obtaining a
13  * copy of this software and associated documentation files (the "Software"),
14  * to deal in the Software without restriction, including without limitation
15  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16  * and/or sell copies of the Software, and to permit persons to whom the
17  * Software is furnished to do so, subject to the following conditions:
18  *
19  * The above copyright notice and this permission notice shall be included
20  * in all copies or substantial portions of the Software.
21  *
22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
25  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28  * DEALINGS IN THE SOFTWARE.
29  */
30 include_once(dirname(__FILE__)."/../Widget.php");
31
32 /**
33  * JSList Widget
34  *
35  * @desc a widget that creates a list box from a javascript shared resource.
36  *
37  * A JSList tag can have OPTION sub tags as follows:
38  * <option label="a label" value="an optional value" selected="true|false"/>
39  *
40  * label is seen by the user
41  * value is optional, the value is label by default unless provided
42  * selected is boolean true or false to make the option the default selected
43  * option.
44  */
45 class JSList extends CWCWidget
46 {
47      /* the name of the shared resource to populate the list with */
48      var $mszJSSharedResource = "";
49
50      /* the property (in the JSSR) to use as Labels */
51      var $mszLabelField = "";
52
53      /* the property (in the JSSR) to use as Values. */
54      var $mszValueField = "";
55    
56      /* a javascript function to call when the selection changes */
57      var $mszJSCallback = "";
58
59      /* the css class to apply to the select box */
60      var $mszClass = "";
61
62      /* the css style to apply to the select box */
63      var $mszStyle = "";
64
65      /* the name by which this will be referred to in the form */
66      var $mszName = "";
67    
68     /**
69      * JSList
70      *
71      * Constructor method for the JSList widget.
72      */
73     function JSList()
74     {         
75         // invoke constructor of parent
76         parent::CWCWidget();
77
78         // set the description for this widget
79         $this->szWidgetDescription = <<<EOT
80 The JSList widget creates a SELECT input and populates it with values from a
81 SharedResource.
82 EOT;
83
84         $this->maAttributes["JSSHAREDRESOURCE"] = new StringAttribute( "JSSHAREDRESOURCE", true );
85     $this->maAttributes["LABELFIELD"] = new StringAttribute( "LABELFIELD", false );
86         $this->maAttributes["VALUEFIELD"] = new StringAttribute( "VALUEFIELD", false );
87     $this->maAttributes["ONCHANGE"] = new StringAttribute( "ONCHANGE", false );
88         $this->maAttributes["WIDGETCLASS"] = new StringAttribute( "WIDGETCLASS", false );
89         $this->maAttributes["WIDGETSTYLE"] = new StringAttribute( "WIDGETSTYLE", false );
90         $this->mnMaturityLevel = MATURITY_BETA;
91     }
92
93     /**
94      * initialize respectable defaults
95      */
96     function InitDefaults()
97     {
98         parent::InitDefaults();
99
100         if(isset($this->maParams["JSSHAREDRESOURCE"]))
101             $this->mszJSSharedResource = $this->maParams["JSSHAREDRESOURCE"];
102         if(isset($this->maParams["LABELFIELD"]))
103             $this->mszLabelField = $this->maParams["LABELFIELD"];
104         if(isset($this->maParams["VALUEFIELD"]))
105             $this->mszValueField = $this->maParams["VALUEFIELD"];
106         if(isset($this->maParams["ONCHANGE"]))
107             $this->mszOnChange = $this->maParams["ONCHANGE"];
108         if (isset($this->maParams["WIDGETCLASS"]))
109             $this->mszClass = $this->maParams["WIDGETCLASS"];
110         if (isset($this->maParams["WIDGETSTYLE"]))
111             $this->mszStyle = $this->maParams["WIDGETSTYLE"];
112         $this->mszName = "JSList_".$this->mnId;
113     }
114
115     /**
116      * take action from the previous page
117      */
118     function ParseURL()
119     {   
120         //action to take: look for a previous value and reselect it
121
122         return true;
123     }
124
125     /**
126      * return javascript functions to be used by the list
127
128      */
129     function GetJavascriptFunctions()
130     {
131         $aReturn = array();
132
133         $szName = "initJSList_".$this->mnId;
134         $szFunction = <<<EOT
135 /**
136  * initialize a JSList instance
137  */
138 function {$szName}()
139 {
140     var sr;
141     var i;
142     var selected = {$this->mszHTMLForm}.{$this->mszJSSharedResource}.value;
143     sr = goJSSR.GetValue( "{$this->mszJSSharedResource}" );
144     var nIdx = {$this->mszHTMLForm}.{$this->mszName}.selectedIndex;
145     for (i=0; i<sr.maoChildren.length; i++)
146     {
147         var opt = new Option();
148         opt.value = sr.maoChildren[i].GetValue( '{$this->mszValueField}' );
149         opt.text = sr.maoChildren[i].GetValue( '{$this->mszLabelField}' );
150
151         if (opt.value == selected)
152         {
153             opt.selected = true;
154             nIdx = {$this->mszHTMLForm}.{$this->mszName}.options.length;
155         }
156         {$this->mszHTMLForm}.{$this->mszName}.options.add(opt);
157     }
158     {$this->mszHTMLForm}.{$this->mszName}.selectedIndex = nIdx;
159 }
160
161 EOT;
162        
163         $aReturn[$szName] = $szFunction;
164
165         return $aReturn;
166     }
167
168     function GetJavascriptInitFunctions()
169     {
170         $aReturn = array();
171      
172         $szName = "initJSList_".$this->mnId;
173         $aReturn[$szName] = $szName."();";
174
175         return $aReturn;
176     }
177
178     /**
179      * render widget as HTML
180      */
181     function DrawPublish()
182     {
183      if (!$this->mbVisible)
184      {
185           return "";
186      }
187      
188      $szResult = '<select name="'.$this->mszName.'"';
189
190      if (strlen(trim($this->mszOnChange)) > 0)
191      {
192              $szResult .= ' onChange="'.$this->mszOnChange.
193                  '(this, this.options[this.selectedIndex])" ';
194      }
195
196         $szClass = "";
197         if ( strlen($this->mszClass) > 0 )
198         {
199             $szClass = " CLASS=\"$this->mszClass\"";
200         }
201
202         $szStyle = "";
203         if ( strlen($this->mszStyle) > 0 )
204         {
205             $szStyle = " STYLE=\"$this->mszStyle\"";
206         }
207
208
209         $szResult .= $szClass.$szStyle.">";
210              
211         if (isset($this->maszContents["OPTION"]))
212         {
213             $szResult .= "\n";
214             foreach($this->maszContents["OPTION"] as $aOption)
215             {
216                 if (isset($aOption["LABEL"]))
217                 {
218                     $szResult .= "<OPTION";
219                     if (isset($aOption["VALUE"]))
220                     {
221                         $szResult .= ' value="'.$aOption["VALUE"].'"';
222                     }
223                     if (isset($aOption["SELECTED"]) && strcasecmp($aOption["SELECTED"], "true") == 0)
224                     {
225                         $szResult .= " SELECTED";
226                     }
227                     $szResult .= ">".$aOption["LABEL"]."</OPTION>\n";
228                 }
229             }
230         }
231         $szResult .= "</select>\n";
232
233         return $szResult;
234     }   
235 }
236
237 ?>
Note: See TracBrowser for help on using the browser.