74e9a48804db90b664f2d9916ef521ba6a650589
[plewww.git] / planetlab / includes / plc_toggles.php
1 <?php
2
3   // $Id$
4
5 require_once 'prototype.php';
6
7 drupal_set_html_head('
8 <script type="text/javascript" src="/planetlab/js/plc_toggles.js"></script>
9 <link href="/planetlab/css/plc_toggles.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 // (*) switch 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 // (*) switch:  the html text for the switch
21 // (*) options: a hash that can define
22 //      - switch-tagname : to be used instead of <span> for wrapping the switch
23 //      - switch-bubble : might not work if switch-tagname is redefined
24 //      - init-hidden : start hidden rather than visible
25 // 
26 // methods are as follows
27 // (*) switch_html ():  return the html code for the switch
28 // (*) image_html ():   returns the html code for the image
29 // (*) area_start ():   because we have too many places where php 'prints' code instead 
30 // (*) area_end():        of returning it, we do not expect the code for the area to be passed
31 //                        so these methods can be used to delimit the area in question
32
33 class PlcToggle {
34   // mandatory
35   var $id;
36
37   function PlcToggle ($id,$switch,$options=NULL) {
38     $this->id = $id;
39     $this->switch=$switch;
40     if ( ! $options ) $options = array();
41     $this->options = $options;
42   }
43
44   function id_name ($zonename) { return "toggle-$zonename-$this->id"; }
45
46   // create two images that get shown/hidden - could not find a better way to do it
47   function image_html () {
48     $html="";
49     if ( ! $this->options['start-hidden'])      { $x1=""; $x2=" style='display:none'"; }
50     else                                        { $x2=""; $x1=" style='display:none'"; }
51     $image_id=$this->id_name('image-visible');
52     $html .= "<img id=$image_id class='plc-toggle-visible' src='/planetlab/icons/toggle-visible.png'$x1>";
53     $image_id=$this->id_name('image-hidden');
54     $html .= "<img id=$image_id class='plc-toggle-hidden' src='/planetlab/icons/toggle-hidden.png'$x2>";
55     return $html;
56   }
57
58   // don't define switch as it's a php keyword 
59   function switch_html () {
60     $switch_id=$this->id_name('switch');
61     $tagname='span';
62     if (array_key_exists ('switch-tagname',$this->options)) $tagname=$this->options['switch-tagname'];
63     if (array_key_exists ('switch-bubble',$this->options)) $bubble=$this->options['switch-bubble'];
64     
65     $html="<$tagname";
66     $html .= " id=$switch_id";
67     $html .= " class=plc-toggle-switch";
68     if ($bubble) $html .= " title='$bubble'";
69     $html .= " onclick=\"plc_toggle('$this->id')\"";
70     $html .= ">";
71     $html .= $this->image_html();
72     $html .= $this->switch;
73     $html .= "</$tagname>";
74     return $html;
75   }
76
77   function area_start () { print $this->area_start_html(); }
78   function area_start_html () {
79     $area_id=$this->id_name('area');
80     $html="";
81     $html .= "<div";
82     $html .= " class=plc-toggle-area";
83     $html .= " id=$area_id";
84     if ($this->options['start-hidden']) $html .= " style='display:none'";
85     $html .= ">";
86     return $html;
87   }
88
89   function area_end () { print $this->area_end_html(); }
90   function area_end_html () {
91     return "</div>";
92   }
93
94 }
95
96 ?>