Skip to content

Commit

Permalink
Only switch groups declared in the configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
raboof committed May 11, 2017
1 parent ab03fa8 commit cfcf299
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ class ParadoxProcessor(reader: Reader = new Reader, writer: Writer = new Writer)
navigationDepth: Int,
pageTemplate: PageTemplate,
errorListener: STErrorListener): Seq[(File, String)] = {
require(!groups.values.flatten.map(_.toLowerCase).groupBy(identity).values.exists(_.size > 1), "Group names may not overlap")

val pages = parsePages(mappings, Path.replaceSuffix(sourceSuffix, targetSuffix))
val paths = Page.allPaths(pages).toSet
val globalPageMappings = rootPageMappings(pages)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ package com.lightbend.paradox.markdown
object Groups {
def html(supergroups: Map[String, Seq[String]]) = {
supergroups.map {
case (_, groups) =>
"""<select class="supergroup">""" +
case (supergroup, groups) =>
s"""<select class="supergroup" name="$supergroup">""" +
groups.map(group => s"""<option class="group" value="group-${group.toLowerCase}">$group</option>""").mkString +
"</select>"
}.mkString("\n")
Expand Down
56 changes: 21 additions & 35 deletions themes/generic/src/main/assets/js/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ $(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 = "groupsPref";
var groupCookie = "paradoxGroups";
var cookieTg = getCookie(groupCookie);
var cookieTgList = [];
var currentGroups = {};

// 'reverse lookup' supergroup by group
var supergroupByGroup = {};

if(cookieTg != "")
cookieTgList = JSON.parse(cookieTg);
currentGroups = JSON.parse(cookieTg);

// http://www.w3schools.com/js/js_cookies.asp
function setCookie(cname,cvalue,exdays) {
Expand Down Expand Up @@ -35,41 +39,19 @@ $(function() {
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);
}

$(".supergroup").each(function() {
var groups = $(this).find(".group")
var supergroup = $(this).attr('name').toLowerCase();
var groups = $(this).find(".group");

var current;
for(var i = 0; i < cookieTgList.length && !current; i++) {
groups.each(function() {
var group = "group-" + $(this).text().toLowerCase();
if(group == cookieTgList[i])
current = group;
});
}
var current = currentGroups[supergroup];
if (!current) {
current = "group-" + groups.first().text().toLowerCase();
cookieTgList = addToList(cookieTgList, current);
currentGroups[supergroup] = current;
}

groups.each(function() {
var group = "group-" + $(this).text().toLowerCase();
supergroupByGroup[group] = supergroup;
if(group == current) {
switchToGroup(this.value);
} else {
Expand Down Expand Up @@ -100,11 +82,11 @@ $(function() {
});

var current;
for(var i = 0; i < cookieTgList.length && !current; i++) {
for (var supergroup in currentGroups) {
dts.each(function() {
var dt = $(this);
var pre = dt.next("dd").find("pre");
if(pre.hasClass(cookieTgList[i]))
if(pre.hasClass(currentGroups[supergroup]))
current = dt.addClass("current");
});
}
Expand Down Expand Up @@ -135,9 +117,13 @@ $(function() {
});

function switchToGroup(group) {
// Cookie:
cookieTgList = addToList(cookieTgList, group);
setCookie(groupCookie, arrayToJson(cookieTgList));
var supergroup = supergroupByGroup[group]
if (!supergroup) {
return;
}

currentGroups[supergroup] = group;
setCookie(groupCookie, JSON.stringify(currentGroups));

// Dropdown switcher:
$("select")
Expand Down

0 comments on commit cfcf299

Please sign in to comment.