tags now have roles and not min_role_id
[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 //      - 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   var $nifty;
38
39   function PlekitToggle ($id,$trigger,$options=NULL) {
40     $this->id = $id;
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']);
46     }
47     if (!isset ($options['start-hidden'])) $options['start-hidden']=false;
48     $this->options = $options;
49   }
50
51   // the simple, usual way to use it :
52   // a container that contains the switch and the area in sequence
53   function start ()             { print $this->start_html(); }
54   function start_html () {
55     $html = "";
56     $html .= $this->container_start();
57     $html .= $this->trigger_html();
58     $html .= $this->area_start_html();
59     return $html;
60   }
61
62   function end ()               { print $this->end_html(); }
63   function end_html () {
64     $html = "";
65     $html .= $this->area_end_html();
66     $html .= $this->container_end();
67     return $html;
68   }
69
70
71   // create two images that get shown/hidden - could not find a better way to do it
72   function image_html () {
73     $html="";
74     if ( ! $this->options['start-hidden'])      { $x1=""; $x2=" style='display:none'"; }
75     else                                        { $x2=""; $x1=" style='display:none'"; }
76     $image_id=$this->id_name('image-visible');
77     $html .= "<img id='$image_id' class='plc-toggle-visible' src='/plekit/icons/toggle-visible.png'$x1";
78     $html .= " alt='Hide this section' />";
79     $image_id=$this->id_name('image-hidden');
80     $html .= "<img id='$image_id' class='plc-toggle-hidden' src='/plekit/icons/toggle-hidden.png'$x2";
81     $html .= " alt='Show this section' />";
82     return $html;
83   }
84
85   function trigger ()           { print $this->trigger_html(); }
86   function trigger_html () {
87     $trigger_id=$this->id_name('trigger');
88     if (array_key_exists ('trigger-tagname',$this->options)) $tagname=$this->options['trigger-tagname'];
89     if (empty($tagname)) $tagname="span";
90     $bubble="";
91     if (array_key_exists ('bubble',$this->options)) $bubble=$this->options['bubble'];
92     
93     $html="<$tagname";
94     $html .= " id='$trigger_id'";
95     $html .= " class='plc-toggle-trigger'";
96     if ($bubble) $html .= " title='$bubble'";
97     $html .= " onclick=\"plc_toggle('$this->id')\"";
98     $html .= ">";
99     $html .= $this->image_html();
100     $html .= $this->trigger;
101     $html .= "</$tagname>";
102     return $html;
103   }
104
105   function area_start () { print $this->area_start_html(); }
106   function area_start_html () {
107     $area_id=$this->id_name('area');
108     $html="";
109     $html .= "<div";
110     $html .= " class='plc-toggle-area'";
111     $html .= " id='$area_id'";
112     if ($this->options['start-hidden']) $html .= " style='display:none'";
113     $html .= ">";
114     return $html;
115   }
116
117   function area_end () { print $this->area_end_html(); }
118   function area_end_html () {
119     return "</div>";
120   }
121
122   /* if desired, you can embed the whole (trigger+area) in another div for visual effects */
123   function container_start ()           { print $this->container_start_html(); }
124   function container_start_html ()      { 
125     $id=$this->id_name('container');
126     $this->nifty=new PlekitNifty ($id,'plc-toggle-container','medium');
127     return $this->nifty->start_html();
128   }
129
130   function container_end ()             { print $this->container_end_html(); }
131   function container_end_html ()        { return $this->nifty->end_html(); }
132
133   // build id names
134   function id_name ($zonename) { return "toggle-$zonename-$this->id"; }
135
136 }
137
138 ?>