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