forked from stephlocke's block: Working Site Tree
Last active
January 6, 2018 20:06
-
-
Save timelyportfolio/e563b1397d6e32bcd823633aa7fb66a8 to your computer and use it in GitHub Desktop.
Working Site Tree - conditional
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
license: mit |
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
.collapsibleTree .node { | |
cursor: pointer; | |
} | |
.collapsibleTree .node circle { | |
fill: #fff; | |
stroke: #000; | |
} | |
.collapsibleTree .node text { | |
font: 10px sans-serif; | |
} | |
.collapsibleTree .link { | |
fill: none; | |
stroke: #ccc; | |
stroke-width: 1.5px; | |
} | |
.collapsibleTree div.tooltip { | |
position: absolute; | |
text-align: center; | |
padding: 3px; | |
font: 10px sans-serif; | |
background: #fff; | |
border: 2px; | |
border-radius: 8px; | |
border-style: solid; | |
pointer-events: none; | |
} |
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
HTMLWidgets.widget({ | |
name: 'collapsibleTree', | |
type: 'output', | |
factory: function(el, width, height) { | |
var i = 0, | |
duration = 750, | |
root = {}, | |
options = {}, | |
treemap; | |
// Optionally enable zooming, and limit to 1/5x or 5x of the original viewport | |
var zoom = d3.zoom() | |
.scaleExtent([1/5, 5]) | |
.on('zoom', function () { | |
if (options.zoomable) svg.attr('transform', d3.event.transform) | |
}) | |
// create our tree object and bind it to the element | |
// appends a 'group' element to 'svg' | |
// moves the 'group' element to the top left margin | |
var svg = d3.select(el).append('svg') | |
.attr('width', width) | |
.attr('height', height) | |
.call(zoom) | |
.append('g'); | |
// Define the div for the tooltip | |
var tooltip = d3.select(el).append('div') | |
.attr('class', 'tooltip') | |
.style('opacity', 0); | |
function update(source) { | |
// Assigns the x and y position for the nodes | |
var treeData = treemap(root); | |
// Compute the new tree layout. | |
var nodes = treeData.descendants(), | |
links = treeData.descendants().slice(1); | |
// Normalize for fixed-depth. | |
nodes.forEach(function(d) {d.y = d.depth * options.linkLength}); | |
// ****************** Nodes section *************************** | |
// Update the nodes... | |
var node = svg.selectAll('g.node') | |
.data(nodes, function(d) {return d.id || (d.id = ++i); }); | |
// Enter any new modes at the parent's previous position. | |
var nodeEnter = node.enter().append('g') | |
.attr('class', 'node') | |
.attr('transform', function(d) { | |
return 'translate(' + source.y0 + ',' + source.x0 + ')'; | |
}) | |
.on('click', click); | |
// Add tooltips, if specified in options | |
if (options.tooltip) { | |
nodeEnter = nodeEnter | |
.on('mouseover', mouseover) | |
.on('mouseout', mouseout); | |
} | |
// Add Circle for the nodes | |
nodeEnter.append('circle') | |
.attr('class', 'node') | |
.attr('r', 1e-6) | |
.style('fill', function(d) { | |
return d.data.fill || (d._children ? options.fill : '#E8830C'); | |
}) | |
.style('stroke-width', function(d) { | |
return d._children ? 3 : 1; | |
}); | |
// Add fontawesome node circles | |
nodeEnter.append('svg:foreignObject') | |
.attr('class', 'handle') | |
.html(function(d) { return d.data.url ? '<a href="'+d.data.url+'"><i class="fa ' + d.data.fa + '"></i></a>': '<i class="fa ' + d.data.fa + '"></i>'; }); | |
// Add links for the nodes with no url | |
nodeEnter.filter(function(d) {return typeof(d.data.url) === "undefined"}) | |
.append("text") | |
.attr('text-anchor', 'end') | |
.style('font-size', options.fontSize + 'px') | |
.text(function(d) { return d.data.name; }); | |
// Add links for the nodes with url | |
nodeEnter.filter(function(d) {return d.data.url}) | |
.append("text") | |
.append("a") | |
.attr("href", function(d) {return d.data.url}) | |
.attr('text-anchor', 'end') | |
.style('font-size', options.fontSize + 'px') | |
.text(function(d) { return d.data.name; }); | |
// UPDATE | |
var nodeUpdate = nodeEnter.merge(node); | |
// Transition to the proper position for the node | |
nodeUpdate.transition() | |
.duration(duration) | |
.attr('transform', function(d) { | |
return 'translate(' + d.y + ',' + d.x + ')'; | |
}); | |
// Update the node attributes and style | |
nodeUpdate.select('circle.node') | |
.attr('r', function(d) { | |
return d.data.SizeOfNode || 10; // default radius is 10 | |
}) | |
.style('fill', function(d) { | |
return d.data.fill || (d._children ? options.fill : '#E8830C'); | |
}) | |
.style('stroke-width', function(d) { | |
return d._children ? 3 : 1; | |
}) | |
.attr('cursor', 'pointer'); | |
// Remove any exiting nodes | |
var nodeExit = node.exit().transition() | |
.duration(duration) | |
.attr('transform', function(d) { | |
return 'translate(' + source.y + ',' + source.x + ')'; | |
}) | |
.remove(); | |
// On exit reduce the node circles size to 0 | |
nodeExit.select('circle') | |
.attr('r', 1e-6); | |
// On exit reduce the opacity of text labels | |
nodeExit.select('text') | |
.style('fill-opacity', 1e-6); | |
// ****************** links section *************************** | |
// Update the links... | |
var link = svg.selectAll('path.link') | |
.data(links, function(d) { return d.id; }); | |
// Enter any new links at the parent's previous position. | |
var linkEnter = link.enter().insert('path', 'g') | |
.attr('class', 'link') | |
// Potentially, this may one day be mappable | |
// .style('stroke-width', function(d) { return d.data.linkWidth || 1 }) | |
.attr('d', function(d){ | |
var o = { x: source.x0, y: source.y0 } | |
return diagonal(o, o) | |
}); | |
// UPDATE | |
var linkUpdate = linkEnter.merge(link); | |
// Transition back to the parent element position | |
linkUpdate.transition() | |
.duration(duration) | |
.attr('d', function(d){ return diagonal(d, d.parent) }); | |
// Remove any exiting links | |
var linkExit = link.exit().transition() | |
.duration(duration) | |
.attr('d', function(d) { | |
var o = {x: source.x, y: source.y} | |
return diagonal(o, o) | |
}) | |
.remove(); | |
// Store the old positions for transition. | |
nodes.forEach(function(d){ | |
d.x0 = d.x; | |
d.y0 = d.y; | |
}); | |
// Creates a curved (diagonal) path from parent to the child nodes | |
function diagonal(s, d) { | |
path = 'M ' + s.y + ' ' + s.x + ' C ' + | |
(s.y + d.y) / 2 + ' ' + s.x + ', ' + | |
(s.y + d.y) / 2 + ' ' + d.x + ', ' + | |
d.y + ' ' + d.x; | |
return path | |
} | |
// Toggle children on click. | |
function click(d) { | |
if (d.children) { | |
d._children = d.children; | |
d.children = null; | |
} else { | |
d.children = d._children; | |
d._children = null; | |
} | |
update(d); | |
// Hide the tooltip after clicking | |
tooltip.transition() | |
.duration(100) | |
.style('opacity', 0) | |
// Update Shiny inputs, if applicable | |
if (options.input) { | |
var nest = {}, | |
obj = d; | |
// Navigate up the list and recursively find parental nodes | |
for (var n = d.depth; n > 0; n--) { | |
nest[options.hierarchy[n-1]] = obj.data.name | |
obj = obj.parent | |
} | |
Shiny.onInputChange(options.input, nest) | |
} | |
} | |
// Show tooltip on mouseover | |
function mouseover(d) { | |
tooltip.transition() | |
.duration(200) | |
.style('opacity', .9); | |
// Show either a constructed tooltip, or override with one from the data | |
tooltip.html( | |
d.data.tooltip || d.data.name + '<br>' + | |
options.attribute + ': ' + d.data.WeightOfNode | |
) | |
// Make the tooltip font size just a little bit bigger | |
.style('font-size', (options.fontSize + 1) + 'px') | |
.style('left', (d3.event.layerX) + 'px') | |
.style('top', (d3.event.layerY - 30) + 'px'); | |
} | |
// Hide tooltip on mouseout | |
function mouseout(d) { | |
tooltip.transition() | |
.duration(500) | |
.style('opacity', 0); | |
} | |
} | |
return { | |
renderValue: function(x) { | |
// Assigns parent, children, height, depth | |
root = d3.hierarchy(x.data, function(d) { return d.children; }); | |
root.x0 = height / 2; | |
root.y0 = 0; | |
// Attach options as a property of the instance | |
options = x.options; | |
// Update the canvas with the new dimensions | |
svg = svg.attr('transform', 'translate(' | |
+ options.margin.left + ',' + options.margin.top + ')') | |
// width and height, corrected for margins | |
var heightMargin = height - options.margin.top - options.margin.bottom, | |
widthMargin = width - options.margin.left - options.margin.right; | |
// declares a tree layout and assigns the size | |
treemap = d3.tree().size([heightMargin, widthMargin]); | |
// Calculate a reasonable link length, if not otherwise specified | |
if (!options.linkLength) { | |
options.linkResponsive = true | |
options.linkLength = widthMargin / options.hierarchy.length | |
if (options.linkLength < 10) { | |
options.linkLength = 10 // Offscreen or too short | |
} | |
} | |
// Optionally collapse after the second level | |
if (options.collapsed) root.children.forEach(collapse); | |
update(root); | |
// Collapse the node and all it's children | |
function collapse(d) { | |
if(d.children) { | |
d._children = d.children | |
d._children.forEach(collapse) | |
d.children = null | |
} | |
} | |
}, | |
resize: function(width, height) { | |
// Resize the canvas | |
d3.select(el).select('svg') | |
.attr('width', width) | |
.attr('height', height); | |
// width and height, corrected for margins | |
var heightMargin = height - options.margin.top - options.margin.bottom, | |
widthMargin = width - options.margin.left - options.margin.right; | |
// Calculate a reasonable link length, if not originally specified | |
if (options.linkResponsive) { | |
options.linkLength = widthMargin / options.hierarchy.length | |
if (options.linkLength < 10) { | |
options.linkLength = 10 // Offscreen or too short | |
} | |
} | |
// Update the treemap to fit the new canvas size | |
treemap = d3.tree().size([heightMargin, widthMargin]); | |
update(root) | |
}, | |
// Make the instance properties available as a property of the widget | |
svg: svg, | |
root: root, | |
options: options | |
}; | |
} | |
}); |
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
(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); | |
} | |
// Add a method to delegator that, when invoked, calls | |
// delegatee.methodName. If there is no such method on | |
// the delegatee, but there was one on delegator before | |
// delegateMethod was called, then the original version | |
// is invoked instead. | |
// For example: | |
// | |
// var a = { | |
// method1: function() { console.log('a1'); } | |
// method2: function() { console.log('a2'); } | |
// }; | |
// var b = { | |
// method1: function() { console.log('b1'); } | |
// }; | |
// delegateMethod(a, b, "method1"); | |
// delegateMethod(a, b, "method2"); | |
// a.method1(); | |
// a.method2(); | |
// | |
// The output would be "b1", "a2". | |
function delegateMethod(delegator, delegatee, methodName) { | |
var inherited = delegator[methodName]; | |
delegator[methodName] = function() { | |
var target = delegatee; | |
var method = delegatee[methodName]; | |
// The method doesn't exist on the delegatee. Instead, | |
// call the method on the delegator, if it exists. | |
if (!method) { | |
target = delegator; | |
method = inherited; | |
} | |
if (method) { | |
return method.apply(target, arguments); | |
} | |
}; | |
} | |
// 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; | |
} | |
} | |
// @param tasks Array of strings (or falsy value, in which case no-op). | |
// Each element must be a valid JavaScript expression that yields a | |
// function. Or, can be an array of objects with "code" and "data" | |
// properties; in this case, the "code" property should be a string | |
// of JS that's an expr that yields a function, and "data" should be | |
// an object that will be added as an additional argument when that | |
// function is called. | |
// @param target The object that will be "this" for each function | |
// execution. | |
// @param args Array of arguments to be passed to the functions. (The | |
// same arguments will be passed to all functions.) | |
function evalAndRun(tasks, target, args) { | |
if (tasks) { | |
forEach(tasks, function(task) { | |
var theseArgs = args; | |
if (typeof(task) === "object") { | |
theseArgs = theseArgs.concat([task.data]); | |
task = task.code; | |
} | |
var taskFunc = eval("(" + task + ")"); | |
if (typeof(taskFunc) !== "function") { | |
throw new Error("Task must be a function! Source:\n" + task); | |
} | |
taskFunc.apply(target, theseArgs); | |
}); | |
} | |
} | |
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 | |
// Support new-style instance-bound definitions. Old-style class-bound | |
// definitions have one widget "object" per widget per type/class of | |
// widget; the renderValue and resize methods on such widget objects | |
// take el and instance arguments, because the widget object can't | |
// store them. New-style instance-bound definitions have one widget | |
// object per widget instance; the definition that's passed in doesn't | |
// provide renderValue or resize methods at all, just the single method | |
// factory(el, width, height) | |
// which returns an object that has renderValue(x) and resize(w, h). | |
// This enables a far more natural programming style for the widget | |
// author, who can store per-instance state using either OO-style | |
// instance fields or functional-style closure variables (I guess this | |
// is in contrast to what can only be called C-style pseudo-OO which is | |
// what we required before). | |
if (definition.factory) { | |
definition = createLegacyDefinitionAdapter(definition); | |
} | |
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 with an output binding. | |
// The definition itself will not be the output binding, instead | |
// we will make an output binding object that delegates to the | |
// definition. This is because we foolishly used the same method | |
// name (renderValue) for htmlwidgets definition and Shiny bindings | |
// but they actually have quite different semantics (the Shiny | |
// bindings receive data that includes lots of metadata that it | |
// strips off before calling htmlwidgets renderValue). We can't | |
// just ignore the difference because in some widgets it's helpful | |
// to call this.renderValue() from inside of resize(), and if | |
// we're not delegating, then that call will go to the Shiny | |
// version instead of the htmlwidgets version. | |
// Merge defaults with definition, without mutating either. | |
var bindingDef = extend({}, defaults, definition); | |
// This object will be our actual Shiny binding. | |
var shinyBinding = new Shiny.OutputBinding(); | |
// With a few exceptions, we'll want to simply use the bindingDef's | |
// version of methods if they are available, otherwise fall back to | |
// Shiny's defaults. NOTE: If Shiny's output bindings gain additional | |
// methods in the future, and we want them to be overrideable by | |
// HTMLWidget binding definitions, then we'll need to add them to this | |
// list. | |
delegateMethod(shinyBinding, bindingDef, "getId"); | |
delegateMethod(shinyBinding, bindingDef, "onValueChange"); | |
delegateMethod(shinyBinding, bindingDef, "onValueError"); | |
delegateMethod(shinyBinding, bindingDef, "renderError"); | |
delegateMethod(shinyBinding, bindingDef, "clearError"); | |
delegateMethod(shinyBinding, bindingDef, "showProgress"); | |
// The find, renderValue, and resize are handled differently, because we | |
// want to actually decorate the behavior of the bindingDef methods. | |
shinyBinding.find = function(scope) { | |
var results = bindingDef.find(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; | |
}; | |
// Wrap renderValue to handle initialization, which unfortunately isn't | |
// supported natively by Shiny at the time of this writing. | |
shinyBinding.renderValue = function(el, data) { | |
Shiny.renderDependencies(data.deps); | |
// 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 (!bindingDef.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 (bindingDef.initialize) { | |
var result = bindingDef.initialize(el, el.offsetWidth, | |
el.offsetHeight); | |
elementData(el, "init_result", result); | |
} | |
} | |
bindingDef.renderValue(el, data.x, elementData(el, "init_result")); | |
evalAndRun(data.jsHooks.render, elementData(el, "init_result"), [el, data.x]); | |
}; | |
// Only override resize if bindingDef implements it | |
if (bindingDef.resize) { | |
shinyBinding.resize = 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")) { | |
bindingDef.resize(el, width, height, elementData(el, "init_result")); | |
} | |
}; | |
} | |
Shiny.outputBindings.register(shinyBinding, bindingDef.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 | |
); | |
elementData(el, "init_result", initResult); | |
} | |
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.htmlwidgets shown.bs.tab.htmlwidgets shown.bs.collapse.htmlwidgets", | |
resizeHandler | |
); | |
window.jQuery(document).on( | |
"hidden.htmlwidgets hidden.bs.tab.htmlwidgets hidden.bs.collapse.htmlwidgets", | |
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); | |
evalAndRun(data.jsHooks.render, initResult, [el, data.x]); | |
} | |
}); | |
}); | |
invokePostRenderHandlers(); | |
} | |
// 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) { | |
if (array.length === 0) return 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]; | |
} | |
} | |
} | |
}; | |
// Retrieve the HTMLWidget instance (i.e. the return value of an | |
// HTMLWidget binding's initialize() or factory() function) | |
// associated with an element, or null if none. | |
window.HTMLWidgets.getInstance = function(el) { | |
return elementData(el, "init_result"); | |
}; | |
// Finds the first element in the scope that matches the selector, | |
// and returns the HTMLWidget instance (i.e. the return value of | |
// an HTMLWidget binding's initialize() or factory() function) | |
// associated with that element, if any. If no element matches the | |
// selector, or the first matching element has no HTMLWidget | |
// instance associated with it, then null is returned. | |
// | |
// The scope argument is optional, and defaults to window.document. | |
window.HTMLWidgets.find = function(scope, selector) { | |
if (arguments.length == 1) { | |
selector = scope; | |
scope = document; | |
} | |
var el = scope.querySelector(selector); | |
if (el === null) { | |
return null; | |
} else { | |
return window.HTMLWidgets.getInstance(el); | |
} | |
}; | |
// Finds all elements in the scope that match the selector, and | |
// returns the HTMLWidget instances (i.e. the return values of | |
// an HTMLWidget binding's initialize() or factory() function) | |
// associated with the elements, in an array. If elements that | |
// match the selector don't have an associated HTMLWidget | |
// instance, the returned array will contain nulls. | |
// | |
// The scope argument is optional, and defaults to window.document. | |
window.HTMLWidgets.findAll = function(scope, selector) { | |
if (arguments.length == 1) { | |
selector = scope; | |
scope = document; | |
} | |
var nodes = scope.querySelectorAll(selector); | |
var results = []; | |
for (var i = 0; i < nodes.length; i++) { | |
results.push(window.HTMLWidgets.getInstance(nodes[i])); | |
} | |
return results; | |
}; | |
var postRenderHandlers = []; | |
function invokePostRenderHandlers() { | |
while (postRenderHandlers.length) { | |
var handler = postRenderHandlers.shift(); | |
if (handler) { | |
handler(); | |
} | |
} | |
} | |
// Register the given callback function to be invoked after the | |
// next time static widgets are rendered. | |
window.HTMLWidgets.addPostRenderHandler = function(callback) { | |
postRenderHandlers.push(callback); | |
}; | |
// Takes a new-style instance-bound definition, and returns an | |
// old-style class-bound definition. This saves us from having | |
// to rewrite all the logic in this file to accomodate both | |
// types of definitions. | |
function createLegacyDefinitionAdapter(defn) { | |
var result = { | |
name: defn.name, | |
type: defn.type, | |
initialize: function(el, width, height) { | |
return defn.factory(el, width, height); | |
}, | |
renderValue: function(el, x, instance) { | |
return instance.renderValue(x); | |
}, | |
resize: function(el, width, height, instance) { | |
return instance.resize(width, height); | |
} | |
}; | |
if (defn.find) | |
result.find = defn.find; | |
if (defn.renderError) | |
result.renderError = defn.renderError; | |
if (defn.clearError) | |
result.clearError = defn.clearError; | |
return result; | |
} | |
})(); |
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
<!DOCTYPE html> | |
<script src="https://unpkg.com/d3@4.12.2/build/d3.min.js"></script> | |
<script src="./htmlwidgets.js"></script> | |
<script src="./collapsibleTree.js"></script> | |
<script src="https://use.fontawesome.com/9f12702d26.js"></script> | |
<link href="./collapsibleTree.css" rel="stylesheet" /> | |
<link href="./LockeTree.css" rel="stylesheet" /> | |
<script type="application/json" data-for="htmlwidget-219e4959c6412cc5305e">{"x":{"data":{"name":"Locke Data","fa":"fa-home","SizeOfNode":30,"children":[{"name":"For your organisation","fa":"fa-users","SizeOfNode":25,"children":[{"name":"Training","fa":"fa-graduation-cap","SizeOfNode":20,"children":[{"name":"On-site »","fa":"fa-building","SizeOfNode":15,"url":"https://itsalocke.com/company/recommendedtraining"},{"name":"Public »","fa":"fa-ticket","SizeOfNode":15,"url":"https://itsalocke.com/#calendar"},{"name":"Online»","fa":"fa-laptop","SizeOfNode":15,"url":"https://itsalocke.com/company/onlinetraining"}]},{"name":"Support","fa":"fa-headphones","SizeOfNode":20,"children":[{"name":"Remote Guru »","fa":"fa-user-circle","SizeOfNode":15,"url":"https://itsalocke.com/company/remoteguru"}]},{"name":"Consultancy","fa":"fa-user-md","SizeOfNode":20,"children":[{"name":"Data Science Readiness »","fa":"fa-flask","SizeOfNode":15,"url":"https://itsalocke.com/company/readiness"},{"name":"Help on projects »","fa":"fa-gears","SizeOfNode":15,"url":"https://itsalocke.com/company/techleadership"},{"name":"Need a review »","fa":"fa-edit","SizeOfNode":15,"url":"https://itsalocke.com/company/peerreview"},{"name":"Other »","fa":"fa-envelope","SizeOfNode":15,"url":"https://itsalocke.com/#contact"}]}]},{"name":"For you","fa":"fa-user","SizeOfNode":25,"children":[{"name":"Read","fa":"fa-book","SizeOfNode":20,"children":[{"name":"Books »","fa":"fa-book","SizeOfNode":15,"url":"https://itsalocke.com/company/books"},{"name":"Blog »","fa":"fa-newspaper-o","SizeOfNode":15,"url":"https://itsalocke.com/blog"}]},{"name":"Events","fa":"fa-calendar","SizeOfNode":20,"children":[{"name":"Upcoming events »","fa":"fa-ticket","SizeOfNode":15,"url":"https://itsalocke.com/#calendar"},{"name":"Past talks and slides »","fa":"fa-upload","SizeOfNode":15,"url":"https://itsalocke.com/talks"}]},{"name":"Online","fa":"fa-globe","SizeOfNode":20,"children":[{"name":"Learn R by email »","fa":"fa-inbox","SizeOfNode":15,"url":"http://eepurl.com/df8me1"},{"name":"Newsletter »","fa":"fa-envelope-open","SizeOfNode":15,"url":"http://eepurl.com/df8me1"},{"name":"GitHub »","fa":"fa-github","SizeOfNode":15,"url":"https://github.com/lockedata"},{"name":"Packages »","fa":"fa-cube","SizeOfNode":15,"url":"https://itsalocke.com/oss/packages"},{"name":"Videos »","fa":"fa-youtube-play","SizeOfNode":15,"url":"https://www.youtube.com/channel/UCAg9ZUc9v3YedOuyAd-nDZQ"}]}]}]},"options":{"hierarchy":["L1","L2","L3"],"input":null,"attribute":"leafCount","linkLength":null,"fontSize":24,"tooltip":false,"collapsed":true,"zoomable":true,"margin":{"top":20,"bottom":20,"left":179.05,"right":190}}},"evals":[],"jsHooks":[]}</script> | |
<script type="application/htmlwidget-sizing" data-for="htmlwidget-219e4959c6412cc5305e">{"viewer":{"width":450,"height":350,"padding":0,"fill":true},"browser":{"width":960,"height":500,"padding":40,"fill":false}}</script> | |
<div id="htmlwidget_container" style="background-color:#2165B6;"> | |
<div id="htmlwidget-219e4959c6412cc5305e" style="width:100%;height:500px;" class="collapsibleTree html-widget"></div> | |
</div> |
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
.collapsibleTree .node circle { | |
fill: #E8830C; | |
stroke: #E8830C; | |
} | |
.collapsibleTree .node text { | |
fill: white; | |
} | |
.collapsibleTree .link { | |
stroke-opacity: 0.5; | |
stroke: white; | |
} | |
.collapsibleTree .node a { | |
fill: white; | |
text-decoration: underline; | |
} | |
.fa { | |
color: white; | |
font-size: 3em; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment