Skip to content

Commit

Permalink
Add tab snippets to cookie
Browse files Browse the repository at this point in the history
  • Loading branch information
gsechaud committed Feb 2, 2017
1 parent b39cf76 commit f07cad6
Showing 1 changed file with 69 additions and 4 deletions.
73 changes: 69 additions & 4 deletions themes/generic/src/main/assets/js/page.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,56 @@
$(function() {

// Tabbed code samples

var tabGroupClass = "tab-group";
var tabGroupCookie = "tabGroupsPref";
var cookieTg = getCookie(tabGroupCookie);
var cookieTgList = [];
if(cookieTg != "")
cookieTgList = 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 "";
}

function arrayToJson(arr) {
return JSON.stringify(arr);
}

// http://stackoverflow.com/questions/12551635/jquery-remove-duplicates-from-an-array-of-strings/12551709#12551709
function addToList(arr, elem) {
function unique(list) {
var result = [];
$.each(list, function(i, e) {
if ($.inArray(e, result) == -1) result.push(e);
});
return result;
}
arr.unshift(elem);
return unique(arr);
}

$("dl").has("dd > pre").each(function() {
var dl = $(this);
Expand All @@ -18,8 +68,21 @@ $(function() {
dd.addClass("has-note");
}
});
var current = dts.first().addClass("first").addClass("current");

var current;
for(var i = 0; i < cookieTgList.length && !current; i++) {
dts.each(function() {
var dt = $(this);
var pre = dt.next("dd").find("pre");
if(pre.hasClass(cookieTgList[i]))
current = dt.addClass("current");
});
}

if(!current)
current = dts.first().addClass("current");
var currentContent = current.next("dd").addClass("current").show();
dts.first().addClass("first");
dts.last().addClass("last");
dl.css("height", current.height() + currentContent.height());
});
Expand All @@ -30,14 +93,16 @@ $(function() {
var currentDl = currentDt.parent("dl");
var currentClasses = currentDt.next("dd").find("pre").attr("class").split(' ');
var currentGroup;
var regex = new RegExp("^tab-group-.*");
for(var i = 0; i < currentClasses.length && currentGroup == undefined; i++) {
var regex = new RegExp("^" + tabGroupClass + "-.*");
for(var i = 0; i < currentClasses.length && !currentGroup; i++) {
if(regex.test(currentClasses[i])) {
currentGroup = currentClasses[i];
cookieTgList = addToList(cookieTgList, currentGroup);
setCookie(tabGroupCookie, arrayToJson(cookieTgList));
}
}

if(currentGroup == undefined) {
if(!currentGroup) {
currentDl.find(".current").removeClass("current").next("dd").removeClass("current").hide();
currentDt.addClass("current");
var currentContent = currentDt.next("dd").addClass("current").show();
Expand Down

0 comments on commit f07cad6

Please sign in to comment.