-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Put groups/tabs logic in its own .js file
- Loading branch information
Showing
3 changed files
with
172 additions
and
169 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
groupChangeListeners = []; | ||
|
||
window.groupChanged = function(callback) { | ||
groupChangeListeners.push(callback); | ||
} | ||
|
||
$(function() { | ||
|
||
// Groups (like 'java' and 'scala') represent groups of 'switchable' content, either in tabs or in regular text. | ||
// The catalog of groups can be defined in the sbt parameters to initialize the group. | ||
|
||
var groupCookie = "paradoxGroups"; | ||
var cookieTg = getCookie(groupCookie); | ||
var currentGroups = {}; | ||
|
||
var catalog = {} | ||
var supergroupByGroup = {}; | ||
|
||
|
||
if(cookieTg != "") | ||
currentGroups = JSON.parse(cookieTg); | ||
|
||
// http://www.w3schools.com/js/js_cookies.asp | ||
function setCookie(cname,cvalue,exdays) { | ||
if(!exdays) exdays = 365; | ||
var d = new Date(); | ||
d.setTime(d.getTime() + (exdays*24*60*60*1000)); | ||
var expires = "expires=" + d.toGMTString(); | ||
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/"; | ||
} | ||
|
||
// http://www.w3schools.com/js/js_cookies.asp | ||
function getCookie(cname) { | ||
var name = cname + "="; | ||
var decodedCookie = decodeURIComponent(document.cookie); | ||
var ca = decodedCookie.split(';'); | ||
for(var i = 0; i < ca.length; i++) { | ||
var c = ca[i]; | ||
while (c.charAt(0) == ' ') { | ||
c = c.substring(1); | ||
} | ||
if (c.indexOf(name) == 0) { | ||
return c.substring(name.length, c.length); | ||
} | ||
} | ||
return ""; | ||
} | ||
|
||
$("dl").has("dd > pre").each(function() { | ||
var dl = $(this); | ||
dl.addClass("tabbed"); | ||
var dts = dl.find("dt"); | ||
dts.each(function(i) { | ||
var dt = $(this); | ||
dt.html("<a href=\"#tab" + i + "\">" + dt.text() + "</a>"); | ||
}); | ||
var dds = dl.find("dd"); | ||
dds.each(function(i) { | ||
var dd = $(this); | ||
dd.hide(); | ||
if (dd.find("blockquote").length) { | ||
dd.addClass("has-note"); | ||
} | ||
}); | ||
|
||
// Default to the first tab, for grouped tabs switch again later | ||
switchToTab(dts.first()); | ||
|
||
dts.first().addClass("first"); | ||
dts.last().addClass("last"); | ||
}); | ||
|
||
$(".supergroup").each(function() { | ||
var supergroup = $(this).attr('name').toLowerCase(); | ||
var groups = $(this).find(".group"); | ||
|
||
var current = currentGroups[supergroup]; | ||
if (!current) { | ||
current = "group-" + groups.first().text().toLowerCase(); | ||
currentGroups[supergroup] = current; | ||
} | ||
|
||
catalog[supergroup] = []; | ||
|
||
groups.each(function() { | ||
var group = "group-" + $(this).text().toLowerCase(); | ||
catalog[supergroup].push(group); | ||
supergroupByGroup[group] = supergroup; | ||
}); | ||
|
||
switchToGroup(supergroup, current); | ||
|
||
$(this).on("change", function() { | ||
switchToGroup(supergroup, this.value); | ||
}); | ||
}); | ||
|
||
$("dl.tabbed dt a").click(function(e){ | ||
e.preventDefault(); | ||
var currentDt = $(this).parent("dt"); | ||
var currentDl = currentDt.parent("dl"); | ||
|
||
var currentGroup = groupOf(currentDt); | ||
|
||
var supergroup = supergroupByGroup[currentGroup] | ||
if (supergroup) { | ||
switchToGroup(supergroup, currentGroup); | ||
} else { | ||
switchToTab(currentDt); | ||
} | ||
}); | ||
|
||
function switchToGroup(supergroup, group) { | ||
currentGroups[supergroup] = group; | ||
setCookie(groupCookie, JSON.stringify(currentGroups)); | ||
|
||
// Dropdown switcher: | ||
$("select") | ||
.has("option[value=" + group +"]") | ||
.val(group); | ||
|
||
// Inline snippets: | ||
for (var i = 0; i < catalog[supergroup].length; i++) { | ||
var peer = catalog[supergroup][i]; | ||
if (peer == group) { | ||
$("span." + group).show(); | ||
} else { | ||
$("span." + peer).hide(); | ||
} | ||
} | ||
|
||
// Tabbed snippets: | ||
$("dl.tabbed").each(function() { | ||
var dl = $(this); | ||
dl.find("dt").each(function() { | ||
var dt = $(this); | ||
if(groupOf(dt) == group) { | ||
switchToTab(dt); | ||
} | ||
}); | ||
}); | ||
|
||
for (var i = 0; i < groupChangeListeners.length; i++) { | ||
groupChangeListeners[i](group, supergroup, catalog); | ||
} | ||
} | ||
|
||
function switchToTab(dt) { | ||
var dl = dt.parent("dl"); | ||
dl.find(".current").removeClass("current").next("dd").removeClass("current").hide(); | ||
dt.addClass("current"); | ||
var currentContent = dt.next("dd").addClass("current").show(); | ||
dl.css("height", dt.height() + currentContent.height()); | ||
} | ||
|
||
function groupOf(elem) { | ||
var classAttribute = elem.next("dd").find("pre").attr("class"); | ||
if (classAttribute) { | ||
var currentClasses = classAttribute.split(' '); | ||
var regex = new RegExp("^group-.*"); | ||
for(var i = 0; i < currentClasses.length; i++) { | ||
if(regex.test(currentClasses[i])) { | ||
return currentClasses[i]; | ||
} | ||
} | ||
} | ||
|
||
// No class found? Then use the tab title | ||
return "group-" + elem.find('a').text().toLowerCase(); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,171 +1,2 @@ | ||
groupChangeListeners = []; | ||
|
||
window.groupChanged = function(callback) { | ||
groupChangeListeners.push(callback); | ||
} | ||
|
||
$(function() { | ||
|
||
// Groups (like 'java' and 'scala') represent groups of 'switchable' content, either in tabs or in regular text. | ||
// The catalog of groups can be defined in the sbt parameters to initialize the group. | ||
|
||
var groupCookie = "paradoxGroups"; | ||
var cookieTg = getCookie(groupCookie); | ||
var currentGroups = {}; | ||
|
||
var catalog = {} | ||
var supergroupByGroup = {}; | ||
|
||
|
||
if(cookieTg != "") | ||
currentGroups = JSON.parse(cookieTg); | ||
|
||
// http://www.w3schools.com/js/js_cookies.asp | ||
function setCookie(cname,cvalue,exdays) { | ||
if(!exdays) exdays = 365; | ||
var d = new Date(); | ||
d.setTime(d.getTime() + (exdays*24*60*60*1000)); | ||
var expires = "expires=" + d.toGMTString(); | ||
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/"; | ||
} | ||
|
||
// http://www.w3schools.com/js/js_cookies.asp | ||
function getCookie(cname) { | ||
var name = cname + "="; | ||
var decodedCookie = decodeURIComponent(document.cookie); | ||
var ca = decodedCookie.split(';'); | ||
for(var i = 0; i < ca.length; i++) { | ||
var c = ca[i]; | ||
while (c.charAt(0) == ' ') { | ||
c = c.substring(1); | ||
} | ||
if (c.indexOf(name) == 0) { | ||
return c.substring(name.length, c.length); | ||
} | ||
} | ||
return ""; | ||
} | ||
|
||
$("dl").has("dd > pre").each(function() { | ||
var dl = $(this); | ||
dl.addClass("tabbed"); | ||
var dts = dl.find("dt"); | ||
dts.each(function(i) { | ||
var dt = $(this); | ||
dt.html("<a href=\"#tab" + i + "\">" + dt.text() + "</a>"); | ||
}); | ||
var dds = dl.find("dd"); | ||
dds.each(function(i) { | ||
var dd = $(this); | ||
dd.hide(); | ||
if (dd.find("blockquote").length) { | ||
dd.addClass("has-note"); | ||
} | ||
}); | ||
|
||
// Default to the first tab, for grouped tabs switch again later | ||
switchToTab(dts.first()); | ||
|
||
dts.first().addClass("first"); | ||
dts.last().addClass("last"); | ||
}); | ||
|
||
$(".supergroup").each(function() { | ||
var supergroup = $(this).attr('name').toLowerCase(); | ||
var groups = $(this).find(".group"); | ||
|
||
var current = currentGroups[supergroup]; | ||
if (!current) { | ||
current = "group-" + groups.first().text().toLowerCase(); | ||
currentGroups[supergroup] = current; | ||
} | ||
|
||
catalog[supergroup] = []; | ||
|
||
groups.each(function() { | ||
var group = "group-" + $(this).text().toLowerCase(); | ||
catalog[supergroup].push(group); | ||
supergroupByGroup[group] = supergroup; | ||
}); | ||
|
||
switchToGroup(supergroup, current); | ||
|
||
$(this).on("change", function() { | ||
switchToGroup(supergroup, this.value); | ||
}); | ||
}); | ||
|
||
$("dl.tabbed dt a").click(function(e){ | ||
e.preventDefault(); | ||
var currentDt = $(this).parent("dt"); | ||
var currentDl = currentDt.parent("dl"); | ||
|
||
var currentGroup = groupOf(currentDt); | ||
|
||
var supergroup = supergroupByGroup[currentGroup] | ||
if (supergroup) { | ||
switchToGroup(supergroup, currentGroup); | ||
} else { | ||
switchToTab(currentDt); | ||
} | ||
}); | ||
|
||
function switchToGroup(supergroup, group) { | ||
currentGroups[supergroup] = group; | ||
setCookie(groupCookie, JSON.stringify(currentGroups)); | ||
|
||
// Dropdown switcher: | ||
$("select") | ||
.has("option[value=" + group +"]") | ||
.val(group); | ||
|
||
// Inline snippets: | ||
for (var i = 0; i < catalog[supergroup].length; i++) { | ||
var peer = catalog[supergroup][i]; | ||
if (peer == group) { | ||
$("span." + group).show(); | ||
} else { | ||
$("span." + peer).hide(); | ||
} | ||
} | ||
|
||
// Tabbed snippets: | ||
$("dl.tabbed").each(function() { | ||
var dl = $(this); | ||
dl.find("dt").each(function() { | ||
var dt = $(this); | ||
if(groupOf(dt) == group) { | ||
switchToTab(dt); | ||
} | ||
}); | ||
}); | ||
|
||
for (var i = 0; i < groupChangeListeners.length; i++) { | ||
groupChangeListeners[i](group, supergroup, catalog); | ||
} | ||
} | ||
|
||
function switchToTab(dt) { | ||
var dl = dt.parent("dl"); | ||
dl.find(".current").removeClass("current").next("dd").removeClass("current").hide(); | ||
dt.addClass("current"); | ||
var currentContent = dt.next("dd").addClass("current").show(); | ||
dl.css("height", dt.height() + currentContent.height()); | ||
} | ||
|
||
function groupOf(elem) { | ||
var classAttribute = elem.next("dd").find("pre").attr("class"); | ||
if (classAttribute) { | ||
var currentClasses = classAttribute.split(' '); | ||
var regex = new RegExp("^group-.*"); | ||
for(var i = 0; i < currentClasses.length; i++) { | ||
if(regex.test(currentClasses[i])) { | ||
return currentClasses[i]; | ||
} | ||
} | ||
} | ||
|
||
// No class found? Then use the tab title | ||
return "group-" + elem.find('a').text().toLowerCase(); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters