ee0dc0cc49d243d4e85fbc3586eb80f4f5da583f
[plewww.git] / plekit / php / toggle.php
1 <?php
2
3 require_once 'prototype.php';
4 require_once 'jstorage.php';
5 require_once 'nifty.php';
6
7 drupal_set_html_head('
8 <script type="text/javascript" src="/plekit/toggle/toggle.js"></script>
9 <link href="/plekit/toggle/toggle.css" rel="stylesheet" type="text/css" />
10 ');
11
12 // This is for creating an area that users can hide and show
13 // It is logically made of 3 parts:
14 // (*) area is what gets hidden and shown
15 // (*) trigger is the area that can be clicked for toggling
16 // (*) image contains a visual indication of the current status
17 // 
18 // constructor needs 
19 // (*) id:      an 'id', used for naming the three parts
20 // (*) trigger: the html text for the trigger
21 // (*) options: a hash that can define
22 //      - trigger-tagname : to be used instead of <span> for wrapping the trigger
23 //      - bubble : might not work if trigger-tagname is redefined
24 //      - visible : if set to false, start hidden rather than visible
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     // turn or or off from local storage
71     $html .= $this->visible_from_store_html();
72     return $html;
73   }
74
75   function visible_from_store_html() {
76     $id=$this->id;
77     $html = "";
78     $html .= "<script type='text/javascript'>";
79     // javascript code can't take -
80     //    $idj=str_replace('-','_',$id);
81     //    $html .= "function init_$idj () { plekit_toggle_from_store('$id');}";
82     //    $html .= "Event.observe(window,'load',init_$idj);";
83     $html .= "plekit_toggle_from_store('$id');";
84     $html .= "</script>";
85     return $html;
86   }
87
88   // create two images that get shown/hidden - could not find a better way to do it
89   function image_html () {
90     $html="";
91     if ( ! $this->options['start-hidden'])      { $x1=""; $x2=" style='display:none'"; }
92     else                                        { $x2=""; $x1=" style='display:none'"; }
93     $image_id=$this->id_name('image-visible');
94     $html .= "<img id='$image_id' class='plc-toggle-visible' src='/plekit/icons/toggle-visible.png'$x1";
95     $html .= " alt='Hide this section' />";
96     $image_id=$this->id_name('image-hidden');
97     $html .= "<img id='$image_id' class='plc-toggle-hidden' src='/plekit/icons/toggle-hidden.png'$x2";
98     $html .= " alt='Show this section' />";
99     return $html;
100   }
101
102   function trigger ()           { print $this->trigger_html(); }
103   function trigger_html () {
104     $trigger_id=$this->id_name('trigger');
105     if (array_key_exists ('trigger-tagname',$this->options)) $tagname=$this->options['trigger-tagname'];
106     if (empty($tagname)) $tagname="span";
107     $bubble="";
108     if (array_key_exists ('bubble',$this->options)) $bubble=$this->options['bubble'];
109     
110     $html="<$tagname";
111     $html .= " id='$trigger_id'";
112     $html .= " class='plc-toggle-trigger'";
113     if ($bubble) $html .= " title='$bubble'";
114     $html .= " onclick=\"plekit_toggle('$this->id')\"";
115     $html .= ">";
116     $html .= $this->image_html();
117     $html .= $this->trigger;
118     $html .= "</$tagname>";
119     if (array_key_exists ('info-text',$this->options)) {
120       $id=$this->id;
121       $html .= "<span class='toggle-info-button' onClick='plekit_toggle_info(\"$id\");'><img height=20 src='/planetlab/icons/info.png' alt='close info'/></span>";
122     }
123     return $html;
124   }
125
126   function info()               { print $this->info_html();}
127   function info_html () {
128     if (! array_key_exists ('info-text',$this->options)) return "";
129
130     // compute if info should be visible at startup
131     // xxx in fact the default should be fetched in the browser storage xxx
132     $info_visible=TRUE;
133     // if info-visible is set, use this value
134     if (array_key_exists ('info-visible',$this->options)) 
135       $info_visible=$this->options['info-visible'];
136
137     $id=$this->id;
138     $div_id=$this->id_name('info');
139     $html="";
140     $html .= "<div class='toggle-info'";
141     $html .= " id='$div_id'";
142     if ($info_visible) $html .= " style='display:none'";
143     $html .= ">";
144     // tmp
145     $html .= "<table class='center'><tr><td class='top'>";
146     $html .= $this->options['info-text'];
147     $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>";
148     $html .= "</td></tr></table></div>";
149     return $html;
150   }
151     
152
153   function area_start () { print $this->area_start_html(); }
154   function area_start_html () {
155     $area_id=$this->id_name('area');
156     $html="";
157     $html .= "<div";
158     $html .= " class='plc-toggle-area'";
159     $html .= " id='$area_id'";
160     if ($this->options['start-hidden']) $html .= " style='display:none'";
161     $html .= ">";
162     return $html;
163   }
164
165   function area_end () { print $this->area_end_html(); }
166   function area_end_html () {
167     return "</div>";
168   }
169
170   /* if desired, you can embed the whole (trigger+area) in another div for visual effects */
171   function container_start ()           { print $this->container_start_html(); }
172   function container_start_html ()      { 
173     $id=$this->id_name('container');
174     $this->nifty=new PlekitNifty ($id,'plc-toggle-container','medium');
175     return $this->nifty->start_html();
176   }
177
178   function container_end ()             { print $this->container_end_html(); }
179   function container_end_html ()        { return $this->nifty->end_html(); }
180
181   // build id names
182   function id_name ($zonename) { return "toggle-$zonename-$this->id"; }
183
184 }
185
186 ?>