1 // storing tabs active component in localStorage
3 // based on plugin_helper.js, extended to store the domid of an active tab
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);
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);
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 console.log("selector="+selector);
37 $(selector).tab('show');
39 set_from_saved_active_domid : function (domid) {
40 var active_domid=tabs_helper.retrieve_last_active_domid (domid);
41 if (tabs_helper.debug) messages.debug("restoring active domid for domid " + domid + " -> " + active_domid);
42 tabs_helper.set_active_domid (domid,active_domid);
45 init_all_tabs : function () {
47 $('.persistent-active').each(function() {
49 tabs_helper.set_from_saved_active_domid(domid);
51 // on all the children of the persistent-active element
52 $('.persistent-active>*').on('shown.bs.tab', function (e) {
53 var active_domid=e.target;
54 // at this point we have something like
55 // http://localhost:8080/portal/slice/ple.inria.f14#tab-resources-list
56 active_domid=active_domid.hash.replace("#","");
57 // find out the domid, which is for the nearest ancestor with class
59 var domid=$(e.target).closest(".persistent-active").attr('id');
60 tabs_helper.store_active_domid(domid,active_domid);
64 } // global var tabs_helper
66 $(document).ready(tabs_helper.init_all_tabs);
69 $.fn.Tabs = function( method ) {
70 // In Bootstrap 3, we need 'shown.bs.tab' instead of 'shown'
71 // see: http://bootply.com/bootstrap-3-migration-guide
72 $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
73 // find the plugin object inside the tab content referenced by the current tabs
74 $('.plugin', $($(e.target).attr('href'))).trigger('show');