Skip to content

Commit

Permalink
set default tab
Browse files Browse the repository at this point in the history
  • Loading branch information
zzhaobraze committed Jan 31, 2025
1 parent bbfa8ef commit eb05b36
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 23 deletions.
14 changes: 10 additions & 4 deletions _plugins/sdktabs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ def render(context)
tabslist = '<ul class="sdk-ab-nav sdk-ab-nav-tabs ' + @tabclass + '_ul" id="' + @tabid + '_nav">' + "\n"
if tabs.length > 0
tabs.each_with_index do |tab, ind|
itemid = (0...12).map { (97 + rand(26)).chr }.join

tabslug = tab[0].gsub(/[^0-9a-z]/i, '')
tabslug = Digest::MD5.hexdigest(tab[0]) if tabslug.empty?

# scan returns array of results, only care about first match
tabslist += ' <li class="sdkrow ' + tabslug
tabslist += ' <li id="sdkt_' + itemid + '" class="sdkrow ' + tabslug
if ind == 0
tabslist += ' active'
end
Expand Down Expand Up @@ -49,14 +51,15 @@ def render(context)
match ? match[1].size : 0
end
indentation = indentation.min
contentid = (0...12).map { (97 + rand(26)).chr }.join

content = indentation ? super.gsub(/^#{' |\t' * indentation}/, '') : super
content = converter.convert(content)
content = content.strip # Strip again to avoid "\n"
tabslug = @tab.gsub(/[^0-9a-z]/i, '')
tabslug = Digest::MD5.hexdigest(@tab) if tabslug.empty?

return '<div class="sdk-ab-tab-pane ' + tabslug + '_tab " data-sdk-tab="sdk-' + @tab + '">' + content + "</div>"
return '<div id="sdkc_' + contentid + '" class="sdk-ab-tab-pane ' + tabslug + '_tab " data-sdk-tab="sdk-' + @tab + '">' + content + "</div>"
end
end

Expand All @@ -75,11 +78,13 @@ def render(context)

if tabs.length > 0
tabs.each_with_index do |tab, ind|

itemid = (0...12).map { (97 + rand(26)).chr }.join
tabslug = tab[0].gsub(/[^0-9a-z]/i, '')
tabslug = Digest::MD5.hexdigest(tab[0]) if tabslug.empty?

# scan returns array of results, only care about first match
tabslist += ' <li class="coderow ' + tabslug + '_sub_sdk_tab'
tabslist += ' <li id="sdkst_' + itemid + '" class="coderow ' + tabslug + '_sub_sdk_tab'
if ind == 0
tabslist += ' sub_active'
end
Expand Down Expand Up @@ -109,14 +114,15 @@ def render(context)
match ? match[1].size : 0
end
indentation = indentation.min
contentid = (0...12).map { (97 + rand(26)).chr }.join

content = indentation ? super.gsub(/^#{' |\t' * indentation}/, '') : super
content = converter.convert(content)
content = content.strip # Strip again to avoid "\n"
tabslug = @tab.gsub(/[^0-9a-z]/i, '')
tabslug = Digest::MD5.hexdigest(@tab) if tabslug.empty?

return '<div class="sdk-ab-sub_tab-pane ' + tabslug + '_sub_sdk_tab " data-sdk-sub_tab="' + @tab + '">' + content + "</div>"
return '<div id="sdksc_' + contentid + '" class="sdk-ab-sub_tab-pane ' + tabslug + '_sub_sdk_tab " data-sdk-sub_tab="' + @tab + '">' + content + "</div>"
end
end
end
Expand Down
19 changes: 15 additions & 4 deletions _plugins/tabs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@ def render(context)
tabslist = '<ul class="ab-nav ab-nav-tabs ' + @tabclass + '_ul" id="' + @tabid + '_nav">' + "\n"
if tabs.length > 0
tabs.each_with_index do |tab, ind|
itemid = (0...12).map { (97 + rand(26)).chr }.join
tabslug = tab[0].gsub(/[^0-9a-z]/i, '')
tabslug = Digest::MD5.hexdigest(tab[0]) if tabslug.empty?

# scan returns array of results, only care about first match
tabslist += ' <li class="coderow ' + tab[0].gsub(' ', '-')
tabslist += ' <li tabindex="0" id="mt_' + itemid + '" class="coderow ' + tabslug
if ind == 0
tabslist += ' active'
end
Expand Down Expand Up @@ -44,13 +48,14 @@ def render(context)
match ? match[1].size : 0
end
indentation = indentation.min
contentid = (0...12).map { (97 + rand(26)).chr }.join

content = indentation ? super.gsub(/^#{' |\t' * indentation}/, '') : super
content = converter.convert(content)
content = content.strip # Strip again to avoid "\n"
tabslug = @tab.gsub(' ', '-')

return '<div class="ab-tab-pane ' + tabslug + '_tab " data-tab="' + @tab + '">' + content + "</div>"
return '<div id="mc_' + contentid + '" class="ab-tab-pane ' + tabslug + '_tab " data-tab="' + @tab + '">' + content + "</div>"
end
end

Expand All @@ -69,8 +74,13 @@ def render(context)

if tabs.length > 0
tabs.each_with_index do |tab, ind|
itemid = (0...12).map { (97 + rand(26)).chr }.join

tabslug = tab[0].gsub(/[^0-9a-z]/i, '')
tabslug = Digest::MD5.hexdigest(tab[0]) if tabslug.empty?

# scan returns array of results, only care about first match
tabslist += ' <li class="coderow ' + tab[0].gsub(' ', '-') + '_sub_tab'
tabslist += ' <li tabindex="0" id="st_' + itemid + '" class="coderow ' + tabslug + '_sub_tab'
if ind == 0
tabslist += ' sub_active'
end
Expand Down Expand Up @@ -100,13 +110,14 @@ def render(context)
match ? match[1].size : 0
end
indentation = indentation.min
contentid = (0...12).map { (97 + rand(26)).chr }.join

content = indentation ? super.gsub(/^#{' |\t' * indentation}/, '') : super
content = converter.convert(content)
content = content.strip # Strip again to avoid "\n"
tabslug = @tab.gsub(' ', '-')

return '<div class="ab-sub_tab-pane ' + tabslug + '_sub_tab " data-sub_tab="' + @tab + '">' + content + "</div>"
return '<div id="sc_' + contentid + '" class="ab-sub_tab-pane ' + tabslug + '_sub_tab " data-sub_tab="' + @tab + '">' + content + "</div>"
end
end
end
Expand Down
92 changes: 77 additions & 15 deletions assets/js/documents.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ var custom_word_mapping = {
};
// Track Specific tabs to remember on reload
var tab_track = {
'android': 'tb_android',
'swift': 'tb_ios',
'objective-c': 'tb_ios',
'java': 'tb_android',
Expand Down Expand Up @@ -280,8 +281,18 @@ $(document).ready(function() {
else {
window.history.replaceState(null, null, window.location.pathname + query_str);
}
if (tab_track[tab_norm]){
Cookies.set(tab_track[tab_norm],tab_norm, { expires: 365 });
switch(query_name) {
case 'sdktab': {
Cookies.set('sdktab',tab_norm, { expires: 365 });
}
case 'sdksubtab': {
Cookies.set('sdksubtab',tab_norm, { expires: 365 });
}
default: {
if (tab_track[tab_norm]){
Cookies.set(tab_track[tab_norm],tab_norm, { expires: 365 });
}
}
}
}

Expand Down Expand Up @@ -375,10 +386,56 @@ $(document).ready(function() {
}

function setTabClass(tabtype, prefix, postfix, curtab){
$('.' + tabtype + prefix +'tab_toggle_ul.' + tabtype + 'ab-' + prefix +'nav-' + prefix +'tabs li').removeClass(prefix + 'active');
$('.' + tabtype+ prefix +'tab_toggle_ul.' + tabtype + 'ab-' + prefix +'nav-' + prefix +'tabs li.' + curtab).addClass(prefix + 'active');
$('div.' + tabtype+ prefix +'tab_toggle_div div.' + tabtype + 'ab-' + prefix + 'tab-pane').removeClass(prefix + 'active');
$('div.' + tabtype+ prefix +'tab_toggle_div div.' + curtab + postfix).addClass(prefix + 'active');
var tab_selecter = '.' + tabtype + prefix +'tab_toggle_ul.' + tabtype + 'ab-' + prefix +'nav-' + prefix +'tabs';
var content_selecter ='div.' + tabtype + prefix +'tab_toggle_div';

var last_active_tab = {};
var last_active_content = {};
$(tab_selecter).each(function (k,v) {
var el = $(v);
var li = el.children('li')
var last_active = li.filter('.' + prefix + 'active');
if (last_active.length) {
last_active_tab[el.attr('id')] = last_active.first().attr('id');
}
else {
last_active_tab[el.attr('id')] = li.first().attr('id');
}
});

$(content_selecter).each(function (k,v) {
var el = $(v);
var div = el.children('div')
var last_active = div.filter('.' + tabtype + 'ab-' + prefix + 'tab-pane ' + prefix + 'active');
if (last_active.length) {
last_active_content[el.attr('id')] = last_active.first().attr('id');
}
else {
last_active_content[el.attr('id')] = div.first().attr('id');
}
});

$(tab_selecter + ' li').removeClass(prefix + 'active');
$(tab_selecter + ' li.' + curtab).addClass(prefix + 'active');
$(content_selecter + ' div.' + tabtype + 'ab-' + prefix + 'tab-pane').removeClass(prefix + 'active');
$('div.' + tabtype + prefix +'tab_toggle_div div.' + curtab + postfix).addClass(prefix + 'active');

$(tab_selecter).each(function (k,v) {
var el = $(v);
var li = el.children('li')
var last_active = li.filter('.' + prefix + 'active');
if (!last_active.length) {
$('#' + last_active_tab[el.attr('id')]).addClass(prefix + 'active');
}
});
$(content_selecter).each(function (k,v) {
var el = $(v);
var div = el.children('div')
var last_active = div.filter('.' + tabtype + 'ab-' + prefix + 'tab-pane.' + prefix + 'active');
if (!last_active.length) {
$('#' + last_active_content[el.attr('id')]).addClass(prefix + 'active');
}
});
}

function setTabOnlyClass(tabtype, prefix, postfix, partab, curtab){
Expand Down Expand Up @@ -416,6 +473,8 @@ $(document).ready(function() {
$('.sub_tab_toggle, sub_sdk-tab_toggle').click(function(e){
e.preventDefault();
var $this = $(this);
var tabtype = $this.attr("class").includes('sdk-') ? 'sdk-' : '';
var curtab = $this.attr('data-' + tabtype + 'sub_tab');
var tabstate = $this.attr("class").includes('sdk-') ? 'sdksubtab' : 'subtab';

setTabClass('','sub_', '', curtab)
Expand Down Expand Up @@ -453,8 +512,9 @@ $(document).ready(function() {
}
else if (tab_track[curtab_name]){
let tab_cookie = Cookies.get(tab_track[curtab_name]) || '';
if (tab_cookie && (curtab_name == tab_cookie))
setTabClass(tabtype,'', '_tab', curtab)
if (tab_cookie && (curtab_name == tab_cookie)) {
setTabClass(tabtype,'', '_tab', curtab)
}
}

});
Expand All @@ -470,8 +530,9 @@ $(document).ready(function() {
}
else if (tab_track[curtab_name]){
let tab_cookie = Cookies.get(tab_track[curtab]) || '';
if (tab_cookie && (curtab_name == tab_cookie))
setTabOnlyClass(tabtype,'', '_tab', partab, curtab)
if (tab_cookie && (curtab_name == tab_cookie)) {
setTabOnlyClass(tabtype,'', '_tab', partab, curtab)
}
}
});

Expand All @@ -483,13 +544,13 @@ $(document).ready(function() {
if ((tab_query && (tab_query == curtab_name)) ||
(sub_tab_query && (sub_tab_query == curtab_name))
){

setTabClass(tabtype,'sub_','', curtab)
}
else if (tab_track[curtab_name]){
let tab_cookie = Cookies.get(tab_track[curtab_name]) || '';
if (tab_cookie && (curtab_name == tab_cookie))
setTabClass(tabtype,'sub_','', curtab)
if (tab_cookie && (curtab_name == tab_cookie)){
setTabClass(tabtype,'sub_','', curtab)
}
}
});

Expand All @@ -508,8 +569,9 @@ $(document).ready(function() {
}
else if (tab_track[curtab_name]){
let tab_cookie = Cookies.get(tab_track[curtab_name]) || '';
if (tab_cookie && (curtab_name == tab_cookie))
setTabOnlyClass(tabtype,'sub_','', partab, curtab)
if (tab_cookie && (curtab_name == tab_cookie)) {
setTabOnlyClass(tabtype,'sub_','', partab, curtab)
}
}
});

Expand Down

0 comments on commit eb05b36

Please sign in to comment.