599d0794a53bde85bb1710d946552b8154612a14
[myslice.git] / unfold / js / plugin.js
1 // xxx NOTE : pending move towards a more elaborate mode for 'toggled'
2 // for now it's just True or False and most of this code is not in action yet
3 // init_all_plugins does kick in though
4 var plugin = {
5
6     debug:true,
7
8     ////////// use local storage to remember open/closed toggles
9     store_status : function (domid) {
10         var plugin=$('#'+domid);
11         key='toggle.'+domid;
12         if (debug) console.log('storing toggle status for '+domid);
13         $.localStorage.setItem(key,plugin.visible());
14     },
15     // restore last status
16     restore_last_status : function (domid) {
17         key='toggle.'+domid;
18         // don't do anything if nothing stored
19         var retrieved=$.localStorage.getItem(key,undefined);
20         if (retrieved===null) return;
21         if (debug) console.log ("Applying retrieved status " + retrieved +  " to " + domid);
22         set_visible(domid,retrieved);
23     },
24     toggle : function (domid) {
25         var plugin=$('#'+domid);
26         plugin.toggle();
27         var showbtn=$('#show-'+domid);
28         var hidebtn=$('#hide-'+domid);
29         if (plugin.visible()) {
30             hidebtn.show();
31             showbtn.hide();
32         } else {
33             hidebtn.hide();
34             showbtn.show();
35         }
36         plugin.store_status(domid);
37     },
38     // 'target' is retrieved from storage so essentially a string 'true' or 'false'
39     set_visible : function (domid, target) {
40         var plugin=$('#'+domid);
41         if (plugin.visible()!=target) {
42             if (debug) console.log('set_visible: toggling ' + domid);
43             plugin.toggle (domid);
44         }
45     },
46     // triggered upon $(document).ready
47     init_all_plugins: function() {
48         $('.plugin-hide').each(function() {
49             $(this).click(function () { 
50                 var plugin='#'+this.id.replace('hide-',''); 
51                 var show='#'+this.id.replace('hide-','show-'); 
52                 $(plugin).slideUp(); $(show).show(); $(this).hide();});
53         });
54         $('.plugin-show').each(function() {
55             $(this).click(function () { 
56                 var plugin='#'+this.id.replace('show-',''); 
57                 var hide='#'+this.id.replace('show-','hide-'); 
58                 $(plugin).slideDown(); $(hide).show(); $(this).hide();});
59         });
60         $('.plugin-tooltip').each(function(){ $(this).tooltip({'selector':'','placement':'right'}); });
61     },
62 } // global unfold
63
64 /* upon document completion, we locate all the hide and show areas, 
65  * and configure their behaviour 
66  */
67 $(document).ready(plugin.init_all_plugins)
68