65e39969e663bfc92a121dbe845100011a0201ed
[plewww.git] / plekit / php / toggle.php
1 <?php
2
3 require_once 'prototype.php';
4 require_once 'nifty.php';
5
6 drupal_set_html_head('
7 <script type="text/javascript" src="/plekit/toggle/toggle.js"></script>
8 <link href="/plekit/toggle/toggle.css" rel="stylesheet" type="text/css" />
9 ');
10
11 // This is for creating an area that users can hide and show
12 // It is logically made of 3 parts:
13 // (*) area is what gets hidden and shown
14 // (*) trigger is the area that can be clicked for toggling
15 // (*) image contains a visual indication of the current status
16 // 
17 // constructor needs 
18 // (*) id:      an 'id', used for naming the three parts
19 // (*) trigger: the html text for the trigger
20 // (*) options: a hash that can define
21 //      - trigger-tagname : to be used instead of <span> for wrapping the trigger
22 //      - bubble : might not work if trigger-tagname is redefined
23 //      - visible : if set to false, start hidden rather than visible
24 //      - info-text : the text for help on the tab
25 //      - info-visible : whether info needs to be visible at startup
26 // 
27 // methods are as follows
28 // (*) trigger_html (): return the html code for the trigger
29 // (*) image_html ():   returns the html code for the image
30 // (*) area_start ():   because we have too many places where php 'prints' code: instead 
31 // (*) area_end():        of returning it, we do not expect the code for the area to be passed
32 //                        so these methods can be used to delimit the area in question
33
34 class PlekitToggle {
35   // mandatory
36   var $id;
37   var $nifty;
38
39   function PlekitToggle ($id,$trigger,$options=NULL) {
40     $this->id = $id;
41     $this->trigger=$trigger;
42     if ( ! $options ) $options = array();
43     if (array_key_exists ('visible',$options)) {
44       $options['start-hidden'] = ! $options['visible'];
45       unset ($options['visible']);
46     }
47
48     if (!isset ($options['start-hidden'])) $options['start-hidden']=false;
49     $this->options = $options;
50   }
51
52   // the simple, usual way to use it :
53   // a container that contains the switch and the area in sequence
54   function start ()             { print $this->start_html(); }
55   function start_html () {
56     $html = "";
57     $html .= $this->container_start();
58     $html .= $this->trigger_html();
59     $html .= $this->area_start_html();
60     $html .= $this->info_html();
61     return $html;
62   }
63
64   function end ()               { print $this->end_html(); }
65   function end_html () {
66     $html = "";
67     $html .= $this->area_end_html();
68     $html .= $this->container_end();
69     // turn or or off from local storage
70     $html .= $this->visible_from_store_html();
71     return $html;
72   }
73
74   function visible_from_store_html() {
75     $id=$this->id;
76     $html = "";
77     $html .= "<script type='text/javascript'>";
78     // javascript code can't take -
79     //    $idj=str_replace('-','_',$id);
80     //    $html .= "function init_$idj () { plekit_toggle_from_store('$id');}";
81     //    $html .= "Event.observe(window,'load',init_$idj);";
82     $html .= "plekit_toggle_from_store('$id');";
83     $html .= "</script>";
84     return $html;
85   }
86
87   // create two images that get shown/hidden - could not find a better way to do it
88   function image_html () {
89     $html="";
90     if ( ! $this->options['start-hidden'])      { $x1=""; $x2=" style='display:none'"; }
91     else                                        { $x2=""; $x1=" style='display:none'"; }
92     $image_id=$this->id_name('image-visible');
93     $html .= "<img id='$image_id' class='plc-toggle-visible' src='/plekit/icons/toggle-visible.png'$x1";
94     $html .= " alt='Hide this section' />";
95     $image_id=$this->id_name('image-hidden');
96     $html .= "<img id='$image_id' class='plc-toggle-hidden' src='/plekit/icons/toggle-hidden.png'$x2";
97     $html .= " alt='Show this section' />";
98     return $html;
99   }
100
101   function trigger ()           { print $this->trigger_html(); }
102   function trigger_html () {
103     $trigger_id=$this->id_name('trigger');
104     if (array_key_exists ('trigger-tagname',$this->options)) $tagname=$this->options['trigger-tagname'];
105     if (empty($tagname)) $tagname="span";
106     $bubble="";
107     if (array_key_exists ('bubble',$this->options)) $bubble=$this->options['bubble'];
108     
109     $html="<$tagname";
110     $html .= " id='$trigger_id'";
111     $html .= " class='plc-toggle-trigger'";
112     if ($bubble) $html .= " title='$bubble'";
113     $html .= " onclick=\"plekit_toggle('$this->id')\"";
114     $html .= ">";
115     $html .= $this->image_html();
116     $html .= $this->trigger;
117     $html .= "</$tagname>";
118     if (array_key_exists ('info-text',$this->options)) {
119       $id=$this->id;
120       $html .= "<span class='toggle-info-button' onClick='plekit_toggle_info(\"$id\");'><img height=20 src='/planetlab/icons/info.png' alt='close info'/></span>";
121     }
122     return $html;
123   }
124
125   function info()               { print $this->info_html();}
126   function info_html () {
127     if (! array_key_exists ('info-text',$this->options)) return "";
128
129     // compute if info should be visible at startup
130     // xxx in fact the default should be fetched in the browser storage xxx
131     $info_visible=TRUE;
132     // if info-visible is set, use this value
133     if (array_key_exists ('info-visible',$this->options)) 
134       $info_visible=$this->options['info-visible'];
135
136     $id=$this->id;
137     $div_id=$this->id_name('info');
138     $html="";
139     $html .= "<div class='toggle-info'";
140     $html .= " id='$div_id'";
141     if ($info_visible) $html .= " style='display:none'";
142     // tmp
143     $html .= "<table class='center'><tr><td class='top'>";
144     $html .= $this->options['info-text'];
145     $html .= "</td><td class='top'><span onClick='plekit_toggle_info(\"$id\");'><img height=20 class='reset' src='/planetlab/icons/close.png' alt='toggle info' /></span>";
146     $html .= "</td></tr></table></div>";
147     return $html;
148   }
149     
150
151   function area_start () { print $this->area_start_html(); }
152   function area_start_html () {
153     $area_id=$this->id_name('area');
154     $html="";
155     $html .= "<div";
156     $html .= " class='plc-toggle-area'";
157     $html .= " id='$area_id'";
158     if ($this->options['start-hidden']) $html .= " style='display:none'";
159     $html .= ">";
160     return $html;
161   }
162
163   function area_end () { print $this->area_end_html(); }
164   function area_end_html () {
165     return "</div>";
166   }
167
168   /* if desired, you can embed the whole (trigger+area) in another div for visual effects */
169   function container_start ()           { print $this->container_start_html(); }
170   function container_start_html ()      { 
171     $id=$this->id_name('container');
172     $this->nifty=new PlekitNifty ($id,'plc-toggle-container','medium');
173     return $this->nifty->start_html();
174   }
175
176   function container_end ()             { print $this->container_end_html(); }
177   function container_end_html ()        { return $this->nifty->end_html(); }
178
179   // build id names
180   function id_name ($zonename) { return "toggle-$zonename-$this->id"; }
181
182 }
183
184 ?>