3 require_once 'prototype.php';
4 require_once 'nifty.php';
7 <script type="text/javascript" src="/plekit/toggle/toggle.js"></script>
8 <link href="/plekit/toggle/toggle.css" rel="stylesheet" type="text/css" />
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
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
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
39 function PlekitToggle ($id,$trigger,$options=NULL) {
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']);
48 if (!isset ($options['start-hidden'])) $options['start-hidden']=false;
49 $this->options = $options;
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 () {
57 $html .= $this->container_start();
58 $html .= $this->trigger_html();
59 $html .= $this->area_start_html();
60 $html .= $this->info_html();
64 function end () { print $this->end_html(); }
65 function end_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();
74 function visible_from_store_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');";
87 // create two images that get shown/hidden - could not find a better way to do it
88 function image_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' />";
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";
107 if (array_key_exists ('bubble',$this->options)) $bubble=$this->options['bubble'];
110 $html .= " id='$trigger_id'";
111 $html .= " class='plc-toggle-trigger'";
112 if ($bubble) $html .= " title='$bubble'";
113 $html .= " onclick=\"plekit_toggle('$this->id')\"";
115 $html .= $this->image_html();
116 $html .= $this->trigger;
117 $html .= "</$tagname>";
118 if (array_key_exists ('info-text',$this->options)) {
120 $html .= "<span class='toggle-info-button' onClick='plekit_toggle_info(\"$id\");'><img height=20 src='/planetlab/icons/info.png' alt='close info'/></span>";
125 function info() { print $this->info_html();}
126 function info_html () {
127 if (! array_key_exists ('info-text',$this->options)) return "";
129 // compute if info should be visible at startup
130 // xxx in fact the default should be fetched in the browser storage xxx
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'];
137 $div_id=$this->id_name('info');
139 $html .= "<div class='toggle-info'";
140 $html .= " id='$div_id'";
141 if ($info_visible) $html .= " style='display:none'";
144 $html .= "<table class='center'><tr><td class='top'>";
145 $html .= $this->options['info-text'];
146 $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>";
147 $html .= "</td></tr></table></div>";
152 function area_start () { print $this->area_start_html(); }
153 function area_start_html () {
154 $area_id=$this->id_name('area');
157 $html .= " class='plc-toggle-area'";
158 $html .= " id='$area_id'";
159 if ($this->options['start-hidden']) $html .= " style='display:none'";
164 function area_end () { print $this->area_end_html(); }
165 function area_end_html () {
169 /* if desired, you can embed the whole (trigger+area) in another div for visual effects */
170 function container_start () { print $this->container_start_html(); }
171 function container_start_html () {
172 $id=$this->id_name('container');
173 $this->nifty=new PlekitNifty ($id,'plc-toggle-container','medium');
174 return $this->nifty->start_html();
177 function container_end () { print $this->container_end_html(); }
178 function container_end_html () { return $this->nifty->end_html(); }
181 function id_name ($zonename) { return "toggle-$zonename-$this->id"; }