miscell cosmetic
[myslice.git] / plugins / tabs / static / js / tabs.js
1 // storing tabs active component in localStorage
2 //
3 // based on plugin_helper.js, extended to store the domid of an active tab
4 //
5
6 var tabs_helper = {
7
8     debug:false,
9
10     ////////// use local storage to remember the active son
11     // xxx this is a bit fragile in that we assume the elements that need to have the 'active' class
12     // are direct sons of the .persistent-active elements
13     // it would be simple to overcome this weakness by requiring the template to set
14     // .persistent-active-item on these elements active-able instead 
15     store_active_domid : function (domid,active_domid) {
16         var key='active.'+domid;
17         if (tabs_helper.debug) messages.debug("storing for domid " + domid + " (key=" + key + ") active_domid=" + active_domid);
18         $.localStorage.setItem(key,active_domid);
19     },
20     // restore last status
21     retrieve_last_active_domid : function (domid) {
22         var key='active.'+domid;
23         // don't do anything if nothing stored
24         var retrieved=$.localStorage.getItem(key);
25         // set default to undefined
26         if (retrieved==null) retrieved=undefined;
27         if (tabs_helper.debug) messages.debug ("retrieved active status for " + domid + " (key=" + key + ") -> " + retrieved);
28         return retrieved;
29     },
30     // tabs_helper.set_active_domid("tabs-resources","tab-resources-list")
31     set_active_domid : function (domid,active_domid) {
32         if ( ! active_domid ) return;
33         if (tabs_helper.debug) messages.debug ("setting active for " + domid + " to active_domid=" + active_domid);
34         // build something like "#uldomid a[href='#active_domid']"
35         var selector="#"+domid+" a[href='#"+active_domid+"']";
36         $(selector).tab('show');
37     },
38     set_from_saved_active_domid : function (domid) {
39         var active_domid=tabs_helper.retrieve_last_active_domid (domid);
40         if (tabs_helper.debug) messages.debug("restoring active domid for domid " + domid + " -> " + active_domid);
41         tabs_helper.set_active_domid (domid,active_domid);
42     },
43
44     init_all_tabs : function () {
45         ////////// active
46         $('.persistent-active').each(function() {
47             var domid=this.id;
48             tabs_helper.set_from_saved_active_domid(domid);
49         });
50         // on all the children of the persistent-active element
51         $('.persistent-active>*').on('shown.bs.tab', function (e) {
52             var active_domid=e.target;
53             // at this point we have something like 
54             // http://localhost:8080/portal/slice/ple.inria.f14#tab-resources-list
55             active_domid=active_domid.hash.replace("#","");
56             // find out the domid, which is for the nearest ancestor with class
57             // persistent-active
58             var domid=$(e.target).closest(".persistent-active").attr('id');
59             tabs_helper.store_active_domid(domid,active_domid);
60         });
61     },
62
63 } // global var tabs_helper
64
65 $(document).ready(tabs_helper.init_all_tabs);
66
67 (function($){
68     $.fn.Tabs = function( method ) {
69         // In Bootstrap 3, we need 'shown.bs.tab' instead of 'shown'
70         // see: http://bootply.com/bootstrap-3-migration-guide
71         $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
72           // find the plugin object inside the tab content referenced by the current tabs
73           $('.plugin', $($(e.target).attr('href'))).trigger('show');
74         });
75     };
76 })( jQuery );