diff --git a/static/js/proxyMapper.js b/static/js/proxyMapper.js index 6198258..cffd563 100644 --- a/static/js/proxyMapper.js +++ b/static/js/proxyMapper.js @@ -35,7 +35,6 @@ constructor: ProxyMapper, perform_mapping: function(data) { - console.log(this.mapping); if (this.mapping.dates.length > 0) { this.c_dates(this.data, this.mapping.dates); // probe and fetch all dates } @@ -65,7 +64,9 @@ let label = intermediate[index]; let val = []; for (var i=0; i 800 ? container.width()/2 : 800, + height: container.height() > 800 ? container.height()/2 : 800, treeNodes : { width: 3, depth: 5 }, + maxCharDisplay: 20, itemColors: ['#fc440f', '#a5e12e', '#5d2e8c', '#2ec4b6', '#65524d', '#adcad6', '#99c24d'], duration: 500, interaction: true, @@ -29,7 +31,11 @@ this.data = data; this.treeData = [this.create_tree(data, '', this.options.treeNodes.depth, this.options.treeNodes.depth, this.options.treeNodes.width)]; - this.letterWidth = 10; + this.letterWidth = 8; + this.treeDiv = $('
'); + this.container.append( + $('
').append(this.treeDiv) + ); this.width = this.options.width - this.options.margin.right - this.options.margin.left, this.height = this.options.height - this.options.margin.top - this.options.margin.bottom; @@ -47,7 +53,7 @@ this.diagonal = d3.svg.diagonal() .projection(function(d) { return [d.y, d.x]; }); - this.svg = d3.select(this.container[0]).append("svg") + this.svg = d3.select(this.treeDiv[0]).append("svg") .attr("width", this.width + this.options.margin.right + this.options.margin.left) .attr("height", this.height + this.options.margin.top + this.options.margin.bottom) .append("g") @@ -70,10 +76,17 @@ this.set_current_mapping_item(); } + this.jsonDivIn = $('
'); + this.treeDiv.append(this.jsonDivIn); + var j = this.syntaxHighlightJson(this.data); + this.jsonDivIn.html(j); if (this.options.interaction) { - //var result = new $.proxyMapper(this.instructions, this.treeData, {}); this.treeDivResult = $('
'); - this.container.append(this.treeDivResult); + this.jsonDivOut = $('
'); + this.treeDivResult.append(this.jsonDivOut); + this.container.children().append( + this.treeDivResult + ); this.update_result_tree(); } @@ -90,8 +103,25 @@ var nodes = this.tree.nodes(this.root).reverse(), links = this.tree.links(nodes); - // Normalize for fixed-depth. - nodes.forEach(function(d) { d.y = d.depth * 100; }); + // Compute depth size based on the link name + var maxSizePerDepth = []; + nodes.forEach(function(d) { + let m = maxSizePerDepth[d.depth] !== undefined ? maxSizePerDepth[d.depth] : 0; + let text = that.adjust_text_length(d.linkname).length; + let size = d.linkname !== undefined ? text : 0; + maxSizePerDepth[d.depth] = size > m ? size : m; + }); + // add previous level together + for (var i=1; i this.options.maxCharDisplay) { + text += '...'; + } + return text; + }, + create_tree: function(root, linkname, depth, maxDepth, maxWidth) { if (depth == 0) { return; @@ -522,7 +567,7 @@ child.children.push(this.create_tree(node, k, depth-1, maxDepth, maxWidth)); i++; } - if (root.length > maxWidth) { + if (Object.keys(root).length > maxWidth) { var addNode = {}; addNode.name = '...'; addNode.parent = null; @@ -534,8 +579,32 @@ child.name = root; } return child; + }, + + syntaxHighlightJson: function(json) { + if (typeof json == 'string') { + json = JSON.parse(json); + } + json = JSON.stringify(json, undefined, 2); + json = json.replace(/&/g, '&').replace(//g, '>').replace(/(?:\r\n|\r|\n)/g, '
').replace(/ /g, ' '); + return json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function (match) { + var cls = 'json_number'; + if (/^"/.test(match)) { + if (/:$/.test(match)) { + cls = 'json_key'; + } else { + cls = 'json_string'; + } + } else if (/true|false/.test(match)) { + cls = 'json_boolean'; + } else if (/null/.test(match)) { + cls = 'json_null'; + } + return '' + match + ''; + }); } + } $.treeFromJson = TreeFromJson; diff --git a/templates/test.html b/templates/test.html index d6ad5d5..e984461 100644 --- a/templates/test.html +++ b/templates/test.html @@ -23,9 +23,15 @@ @@ -87,55 +108,6 @@