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