first commit
This commit is contained in:
commit
13e2fd6e74
125 changed files with 19943 additions and 0 deletions
638
public/javascripts/bigfoot.js
Executable file
638
public/javascripts/bigfoot.js
Executable file
|
@ -0,0 +1,638 @@
|
|||
(function() {
|
||||
(function($) {
|
||||
return $.bigfoot = function(options) {
|
||||
var addBreakpoint, baseFontSize, bigfoot, buttonHover, calculatePixelDimension, cleanFootnoteLinks, clickButton, createPopover, defaults, deleteEmptyOrHR, escapeKeypress, footnoteInit, getSetting, makeDefaultCallbacks, popoverStates, positionTooltip, removeBackLinks, removeBreakpoint, removePopovers, replaceWithReferenceAttributes, repositionFeet, roomCalc, settings, touchClick, unhoverFeet, updateSetting, viewportDetails;
|
||||
bigfoot = void 0;
|
||||
defaults = {
|
||||
actionOriginalFN: "hide",
|
||||
activateCallback: function() {},
|
||||
activateOnHover: false,
|
||||
allowMultipleFN: false,
|
||||
anchorPattern: /(fn|footnote|note)[:\-_\d]/gi,
|
||||
anchorParentTagname: 'sup',
|
||||
breakpoints: {},
|
||||
deleteOnUnhover: false,
|
||||
footnoteParentClass: 'footnote',
|
||||
footnoteTagname: 'li',
|
||||
hoverDelay: 250,
|
||||
numberResetSelector: void 0,
|
||||
popoverDeleteDelay: 300,
|
||||
popoverCreateDelay: 100,
|
||||
positionContent: true,
|
||||
preventPageScroll: true,
|
||||
scope: false,
|
||||
useFootnoteOnlyOnce: true,
|
||||
contentMarkup: "<aside class=\"bigfoot-footnote is-positioned-bottom\" data-footnote-number=\"{{FOOTNOTENUM}}\" data-footnote-identifier=\"{{FOOTNOTEID}}\" alt=\"Footnote {{FOOTNOTENUM}}\"> <div class=\"bigfoot-footnote__wrapper\"> <div class=\"bigfoot-footnote__content\"> {{FOOTNOTECONTENT}} </div></div> <div class=\"bigfoot-footnote__tooltip\"></div> </aside>",
|
||||
buttonMarkup: "<div class='bigfoot-footnote__container'> <button class=\"bigfoot-footnote__button\" id=\"{{SUP:data-footnote-backlink-ref}}\" data-footnote-number=\"{{FOOTNOTENUM}}\" data-footnote-identifier=\"{{FOOTNOTEID}}\" alt=\"See Footnote {{FOOTNOTENUM}}\" rel=\"footnote\" data-bigfoot-footnote=\"{{FOOTNOTECONTENT}}\"> <svg class=\"bigfoot-footnote__button__circle\" viewbox=\"0 0 6 6\" preserveAspectRatio=\"xMinYMin\"><circle r=\"3\" cx=\"3\" cy=\"3\" fill=\"white\"></circle></svg> <svg class=\"bigfoot-footnote__button__circle\" viewbox=\"0 0 6 6\" preserveAspectRatio=\"xMinYMin\"><circle r=\"3\" cx=\"3\" cy=\"3\" fill=\"white\"></circle></svg> <svg class=\"bigfoot-footnote__button__circle\" viewbox=\"0 0 6 6\" preserveAspectRatio=\"xMinYMin\"><circle r=\"3\" cx=\"3\" cy=\"3\" fill=\"white\"></circle></svg> </button></div>"
|
||||
};
|
||||
settings = $.extend(defaults, options);
|
||||
popoverStates = {};
|
||||
footnoteInit = function() {
|
||||
var $curResetElement, $currentLastFootnoteLink, $footnoteAnchors, $footnoteButton, $lastResetElement, $parent, $relevantFNLink, $relevantFootnote, finalFNLinks, footnoteButton, footnoteButtonSearchQuery, footnoteContent, footnoteIDNum, footnoteLinks, footnoteNum, footnotes, i, _i, _ref, _results;
|
||||
footnoteButtonSearchQuery = settings.scope ? "" + settings.scope + " a[href*=\"#\"]" : "a[href*=\"#\"]";
|
||||
$footnoteAnchors = $(footnoteButtonSearchQuery).filter(function() {
|
||||
var $this, relAttr;
|
||||
$this = $(this);
|
||||
relAttr = $this.attr("rel");
|
||||
if (relAttr === "null" || (relAttr == null)) {
|
||||
relAttr = "";
|
||||
}
|
||||
return ("" + ($this.attr("href")) + relAttr).match(settings.anchorPattern) && $this.closest("[class*=" + settings.footnoteParentClass + "]:not(a):not(" + settings.anchorParentTagname + ")").length < 1;
|
||||
});
|
||||
footnotes = [];
|
||||
footnoteLinks = [];
|
||||
finalFNLinks = [];
|
||||
cleanFootnoteLinks($footnoteAnchors, footnoteLinks);
|
||||
$(footnoteLinks).each(function() {
|
||||
var $closestFootnoteEl, relatedFN;
|
||||
relatedFN = $(this).data("footnote-ref").replace(/[:.+~*\]\[]/g, "\\$&");
|
||||
if (settings.useFootnoteOnlyOnce) {
|
||||
relatedFN = "" + relatedFN + ":not(.footnote-processed)";
|
||||
}
|
||||
$closestFootnoteEl = $(relatedFN).closest(settings.footnoteTagname);
|
||||
if ($closestFootnoteEl.length > 0) {
|
||||
footnotes.push($closestFootnoteEl.first().addClass("footnote-processed"));
|
||||
return finalFNLinks.push(this);
|
||||
}
|
||||
});
|
||||
$currentLastFootnoteLink = $("[data-footnote-identifier]:last");
|
||||
footnoteIDNum = $currentLastFootnoteLink.length < 1 ? 0 : +$currentLastFootnoteLink.data("footnote-identifier");
|
||||
_results = [];
|
||||
for (i = _i = 0, _ref = footnotes.length; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) {
|
||||
footnoteContent = removeBackLinks($(footnotes[i]).html().trim(), $(finalFNLinks[i]).data("footnote-backlink-ref"));
|
||||
footnoteContent = footnoteContent.replace(/"/g, """).replace(/</g, "<sym;").replace(/>/g, ">sym;");
|
||||
footnoteIDNum += 1;
|
||||
footnoteButton = "";
|
||||
$relevantFNLink = $(finalFNLinks[i]);
|
||||
$relevantFootnote = $(footnotes[i]);
|
||||
if (settings.numberResetSelector != null) {
|
||||
$curResetElement = $relevantFNLink.closest(settings.numberResetSelector);
|
||||
if ($curResetElement.is($lastResetElement)) {
|
||||
footnoteNum += 1;
|
||||
} else {
|
||||
footnoteNum = 1;
|
||||
}
|
||||
$lastResetElement = $curResetElement;
|
||||
} else {
|
||||
footnoteNum = footnoteIDNum;
|
||||
}
|
||||
if (footnoteContent.indexOf("<") !== 0) {
|
||||
footnoteContent = "<p>" + footnoteContent + "</p>";
|
||||
}
|
||||
footnoteButton = settings.buttonMarkup.replace(/\{\{FOOTNOTENUM\}\}/g, footnoteNum).replace(/\{\{FOOTNOTEID\}\}/g, footnoteIDNum).replace(/\{\{FOOTNOTECONTENT\}\}/g, footnoteContent);
|
||||
footnoteButton = replaceWithReferenceAttributes(footnoteButton, "SUP", $relevantFNLink);
|
||||
footnoteButton = replaceWithReferenceAttributes(footnoteButton, "FN", $relevantFootnote);
|
||||
$footnoteButton = $(footnoteButton).insertBefore($relevantFNLink);
|
||||
$parent = $relevantFootnote.parent();
|
||||
switch (settings.actionOriginalFN.toLowerCase()) {
|
||||
case "hide":
|
||||
$relevantFNLink.addClass("footnote-print-only");
|
||||
$relevantFootnote.addClass("footnote-print-only");
|
||||
_results.push(deleteEmptyOrHR($parent));
|
||||
break;
|
||||
case "delete":
|
||||
$relevantFNLink.remove();
|
||||
$relevantFootnote.remove();
|
||||
_results.push(deleteEmptyOrHR($parent));
|
||||
break;
|
||||
default:
|
||||
_results.push($relevantFNLink.addClass("footnote-print-only"));
|
||||
}
|
||||
}
|
||||
return _results;
|
||||
};
|
||||
cleanFootnoteLinks = function($footnoteAnchors, footnoteLinks) {
|
||||
var $parent, $supChild, linkHREF, linkID;
|
||||
if (footnoteLinks == null) {
|
||||
footnoteLinks = [];
|
||||
}
|
||||
$parent = void 0;
|
||||
$supChild = void 0;
|
||||
linkHREF = void 0;
|
||||
linkID = void 0;
|
||||
$footnoteAnchors.each(function() {
|
||||
var $child, $this;
|
||||
$this = $(this);
|
||||
linkHREF = "#" + ($this.attr("href")).split("#")[1];
|
||||
$parent = $this.closest(settings.anchorParentTagname);
|
||||
$child = $this.find(settings.anchorParentTagname);
|
||||
if ($parent.length > 0) {
|
||||
linkID = ($parent.attr("id") || "") + ($this.attr("id") || "");
|
||||
return footnoteLinks.push($parent.attr({
|
||||
"data-footnote-backlink-ref": linkID,
|
||||
"data-footnote-ref": linkHREF
|
||||
}));
|
||||
} else if ($child.length > 0) {
|
||||
linkID = ($child.attr("id") || "") + ($this.attr("id") || "");
|
||||
return footnoteLinks.push($this.attr({
|
||||
"data-footnote-backlink-ref": linkID,
|
||||
"data-footnote-ref": linkHREF
|
||||
}));
|
||||
} else {
|
||||
linkID = $this.attr("id") || "";
|
||||
return footnoteLinks.push($this.attr({
|
||||
"data-footnote-backlink-ref": linkID,
|
||||
"data-footnote-ref": linkHREF
|
||||
}));
|
||||
}
|
||||
});
|
||||
};
|
||||
deleteEmptyOrHR = function($el) {
|
||||
var $parent;
|
||||
$parent = void 0;
|
||||
if ($el.is(":empty") || $el.children(":not(.footnote-print-only)").length === 0) {
|
||||
$parent = $el.parent();
|
||||
if (settings.actionOriginalFN.toLowerCase() === "delete") {
|
||||
$el.remove();
|
||||
} else {
|
||||
$el.addClass("footnote-print-only");
|
||||
}
|
||||
return deleteEmptyOrHR($parent);
|
||||
} else if ($el.children(":not(.footnote-print-only)").length === $el.children("hr:not(.footnote-print-only)").length) {
|
||||
$parent = $el.parent();
|
||||
if (settings.actionOriginalFN.toLowerCase() === "delete") {
|
||||
$el.remove();
|
||||
} else {
|
||||
$el.children("hr").addClass("footnote-print-only");
|
||||
$el.addClass("footnote-print-only");
|
||||
}
|
||||
return deleteEmptyOrHR($parent);
|
||||
}
|
||||
};
|
||||
removeBackLinks = function(footnoteHTML, backlinkID) {
|
||||
var regex;
|
||||
if (backlinkID.indexOf(' ') >= 0) {
|
||||
backlinkID = backlinkID.trim().replace(/\s+/g, "|").replace(/(.*)/g, "($1)");
|
||||
}
|
||||
regex = new RegExp("(\\s| )*<\\s*a[^#<]*#" + backlinkID + "[^>]*>(.*?)<\\s*/\\s*a>", "g");
|
||||
return footnoteHTML.replace(regex, "").replace("[]", "");
|
||||
};
|
||||
replaceWithReferenceAttributes = function(string, referenceKeyword, $referenceElement) {
|
||||
var refMatches, refRegex, refReplaceRegex, refReplaceText;
|
||||
refRegex = new RegExp("\\{\\{" + referenceKeyword + ":([^\\}]*)\\}\\}", "g");
|
||||
refMatches = void 0;
|
||||
refReplaceText = void 0;
|
||||
refReplaceRegex = void 0;
|
||||
refMatches = refRegex.exec(string);
|
||||
while (refMatches) {
|
||||
if (refMatches[1]) {
|
||||
refReplaceText = $referenceElement.attr(refMatches[1]) || "";
|
||||
string = string.replace("{{" + referenceKeyword + ":" + refMatches[1] + "}}", refReplaceText);
|
||||
}
|
||||
refMatches = refRegex.exec(string);
|
||||
}
|
||||
return string;
|
||||
};
|
||||
buttonHover = function(event) {
|
||||
var $buttonHovered, dataIdentifier, otherPopoverSelector;
|
||||
if (settings.activateOnHover) {
|
||||
$buttonHovered = $(event.target).closest(".bigfoot-footnote__button");
|
||||
dataIdentifier = "[data-footnote-identifier=\"" + ($buttonHovered.attr("data-footnote-identifier")) + "\"]";
|
||||
if ($buttonHovered.hasClass("is-active")) {
|
||||
return;
|
||||
}
|
||||
$buttonHovered.addClass("is-hover-instantiated");
|
||||
if (!settings.allowMultipleFN) {
|
||||
otherPopoverSelector = ".bigfoot-footnote:not(" + dataIdentifier + ")";
|
||||
removePopovers(otherPopoverSelector);
|
||||
}
|
||||
createPopover(".bigfoot-footnote__button" + dataIdentifier).addClass("is-hover-instantiated");
|
||||
}
|
||||
};
|
||||
touchClick = function(event) {
|
||||
var $nearButton, $nearFootnote, $target;
|
||||
$target = $(event.target);
|
||||
$nearButton = $target.closest(".bigfoot-footnote__button");
|
||||
$nearFootnote = $target.closest(".bigfoot-footnote");
|
||||
if ($nearButton.length > 0) {
|
||||
event.preventDefault();
|
||||
clickButton($nearButton);
|
||||
} else if ($nearFootnote.length < 1) {
|
||||
if ($(".bigfoot-footnote").length > 0) {
|
||||
removePopovers();
|
||||
}
|
||||
}
|
||||
};
|
||||
clickButton = function($button) {
|
||||
var dataIdentifier;
|
||||
$button.blur();
|
||||
dataIdentifier = "data-footnote-identifier=\"" + ($button.attr("data-footnote-identifier")) + "\"";
|
||||
if ($button.hasClass("changing")) {
|
||||
return;
|
||||
} else if (!$button.hasClass("is-active")) {
|
||||
$button.addClass("changing");
|
||||
setTimeout((function() {
|
||||
return $button.removeClass("changing");
|
||||
}), settings.popoverCreateDelay);
|
||||
createPopover(".bigfoot-footnote__button[" + dataIdentifier + "]");
|
||||
$button.addClass("is-click-instantiated");
|
||||
if (!settings.allowMultipleFN) {
|
||||
removePopovers(".bigfoot-footnote:not([" + dataIdentifier + "])");
|
||||
}
|
||||
} else {
|
||||
if (!settings.allowMultipleFN) {
|
||||
removePopovers();
|
||||
} else {
|
||||
removePopovers(".bigfoot-footnote[" + dataIdentifier + "]");
|
||||
}
|
||||
}
|
||||
};
|
||||
createPopover = function(selector) {
|
||||
var $buttons, $popoversCreated;
|
||||
$buttons = void 0;
|
||||
if (typeof selector !== "string" && settings.allowMultipleFN) {
|
||||
$buttons = selector;
|
||||
} else if (typeof selector !== "string") {
|
||||
$buttons = selector.first();
|
||||
} else if (settings.allowMultipleFN) {
|
||||
$buttons = $(selector).closest(".bigfoot-footnote__button");
|
||||
} else {
|
||||
$buttons = $(selector + ":first").closest(".bigfoot-footnote__button");
|
||||
}
|
||||
$popoversCreated = $();
|
||||
$buttons.each(function() {
|
||||
var $content, $contentContainer, $this, content;
|
||||
$this = $(this);
|
||||
content = void 0;
|
||||
try {
|
||||
content = settings.contentMarkup.replace(/\{\{FOOTNOTENUM\}\}/g, $this.attr("data-footnote-number")).replace(/\{\{FOOTNOTEID\}\}/g, $this.attr("data-footnote-identifier")).replace(/\{\{FOOTNOTECONTENT\}\}/g, $this.attr("data-bigfoot-footnote")).replace(/\>sym\;/g, ">").replace(/\<sym\;/g, "<");
|
||||
return content = replaceWithReferenceAttributes(content, "BUTTON", $this);
|
||||
} finally {
|
||||
$content = $(content);
|
||||
try {
|
||||
settings.activateCallback($content, $this);
|
||||
} catch (_error) {}
|
||||
$content.insertAfter($buttons);
|
||||
popoverStates[$this.attr("data-footnote-identifier")] = "init";
|
||||
$content.attr("bigfoot-max-width", calculatePixelDimension($content.css("max-width"), $content));
|
||||
$content.css("max-width", 10000);
|
||||
$contentContainer = $content.find(".bigfoot-footnote__content");
|
||||
$content.attr("data-bigfoot-max-height", calculatePixelDimension($contentContainer.css("max-height"), $contentContainer));
|
||||
repositionFeet();
|
||||
$this.addClass("is-active");
|
||||
$content.find(".bigfoot-footnote__content").bindScrollHandler();
|
||||
$popoversCreated = $popoversCreated.add($content);
|
||||
}
|
||||
});
|
||||
setTimeout((function() {
|
||||
return $popoversCreated.addClass("is-active");
|
||||
}), settings.popoverCreateDelay);
|
||||
return $popoversCreated;
|
||||
};
|
||||
baseFontSize = function() {
|
||||
var el, size;
|
||||
el = document.createElement("div");
|
||||
el.style.cssText = "display:inline-block;padding:0;line-height:1;position:absolute;visibility:hidden;font-size:1em;";
|
||||
el.appendChild(document.createElement("M"));
|
||||
document.body.appendChild(el);
|
||||
size = el.offsetHeight;
|
||||
document.body.removeChild(el);
|
||||
return size;
|
||||
};
|
||||
calculatePixelDimension = function(dim, $el) {
|
||||
if (dim === "none") {
|
||||
dim = 10000;
|
||||
} else if (dim.indexOf("rem") >= 0) {
|
||||
dim = parseFloat(dim) * baseFontSize();
|
||||
} else if (dim.indexOf("em") >= 0) {
|
||||
dim = parseFloat(dim) * parseFloat($el.css("font-size"));
|
||||
} else if (dim.indexOf("px") >= 0) {
|
||||
dim = parseFloat(dim);
|
||||
if (dim <= 60) {
|
||||
dim = dim / parseFloat($el.parent().css("width"));
|
||||
}
|
||||
} else if (dim.indexOf("%") >= 0) {
|
||||
dim = parseFloat(dim) / 100;
|
||||
}
|
||||
return dim;
|
||||
};
|
||||
$.fn.bindScrollHandler = function() {
|
||||
if (!settings.preventPageScroll) {
|
||||
return $(this);
|
||||
}
|
||||
$(this).on("DOMMouseScroll mousewheel", function(event) {
|
||||
var $popover, $this, delta, height, prevent, scrollHeight, scrollTop, up;
|
||||
$this = $(this);
|
||||
scrollTop = $this.scrollTop();
|
||||
scrollHeight = $this[0].scrollHeight;
|
||||
height = parseInt($this.css("height"));
|
||||
$popover = $this.closest(".bigfoot-footnote");
|
||||
if ($this.scrollTop() > 0 && $this.scrollTop() < 10) {
|
||||
$popover.addClass("is-scrollable");
|
||||
}
|
||||
if (!$popover.hasClass("is-scrollable")) {
|
||||
return;
|
||||
}
|
||||
delta = event.type === "DOMMouseScroll" ? event.originalEvent.detail * -40 : event.originalEvent.wheelDelta;
|
||||
up = delta > 0;
|
||||
prevent = function() {
|
||||
event.stopPropagation();
|
||||
event.preventDefault();
|
||||
event.returnValue = false;
|
||||
return false;
|
||||
};
|
||||
if (!up && -delta > scrollHeight - height - scrollTop) {
|
||||
$this.scrollTop(scrollHeight);
|
||||
$popover.addClass("is-fully-scrolled");
|
||||
return prevent();
|
||||
} else if (up && delta > scrollTop) {
|
||||
$this.scrollTop(0);
|
||||
$popover.removeClass("is-fully-scrolled");
|
||||
return prevent();
|
||||
} else {
|
||||
return $popover.removeClass("is-fully-scrolled");
|
||||
}
|
||||
});
|
||||
return $(this);
|
||||
};
|
||||
unhoverFeet = function(e) {
|
||||
if (settings.deleteOnUnhover && settings.activateOnHover) {
|
||||
return setTimeout((function() {
|
||||
var $target;
|
||||
$target = $(e.target).closest(".bigfoot-footnote, .bigfoot-footnote__button");
|
||||
if ($(".bigfoot-footnote__button:hover, .bigfoot-footnote:hover").length < 1) {
|
||||
return removePopovers();
|
||||
}
|
||||
}), settings.hoverDelay);
|
||||
}
|
||||
};
|
||||
escapeKeypress = function(event) {
|
||||
if (event.keyCode === 27) {
|
||||
return removePopovers();
|
||||
}
|
||||
};
|
||||
removePopovers = function(footnotes, timeout) {
|
||||
var $buttonsClosed, $linkedButton, $this, footnoteID;
|
||||
if (footnotes == null) {
|
||||
footnotes = ".bigfoot-footnote";
|
||||
}
|
||||
if (timeout == null) {
|
||||
timeout = settings.popoverDeleteDelay;
|
||||
}
|
||||
$buttonsClosed = $();
|
||||
footnoteID = void 0;
|
||||
$linkedButton = void 0;
|
||||
$this = void 0;
|
||||
$(footnotes).each(function() {
|
||||
$this = $(this);
|
||||
footnoteID = $this.attr("data-footnote-identifier");
|
||||
$linkedButton = $(".bigfoot-footnote__button[data-footnote-identifier=\"" + footnoteID + "\"]");
|
||||
if (!$linkedButton.hasClass("changing")) {
|
||||
$buttonsClosed = $buttonsClosed.add($linkedButton);
|
||||
$linkedButton.removeClass("is-active is-hover-instantiated is-click-instantiated").addClass("changing");
|
||||
$this.removeClass("is-active").addClass("disapearing");
|
||||
return setTimeout((function() {
|
||||
$this.remove();
|
||||
delete popoverStates[footnoteID];
|
||||
return $linkedButton.removeClass("changing");
|
||||
}), timeout);
|
||||
}
|
||||
});
|
||||
return $buttonsClosed;
|
||||
};
|
||||
repositionFeet = function(e) {
|
||||
var type;
|
||||
if (settings.positionContent) {
|
||||
type = e ? e.type : "resize";
|
||||
$(".bigfoot-footnote").each(function() {
|
||||
var $button, $contentWrapper, $mainWrap, $this, dataIdentifier, identifier, lastState, marginSize, maxHeightInCSS, maxHeightOnScreen, maxWidth, maxWidthInCSS, positionOnTop, relativeToWidth, roomLeft, totalHeight;
|
||||
$this = $(this);
|
||||
identifier = $this.attr("data-footnote-identifier");
|
||||
dataIdentifier = "data-footnote-identifier=\"" + identifier + "\"";
|
||||
$contentWrapper = $this.find(".bigfoot-footnote__content");
|
||||
$button = $this.siblings(".bigfoot-footnote__button");
|
||||
roomLeft = roomCalc($button);
|
||||
marginSize = parseFloat($this.css("margin-top"));
|
||||
maxHeightInCSS = +($this.attr("data-bigfoot-max-height"));
|
||||
totalHeight = 2 * marginSize + $this.outerHeight();
|
||||
maxHeightOnScreen = 10000;
|
||||
positionOnTop = roomLeft.bottomRoom < totalHeight && roomLeft.topRoom > roomLeft.bottomRoom;
|
||||
lastState = popoverStates[identifier];
|
||||
if (positionOnTop) {
|
||||
if (lastState !== "top") {
|
||||
popoverStates[identifier] = "top";
|
||||
$this.addClass("is-positioned-top").removeClass("is-positioned-bottom");
|
||||
$this.css("transform-origin", (roomLeft.leftRelative * 100) + "% 100%");
|
||||
}
|
||||
maxHeightOnScreen = roomLeft.topRoom - marginSize - 15;
|
||||
} else {
|
||||
if (lastState !== "bottom" || lastState === "init") {
|
||||
popoverStates[identifier] = "bottom";
|
||||
$this.removeClass("is-positioned-top").addClass("is-positioned-bottom");
|
||||
$this.css("transform-origin", (roomLeft.leftRelative * 100) + "% 0%");
|
||||
}
|
||||
maxHeightOnScreen = roomLeft.bottomRoom - marginSize - 15;
|
||||
}
|
||||
$this.find(".bigfoot-footnote__content").css({
|
||||
"max-height": Math.min(maxHeightOnScreen, maxHeightInCSS) + "px"
|
||||
});
|
||||
if (type === "resize") {
|
||||
maxWidthInCSS = parseFloat($this.attr("bigfoot-max-width"));
|
||||
$mainWrap = $this.find(".bigfoot-footnote__wrapper");
|
||||
maxWidth = maxWidthInCSS;
|
||||
if (maxWidthInCSS <= 1) {
|
||||
relativeToWidth = (function() {
|
||||
var jq, userSpecifiedRelativeElWidth;
|
||||
userSpecifiedRelativeElWidth = 10000;
|
||||
if (settings.maxWidthRelativeTo) {
|
||||
jq = $(settings.maxWidthRelativeTo);
|
||||
if (jq.length > 0) {
|
||||
userSpecifiedRelativeElWidth = jq.outerWidth();
|
||||
}
|
||||
}
|
||||
return Math.min(window.innerWidth, userSpecifiedRelativeElWidth);
|
||||
})();
|
||||
maxWidth = relativeToWidth * maxWidthInCSS;
|
||||
}
|
||||
maxWidth = Math.min(maxWidth, $this.find(".bigfoot-footnote__content").outerWidth() + 1);
|
||||
$mainWrap.css("max-width", maxWidth + "px");
|
||||
$this.css({
|
||||
left: (-roomLeft.leftRelative * maxWidth + parseFloat($button.css("margin-left")) + $button.outerWidth() / 2) + "px"
|
||||
});
|
||||
positionTooltip($this, roomLeft.leftRelative);
|
||||
}
|
||||
if (parseInt($this.outerHeight()) < $this.find(".bigfoot-footnote__content")[0].scrollHeight) {
|
||||
return $this.addClass("is-scrollable");
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
positionTooltip = function($popover, leftRelative) {
|
||||
var $tooltip;
|
||||
if (leftRelative == null) {
|
||||
leftRelative = 0.5;
|
||||
}
|
||||
$tooltip = $popover.find(".bigfoot-footnote__tooltip");
|
||||
if ($tooltip.length > 0) {
|
||||
$tooltip.css("left", "" + (leftRelative * 100) + "%");
|
||||
}
|
||||
};
|
||||
roomCalc = function($el) {
|
||||
var elHeight, elLeftMargin, elWidth, leftRoom, topRoom, w;
|
||||
elLeftMargin = parseFloat($el.css("margin-left"));
|
||||
elWidth = parseFloat($el.outerWidth()) - elLeftMargin;
|
||||
elHeight = parseFloat($el.outerHeight());
|
||||
w = viewportDetails();
|
||||
topRoom = $el.offset().top - w.scrollY + elHeight / 2;
|
||||
leftRoom = $el.offset().left - w.scrollX + elWidth / 2;
|
||||
return {
|
||||
topRoom: topRoom,
|
||||
bottomRoom: w.height - topRoom,
|
||||
leftRoom: leftRoom,
|
||||
rightRoom: w.width - leftRoom,
|
||||
leftRelative: leftRoom / w.width,
|
||||
topRelative: topRoom / w.height
|
||||
};
|
||||
};
|
||||
viewportDetails = function() {
|
||||
var $window;
|
||||
$window = $(window);
|
||||
return {
|
||||
width: window.innerWidth,
|
||||
height: window.innerHeight,
|
||||
scrollX: $window.scrollLeft(),
|
||||
scrollY: $window.scrollTop()
|
||||
};
|
||||
};
|
||||
addBreakpoint = function(size, trueCallback, falseCallback, deleteDelay, removeOpen) {
|
||||
var falseDefaultPositionSetting, minMax, mqListener, mql, query, s, trueDefaultPositionSetting;
|
||||
if (deleteDelay == null) {
|
||||
deleteDelay = settings.popoverDeleteDelay;
|
||||
}
|
||||
if (removeOpen == null) {
|
||||
removeOpen = true;
|
||||
}
|
||||
mql = void 0;
|
||||
minMax = void 0;
|
||||
s = void 0;
|
||||
if (typeof size === "string") {
|
||||
s = size.toLowerCase() === "iphone" ? "<320px" : size.toLowerCase() === "ipad" ? "<768px" : size;
|
||||
minMax = s.charAt(0) === ">" ? "min" : s.charAt(0) === "<" ? "max" : null;
|
||||
query = minMax ? "(" + minMax + "-width: " + (s.substring(1)) + ")" : s;
|
||||
mql = window.matchMedia(query);
|
||||
} else {
|
||||
mql = size;
|
||||
}
|
||||
if (mql.media && mql.media === "invalid") {
|
||||
return {
|
||||
added: false,
|
||||
mq: mql,
|
||||
listener: null
|
||||
};
|
||||
}
|
||||
trueDefaultPositionSetting = minMax === "min";
|
||||
falseDefaultPositionSetting = minMax === "max";
|
||||
trueCallback = trueCallback || makeDefaultCallbacks(removeOpen, deleteDelay, trueDefaultPositionSetting, function($popover) {
|
||||
return $popover.addClass("is-bottom-fixed");
|
||||
});
|
||||
falseCallback = falseCallback || makeDefaultCallbacks(removeOpen, deleteDelay, falseDefaultPositionSetting, function() {});
|
||||
mqListener = function(mq) {
|
||||
if (mq.matches) {
|
||||
trueCallback(removeOpen, bigfoot);
|
||||
} else {
|
||||
falseCallback(removeOpen, bigfoot);
|
||||
}
|
||||
};
|
||||
mql.addListener(mqListener);
|
||||
mqListener(mql);
|
||||
settings.breakpoints[size] = {
|
||||
added: true,
|
||||
mq: mql,
|
||||
listener: mqListener
|
||||
};
|
||||
return settings.breakpoints[size];
|
||||
};
|
||||
makeDefaultCallbacks = function(removeOpen, deleteDelay, position, callback) {
|
||||
return function(removeOpen, bigfoot) {
|
||||
var $closedPopovers;
|
||||
$closedPopovers = void 0;
|
||||
if (removeOpen) {
|
||||
$closedPopovers = bigfoot.close();
|
||||
bigfoot.updateSetting("activateCallback", callback);
|
||||
}
|
||||
return setTimeout((function() {
|
||||
bigfoot.updateSetting("positionContent", position);
|
||||
if (removeOpen) {
|
||||
return bigfoot.activate($closedPopovers);
|
||||
}
|
||||
}), deleteDelay);
|
||||
};
|
||||
};
|
||||
removeBreakpoint = function(target, callback) {
|
||||
var b, breakpoint, mq, mqFound;
|
||||
mq = null;
|
||||
b = void 0;
|
||||
mqFound = false;
|
||||
if (typeof target === "string") {
|
||||
mqFound = settings.breakpoints[target] !== undefined;
|
||||
} else {
|
||||
for (b in settings.breakpoints) {
|
||||
if (settings.breakpoints.hasOwnProperty(b) && settings.breakpoints[b].mq === target) {
|
||||
mqFound = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (mqFound) {
|
||||
breakpoint = settings.breakpoints[b || target];
|
||||
if (callback) {
|
||||
callback({
|
||||
matches: false
|
||||
});
|
||||
} else {
|
||||
breakpoint.listener({
|
||||
matches: false
|
||||
});
|
||||
}
|
||||
breakpoint.mq.removeListener(breakpoint.listener);
|
||||
delete settings.breakpoints[b || target];
|
||||
}
|
||||
return mqFound;
|
||||
};
|
||||
updateSetting = function(newSettings, value) {
|
||||
var oldValue, prop;
|
||||
oldValue = void 0;
|
||||
if (typeof newSettings === "string") {
|
||||
oldValue = settings[newSettings];
|
||||
settings[newSettings] = value;
|
||||
} else {
|
||||
oldValue = {};
|
||||
for (prop in newSettings) {
|
||||
if (newSettings.hasOwnProperty(prop)) {
|
||||
oldValue[prop] = settings[prop];
|
||||
settings[prop] = newSettings[prop];
|
||||
}
|
||||
}
|
||||
}
|
||||
return oldValue;
|
||||
};
|
||||
getSetting = function(setting) {
|
||||
return settings[setting];
|
||||
};
|
||||
$(document).ready(function() {
|
||||
footnoteInit();
|
||||
$(document).on("mouseenter", ".bigfoot-footnote__button", buttonHover);
|
||||
$(document).on("touchend click", touchClick);
|
||||
$(document).on("mouseout", ".is-hover-instantiated", unhoverFeet);
|
||||
$(document).on("keyup", escapeKeypress);
|
||||
$(window).on("scroll resize", repositionFeet);
|
||||
return $(document).on("gestureend", function() {
|
||||
return repositionFeet();
|
||||
});
|
||||
});
|
||||
bigfoot = {
|
||||
removePopovers: removePopovers,
|
||||
close: removePopovers,
|
||||
createPopover: createPopover,
|
||||
activate: createPopover,
|
||||
repositionFeet: repositionFeet,
|
||||
reposition: repositionFeet,
|
||||
addBreakpoint: addBreakpoint,
|
||||
removeBreakpoint: removeBreakpoint,
|
||||
getSetting: getSetting,
|
||||
updateSetting: updateSetting
|
||||
};
|
||||
return bigfoot;
|
||||
};
|
||||
})(jQuery);
|
||||
|
||||
}).call(this);
|
1
public/javascripts/bigfoot.min.js
vendored
Executable file
1
public/javascripts/bigfoot.min.js
vendored
Executable file
File diff suppressed because one or more lines are too long
5
public/javascripts/d3-3.5.2/d3.min.js
vendored
Executable file
5
public/javascripts/d3-3.5.2/d3.min.js
vendored
Executable file
File diff suppressed because one or more lines are too long
625
public/javascripts/htmlwidgets-0.5/htmlwidgets.js
Executable file
625
public/javascripts/htmlwidgets-0.5/htmlwidgets.js
Executable file
|
@ -0,0 +1,625 @@
|
|||
(function() {
|
||||
// If window.HTMLWidgets is already defined, then use it; otherwise create a
|
||||
// new object. This allows preceding code to set options that affect the
|
||||
// initialization process (though none currently exist).
|
||||
window.HTMLWidgets = window.HTMLWidgets || {};
|
||||
|
||||
// See if we're running in a viewer pane. If not, we're in a web browser.
|
||||
var viewerMode = window.HTMLWidgets.viewerMode =
|
||||
/\bviewer_pane=1\b/.test(window.location);
|
||||
|
||||
// See if we're running in Shiny mode. If not, it's a static document.
|
||||
// Note that static widgets can appear in both Shiny and static modes, but
|
||||
// obviously, Shiny widgets can only appear in Shiny apps/documents.
|
||||
var shinyMode = window.HTMLWidgets.shinyMode =
|
||||
typeof(window.Shiny) !== "undefined" && !!window.Shiny.outputBindings;
|
||||
|
||||
// We can't count on jQuery being available, so we implement our own
|
||||
// version if necessary.
|
||||
function querySelectorAll(scope, selector) {
|
||||
if (typeof(jQuery) !== "undefined" && scope instanceof jQuery) {
|
||||
return scope.find(selector);
|
||||
}
|
||||
if (scope.querySelectorAll) {
|
||||
return scope.querySelectorAll(selector);
|
||||
}
|
||||
}
|
||||
|
||||
function asArray(value) {
|
||||
if (value === null)
|
||||
return [];
|
||||
if ($.isArray(value))
|
||||
return value;
|
||||
return [value];
|
||||
}
|
||||
|
||||
// Implement jQuery's extend
|
||||
function extend(target /*, ... */) {
|
||||
if (arguments.length == 1) {
|
||||
return target;
|
||||
}
|
||||
for (var i = 1; i < arguments.length; i++) {
|
||||
var source = arguments[i];
|
||||
for (var prop in source) {
|
||||
if (source.hasOwnProperty(prop)) {
|
||||
target[prop] = source[prop];
|
||||
}
|
||||
}
|
||||
}
|
||||
return target;
|
||||
}
|
||||
|
||||
// IE8 doesn't support Array.forEach.
|
||||
function forEach(values, callback, thisArg) {
|
||||
if (values.forEach) {
|
||||
values.forEach(callback, thisArg);
|
||||
} else {
|
||||
for (var i = 0; i < values.length; i++) {
|
||||
callback.call(thisArg, values[i], i, values);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Replaces the specified method with the return value of funcSource.
|
||||
//
|
||||
// Note that funcSource should not BE the new method, it should be a function
|
||||
// that RETURNS the new method. funcSource receives a single argument that is
|
||||
// the overridden method, it can be called from the new method. The overridden
|
||||
// method can be called like a regular function, it has the target permanently
|
||||
// bound to it so "this" will work correctly.
|
||||
function overrideMethod(target, methodName, funcSource) {
|
||||
var superFunc = target[methodName] || function() {};
|
||||
var superFuncBound = function() {
|
||||
return superFunc.apply(target, arguments);
|
||||
};
|
||||
target[methodName] = funcSource(superFuncBound);
|
||||
}
|
||||
|
||||
// Implement a vague facsimilie of jQuery's data method
|
||||
function elementData(el, name, value) {
|
||||
if (arguments.length == 2) {
|
||||
return el["htmlwidget_data_" + name];
|
||||
} else if (arguments.length == 3) {
|
||||
el["htmlwidget_data_" + name] = value;
|
||||
return el;
|
||||
} else {
|
||||
throw new Error("Wrong number of arguments for elementData: " +
|
||||
arguments.length);
|
||||
}
|
||||
}
|
||||
|
||||
// http://stackoverflow.com/questions/3446170/escape-string-for-use-in-javascript-regex
|
||||
function escapeRegExp(str) {
|
||||
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
|
||||
}
|
||||
|
||||
function hasClass(el, className) {
|
||||
var re = new RegExp("\\b" + escapeRegExp(className) + "\\b");
|
||||
return re.test(el.className);
|
||||
}
|
||||
|
||||
// elements - array (or array-like object) of HTML elements
|
||||
// className - class name to test for
|
||||
// include - if true, only return elements with given className;
|
||||
// if false, only return elements *without* given className
|
||||
function filterByClass(elements, className, include) {
|
||||
var results = [];
|
||||
for (var i = 0; i < elements.length; i++) {
|
||||
if (hasClass(elements[i], className) == include)
|
||||
results.push(elements[i]);
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
function on(obj, eventName, func) {
|
||||
if (obj.addEventListener) {
|
||||
obj.addEventListener(eventName, func, false);
|
||||
} else if (obj.attachEvent) {
|
||||
obj.attachEvent(eventName, func);
|
||||
}
|
||||
}
|
||||
|
||||
function off(obj, eventName, func) {
|
||||
if (obj.removeEventListener)
|
||||
obj.removeEventListener(eventName, func, false);
|
||||
else if (obj.detachEvent) {
|
||||
obj.detachEvent(eventName, func);
|
||||
}
|
||||
}
|
||||
|
||||
// Translate array of values to top/right/bottom/left, as usual with
|
||||
// the "padding" CSS property
|
||||
// https://developer.mozilla.org/en-US/docs/Web/CSS/padding
|
||||
function unpackPadding(value) {
|
||||
if (typeof(value) === "number")
|
||||
value = [value];
|
||||
if (value.length === 1) {
|
||||
return {top: value[0], right: value[0], bottom: value[0], left: value[0]};
|
||||
}
|
||||
if (value.length === 2) {
|
||||
return {top: value[0], right: value[1], bottom: value[0], left: value[1]};
|
||||
}
|
||||
if (value.length === 3) {
|
||||
return {top: value[0], right: value[1], bottom: value[2], left: value[1]};
|
||||
}
|
||||
if (value.length === 4) {
|
||||
return {top: value[0], right: value[1], bottom: value[2], left: value[3]};
|
||||
}
|
||||
}
|
||||
|
||||
// Convert an unpacked padding object to a CSS value
|
||||
function paddingToCss(paddingObj) {
|
||||
return paddingObj.top + "px " + paddingObj.right + "px " + paddingObj.bottom + "px " + paddingObj.left + "px";
|
||||
}
|
||||
|
||||
// Makes a number suitable for CSS
|
||||
function px(x) {
|
||||
if (typeof(x) === "number")
|
||||
return x + "px";
|
||||
else
|
||||
return x;
|
||||
}
|
||||
|
||||
// Retrieves runtime widget sizing information for an element.
|
||||
// The return value is either null, or an object with fill, padding,
|
||||
// defaultWidth, defaultHeight fields.
|
||||
function sizingPolicy(el) {
|
||||
var sizingEl = document.querySelector("script[data-for='" + el.id + "'][type='application/htmlwidget-sizing']");
|
||||
if (!sizingEl)
|
||||
return null;
|
||||
var sp = JSON.parse(sizingEl.textContent || sizingEl.text || "{}");
|
||||
if (viewerMode) {
|
||||
return sp.viewer;
|
||||
} else {
|
||||
return sp.browser;
|
||||
}
|
||||
}
|
||||
|
||||
function initSizing(el) {
|
||||
var sizing = sizingPolicy(el);
|
||||
if (!sizing)
|
||||
return;
|
||||
|
||||
var cel = document.getElementById("htmlwidget_container");
|
||||
if (!cel)
|
||||
return;
|
||||
|
||||
if (typeof(sizing.padding) !== "undefined") {
|
||||
document.body.style.margin = "0";
|
||||
document.body.style.padding = paddingToCss(unpackPadding(sizing.padding));
|
||||
}
|
||||
|
||||
if (sizing.fill) {
|
||||
document.body.style.overflow = "hidden";
|
||||
document.body.style.width = "100%";
|
||||
document.body.style.height = "100%";
|
||||
document.documentElement.style.width = "100%";
|
||||
document.documentElement.style.height = "100%";
|
||||
if (cel) {
|
||||
cel.style.position = "absolute";
|
||||
var pad = unpackPadding(sizing.padding);
|
||||
cel.style.top = pad.top + "px";
|
||||
cel.style.right = pad.right + "px";
|
||||
cel.style.bottom = pad.bottom + "px";
|
||||
cel.style.left = pad.left + "px";
|
||||
el.style.width = "100%";
|
||||
el.style.height = "100%";
|
||||
}
|
||||
|
||||
return {
|
||||
getWidth: function() { return cel.offsetWidth; },
|
||||
getHeight: function() { return cel.offsetHeight; }
|
||||
};
|
||||
|
||||
} else {
|
||||
el.style.width = px(sizing.width);
|
||||
el.style.height = px(sizing.height);
|
||||
|
||||
return {
|
||||
getWidth: function() { return el.offsetWidth; },
|
||||
getHeight: function() { return el.offsetHeight; }
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// Default implementations for methods
|
||||
var defaults = {
|
||||
find: function(scope) {
|
||||
return querySelectorAll(scope, "." + this.name);
|
||||
},
|
||||
renderError: function(el, err) {
|
||||
var $el = $(el);
|
||||
|
||||
this.clearError(el);
|
||||
|
||||
// Add all these error classes, as Shiny does
|
||||
var errClass = "shiny-output-error";
|
||||
if (err.type !== null) {
|
||||
// use the classes of the error condition as CSS class names
|
||||
errClass = errClass + " " + $.map(asArray(err.type), function(type) {
|
||||
return errClass + "-" + type;
|
||||
}).join(" ");
|
||||
}
|
||||
errClass = errClass + " htmlwidgets-error";
|
||||
|
||||
// Is el inline or block? If inline or inline-block, just display:none it
|
||||
// and add an inline error.
|
||||
var display = $el.css("display");
|
||||
$el.data("restore-display-mode", display);
|
||||
|
||||
if (display === "inline" || display === "inline-block") {
|
||||
$el.hide();
|
||||
if (err.message !== "") {
|
||||
var errorSpan = $("<span>").addClass(errClass);
|
||||
errorSpan.text(err.message);
|
||||
$el.after(errorSpan);
|
||||
}
|
||||
} else if (display === "block") {
|
||||
// If block, add an error just after the el, set visibility:none on the
|
||||
// el, and position the error to be on top of the el.
|
||||
// Mark it with a unique ID and CSS class so we can remove it later.
|
||||
$el.css("visibility", "hidden");
|
||||
if (err.message !== "") {
|
||||
var errorDiv = $("<div>").addClass(errClass).css("position", "absolute")
|
||||
.css("top", el.offsetTop)
|
||||
.css("left", el.offsetLeft)
|
||||
// setting width can push out the page size, forcing otherwise
|
||||
// unnecessary scrollbars to appear and making it impossible for
|
||||
// the element to shrink; so use max-width instead
|
||||
.css("maxWidth", el.offsetWidth)
|
||||
.css("height", el.offsetHeight);
|
||||
errorDiv.text(err.message);
|
||||
$el.after(errorDiv);
|
||||
|
||||
// Really dumb way to keep the size/position of the error in sync with
|
||||
// the parent element as the window is resized or whatever.
|
||||
var intId = setInterval(function() {
|
||||
if (!errorDiv[0].parentElement) {
|
||||
clearInterval(intId);
|
||||
return;
|
||||
}
|
||||
errorDiv
|
||||
.css("top", el.offsetTop)
|
||||
.css("left", el.offsetLeft)
|
||||
.css("maxWidth", el.offsetWidth)
|
||||
.css("height", el.offsetHeight);
|
||||
}, 500);
|
||||
}
|
||||
}
|
||||
},
|
||||
clearError: function(el) {
|
||||
var $el = $(el);
|
||||
var display = $el.data("restore-display-mode");
|
||||
$el.data("restore-display-mode", null);
|
||||
|
||||
if (display === "inline" || display === "inline-block") {
|
||||
if (display)
|
||||
$el.css("display", display);
|
||||
$(el.nextSibling).filter(".htmlwidgets-error").remove();
|
||||
} else if (display === "block"){
|
||||
$el.css("visibility", "inherit");
|
||||
$(el.nextSibling).filter(".htmlwidgets-error").remove();
|
||||
}
|
||||
},
|
||||
sizing: {}
|
||||
};
|
||||
|
||||
// Called by widget bindings to register a new type of widget. The definition
|
||||
// object can contain the following properties:
|
||||
// - name (required) - A string indicating the binding name, which will be
|
||||
// used by default as the CSS classname to look for.
|
||||
// - initialize (optional) - A function(el) that will be called once per
|
||||
// widget element; if a value is returned, it will be passed as the third
|
||||
// value to renderValue.
|
||||
// - renderValue (required) - A function(el, data, initValue) that will be
|
||||
// called with data. Static contexts will cause this to be called once per
|
||||
// element; Shiny apps will cause this to be called multiple times per
|
||||
// element, as the data changes.
|
||||
window.HTMLWidgets.widget = function(definition) {
|
||||
if (!definition.name) {
|
||||
throw new Error("Widget must have a name");
|
||||
}
|
||||
if (!definition.type) {
|
||||
throw new Error("Widget must have a type");
|
||||
}
|
||||
// Currently we only support output widgets
|
||||
if (definition.type !== "output") {
|
||||
throw new Error("Unrecognized widget type '" + definition.type + "'");
|
||||
}
|
||||
// TODO: Verify that .name is a valid CSS classname
|
||||
if (!definition.renderValue) {
|
||||
throw new Error("Widget must have a renderValue function");
|
||||
}
|
||||
|
||||
// For static rendering (non-Shiny), use a simple widget registration
|
||||
// scheme. We also use this scheme for Shiny apps/documents that also
|
||||
// contain static widgets.
|
||||
window.HTMLWidgets.widgets = window.HTMLWidgets.widgets || [];
|
||||
// Merge defaults into the definition; don't mutate the original definition.
|
||||
var staticBinding = extend({}, defaults, definition);
|
||||
overrideMethod(staticBinding, "find", function(superfunc) {
|
||||
return function(scope) {
|
||||
var results = superfunc(scope);
|
||||
// Filter out Shiny outputs, we only want the static kind
|
||||
return filterByClass(results, "html-widget-output", false);
|
||||
};
|
||||
});
|
||||
window.HTMLWidgets.widgets.push(staticBinding);
|
||||
|
||||
if (shinyMode) {
|
||||
// Shiny is running. Register the definition as an output binding.
|
||||
|
||||
// Merge defaults into the definition; don't mutate the original definition.
|
||||
// The base object is a Shiny output binding if we're running in Shiny mode,
|
||||
// or an empty object if we're not.
|
||||
var shinyBinding = extend(new Shiny.OutputBinding(), defaults, definition);
|
||||
|
||||
// Wrap renderValue to handle initialization, which unfortunately isn't
|
||||
// supported natively by Shiny at the time of this writing.
|
||||
|
||||
// NB: shinyBinding.initialize may be undefined, as it's optional.
|
||||
|
||||
// Rename initialize to make sure it isn't called by a future version
|
||||
// of Shiny that does support initialize directly.
|
||||
shinyBinding._htmlwidgets_initialize = shinyBinding.initialize;
|
||||
delete shinyBinding.initialize;
|
||||
|
||||
overrideMethod(shinyBinding, "find", function(superfunc) {
|
||||
return function(scope) {
|
||||
|
||||
var results = superfunc(scope);
|
||||
|
||||
// Only return elements that are Shiny outputs, not static ones
|
||||
var dynamicResults = results.filter(".html-widget-output");
|
||||
|
||||
// It's possible that whatever caused Shiny to think there might be
|
||||
// new dynamic outputs, also caused there to be new static outputs.
|
||||
// Since there might be lots of different htmlwidgets bindings, we
|
||||
// schedule execution for later--no need to staticRender multiple
|
||||
// times.
|
||||
if (results.length !== dynamicResults.length)
|
||||
scheduleStaticRender();
|
||||
|
||||
return dynamicResults;
|
||||
};
|
||||
});
|
||||
|
||||
overrideMethod(shinyBinding, "renderValue", function(superfunc) {
|
||||
return function(el, data) {
|
||||
// Resolve strings marked as javascript literals to objects
|
||||
if (!(data.evals instanceof Array)) data.evals = [data.evals];
|
||||
for (var i = 0; data.evals && i < data.evals.length; i++) {
|
||||
window.HTMLWidgets.evaluateStringMember(data.x, data.evals[i]);
|
||||
}
|
||||
if (!this.renderOnNullValue) {
|
||||
if (data.x === null) {
|
||||
el.style.visibility = "hidden";
|
||||
return;
|
||||
} else {
|
||||
el.style.visibility = "inherit";
|
||||
}
|
||||
}
|
||||
if (!elementData(el, "initialized")) {
|
||||
initSizing(el);
|
||||
|
||||
elementData(el, "initialized", true);
|
||||
if (this._htmlwidgets_initialize) {
|
||||
var result = this._htmlwidgets_initialize(el, el.offsetWidth,
|
||||
el.offsetHeight);
|
||||
elementData(el, "init_result", result);
|
||||
}
|
||||
}
|
||||
Shiny.renderDependencies(data.deps);
|
||||
superfunc(el, data.x, elementData(el, "init_result"));
|
||||
};
|
||||
});
|
||||
|
||||
overrideMethod(shinyBinding, "resize", function(superfunc) {
|
||||
return function(el, width, height) {
|
||||
// Shiny can call resize before initialize/renderValue have been
|
||||
// called, which doesn't make sense for widgets.
|
||||
if (elementData(el, "initialized")) {
|
||||
superfunc(el, width, height, elementData(el, "init_result"));
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
Shiny.outputBindings.register(shinyBinding, shinyBinding.name);
|
||||
}
|
||||
};
|
||||
|
||||
var scheduleStaticRenderTimerId = null;
|
||||
function scheduleStaticRender() {
|
||||
if (!scheduleStaticRenderTimerId) {
|
||||
scheduleStaticRenderTimerId = setTimeout(function() {
|
||||
scheduleStaticRenderTimerId = null;
|
||||
window.HTMLWidgets.staticRender();
|
||||
}, 1);
|
||||
}
|
||||
}
|
||||
|
||||
// Render static widgets after the document finishes loading
|
||||
// Statically render all elements that are of this widget's class
|
||||
window.HTMLWidgets.staticRender = function() {
|
||||
var bindings = window.HTMLWidgets.widgets || [];
|
||||
forEach(bindings, function(binding) {
|
||||
var matches = binding.find(document.documentElement);
|
||||
forEach(matches, function(el) {
|
||||
var sizeObj = initSizing(el, binding);
|
||||
|
||||
if (hasClass(el, "html-widget-static-bound"))
|
||||
return;
|
||||
el.className = el.className + " html-widget-static-bound";
|
||||
|
||||
var initResult;
|
||||
if (binding.initialize) {
|
||||
initResult = binding.initialize(el,
|
||||
sizeObj ? sizeObj.getWidth() : el.offsetWidth,
|
||||
sizeObj ? sizeObj.getHeight() : el.offsetHeight
|
||||
);
|
||||
}
|
||||
|
||||
if (binding.resize) {
|
||||
var lastSize = {};
|
||||
var resizeHandler = function(e) {
|
||||
var size = {
|
||||
w: sizeObj ? sizeObj.getWidth() : el.offsetWidth,
|
||||
h: sizeObj ? sizeObj.getHeight() : el.offsetHeight
|
||||
};
|
||||
if (size.w === 0 && size.h === 0)
|
||||
return;
|
||||
if (size.w === lastSize.w && size.h === lastSize.h)
|
||||
return;
|
||||
lastSize = size;
|
||||
binding.resize(el, size.w, size.h, initResult);
|
||||
};
|
||||
|
||||
on(window, "resize", resizeHandler);
|
||||
|
||||
// This is needed for cases where we're running in a Shiny
|
||||
// app, but the widget itself is not a Shiny output, but
|
||||
// rather a simple static widget. One example of this is
|
||||
// an rmarkdown document that has runtime:shiny and widget
|
||||
// that isn't in a render function. Shiny only knows to
|
||||
// call resize handlers for Shiny outputs, not for static
|
||||
// widgets, so we do it ourselves.
|
||||
if (window.jQuery) {
|
||||
window.jQuery(document).on("shown", resizeHandler);
|
||||
window.jQuery(document).on("hidden", resizeHandler);
|
||||
}
|
||||
|
||||
// This is needed for the specific case of ioslides, which
|
||||
// flips slides between display:none and display:block.
|
||||
// Ideally we would not have to have ioslide-specific code
|
||||
// here, but rather have ioslides raise a generic event,
|
||||
// but the rmarkdown package just went to CRAN so the
|
||||
// window to getting that fixed may be long.
|
||||
if (window.addEventListener) {
|
||||
// It's OK to limit this to window.addEventListener
|
||||
// browsers because ioslides itself only supports
|
||||
// such browsers.
|
||||
on(document, "slideenter", resizeHandler);
|
||||
on(document, "slideleave", resizeHandler);
|
||||
}
|
||||
}
|
||||
|
||||
var scriptData = document.querySelector("script[data-for='" + el.id + "'][type='application/json']");
|
||||
if (scriptData) {
|
||||
var data = JSON.parse(scriptData.textContent || scriptData.text);
|
||||
// Resolve strings marked as javascript literals to objects
|
||||
if (!(data.evals instanceof Array)) data.evals = [data.evals];
|
||||
for (var k = 0; data.evals && k < data.evals.length; k++) {
|
||||
window.HTMLWidgets.evaluateStringMember(data.x, data.evals[k]);
|
||||
}
|
||||
binding.renderValue(el, data.x, initResult);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// Wait until after the document has loaded to render the widgets.
|
||||
if (document.addEventListener) {
|
||||
document.addEventListener("DOMContentLoaded", function() {
|
||||
document.removeEventListener("DOMContentLoaded", arguments.callee, false);
|
||||
window.HTMLWidgets.staticRender();
|
||||
}, false);
|
||||
} else if (document.attachEvent) {
|
||||
document.attachEvent("onreadystatechange", function() {
|
||||
if (document.readyState === "complete") {
|
||||
document.detachEvent("onreadystatechange", arguments.callee);
|
||||
window.HTMLWidgets.staticRender();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
window.HTMLWidgets.getAttachmentUrl = function(depname, key) {
|
||||
// If no key, default to the first item
|
||||
if (typeof(key) === "undefined")
|
||||
key = 1;
|
||||
|
||||
var link = document.getElementById(depname + "-" + key + "-attachment");
|
||||
if (!link) {
|
||||
throw new Error("Attachment " + depname + "/" + key + " not found in document");
|
||||
}
|
||||
return link.getAttribute("href");
|
||||
};
|
||||
|
||||
window.HTMLWidgets.dataframeToD3 = function(df) {
|
||||
var names = [];
|
||||
var length;
|
||||
for (var name in df) {
|
||||
if (df.hasOwnProperty(name))
|
||||
names.push(name);
|
||||
if (typeof(df[name]) !== "object" || typeof(df[name].length) === "undefined") {
|
||||
throw new Error("All fields must be arrays");
|
||||
} else if (typeof(length) !== "undefined" && length !== df[name].length) {
|
||||
throw new Error("All fields must be arrays of the same length");
|
||||
}
|
||||
length = df[name].length;
|
||||
}
|
||||
var results = [];
|
||||
var item;
|
||||
for (var row = 0; row < length; row++) {
|
||||
item = {};
|
||||
for (var col = 0; col < names.length; col++) {
|
||||
item[names[col]] = df[names[col]][row];
|
||||
}
|
||||
results.push(item);
|
||||
}
|
||||
return results;
|
||||
};
|
||||
|
||||
window.HTMLWidgets.transposeArray2D = function(array) {
|
||||
var newArray = array[0].map(function(col, i) {
|
||||
return array.map(function(row) {
|
||||
return row[i]
|
||||
})
|
||||
});
|
||||
return newArray;
|
||||
};
|
||||
// Split value at splitChar, but allow splitChar to be escaped
|
||||
// using escapeChar. Any other characters escaped by escapeChar
|
||||
// will be included as usual (including escapeChar itself).
|
||||
function splitWithEscape(value, splitChar, escapeChar) {
|
||||
var results = [];
|
||||
var escapeMode = false;
|
||||
var currentResult = "";
|
||||
for (var pos = 0; pos < value.length; pos++) {
|
||||
if (!escapeMode) {
|
||||
if (value[pos] === splitChar) {
|
||||
results.push(currentResult);
|
||||
currentResult = "";
|
||||
} else if (value[pos] === escapeChar) {
|
||||
escapeMode = true;
|
||||
} else {
|
||||
currentResult += value[pos];
|
||||
}
|
||||
} else {
|
||||
currentResult += value[pos];
|
||||
escapeMode = false;
|
||||
}
|
||||
}
|
||||
if (currentResult !== "") {
|
||||
results.push(currentResult);
|
||||
}
|
||||
return results;
|
||||
}
|
||||
// Function authored by Yihui/JJ Allaire
|
||||
window.HTMLWidgets.evaluateStringMember = function(o, member) {
|
||||
var parts = splitWithEscape(member, '.', '\\');
|
||||
for (var i = 0, l = parts.length; i < l; i++) {
|
||||
var part = parts[i];
|
||||
// part may be a character or 'numeric' member name
|
||||
if (o !== null && typeof o === "object" && part in o) {
|
||||
if (i == (l - 1)) { // if we are at the end of the line then evalulate
|
||||
if (typeof o[part] === "string")
|
||||
o[part] = eval("(" + o[part] + ")");
|
||||
} else { // otherwise continue to next embedded object
|
||||
o = o[part];
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
})();
|
||||
|
5
public/javascripts/sankey-1/d3-3.5.2/d3.min.js
vendored
Executable file
5
public/javascripts/sankey-1/d3-3.5.2/d3.min.js
vendored
Executable file
File diff suppressed because one or more lines are too long
397
public/javascripts/sankey-1/sankey.js
Executable file
397
public/javascripts/sankey-1/sankey.js
Executable file
|
@ -0,0 +1,397 @@
|
|||
d3.sankey = function() {
|
||||
var sankey = {},
|
||||
nodeWidth = 24,
|
||||
nodePadding = 8,
|
||||
size = [1, 1],
|
||||
nodes = [],
|
||||
links = [],
|
||||
sinksRight = true;
|
||||
|
||||
sankey.nodeWidth = function(_) {
|
||||
if (!arguments.length) return nodeWidth;
|
||||
nodeWidth = +_;
|
||||
return sankey;
|
||||
};
|
||||
|
||||
sankey.nodePadding = function(_) {
|
||||
if (!arguments.length) return nodePadding;
|
||||
nodePadding = +_;
|
||||
return sankey;
|
||||
};
|
||||
|
||||
sankey.nodes = function(_) {
|
||||
if (!arguments.length) return nodes;
|
||||
nodes = _;
|
||||
return sankey;
|
||||
};
|
||||
|
||||
sankey.links = function(_) {
|
||||
if (!arguments.length) return links;
|
||||
links = _;
|
||||
return sankey;
|
||||
};
|
||||
|
||||
sankey.size = function(_) {
|
||||
if (!arguments.length) return size;
|
||||
size = _;
|
||||
return sankey;
|
||||
};
|
||||
|
||||
sankey.sinksRight = function (_) {
|
||||
if (!arguments.length) return sinksRight;
|
||||
sinksRight = _;
|
||||
return sankey;
|
||||
};
|
||||
|
||||
sankey.layout = function(iterations) {
|
||||
computeNodeLinks();
|
||||
computeNodeValues();
|
||||
computeNodeBreadths();
|
||||
computeNodeDepths(iterations);
|
||||
return sankey;
|
||||
};
|
||||
|
||||
sankey.relayout = function() {
|
||||
computeLinkDepths();
|
||||
return sankey;
|
||||
};
|
||||
|
||||
// SVG path data generator, to be used as "d" attribute on "path" element selection.
|
||||
sankey.link = function() {
|
||||
var curvature = .5;
|
||||
|
||||
function link(d) {
|
||||
var xs = d.source.x + d.source.dx,
|
||||
xt = d.target.x,
|
||||
xi = d3.interpolateNumber(xs, xt),
|
||||
xsc = xi(curvature),
|
||||
xtc = xi(1 - curvature),
|
||||
ys = d.source.y + d.sy + d.dy / 2,
|
||||
yt = d.target.y + d.ty + d.dy / 2;
|
||||
|
||||
if (!d.cycleBreaker) {
|
||||
return "M" + xs + "," + ys
|
||||
+ "C" + xsc + "," + ys
|
||||
+ " " + xtc + "," + yt
|
||||
+ " " + xt + "," + yt;
|
||||
} else {
|
||||
var xdelta = (1.5 * d.dy + 0.05 * Math.abs(xs - xt));
|
||||
xsc = xs + xdelta;
|
||||
xtc = xt - xdelta;
|
||||
var xm = xi(0.5);
|
||||
var ym = d3.interpolateNumber(ys, yt)(0.5);
|
||||
var ydelta = (2 * d.dy + 0.1 * Math.abs(xs - xt) + 0.1 * Math.abs(ys - yt)) * (ym < (size[1] / 2) ? -1 : 1);
|
||||
return "M" + xs + "," + ys
|
||||
+ "C" + xsc + "," + ys
|
||||
+ " " + xsc + "," + (ys + ydelta)
|
||||
+ " " + xm + "," + (ym + ydelta)
|
||||
+ "S" + xtc + "," + yt
|
||||
+ " " + xt + "," + yt;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
link.curvature = function(_) {
|
||||
if (!arguments.length) return curvature;
|
||||
curvature = +_;
|
||||
return link;
|
||||
};
|
||||
|
||||
return link;
|
||||
};
|
||||
|
||||
// Populate the sourceLinks and targetLinks for each node.
|
||||
// Also, if the source and target are not objects, assume they are indices.
|
||||
function computeNodeLinks() {
|
||||
nodes.forEach(function(node) {
|
||||
// Links that have this node as source.
|
||||
node.sourceLinks = [];
|
||||
// Links that have this node as target.
|
||||
node.targetLinks = [];
|
||||
});
|
||||
links.forEach(function(link) {
|
||||
var source = link.source,
|
||||
target = link.target;
|
||||
if (typeof source === "number") source = link.source = nodes[link.source];
|
||||
if (typeof target === "number") target = link.target = nodes[link.target];
|
||||
source.sourceLinks.push(link);
|
||||
target.targetLinks.push(link);
|
||||
});
|
||||
}
|
||||
|
||||
// Compute the value (size) of each node by summing the associated links.
|
||||
function computeNodeValues() {
|
||||
nodes.forEach(function(node) {
|
||||
node.value = Math.max(
|
||||
d3.sum(node.sourceLinks, value),
|
||||
d3.sum(node.targetLinks, value)
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
// Iteratively assign the breadth (x-position) for each node.
|
||||
// Nodes are assigned the maximum breadth of incoming neighbors plus one;
|
||||
// nodes with no incoming links are assigned breadth zero, while
|
||||
// nodes with no outgoing links are assigned the maximum breadth.
|
||||
function computeNodeBreadths() {
|
||||
var remainingNodes = nodes,
|
||||
nextNodes,
|
||||
x = 0;
|
||||
|
||||
// Work from left to right.
|
||||
// Keep updating the breath (x-position) of nodes that are target of recently updated nodes.
|
||||
while (remainingNodes.length && x < nodes.length) {
|
||||
nextNodes = [];
|
||||
remainingNodes.forEach(function(node) {
|
||||
node.x = x;
|
||||
node.dx = nodeWidth;
|
||||
node.sourceLinks.forEach(function(link) {
|
||||
if (nextNodes.indexOf(link.target) < 0 && !link.cycleBreaker) {
|
||||
nextNodes.push(link.target);
|
||||
}
|
||||
});
|
||||
});
|
||||
if (nextNodes.length == remainingNodes.length) {
|
||||
// There must be a cycle here. Let's search for a link that breaks it.
|
||||
findAndMarkCycleBreaker(nextNodes);
|
||||
// Start over.
|
||||
// TODO: make this optional?
|
||||
return computeNodeBreadths();
|
||||
}
|
||||
else {
|
||||
remainingNodes = nextNodes;
|
||||
++x;
|
||||
}
|
||||
}
|
||||
|
||||
// Optionally move pure sinks always to the right.
|
||||
if (sinksRight) {
|
||||
moveSinksRight(x);
|
||||
}
|
||||
|
||||
scaleNodeBreadths((size[0] - nodeWidth) / (x - 1));
|
||||
}
|
||||
|
||||
// Find a link that breaks a cycle in the graph (if any).
|
||||
function findAndMarkCycleBreaker(nodes) {
|
||||
// Go through all nodes from the given subset and traverse links searching for cycles.
|
||||
var link;
|
||||
for (var n=nodes.length - 1; n >= 0; n--) {
|
||||
link = depthFirstCycleSearch(nodes[n], []);
|
||||
if (link) {
|
||||
return link;
|
||||
}
|
||||
}
|
||||
|
||||
// Depth-first search to find a link that is part of a cycle.
|
||||
function depthFirstCycleSearch(cursorNode, path) {
|
||||
var target, link;
|
||||
for (var n = cursorNode.sourceLinks.length - 1; n >= 0; n--) {
|
||||
link = cursorNode.sourceLinks[n];
|
||||
if (link.cycleBreaker) {
|
||||
// Skip already known cycle breakers.
|
||||
continue;
|
||||
}
|
||||
|
||||
// Check if target of link makes a cycle in current path.
|
||||
target = link.target;
|
||||
for (var l = 0; l < path.length; l++) {
|
||||
if (path[l].source == target) {
|
||||
// We found a cycle. Search for weakest link in cycle
|
||||
var weakest = link;
|
||||
for (; l < path.length; l++) {
|
||||
if (path[l].value < weakest.value) {
|
||||
weakest = path[l];
|
||||
}
|
||||
}
|
||||
// Mark weakest link as (known) cycle breaker and abort search.
|
||||
weakest.cycleBreaker = true;
|
||||
return weakest;
|
||||
}
|
||||
}
|
||||
|
||||
// Recurse deeper.
|
||||
path.push(link);
|
||||
link = depthFirstCycleSearch(target, path);
|
||||
path.pop();
|
||||
// Stop further search if we found a cycle breaker.
|
||||
if (link) {
|
||||
return link;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function moveSourcesRight() {
|
||||
nodes.forEach(function(node) {
|
||||
if (!node.targetLinks.length) {
|
||||
node.x = d3.min(node.sourceLinks, function(d) { return d.target.x; }) - 1;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function moveSinksRight(x) {
|
||||
nodes.forEach(function(node) {
|
||||
if (!node.sourceLinks.length) {
|
||||
node.x = x - 1;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function scaleNodeBreadths(kx) {
|
||||
nodes.forEach(function(node) {
|
||||
node.x *= kx;
|
||||
});
|
||||
}
|
||||
|
||||
// Compute the depth (y-position) for each node.
|
||||
function computeNodeDepths(iterations) {
|
||||
// Group nodes by breath.
|
||||
var nodesByBreadth = d3.nest()
|
||||
.key(function(d) { return d.x; })
|
||||
.sortKeys(d3.ascending)
|
||||
.entries(nodes)
|
||||
.map(function(d) { return d.values; });
|
||||
|
||||
//
|
||||
initializeNodeDepth();
|
||||
resolveCollisions();
|
||||
computeLinkDepths();
|
||||
for (var alpha = 1; iterations > 0; --iterations) {
|
||||
relaxRightToLeft(alpha *= .99);
|
||||
resolveCollisions();
|
||||
computeLinkDepths();
|
||||
relaxLeftToRight(alpha);
|
||||
resolveCollisions();
|
||||
computeLinkDepths();
|
||||
}
|
||||
|
||||
function initializeNodeDepth() {
|
||||
// Calculate vertical scaling factor.
|
||||
var ky = d3.min(nodesByBreadth, function(nodes) {
|
||||
return (size[1] - (nodes.length - 1) * nodePadding) / d3.sum(nodes, value);
|
||||
});
|
||||
|
||||
nodesByBreadth.forEach(function(nodes) {
|
||||
nodes.forEach(function(node, i) {
|
||||
node.y = i;
|
||||
node.dy = node.value * ky;
|
||||
});
|
||||
});
|
||||
|
||||
links.forEach(function(link) {
|
||||
link.dy = link.value * ky;
|
||||
});
|
||||
}
|
||||
|
||||
function relaxLeftToRight(alpha) {
|
||||
nodesByBreadth.forEach(function(nodes, breadth) {
|
||||
nodes.forEach(function(node) {
|
||||
if (node.targetLinks.length) {
|
||||
// Value-weighted average of the y-position of source node centers linked to this node.
|
||||
var y = d3.sum(node.targetLinks, weightedSource) / d3.sum(node.targetLinks, value);
|
||||
node.y += (y - center(node)) * alpha;
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
function weightedSource(link) {
|
||||
return (link.source.y + link.sy + link.dy / 2) * link.value;
|
||||
}
|
||||
}
|
||||
|
||||
function relaxRightToLeft(alpha) {
|
||||
nodesByBreadth.slice().reverse().forEach(function(nodes) {
|
||||
nodes.forEach(function(node) {
|
||||
if (node.sourceLinks.length) {
|
||||
// Value-weighted average of the y-positions of target nodes linked to this node.
|
||||
var y = d3.sum(node.sourceLinks, weightedTarget) / d3.sum(node.sourceLinks, value);
|
||||
node.y += (y - center(node)) * alpha;
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
function weightedTarget(link) {
|
||||
return (link.target.y + link.ty + link.dy / 2) * link.value;
|
||||
}
|
||||
}
|
||||
|
||||
function resolveCollisions() {
|
||||
nodesByBreadth.forEach(function(nodes) {
|
||||
var node,
|
||||
dy,
|
||||
y0 = 0,
|
||||
n = nodes.length,
|
||||
i;
|
||||
|
||||
// Push any overlapping nodes down.
|
||||
nodes.sort(ascendingDepth);
|
||||
for (i = 0; i < n; ++i) {
|
||||
node = nodes[i];
|
||||
dy = y0 - node.y;
|
||||
if (dy > 0) node.y += dy;
|
||||
y0 = node.y + node.dy + nodePadding;
|
||||
}
|
||||
|
||||
// If the bottommost node goes outside the bounds, push it back up.
|
||||
dy = y0 - nodePadding - size[1];
|
||||
if (dy > 0) {
|
||||
y0 = node.y -= dy;
|
||||
|
||||
// Push any overlapping nodes back up.
|
||||
for (i = n - 2; i >= 0; --i) {
|
||||
node = nodes[i];
|
||||
dy = node.y + node.dy + nodePadding - y0;
|
||||
if (dy > 0) node.y -= dy;
|
||||
y0 = node.y;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function ascendingDepth(a, b) {
|
||||
return a.y - b.y;
|
||||
}
|
||||
}
|
||||
|
||||
// Compute y-offset of the source endpoint (sy) and target endpoints (ty) of links,
|
||||
// relative to the source/target node's y-position.
|
||||
function computeLinkDepths() {
|
||||
nodes.forEach(function(node) {
|
||||
node.sourceLinks.sort(ascendingTargetDepth);
|
||||
node.targetLinks.sort(ascendingSourceDepth);
|
||||
});
|
||||
nodes.forEach(function(node) {
|
||||
var sy = 0, ty = 0;
|
||||
node.sourceLinks.forEach(function(link) {
|
||||
link.sy = sy;
|
||||
sy += link.dy;
|
||||
});
|
||||
node.targetLinks.forEach(function(link) {
|
||||
link.ty = ty;
|
||||
ty += link.dy;
|
||||
});
|
||||
});
|
||||
|
||||
function ascendingSourceDepth(a, b) {
|
||||
return a.source.y - b.source.y;
|
||||
}
|
||||
|
||||
function ascendingTargetDepth(a, b) {
|
||||
return a.target.y - b.target.y;
|
||||
}
|
||||
}
|
||||
|
||||
// Y-position of the middle of a node.
|
||||
function center(node) {
|
||||
return node.y + node.dy / 2;
|
||||
}
|
||||
|
||||
// Value property accessor.
|
||||
function value(x) {
|
||||
return x.value;
|
||||
}
|
||||
|
||||
return sankey;
|
||||
};
|
230
public/javascripts/sankeyNetwork-binding-0.2.8/sankeyNetwork.js
Executable file
230
public/javascripts/sankeyNetwork-binding-0.2.8/sankeyNetwork.js
Executable file
|
@ -0,0 +1,230 @@
|
|||
HTMLWidgets.widget({
|
||||
|
||||
name: "sankeyNetwork",
|
||||
|
||||
type: "output",
|
||||
|
||||
initialize: function(el, width, height) {
|
||||
|
||||
d3.select(el).append("svg")
|
||||
.style("width", "100%")
|
||||
.style("height", "100%");
|
||||
|
||||
return {
|
||||
sankey: d3.sankey(),
|
||||
x: null
|
||||
};
|
||||
},
|
||||
|
||||
resize: function(el, width, height, instance) {
|
||||
/* handle resizing now through the viewBox
|
||||
d3.select(el).select("svg")
|
||||
.attr("width", width)
|
||||
.attr("height", height + height * 0.05);
|
||||
|
||||
this.renderValue(el, instance.x, instance);
|
||||
*/
|
||||
},
|
||||
|
||||
renderValue: function(el, x, instance) {
|
||||
|
||||
// save the x in our instance (for calling back from resize)
|
||||
instance.x = x;
|
||||
|
||||
// alias sankey and options
|
||||
var sankey = instance.sankey;
|
||||
var options = x.options;
|
||||
|
||||
// convert links and nodes data frames to d3 friendly format
|
||||
var links = HTMLWidgets.dataframeToD3(x.links);
|
||||
var nodes = HTMLWidgets.dataframeToD3(x.nodes);
|
||||
|
||||
|
||||
// margin handling
|
||||
// set our default margin to be 20
|
||||
// will override with x.options.margin if provided
|
||||
var margin = {top: 20, right: 20, bottom: 20, left: 20};
|
||||
// go through each key of x.options.margin
|
||||
// use this value if provided from the R side
|
||||
Object.keys(x.options.margin).map(function(ky){
|
||||
if(x.options.margin[ky] !== null) {
|
||||
margin[ky] = x.options.margin[ky];
|
||||
}
|
||||
// set the margin on the svg with css style
|
||||
// commenting this out since not correct
|
||||
// s.style(["margin",ky].join("-"), margin[ky]);
|
||||
});
|
||||
|
||||
// get the width and height
|
||||
var width = el.getBoundingClientRect().width - margin.right - margin.left;
|
||||
var height = el.getBoundingClientRect().height - margin.top - margin.bottom;
|
||||
|
||||
var color = eval(options.colourScale);
|
||||
|
||||
var color_node = function color_node(d){
|
||||
if (d.group){
|
||||
return color(d.group.replace(/ .*/, ""));
|
||||
} else {
|
||||
return "#cccccc";
|
||||
}
|
||||
}
|
||||
|
||||
var color_link = function color_link(d){
|
||||
if (d.group){
|
||||
return color(d.group.replace(/ .*/, ""));
|
||||
} else {
|
||||
return "#000000";
|
||||
}
|
||||
}
|
||||
|
||||
var opacity_link = function opacity_link(d){
|
||||
if (d.group){
|
||||
return 0.7;
|
||||
} else {
|
||||
return 0.2;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
var formatNumber = d3.format(",.0f"),
|
||||
format = function(d) { return formatNumber(d); }
|
||||
|
||||
// create d3 sankey layout
|
||||
sankey
|
||||
.nodes(d3.values(nodes))
|
||||
.links(links)
|
||||
.size([width, height])
|
||||
.nodeWidth(options.nodeWidth)
|
||||
.nodePadding(options.nodePadding)
|
||||
.layout(32);
|
||||
|
||||
// select the svg element and remove existing children
|
||||
d3.select(el).select("svg").selectAll("*").remove();
|
||||
// append g for our container to transform by margin
|
||||
var svg = d3.select(el).select("svg").append("g")
|
||||
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");;
|
||||
|
||||
// draw path
|
||||
var path = sankey.link();
|
||||
|
||||
// draw links
|
||||
var link = svg.selectAll(".link")
|
||||
.data(sankey.links())
|
||||
|
||||
link.enter().append("path")
|
||||
.attr("class", "link")
|
||||
|
||||
link
|
||||
.attr("d", path)
|
||||
.style("stroke-width", function(d) { return Math.max(1, d.dy); })
|
||||
.style("fill", "none")
|
||||
.style("stroke", color_link)
|
||||
.style("stroke-opacity", opacity_link)
|
||||
.sort(function(a, b) { return b.dy - a.dy; })
|
||||
.on("mouseover", function(d) {
|
||||
d3.select(this)
|
||||
.style("stroke-opacity", function(d){return opacity_link(d) + 0.3});
|
||||
})
|
||||
.on("mouseout", function(d) {
|
||||
d3.select(this)
|
||||
.style("stroke-opacity", opacity_link);
|
||||
});
|
||||
|
||||
// add backwards class to cycles
|
||||
link.classed('backwards', function (d) { return d.target.x < d.source.x; });
|
||||
|
||||
svg.selectAll(".link.backwards")
|
||||
.style("stroke-dasharray","9,1")
|
||||
.style("stroke","#402")
|
||||
|
||||
// draw nodes
|
||||
var node = svg.selectAll(".node")
|
||||
.data(sankey.nodes())
|
||||
|
||||
node.enter().append("g")
|
||||
.attr("class", "node")
|
||||
.attr("transform", function(d) { return "translate(" +
|
||||
d.x + "," + d.y + ")"; })
|
||||
.call(d3.behavior.drag()
|
||||
.origin(function(d) { return d; })
|
||||
.on("dragstart", function() { this.parentNode.appendChild(this); })
|
||||
.on("drag", dragmove));
|
||||
|
||||
// note: u2192 is right-arrow
|
||||
link.append("title")
|
||||
.text(function(d) { return d.source.name + " \u2192 " + d.target.name +
|
||||
"\n" + format(d.value) + " " + options.units; });
|
||||
|
||||
node.append("rect")
|
||||
.attr("height", function(d) { return d.dy; })
|
||||
.attr("width", sankey.nodeWidth())
|
||||
.style("fill", function(d) {
|
||||
return d.color = color_node(d); })
|
||||
.style("stroke", function(d) { return d3.rgb(d.color).darker(2); })
|
||||
.style("opacity", 0.9)
|
||||
.style("cursor", "move")
|
||||
.append("title")
|
||||
.text(function(d) { return d.name + "\n" + format(d.value) +
|
||||
" " + options.units; });
|
||||
|
||||
node.append("text")
|
||||
.attr("x", -6)
|
||||
.attr("y", function(d) { return d.dy / 2; })
|
||||
.attr("dy", ".35em")
|
||||
.attr("text-anchor", "end")
|
||||
.attr("transform", null)
|
||||
.text(function(d) { return d.name; })
|
||||
.style("font-size", options.fontSize + "px")
|
||||
.style("font-family", options.fontFamily ? options.fontFamily : "inherit")
|
||||
.filter(function(d) { return d.x < width / 2; })
|
||||
.attr("x", 6 + sankey.nodeWidth())
|
||||
.attr("text-anchor", "start");
|
||||
|
||||
|
||||
// adjust viewBox to fit the bounds of our tree
|
||||
var s = d3.select(svg[0][0].parentNode);
|
||||
s.attr(
|
||||
"viewBox",
|
||||
[
|
||||
d3.min(
|
||||
s.selectAll('g')[0].map(function(d){
|
||||
return d.getBoundingClientRect().left
|
||||
})
|
||||
) - s.node().getBoundingClientRect().left - margin.right,
|
||||
d3.min(
|
||||
s.selectAll('g')[0].map(function(d){
|
||||
return d.getBoundingClientRect().top
|
||||
})
|
||||
) - s.node().getBoundingClientRect().top - margin.top,
|
||||
d3.max(
|
||||
s.selectAll('g')[0].map(function(d){
|
||||
return d.getBoundingClientRect().right
|
||||
})
|
||||
) -
|
||||
d3.min(
|
||||
s.selectAll('g')[0].map(function(d){
|
||||
return d.getBoundingClientRect().left
|
||||
})
|
||||
) + margin.left + margin.right,
|
||||
d3.max(
|
||||
s.selectAll('g')[0].map(function(d){
|
||||
return d.getBoundingClientRect().bottom
|
||||
})
|
||||
) -
|
||||
d3.min(
|
||||
s.selectAll('g')[0].map(function(d){
|
||||
return d.getBoundingClientRect().top
|
||||
})
|
||||
) + margin.top + margin.bottom
|
||||
].join(",")
|
||||
);
|
||||
|
||||
|
||||
function dragmove(d) {
|
||||
d3.select(this).attr("transform", "translate(" + d.x + "," +
|
||||
(d.y = Math.max(0, Math.min(height - d.dy, d3.event.y))) + ")");
|
||||
sankey.relayout();
|
||||
link.attr("d", path);
|
||||
}
|
||||
},
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue