Hi,

Please review the attached patch. Thanks!

jQuery tabs by default will display the first tab, so reloading a page
or opening a page from bookmark may not show the active tab correctly.
The nav_select_tabs() has been added to get the list of active tabs from
the hash values in the URL and then activate the appropriate tabs. It
will be called during page initialization and whenever the hash values
change.

The navigation.js and webui.js has been cleaned up to better utilize
jQuery API. jQuery selectors are used to create DOM objects that can
be used by subsequent codes. Tab selection handler is now added to the
tabs object instead of anchors. The change event no longer needs to be
triggered manually.

--
Endi S. Dewata
>From ad899a324c61efa1360faacd836411119ef978cb Mon Sep 17 00:00:00 2001
From: Endi S. Dewata <edew...@redhat.com>
Date: Mon, 27 Sep 2010 23:35:09 -0500
Subject: [PATCH] Fixed tab selection on page reload.

jQuery tabs by default will display the first tab, so reloading a page
or opening a page from bookmark may not show the active tab correctly.
The nav_select_tabs() has been added to get the list of active tabs from
the hash values in the URL and then activate the appropriate tabs. It
will be called during page initialization and whenever the hash values
change.

The navigation.js and webui.js has been cleaned up to better utilize
jQuery API. jQuery selectors are used to create DOM objects that can
be used by subsequent codes. Tab selection handler is now added to the
tabs object instead of anchors. The change event no longer needs to be
triggered manually.
---
 install/static/navigation.js |   63 ++++++++++++++++++++++++++---------------
 install/static/webui.js      |   18 ++++++------
 2 files changed, 49 insertions(+), 32 deletions(-)

diff --git a/install/static/navigation.js b/install/static/navigation.js
index 351c7c3..683c872 100644
--- a/install/static/navigation.js
+++ b/install/static/navigation.js
@@ -28,17 +28,25 @@ function nav_create(nls, container, tabclass)
     nav_generate_tabs(nls, container, tabclass, 1);
 
     var tabs = $('.' + tabclass);
-    tabs.tabs({event: 'change'});
-    tabs.find('ul.ui-tabs-nav a').click(_nav_tab_on_click);
+    tabs.tabs({
+        select: function(event, ui) {
+            var state = {};
+            var id = $(ui.panel).parent().attr('id');
+            state[id] = ui.index;
+            $.bbq.pushState(state);
+            return true;
+        }
+    });
 }
 
 function nav_generate_tabs(nls, container, tabclass, depth)
 {
     container.addClass(tabclass);
     container.addClass('tabs'+depth);
-    container.prepend('<ul></ul>');
 
-    var ul = container.children().first();
+    var ul = $('<ul/>');
+    container.append(ul);
+
     for (var i = 0; i < nls.length; ++i) {
         var n = nls[i];
 
@@ -47,11 +55,12 @@ function nav_generate_tabs(nls, container, tabclass, depth)
             name = ipa_objs[n[0]].label;
         }
 
-        nav_insert_tab_li(ul, n[0], name);
+        var li = nav_create_tab_li(n[0], name);
+        ul.append(li);
 
-        nav_insert_tab_div(container, n[0]);
+        var div = nav_create_tab_div(n[0]);
+        container.append(div);
 
-        var div = ul.parent().children().last();
         if (typeof n[2] == 'function') {
             n[2](div);
         } else if (n[2].length) {
@@ -60,28 +69,36 @@ function nav_generate_tabs(nls, container, tabclass, depth)
     }
 }
 
-var _nav_li_tab_template = '<li><a href="#I">N</a></li>';
-
-function nav_insert_tab_li(jobj, id, name)
+function nav_create_tab_li(id, name)
 {
-    jobj.append(_nav_li_tab_template.replace('I', id).replace('N', name));
+    return $('<li/>').append($('<a/>', {
+        href: '#'+id,
+        title: id,
+        html: name
+    }));
 }
 
-var _nav_div_tab_template = '<div id="T"></div>';
-
-function nav_insert_tab_div(jobj, id)
+function nav_create_tab_div(id)
 {
-    jobj.append(_nav_div_tab_template.replace('T', id));
+    return $('<div/>', {
+        id: id
+    });
 }
 
-function _nav_tab_on_click(obj)
+function nav_select_tabs(nls, container)
 {
-    var jobj = $(this);
-    var state = {};
-    var id = jobj.closest('.tabs').attr('id');
-    var index = jobj.parent().prevAll().length;
+    var id = container.attr('id');
+    var selectedTab = $.bbq.getState(id, true) || 0;
+    if (selectedTab >= nls.length) selectedTab = 0;
+    container.tabs('select', selectedTab);
 
-    state[id] = index;
-    $.bbq.pushState(state);
-}
+    for (var i = 0; i < nls.length; ++i) {
+        var n = nls[i];
+
+        var div = $('#'+n[0]);
 
+        if (typeof n[2] != 'function' && n[2].length) {
+            nav_select_tabs(n[2], div);
+        }
+    }
+}
diff --git a/install/static/webui.js b/install/static/webui.js
index e904557..aa17b41 100644
--- a/install/static/webui.js
+++ b/install/static/webui.js
@@ -60,7 +60,6 @@ $(function() {
             if (whoami.hasOwnProperty('memberof_rolegroup') &&
                 whoami.memberof_rolegroup.length > 0){
                 nav_tabs_lists = admin_tabs_lists;
-                window_hashchange(null);
             }else{
                 nav_tabs_lists = self_serv_tabs_lists;
 
@@ -69,7 +68,11 @@ $(function() {
                              'details'};
                 $.bbq.pushState(state);
             }
-            nav_create(nav_tabs_lists, $('#navigation'), 'tabs');
+
+            var navigation = $('#navigation');
+            nav_create(nav_tabs_lists, navigation, 'tabs');
+            nav_select_tabs(nav_tabs_lists, navigation);
+
             $('#login_header').html(ipa_messages.login.header);
         }else{
             alert("Unable to find prinicpal for logged in user");
@@ -98,11 +101,8 @@ var window_hash_cache = {};
 /* main loop (hashchange event handler) */
 function window_hashchange(evt)
 {
-    $('.tabs').each(function () {
-        var jobj = $(this);
-        var index = $.bbq.getState(jobj.attr('id'), true) || 0;
-        jobj.find('ul.ui-tabs-nav a').eq(index).triggerHandler('change');
-    });
+    var navigation = $('#navigation');
+    nav_select_tabs(nav_tabs_lists, navigation);
 
     for (var i = 0; i < nav_tabs_lists.length; ++i) {
         var t = nav_tabs_lists[i];
@@ -111,7 +111,7 @@ function window_hashchange(evt)
                 var tt = t[2][j];
                 var obj_name = tt[0];
                 var entity_setup = tt[2];
-                var div = $('#' + t[0] + ' div[title=' + obj_name + ']');
+                var div = $('#' + tt[0]);
 
                 var state = obj_name + '-facet';
                 var facet = $.bbq.getState(state, true) || 'search';
@@ -133,7 +133,7 @@ function window_hashchange(evt)
                     var last_pkey = window_hash_cache[state];
                     if (pkey != last_pkey)
                         entity_setup(div);
-                } else if (facet == 'associate' || facet == 'enroll') {
+                } else if (facet == 'associate') {
                     state = obj_name + '-enroll';
                     var enroll = $.bbq.getState(state, true);
                     var last_enroll = window_hash_cache[state];
-- 
1.6.6.1

_______________________________________________
Freeipa-devel mailing list
Freeipa-devel@redhat.com
https://www.redhat.com/mailman/listinfo/freeipa-devel

Reply via email to