minor fixes for the toggle building block - arrows should be right,
authorThierry Parmentelat <thierry.parmentelat@sophia.inria.fr>
Wed, 30 Mar 2011 17:41:59 +0000 (19:41 +0200)
committerThierry Parmentelat <thierry.parmentelat@sophia.inria.fr>
Wed, 30 Mar 2011 17:41:59 +0000 (19:41 +0200)
and status from storage used only if 'visible' is not set or NULL

plekit/php/toggle.php
plekit/toggle/toggle.js

index ee0dc0c..d43cc78 100644 (file)
@@ -41,9 +41,13 @@ class PlekitToggle {
     $this->id = $id;
     $this->trigger=$trigger;
     if ( ! $options ) $options = array();
+    // 'visible' may be set or not; if set to NULL it's considered as undefined
+    // so using NULL as the default means 'select from local storage i.e. last status'
+    if (array_key_exists ('visible',$options) && $options['visible']==NULL) 
+      unset ($options['visible']);
+    // start-hidden is internal and is always set
     if (array_key_exists ('visible',$options)) {
       $options['start-hidden'] = ! $options['visible'];
-      unset ($options['visible']);
     }
 
     if (!isset ($options['start-hidden'])) $options['start-hidden']=false;
@@ -67,8 +71,9 @@ class PlekitToggle {
     $html = "";
     $html .= $this->area_end_html();
     $html .= $this->container_end();
-    // turn or or off from local storage
-    $html .= $this->visible_from_store_html();
+    // if 'visible' is not set, set or or off from local storage
+    if ( ! array_key_exists('visible',$this->options) )
+      $html .= $this->visible_from_store_html();
     return $html;
   }
 
@@ -78,9 +83,9 @@ class PlekitToggle {
     $html .= "<script type='text/javascript'>";
     // javascript code can't take -
     //    $idj=str_replace('-','_',$id);
-    //    $html .= "function init_$idj () { plekit_toggle_from_store('$id');}";
+    //    $html .= "function init_$idj () { pletoggle_from_store('$id');}";
     //    $html .= "Event.observe(window,'load',init_$idj);";
-    $html .= "plekit_toggle_from_store('$id');";
+    $html .= "pletoggle_from_store('$id');";
     $html .= "</script>";
     return $html;
   }
@@ -111,14 +116,14 @@ class PlekitToggle {
     $html .= " id='$trigger_id'";
     $html .= " class='plc-toggle-trigger'";
     if ($bubble) $html .= " title='$bubble'";
-    $html .= " onclick=\"plekit_toggle('$this->id')\"";
+    $html .= " onclick=\"pletoggle_toggle('$this->id')\"";
     $html .= ">";
     $html .= $this->image_html();
     $html .= $this->trigger;
     $html .= "</$tagname>";
     if (array_key_exists ('info-text',$this->options)) {
       $id=$this->id;
-      $html .= "<span class='toggle-info-button' onClick='plekit_toggle_info(\"$id\");'><img height=20 src='/planetlab/icons/info.png' alt='close info'/></span>";
+      $html .= "<span class='toggle-info-button' onClick='pletoggle_toggle_info(\"$id\");'><img height=20 src='/planetlab/icons/info.png' alt='close info'/></span>";
     }
     return $html;
   }
@@ -144,7 +149,7 @@ class PlekitToggle {
     // tmp
     $html .= "<table class='center'><tr><td class='top'>";
     $html .= $this->options['info-text'];
-    $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>";
+    $html .= "</td><td class='top'><span onClick='pletoggle_toggle_info(\"$id\");'><img height=20 class='reset' src='/planetlab/icons/close.png' alt='toggle info' /></span>";
     $html .= "</td></tr></table></div>";
     return $html;
   }
index 8ac69af..8c2cb1c 100644 (file)
@@ -1,24 +1,25 @@
 ////////// use jstorage to remember open/closed toggles
 // store current status
-function plekit_toggle_store(id) {
+function pletoggle_store(id) {
     var area=$('toggle-area-'+id);
     key='toggle.'+id;
 //    window.console.log('storing toggle status for '+id);
     $.jStorage.set(key,area.visible());
 }
 // restore last status
-function plekit_toggle_from_store (id) {
-    var area=$('toggle-area-'+id);
+function pletoggle_from_store (id) {
     key='toggle.'+id;
-    // on by default
-//    window.console.log('retrieving toggle status for '+id+'=>got '+$.jStorage.get(key,undefined));
-    if ($.jStorage.get(key,true)) area.show();
-    else area.hide();
+    // don't do anything if nothing stored
+    var stored=$.jStorage.get(key,undefined);
+    if (stored==true || stored==false) {
+       //    window.console.log('retrieved toggle status for '+id+'=> '+stored);
+       pletoggle_set_visible(id,stored);
+    }
 }
 
 ////////// manage a toggle
 // toggle it
-function plekit_toggle(id){
+function pletoggle_toggle(id){
 
     var area=$('toggle-area-'+id);
     area.toggle();
@@ -31,17 +32,17 @@ function plekit_toggle(id){
        visible.hide();
        hidden.show();
     }
-    plekit_toggle_store(id);
+    pletoggle_store(id);
 }
 
-// make sure it's open
-function plekit_toggle_show(id) {
+// make sure it's open or closed
+function pletoggle_set_visible(id, status) {
     var area=$('toggle-area-'+id);
-    if (!area.visible()) plekit_toggle (id);
+    if (area.visible()!=status) pletoggle_toggle (id);
 }
 
 // toggle the attached info box
-function plekit_toggle_info(id){
+function pletoggle_toggle_info(id){
 
     // need to take care of the area as well
     var area=$('toggle-area-'+id);
@@ -52,7 +53,7 @@ function plekit_toggle_info(id){
     } else {
        // make sure area is visible, take of the triggers
 //     window.console.log('PTI showing');
-       plekit_toggle_show(id);
+       pletoggle_set_visible(id,true);
        info.show();
     }
 }