c7ffc39c1988415f2e0c654a20862b34e3924f2b
[plewww.git] / plekit / php / toggle.php
1 <?php
2
3   // $Id$
4
5 require_once 'prototype.php';
6 require_once 'nifty.php';
7
8 drupal_set_html_head('
9 <script type="text/javascript" src="/plekit/toggle/toggle.js"></script>
10 <link href="/plekit/toggle/toggle.css" rel="stylesheet" type="text/css" />
11 ');
12
13 // This is for creating an area that users can hide and show
14 // It is logically made of 3 parts:
15 // (*) area is what gets hidden and shown
16 // (*) trigger is the area that can be clicked for toggling
17 // (*) image contains a visual indication of the current status
18 // 
19 // constructor needs 
20 // (*) id:      an 'id', used for naming the three parts
21 // (*) trigger: the html text for the trigger
22 // (*) options: a hash that can define
23 //      - trigger-tagname : to be used instead of <span> for wrapping the trigger
24 //      - trigger-bubble : might not work if trigger-tagname is redefined
25 //      - init-hidden : start hidden rather than visible
26 // 
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
33
34 class PlekitToggle {
35   // mandatory
36   var $id;
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 ('start-visible',$options)) {
43       $options['start-hidden'] = ! $options['start-visible'];
44       unset ($options['start-visible']);
45     }
46     $this->options = $options;
47   }
48
49   // the simple, usual way to use it :
50   // a container that contains the switch and the area in sequence
51   function start ()             { print $this->start_html(); }
52   function start_html () {
53     $html = "";
54     $html .= $this->container_start();
55     $html .= $this->trigger_html();
56     $html .= $this->area_start_html();
57     return $html;
58   }
59
60   function end ()               { print $this->end_html(); }
61   function end_html () {
62     $html = "";
63     $html .= $this->area_end_html();
64     $html .= $this->container_end();
65     return $html;
66   }
67
68
69   // create two images that get shown/hidden - could not find a better way to do it
70   function image_html () {
71     $html="";
72     if ( ! $this->options['start-hidden'])      { $x1=""; $x2=" style='display:none'"; }
73     else                                        { $x2=""; $x1=" style='display:none'"; }
74     $image_id=$this->id_name('image-visible');
75     $html .= "<img id='$image_id' class='plc-toggle-visible' src='/plekit/icons/toggle-visible.png'$x1";
76     $html .= " alt='Hide this section' />";
77     $image_id=$this->id_name('image-hidden');
78     $html .= "<img id='$image_id' class='plc-toggle-hidden' src='/plekit/icons/toggle-hidden.png'$x2";
79     $html .= " alt='Show this section' />";
80     return $html;
81   }
82
83   function trigger ()           { print $this->trigger_html(); }
84   function trigger_html () {
85     $trigger_id=$this->id_name('trigger');
86     if (array_key_exists ('trigger-tagname',$this->options)) $tagname=$this->options['trigger-tagname'];
87     if (array_key_exists ('trigger-bubble',$this->options)) $bubble=$this->options['trigger-bubble'];
88     if (empty($tagname)) $tagname="span";
89     
90     $html="<$tagname";
91     $html .= " id='$trigger_id'";
92     $html .= " class='plc-toggle-trigger'";
93     if ($bubble) $html .= " title='$bubble'";
94     $html .= " onclick=\"plc_toggle('$this->id')\"";
95     $html .= ">";
96     $html .= $this->image_html();
97     $html .= $this->trigger;
98     $html .= "</$tagname>";
99     return $html;
100   }
101
102   function area_start () { print $this->area_start_html(); }
103   function area_start_html () {
104     $area_id=$this->id_name('area');
105     $html="";
106     $html .= "<div";
107     $html .= " class='plc-toggle-area'";
108     $html .= " id='$area_id'";
109     if ($this->options['start-hidden']) $html .= " style='display:none'";
110     $html .= ">";
111     return $html;
112   }
113
114   function area_end () { print $this->area_end_html(); }
115   function area_end_html () {
116     return "</div>";
117   }
118
119   /* if desired, you can embed the whole (trigger+area) in another div for visual effects */
120   function container_start ()           { print $this->container_start_html(); }
121   function container_start_html ()      { 
122     $id=$this->id_name('container');
123
124     $html="<div class='plc-toggle-container nifty-medium'";
125     $html .= " id='$id'";
126     $html .= ">";
127     return $html;
128   }
129
130   function container_end ()             { print $this->container_end_html(); }
131   function container_end_html ()        { return "</div>"; }
132
133   // build id names
134   function id_name ($zonename) { return "toggle-$zonename-$this->id"; }
135
136 }
137
138 ?>