the tabs plugin has a persistent_active feature to enable saving the active tab in...
[myslice.git] / unfold / static / js / plugin_helper.js
1 //
2 // storing toggle's status in localStorage
3 // NOTE that localStorage only stores strings, so true becomes "true"
4 //
5 var plugin_helper = {
6
7     debug:false,
8
9     ////////// use local storage to remember open/closed toggles
10     store_toggle_status : function (domid,status) {
11         var key='toggle.'+domid;
12         if (plugin_helper.debug) messages.debug("storing toggle status " + status + " for " + domid + " key=" + key);
13         $.localStorage.setItem(key,status);
14     },
15     // restore last status
16     retrieve_last_toggle_status : function (domid) {
17         var key='toggle.'+domid;
18         // don't do anything if nothing stored
19         var retrieved=$.localStorage.getItem(key);
20         // set default to true
21         if (retrieved==null) retrieved="true";
22         if (plugin_helper.debug) messages.debug ("retrieved toggle status for " + domid + " (key=" + key + ") -> " + retrieved);
23         return retrieved;
24     },
25     set_toggle_status : function (domid,status) {
26         var plugindiv=$('#'+domid);
27         var showbtn=$('#show-'+domid);
28         var hidebtn=$('#hide-'+domid);
29         if (status=="true")     { plugindiv.slideDown(); hidebtn.show(); showbtn.hide(); }
30         else                    { plugindiv.slideUp();   hidebtn.hide(); showbtn.show(); }
31         plugin_helper.store_toggle_status(domid,status);
32     },
33     set_from_saved_toggle_status : function (domid) {
34         var previous_status=plugin_helper.retrieve_last_toggle_status (domid);
35         if (plugin_helper.debug) messages.debug("restoring initial status for domid " + domid + " -> " + previous_status);
36         plugin_helper.set_toggle_status (domid,previous_status);
37     },
38
39
40     // triggered upon $(document).ready
41     init_all_plugins : function() {
42         // plugins marked as persistent start with all 3 parts turned off
43         // let us first make sure the right parts are turned on 
44         $('.persistent-toggle').each(function() {
45             var domid=this.id.replace('complete-','');
46             plugin_helper.set_from_saved_toggle_status(domid);
47         });
48         // program the hide buttons so they do the right thing
49         $('.plugin-hide').each(function() {
50             $(this).click(function () { 
51                 var domid=this.id.replace('hide-','');
52                 plugin_helper.set_toggle_status(domid,"false");
53             })});
54         // same for show buttons
55         $('.plugin-show').each(function() {
56             $(this).click(function () { 
57                 var domid=this.id.replace('show-','');
58                 plugin_helper.set_toggle_status(domid,"true");
59             })});
60         // arm tooltips
61         $('.plugin-tooltip').each(function(){ $(this).tooltip({'selector':'','placement':'right'}); });
62     },
63 } // global var plugin_helper
64
65 /* upon document completion, we locate all the hide and show areas, 
66  * and configure their behaviour 
67  */
68 $(document).ready(plugin_helper.init_all_plugins)
69