Skip to content

Commit

Permalink
Added key function modes: title, id, index & tag-index
Browse files Browse the repository at this point in the history
Also replaced uglify-js with uglify-es to be able to use ES2015+
(ES6+) language features.
  • Loading branch information
Magnus Jacobsson committed Aug 8, 2017
1 parent 49a027b commit 7be7d23
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"jsdom": "11",
"rollup": "0.27",
"tape": "4",
"uglify-js": "2"
"uglify-es": "^3.0.27"
},
"dependencies": {
"d3-selection": "^1.1.0",
Expand Down
34 changes: 23 additions & 11 deletions src/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import * as Viz from "viz.js";
import * as d3 from "d3-selection";
import {transition} from "d3-transition";

export default function(src, rootElement, transitionInstance) {
export default function(src, rootElement, transitionInstance, keyMode = 'title') {

function extractData(element) {
function extractData(element, keyMode, index = 0) {

var datum = {};
var tag = element.node().nodeName;
Expand All @@ -26,9 +26,25 @@ export default function(src, rootElement, transitionInstance) {
datum.comment = element.text();
}
var children = d3.selectAll(element.node().childNodes);
children.each(function () {
if (keyMode == 'index') {
datum.key = index;
} else if (tag[0] != '#') {
if (keyMode == 'id') {
datum.key = element.attr('id');
} else if (keyMode == 'title') {
element.select('title');
var title = element.select('title');
if (!title.empty()) {
datum.key = element.select('title').text();
}
}
}
if (datum.key == null) {
datum.key = tag + '-' + index;
}
children.each(function (d, i) {
if (this !== null) {
var childData = extractData(d3.select(this));
var childData = extractData(d3.select(this), keyMode, i);
if (childData) {
datum.children.push(childData);
}
Expand All @@ -42,12 +58,8 @@ export default function(src, rootElement, transitionInstance) {
return element.node().childNodes;
});
children = children
.data(data, function (d, i, parent) {
if (d.id) {
return d.id;
} else {
return d.tag + '-' + i;
}
.data(data, function (d) {
return d.key;
});
var childrenEnter = children
.enter()
Expand Down Expand Up @@ -106,7 +118,7 @@ export default function(src, rootElement, transitionInstance) {
var newSvg = newDoc
.select('svg');

var data = extractData(newSvg);
var data = extractData(newSvg, keyMode);

var root = d3.select(rootElement);
insertSvg(root, [data]);
Expand Down

0 comments on commit 7be7d23

Please sign in to comment.