reviewed the info text attached to toggles
[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 //      - init-hidden : start hidden rather than visible
24 //      - info_div : the id of a 'div' element that contains a help text
25 //      - info_text : the text for help on the tab
26 //      - info_visible : whether info needs to be visible at startup
27 // 
28 // methods are as follows
29 // (*) trigger_html (): return the html code for the trigger
30 // (*) image_html ():   returns the html code for the image
31 // (*) area_start ():   because we have too many places where php 'prints' code: instead 
32 // (*) area_end():        of returning it, we do not expect the code for the area to be passed
33 //                        so these methods can be used to delimit the area in question
34
35 class PlekitToggle {
36   // mandatory
37   var $id;
38   var $nifty;
39
40   function PlekitToggle ($id,$trigger,$options=NULL) {
41     $this->id = $id;
42     $this->trigger=$trigger;
43     if ( ! $options ) $options = array();
44     if (array_key_exists ('visible',$options)) {
45       $options['start-hidden'] = ! $options['visible'];
46       unset ($options['visible']);
47     }
48
49     if (!isset ($options['start-hidden'])) $options['start-hidden']=false;
50     $this->options = $options;
51   }
52
53   // the simple, usual way to use it :
54   // a container that contains the switch and the area in sequence
55   function start ()             { print $this->start_html(); }
56   function start_html () {
57     $html = "";
58     $html .= $this->container_start();
59     $html .= $this->trigger_html();
60     $html .= $this->area_start_html();
61     $html .= $this->info_html();
62     return $html;
63   }
64
65   function end ()               { print $this->end_html(); }
66   function end_html () {
67     $html = "";
68     $html .= $this->area_end_html();
69     $html .= $this->container_end();
70     return $html;
71   }
72
73
74   // create two images that get shown/hidden - could not find a better way to do it
75   function image_html () {
76     $html="";
77     if ( ! $this->options['start-hidden'])      { $x1=""; $x2=" style='display:none'"; }
78     else                                        { $x2=""; $x1=" style='display:none'"; }
79     $image_id=$this->id_name('image-visible');
80     $html .= "<img id='$image_id' class='plc-toggle-visible' src='/plekit/icons/toggle-visible.png'$x1";
81     $html .= " alt='Hide this section' />";
82     $image_id=$this->id_name('image-hidden');
83     $html .= "<img id='$image_id' class='plc-toggle-hidden' src='/plekit/icons/toggle-hidden.png'$x2";
84     $html .= " alt='Show this section' />";
85     return $html;
86   }
87
88   function trigger ()           { print $this->trigger_html(); }
89   function trigger_html () {
90     $trigger_id=$this->id_name('trigger');
91     if (array_key_exists ('trigger-tagname',$this->options)) $tagname=$this->options['trigger-tagname'];
92     if (empty($tagname)) $tagname="span";
93     $bubble="";
94     if (array_key_exists ('bubble',$this->options)) $bubble=$this->options['bubble'];
95     
96     $html="<$tagname";
97     $html .= " id='$trigger_id'";
98     $html .= " class='plc-toggle-trigger'";
99     if ($bubble) $html .= " title='$bubble'";
100     $html .= " onclick=\"plekit_toggle('$this->id')\"";
101     $html .= ">";
102     $html .= $this->image_html();
103     $html .= $this->trigger;
104     $html .= "</$tagname>";
105     if (array_key_exists ('info_text',$this->options)) {
106       $id=$this->id;
107       $html .= "<span class='toggle-info-button' onClick='plekit_toggle_info(\"$id\");'><img height=20 src='/planetlab/icons/info.png' alt='close info'/></span>";
108     }
109     return $html;
110   }
111
112   function info()               { print $this->info_html();}
113   function info_html () {
114     if (! array_key_exists ('info_text',$this->options)) return "";
115
116     // compute if info should be visible at startup
117     // xxx in fact the default should be fetched in the browser storage xxx
118     $info_visible=TRUE;
119     // if info_visible is set, use this value
120     if (array_key_exists ('info_visible',$this->options)) 
121       $info_visible=$this->options['info_visible'];
122
123     $id=$this->id;
124     $div_id=$this->id_name('info');
125     $html="";
126     $html .= "<div class='toggle-info'";
127     $html .= " id='$div_id'";
128     if ($info_visible) $html .= " style='display:none'";
129     // tmp
130     $html .= "<table class='center'><tr><td class='top'>";
131     $html .= $this->options['info_text'];
132     $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>";
133     $html .= "</td></tr></table></div>";
134     return $html;
135   }
136     
137
138   function area_start () { print $this->area_start_html(); }
139   function area_start_html () {
140     $area_id=$this->id_name('area');
141     $html="";
142     $html .= "<div";
143     $html .= " class='plc-toggle-area'";
144     $html .= " id='$area_id'";
145     if ($this->options['start-hidden']) $html .= " style='display:none'";
146     $html .= ">";
147     return $html;
148   }
149
150   function area_end () { print $this->area_end_html(); }
151   function area_end_html () {
152     return "</div>";
153   }
154
155   /* if desired, you can embed the whole (trigger+area) in another div for visual effects */
156   function container_start ()           { print $this->container_start_html(); }
157   function container_start_html ()      { 
158     $id=$this->id_name('container');
159     $this->nifty=new PlekitNifty ($id,'plc-toggle-container','medium');
160     return $this->nifty->start_html();
161   }
162
163   function container_end ()             { print $this->container_end_html(); }
164   function container_end_html ()        { return $this->nifty->end_html(); }
165
166   // build id names
167   function id_name ($zonename) { return "toggle-$zonename-$this->id"; }
168
169 }
170
171 ?>