cleanup
[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 // 
25 // methods are as follows
26 // (*) trigger_html (): return the html code for the trigger
27 // (*) image_html ():   returns the html code for the image
28 // (*) area_start ():   because we have too many places where php 'prints' code: instead 
29 // (*) area_end():        of returning it, we do not expect the code for the area to be passed
30 //                        so these methods can be used to delimit the area in question
31
32 class PlekitToggle {
33   // mandatory
34   var $id;
35   var $nifty;
36   var $info_div = "";
37
38   function PlekitToggle ($id,$trigger,$options=NULL) {
39     $this->id = $id;
40     $this->trigger=$trigger;
41     if ( ! $options ) $options = array();
42     if (array_key_exists ('visible',$options)) {
43       $options['start-hidden'] = ! $options['visible'];
44       unset ($options['visible']);
45     }
46
47     if (array_key_exists ('info_div',$options)) {
48         $this->info_div = $options['info_div'];
49     }
50
51     if (!isset ($options['start-hidden'])) $options['start-hidden']=false;
52     $this->options = $options;
53   }
54
55   // the simple, usual way to use it :
56   // a container that contains the switch and the area in sequence
57   function start ()             { print $this->start_html(); }
58   function start_html () {
59     $html = "";
60     $html .= $this->container_start();
61     $html .= $this->trigger_html();
62     $html .= $this->area_start_html();
63     return $html;
64   }
65
66   function end ()               { print $this->end_html(); }
67   function end_html () {
68     $html = "";
69     $html .= $this->area_end_html();
70     $html .= $this->container_end();
71     return $html;
72   }
73
74
75   // create two images that get shown/hidden - could not find a better way to do it
76   function image_html () {
77     $html="";
78     if ( ! $this->options['start-hidden'])      { $x1=""; $x2=" style='display:none'"; }
79     else                                        { $x2=""; $x1=" style='display:none'"; }
80     $image_id=$this->id_name('image-visible');
81     $html .= "<img id='$image_id' class='plc-toggle-visible' src='/plekit/icons/toggle-visible.png'$x1";
82     $html .= " alt='Hide this section' />";
83     $image_id=$this->id_name('image-hidden');
84     $html .= "<img id='$image_id' class='plc-toggle-hidden' src='/plekit/icons/toggle-hidden.png'$x2";
85     $html .= " alt='Show this section' />";
86     return $html;
87   }
88
89   function trigger ()           { print $this->trigger_html(); }
90   function trigger_html () {
91     $trigger_id=$this->id_name('trigger');
92     if (array_key_exists ('trigger-tagname',$this->options)) $tagname=$this->options['trigger-tagname'];
93     if (empty($tagname)) $tagname="span";
94     $bubble="";
95     if (array_key_exists ('bubble',$this->options)) $bubble=$this->options['bubble'];
96     
97     $html="<$tagname";
98     $html .= " id='$trigger_id'";
99     $html .= " class='plc-toggle-trigger'";
100     if ($bubble) $html .= " title='$bubble'";
101     $html .= " onclick=\"plc_toggle('$this->id')\"";
102     $html .= ">";
103     $html .= $this->image_html();
104     $html .= $this->trigger;
105     $html .= "</$tagname>";
106     if ($this->info_div != "")
107       $html .= "&nbsp;(<a href=javascript:plc_show_toggle_info('$this->info_div','$this->id')>?</a>)";
108     return $html;
109   }
110
111   function area_start () { print $this->area_start_html(); }
112   function area_start_html () {
113     $area_id=$this->id_name('area');
114     $html="";
115     $html .= "<div";
116     $html .= " class='plc-toggle-area'";
117     $html .= " id='$area_id'";
118     if ($this->options['start-hidden']) $html .= " style='display:none'";
119     $html .= ">";
120     return $html;
121   }
122
123   function area_end () { print $this->area_end_html(); }
124   function area_end_html () {
125     return "</div>";
126   }
127
128   /* if desired, you can embed the whole (trigger+area) in another div for visual effects */
129   function container_start ()           { print $this->container_start_html(); }
130   function container_start_html ()      { 
131     $id=$this->id_name('container');
132     $this->nifty=new PlekitNifty ($id,'plc-toggle-container','medium');
133     return $this->nifty->start_html();
134   }
135
136   function container_end ()             { print $this->container_end_html(); }
137   function container_end_html ()        { return $this->nifty->end_html(); }
138
139   // build id names
140   function id_name ($zonename) { return "toggle-$zonename-$this->id"; }
141
142 }
143
144 ?>