Repository: ivan111/vtree Branch: master Commit: 008e1fe3d6a2 Files: 25 Total size: 120.1 KB Directory structure: gitextract_gick23ka/ ├── .eslintrc ├── .gitignore ├── LICENSE ├── README.md ├── css/ │ ├── custom.css │ └── vtree.css ├── dist/ │ └── vtree.js ├── index.html ├── package.json ├── src/ │ ├── decorator/ │ │ └── linkName.js │ ├── layout/ │ │ ├── array.js │ │ └── tree.js │ ├── node/ │ │ ├── README.md │ │ ├── array.js │ │ ├── dummy.js │ │ ├── node.js │ │ ├── string.js │ │ ├── table.js │ │ └── util.js │ ├── reader/ │ │ ├── object.js │ │ └── test/ │ │ └── object_reader_test.js │ ├── util.js │ └── vtree.js ├── test.html └── webpack.config.js ================================================ FILE CONTENTS ================================================ ================================================ FILE: .eslintrc ================================================ { "env": { "browser": true, "node": true, "es6": true, "mocha": true }, "ecmaFeatures": { "modules": true }, "parserOptions": { "sourceType": "module" }, "extends": "eslint:recommended", "rules": { "no-undef": 2, "no-console": 1 } } ================================================ FILE: .gitignore ================================================ node_modules npm-debug.log lib/ ================================================ FILE: LICENSE ================================================ Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "{}" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright {yyyy} {name of copyright owner} Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ================================================ FILE: README.md ================================================ JSON to Tree Diagram Converter ============================== > For Yakusugi This tool converts JSON strings into tree diagrams. For example, it is used to show AST(Abstract Syntax Tree) as tree diagrams for debugging. In that case, of course, you need to translate AST into JSON. [Online JSON to Tree Diagram Converter](http://vanya.jp.net/vtree/) ![Screenshot](https://github.com/ivan111/vtree/raw/master/vtree.jpg) ================================================ FILE: css/custom.css ================================================ body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; background-color: #f8f9fa; display: flex; align-items: center; min-height: 100vh; } .container { max-width: 1030px; width: 100%; background-color: #ffffff; border-radius: 8px; box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1), 0 2px 4px rgba(0, 0, 0, 0.12), 0 4px 8px rgba(0, 0, 0, 0.14); padding: 2rem; margin: 0 auto; } header { margin-bottom: 2rem; } button { width: 100%; } textarea { resize: none; } footer { margin-top: 3rem; } ================================================ FILE: css/vtree.css ================================================ svg.vtree { overflow: hidden; background: #ffffff; -webkit-box-shadow: 4px 4px 8px rgba(0,0,0,0.5); -moz-box-shadow : 4px 4px 8px rgba(0,0,0,0.5); box-shadow : 4px 4px 8px rgba(0,0,0,0.5); } ================================================ FILE: dist/vtree.js ================================================ var VTree = /******/ (function(modules) { // webpackBootstrap /******/ // The module cache /******/ var installedModules = {}; /******/ /******/ // The require function /******/ function __webpack_require__(moduleId) { /******/ /******/ // Check if module is in cache /******/ if(installedModules[moduleId]) { /******/ return installedModules[moduleId].exports; /******/ } /******/ // Create a new module (and put it into the cache) /******/ var module = installedModules[moduleId] = { /******/ i: moduleId, /******/ l: false, /******/ exports: {} /******/ }; /******/ /******/ // Execute the module function /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); /******/ /******/ // Flag the module as loaded /******/ module.l = true; /******/ /******/ // Return the exports of the module /******/ return module.exports; /******/ } /******/ /******/ /******/ // expose the modules object (__webpack_modules__) /******/ __webpack_require__.m = modules; /******/ /******/ // expose the module cache /******/ __webpack_require__.c = installedModules; /******/ /******/ // define getter function for harmony exports /******/ __webpack_require__.d = function(exports, name, getter) { /******/ if(!__webpack_require__.o(exports, name)) { /******/ Object.defineProperty(exports, name, { enumerable: true, get: getter }); /******/ } /******/ }; /******/ /******/ // define __esModule on exports /******/ __webpack_require__.r = function(exports) { /******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) { /******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); /******/ } /******/ Object.defineProperty(exports, '__esModule', { value: true }); /******/ }; /******/ /******/ // create a fake namespace object /******/ // mode & 1: value is a module id, require it /******/ // mode & 2: merge all properties of value into the ns /******/ // mode & 4: return value when already ns object /******/ // mode & 8|1: behave like require /******/ __webpack_require__.t = function(value, mode) { /******/ if(mode & 1) value = __webpack_require__(value); /******/ if(mode & 8) return value; /******/ if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value; /******/ var ns = Object.create(null); /******/ __webpack_require__.r(ns); /******/ Object.defineProperty(ns, 'default', { enumerable: true, value: value }); /******/ if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key)); /******/ return ns; /******/ }; /******/ /******/ // getDefaultExport function for compatibility with non-harmony modules /******/ __webpack_require__.n = function(module) { /******/ var getter = module && module.__esModule ? /******/ function getDefault() { return module['default']; } : /******/ function getModuleExports() { return module; }; /******/ __webpack_require__.d(getter, 'a', getter); /******/ return getter; /******/ }; /******/ /******/ // Object.prototype.hasOwnProperty.call /******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); }; /******/ /******/ // __webpack_public_path__ /******/ __webpack_require__.p = ""; /******/ /******/ /******/ // Load entry module and return exports /******/ return __webpack_require__(__webpack_require__.s = "./src/vtree.js"); /******/ }) /************************************************************************/ /******/ ({ /***/ "./src/decorator/linkName.js": /*!***********************************!*\ !*** ./src/decorator/linkName.js ***! \***********************************/ /*! no static exports found */ /***/ (function(module, exports, __webpack_require__) { "use strict"; eval("\n\nObject.defineProperty(exports, \"__esModule\", {\n value: true\n});\n\nvar _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if (\"value\" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();\n\nfunction _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError(\"Cannot call a class as a function\"); } }\n\nvar pad = 4;\n\nvar LinkNameDecorator = function () {\n function LinkNameDecorator(linkName) {\n _classCallCheck(this, LinkNameDecorator);\n\n this.linkName = linkName;\n }\n\n _createClass(LinkNameDecorator, [{\n key: 'render',\n value: function render(g, oldG, width) {\n var t = g.append('text').text(this.linkName);\n\n var b = t.node().getBBox();\n var textW = Math.ceil(b.width);\n var textH = Math.ceil(b.height);\n\n var textTotalW = textW + pad * 2;\n\n var newW = width;\n\n if (textTotalW > width) {\n newW = textTotalW;\n }\n\n var textTotalH = textH + pad;\n\n var dw = newW - width;\n var dh = textTotalH;\n var dx = Math.round(dw / 2);\n var dy = textTotalH;\n\n t.attr('x', Math.round(newW / 2)).attr('y', textH).attr('text-anchor', 'middle');\n\n return { dx: dx, dy: dy, dw: dw, dh: dh };\n }\n }]);\n\n return LinkNameDecorator;\n}();\n\nexports.default = LinkNameDecorator;\n\n//# sourceURL=webpack://VTree/./src/decorator/linkName.js?"); /***/ }), /***/ "./src/layout/array.js": /*!*****************************!*\ !*** ./src/layout/array.js ***! \*****************************/ /*! no static exports found */ /***/ (function(module, exports, __webpack_require__) { "use strict"; eval("\n\nObject.defineProperty(exports, \"__esModule\", {\n value: true\n});\n\nvar _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if (\"value\" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();\n\nfunction _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError(\"Cannot call a class as a function\"); } }\n\n/* global d3 */\n\nvar DURATION = 1000;\n\nvar diagonal = d3.svg.diagonal();\n\nvar ArrayLayout = function () {\n function ArrayLayout() {\n var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};\n\n _classCallCheck(this, ArrayLayout);\n\n this.hideLinks = options.hideLinks;\n }\n\n _createClass(ArrayLayout, [{\n key: 'layout',\n value: function layout(node) {\n calcChildrenWidth(node);\n calcTotalSize(node);\n\n _layout(node);\n }\n }, {\n key: 'renderLinks',\n value: function renderLinks(node) {\n if (!this.hideLinks) {\n _renderLinks(node);\n }\n }\n }]);\n\n return ArrayLayout;\n}();\n\nexports.default = ArrayLayout;\n\n\nfunction _layout(node) {\n if (node.children.length === 0) {\n return;\n }\n\n var x = -Math.round(node.childrenWidth / 2);\n var y = 0;\n\n node.children.forEach(function (child) {\n child.x = x + Math.round(child.totalWidth / 2) - Math.round(child.width / 2);\n child.y = y;\n\n child.g.transition().duration(DURATION).attr('transform', 'translate(' + child.x + ',' + child.y + ')');\n\n x += child.totalWidth + node.margin;\n });\n}\n\nfunction _renderLinks(node) {\n if (node.children.length === 0) {\n return;\n }\n\n var h = node.children[0].linkY;\n\n node.children.forEach(function (child) {\n h = Math.min(h, child.linkY);\n });\n\n var orig = { x: 0, y: 0 };\n\n var i, src, dst;\n\n for (i = 0; i < node.children.length; i++) {\n if (i === 0) {\n continue;\n }\n\n var prev = node.children[i - 1];\n var child = node.children[i];\n\n src = {\n x: prev.x + prev.linkX,\n y: prev.y + h\n };\n\n dst = {\n x: child.x + child.linkX,\n y: child.y + h\n };\n\n var link = node.g.insert('path', ':first-child').attr('class', 'vtree-link').attr('d', function () {\n return diagonal({ source: orig, target: orig });\n });\n\n link.transition().duration(DURATION).attr('d', function () {\n return diagonal({ source: src, target: dst });\n });\n }\n}\n\nfunction calcChildrenWidth(node) {\n if (node.children.length === 0) {\n node.childrenWidth = 0;\n\n return;\n }\n\n var w = 0;\n\n node.children.forEach(function (child) {\n w += child.totalWidth;\n });\n\n w += (node.children.length - 1) * node.margin;\n\n node.childrenWidth = w;\n}\n\nfunction calcTotalSize(node) {\n if (node.children.length === 0) {\n node.totalWidth = node.width;\n node.totalHeight = node.height;\n\n return;\n }\n\n node.totalWidth = Math.max(node.width, node.childrenWidth);\n\n var maxChildH = 0;\n\n node.children.forEach(function (child) {\n maxChildH = Math.max(maxChildH, child.totalHeight);\n });\n\n node.totalHeight = maxChildH;\n}\n\n//# sourceURL=webpack://VTree/./src/layout/array.js?"); /***/ }), /***/ "./src/layout/tree.js": /*!****************************!*\ !*** ./src/layout/tree.js ***! \****************************/ /*! no static exports found */ /***/ (function(module, exports, __webpack_require__) { "use strict"; eval("\n\nObject.defineProperty(exports, \"__esModule\", {\n value: true\n});\n\nvar _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if (\"value\" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); /* global d3 */\n\nvar _array = __webpack_require__(/*! ../node/array.js */ \"./src/node/array.js\");\n\nvar _array2 = _interopRequireDefault(_array);\n\nfunction _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }\n\nfunction _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError(\"Cannot call a class as a function\"); } }\n\nvar MARGIN = 10;\nvar HEIGHT = 50;\nvar DURATION = 1000;\n\nvar diagonal = d3.svg.diagonal();\n\nvar TreeLayout = function () {\n function TreeLayout() {\n var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};\n\n _classCallCheck(this, TreeLayout);\n\n this.height = options.height || HEIGHT;\n }\n\n _createClass(TreeLayout, [{\n key: 'layout',\n value: function layout(node) {\n calcChildrenWidth(node);\n calcTotalSize(node, this.height);\n\n _layout(node, this.height);\n }\n }, {\n key: 'renderLinks',\n value: function renderLinks(node) {\n _renderLinks(node);\n }\n }]);\n\n return TreeLayout;\n}();\n\nexports.default = TreeLayout;\n\n\nfunction _layout(node, height) {\n if (node.children.length === 0) {\n return;\n }\n\n var x = Math.round(node.width / 2) - Math.round(node.childrenWidth / 2);\n var y = node.height + height;\n\n node.children.forEach(function (child) {\n child.x = x + Math.round(child.totalWidth / 2) - Math.round(child.width / 2);\n child.y = y;\n\n child.g.transition().duration(DURATION).attr('transform', 'translate(' + child.x + ',' + child.y + ')');\n\n x += child.totalWidth + MARGIN;\n });\n}\n\nfunction _renderLinks(node) {\n var src = {\n x: node.linkX,\n y: node.linkY\n };\n\n node.children.forEach(function (child) {\n var dst = {\n x: child.x + child.linkX,\n y: child.y + child.linkY\n };\n\n if (child.constructor === _array2.default) {\n if (child.children.length !== 0) {\n var gc = child.children[0];\n dst.x += gc.x + gc.linkX;\n dst.y += gc.y + gc.linkY;\n }\n }\n\n var link = node.g.insert('path', ':first-child').attr('class', 'vtree-link').attr('d', function () {\n return diagonal({ source: src, target: src });\n });\n\n link.transition().duration(DURATION).attr('d', function () {\n return diagonal({ source: src, target: dst });\n });\n });\n}\n\nfunction calcChildrenWidth(node) {\n if (node.children.length === 0) {\n node.childrenWidth = 0;\n\n return;\n }\n\n var w = 0;\n\n node.children.forEach(function (child) {\n w += child.totalWidth;\n });\n\n w += (node.children.length - 1) * MARGIN;\n\n node.childrenWidth = w;\n}\n\nfunction calcTotalSize(node, height) {\n if (node.children.length === 0) {\n node.totalWidth = node.width;\n node.totalHeight = node.height;\n\n return;\n }\n\n node.totalWidth = Math.max(node.width, node.childrenWidth);\n\n var maxChildH = 0;\n\n node.children.forEach(function (child) {\n maxChildH = Math.max(maxChildH, child.totalHeight);\n });\n\n node.totalHeight = node.height + height + maxChildH;\n}\n\n//# sourceURL=webpack://VTree/./src/layout/tree.js?"); /***/ }), /***/ "./src/node/array.js": /*!***************************!*\ !*** ./src/node/array.js ***! \***************************/ /*! no static exports found */ /***/ (function(module, exports, __webpack_require__) { "use strict"; eval("\n\nObject.defineProperty(exports, \"__esModule\", {\n value: true\n});\n\nvar _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if (\"value\" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();\n\nvar _node = __webpack_require__(/*! ./node.js */ \"./src/node/node.js\");\n\nvar _node2 = _interopRequireDefault(_node);\n\nvar _array = __webpack_require__(/*! ../layout/array.js */ \"./src/layout/array.js\");\n\nvar _array2 = _interopRequireDefault(_array);\n\nfunction _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }\n\nfunction _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError(\"Cannot call a class as a function\"); } }\n\nfunction _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError(\"this hasn't been initialised - super() hasn't been called\"); } return call && (typeof call === \"object\" || typeof call === \"function\") ? call : self; }\n\nfunction _inherits(subClass, superClass) { if (typeof superClass !== \"function\" && superClass !== null) { throw new TypeError(\"Super expression must either be null or a function, not \" + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }\n\nvar defaultLayout = new _array2.default();\n\nvar MARGIN = 10;\n\nvar ArrayNode = function (_Node) {\n _inherits(ArrayNode, _Node);\n\n function ArrayNode(nodes, layout) {\n var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};\n\n _classCallCheck(this, ArrayNode);\n\n if (!layout) {\n layout = defaultLayout;\n }\n\n var _this = _possibleConstructorReturn(this, (ArrayNode.__proto__ || Object.getPrototypeOf(ArrayNode)).call(this, null, nodes, layout));\n\n _this.width = 0;\n _this.height = 0;\n\n _this.linkX = 0;\n _this.linkY = 0;\n\n _this.margin = options.margin || MARGIN;\n return _this;\n }\n\n _createClass(ArrayNode, [{\n key: '_render',\n value: function _render() {}\n }]);\n\n return ArrayNode;\n}(_node2.default);\n\nexports.default = ArrayNode;\n\n//# sourceURL=webpack://VTree/./src/node/array.js?"); /***/ }), /***/ "./src/node/dummy.js": /*!***************************!*\ !*** ./src/node/dummy.js ***! \***************************/ /*! no static exports found */ /***/ (function(module, exports, __webpack_require__) { "use strict"; eval("\n\nObject.defineProperty(exports, \"__esModule\", {\n value: true\n});\n\nvar _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if (\"value\" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();\n\nvar _node = __webpack_require__(/*! ./node.js */ \"./src/node/node.js\");\n\nvar _node2 = _interopRequireDefault(_node);\n\nfunction _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }\n\nfunction _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError(\"Cannot call a class as a function\"); } }\n\nfunction _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError(\"this hasn't been initialised - super() hasn't been called\"); } return call && (typeof call === \"object\" || typeof call === \"function\") ? call : self; }\n\nfunction _inherits(subClass, superClass) { if (typeof superClass !== \"function\" && superClass !== null) { throw new TypeError(\"Super expression must either be null or a function, not \" + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }\n\nvar DummyNode = function (_Node) {\n _inherits(DummyNode, _Node);\n\n function DummyNode(child) {\n _classCallCheck(this, DummyNode);\n\n var _this = _possibleConstructorReturn(this, (DummyNode.__proto__ || Object.getPrototypeOf(DummyNode)).call(this, null, [child]));\n\n _this.r = 4;\n return _this;\n }\n\n _createClass(DummyNode, [{\n key: '_render',\n value: function _render(g) {\n g.append('circle').attr('cx', this.r).attr('cy', this.r).attr('r', this.r);\n\n this.width = this.r * 2;\n this.height = this.r * 2;\n\n this.linkX = this.r;\n this.linkY = this.r;\n }\n }]);\n\n return DummyNode;\n}(_node2.default);\n\nexports.default = DummyNode;\n\n//# sourceURL=webpack://VTree/./src/node/dummy.js?"); /***/ }), /***/ "./src/node/node.js": /*!**************************!*\ !*** ./src/node/node.js ***! \**************************/ /*! no static exports found */ /***/ (function(module, exports, __webpack_require__) { "use strict"; eval("\n\nObject.defineProperty(exports, \"__esModule\", {\n value: true\n});\n\nvar _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if (\"value\" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();\n\nfunction _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError(\"Cannot call a class as a function\"); } }\n\n/**\n * Node\n *\n * A tree consists of nodes and links.\n * A node consists of the following fields:\n *\n * - id : auto increment ID.\n * - data : data which may be displayed.\n * The data format is determined by the renderer.\n * - children : children of the node\n */\n\nvar curMaxId = 0;\n\nvar Node = function () {\n function Node(data, children, layout) {\n _classCallCheck(this, Node);\n\n this.id = ++curMaxId;\n this.data = data;\n this.children = children;\n\n if (layout) {\n this.layout = layout;\n }\n\n this.width = 0;\n this.height = 0;\n\n this.decorators = [];\n }\n\n _createClass(Node, [{\n key: 'render',\n value: function render(g) {\n var _this = this;\n\n if (this.decorators.length === 0) {\n this._render(g);\n return;\n }\n\n var prevG = g.append('g');\n this._render(prevG);\n\n this.decorators.forEach(function (decorator) {\n var newG = g.append('g');\n\n var dbbox = decorator.render(newG, prevG, _this.width, _this.height);\n\n if (dbbox.dw || dbbox.dh) {\n _this.width += dbbox.dw;\n _this.height += dbbox.dh;\n }\n\n if (dbbox.dx || dbbox.dy) {\n prevG.attr('transform', 'translate(' + dbbox.dx + ',' + dbbox.dy + ')');\n\n _this.linkX += dbbox.dx;\n _this.linkY += dbbox.dy;\n }\n\n prevG = newG;\n });\n }\n }, {\n key: '_render',\n value: function _render() {\n throw new Error('[no overwride errror] _render is not implemented.');\n }\n }]);\n\n return Node;\n}();\n\nexports.default = Node;\n\n//# sourceURL=webpack://VTree/./src/node/node.js?"); /***/ }), /***/ "./src/node/string.js": /*!****************************!*\ !*** ./src/node/string.js ***! \****************************/ /*! no static exports found */ /***/ (function(module, exports, __webpack_require__) { "use strict"; eval("\n\nObject.defineProperty(exports, \"__esModule\", {\n value: true\n});\n\nvar _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if (\"value\" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();\n\nvar _node = __webpack_require__(/*! ./node.js */ \"./src/node/node.js\");\n\nvar _node2 = _interopRequireDefault(_node);\n\nvar _util = __webpack_require__(/*! ./util.js */ \"./src/node/util.js\");\n\nfunction _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }\n\nfunction _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError(\"Cannot call a class as a function\"); } }\n\nfunction _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError(\"this hasn't been initialised - super() hasn't been called\"); } return call && (typeof call === \"object\" || typeof call === \"function\") ? call : self; }\n\nfunction _inherits(subClass, superClass) { if (typeof superClass !== \"function\" && superClass !== null) { throw new TypeError(\"Super expression must either be null or a function, not \" + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }\n\nvar StringNode = function (_Node) {\n _inherits(StringNode, _Node);\n\n function StringNode(data) {\n _classCallCheck(this, StringNode);\n\n var _this = _possibleConstructorReturn(this, (StringNode.__proto__ || Object.getPrototypeOf(StringNode)).call(this, data, []));\n\n _this.textPad = 4;\n return _this;\n }\n\n _createClass(StringNode, [{\n key: '_render',\n value: function _render(g) {\n var bbox = (0, _util.appendRectText)(g, 0, 0, this.data, this.textPad);\n\n this.width = bbox.width;\n this.height = bbox.height;\n\n this.linkX = Math.round(this.width / 2);\n this.linkY = Math.round(this.height / 2);\n }\n }]);\n\n return StringNode;\n}(_node2.default);\n\nexports.default = StringNode;\n\n//# sourceURL=webpack://VTree/./src/node/string.js?"); /***/ }), /***/ "./src/node/table.js": /*!***************************!*\ !*** ./src/node/table.js ***! \***************************/ /*! no static exports found */ /***/ (function(module, exports, __webpack_require__) { "use strict"; eval("\n\nObject.defineProperty(exports, \"__esModule\", {\n value: true\n});\n\nvar _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if (\"value\" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();\n\nvar _node = __webpack_require__(/*! ./node.js */ \"./src/node/node.js\");\n\nvar _node2 = _interopRequireDefault(_node);\n\nvar _util = __webpack_require__(/*! ./util.js */ \"./src/node/util.js\");\n\nfunction _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }\n\nfunction _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError(\"Cannot call a class as a function\"); } }\n\nfunction _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError(\"this hasn't been initialised - super() hasn't been called\"); } return call && (typeof call === \"object\" || typeof call === \"function\") ? call : self; }\n\nfunction _inherits(subClass, superClass) { if (typeof superClass !== \"function\" && superClass !== null) { throw new TypeError(\"Super expression must either be null or a function, not \" + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }\n\nvar TableNode = function (_Node) {\n _inherits(TableNode, _Node);\n\n function TableNode(data, children) {\n _classCallCheck(this, TableNode);\n\n var _this = _possibleConstructorReturn(this, (TableNode.__proto__ || Object.getPrototypeOf(TableNode)).call(this, data, children));\n\n _this.textPad = 4;\n return _this;\n }\n\n _createClass(TableNode, [{\n key: '_render',\n value: function _render(g) {\n if (this.data.length === 0 || this.data[0].length === 0) {\n return;\n }\n\n var tbl = this.renderCells(g);\n var size = this.layoutCells(tbl);\n\n this.width = size.width;\n this.height = size.height;\n\n this.linkX = Math.round(this.width / 2);\n this.linkY = Math.round(this.height / 2);\n }\n }, {\n key: 'renderCells',\n value: function renderCells(g) {\n var _this2 = this;\n\n var tbl = [];\n\n this.data.forEach(function (row) {\n var tblRow = [];\n\n row.forEach(function (col) {\n var colG = g.append('g');\n var bbox = (0, _util.appendRectText)(colG, 0, 0, col, _this2.textPad);\n\n tblRow.push({\n g: colG,\n bbox: bbox\n });\n });\n\n tbl.push(tblRow);\n });\n\n return tbl;\n }\n }, {\n key: 'layoutCells',\n value: function layoutCells(tbl) {\n var maxW = this.calcMaxWidths(tbl);\n var maxH = this.calcMaxHeights(tbl);\n\n var x = 0;\n var y = 0;\n\n tbl.forEach(function (row, rowI) {\n x = 0;\n\n row.forEach(function (col, colI) {\n col.g.attr('transform', 'translate(' + x + ',' + y + ')');\n col.g.select('rect').attr('width', maxW[colI]).attr('height', maxH[rowI]);\n\n x += maxW[colI];\n });\n\n y += maxH[rowI];\n });\n\n return { width: x, height: y };\n }\n }, {\n key: 'calcMaxWidths',\n value: function calcMaxWidths(tbl) {\n var maxW = [];\n var colI, rowI;\n\n for (colI = 0; colI < tbl[0].length; colI++) {\n var w = 0;\n\n for (rowI = 0; rowI < tbl.length; rowI++) {\n w = Math.max(w, tbl[rowI][colI].bbox.width);\n }\n\n maxW.push(w);\n }\n\n return maxW;\n }\n }, {\n key: 'calcMaxHeights',\n value: function calcMaxHeights(tbl) {\n var maxH = [];\n\n tbl.forEach(function (row) {\n var h = 0;\n\n row.forEach(function (col) {\n h = Math.max(h, col.bbox.height);\n });\n\n maxH.push(h);\n });\n\n return maxH;\n }\n }]);\n\n return TableNode;\n}(_node2.default);\n\nexports.default = TableNode;\n\n//# sourceURL=webpack://VTree/./src/node/table.js?"); /***/ }), /***/ "./src/node/util.js": /*!**************************!*\ !*** ./src/node/util.js ***! \**************************/ /*! no static exports found */ /***/ (function(module, exports, __webpack_require__) { "use strict"; eval("\n\nObject.defineProperty(exports, \"__esModule\", {\n value: true\n});\n\nvar _typeof = typeof Symbol === \"function\" && typeof Symbol.iterator === \"symbol\" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === \"function\" && obj.constructor === Symbol && obj !== Symbol.prototype ? \"symbol\" : typeof obj; };\n\nexports.appendRectText = appendRectText;\n\nfunction _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError(\"Cannot call a class as a function\"); } }\n\nvar MAX_LEN = 32;\n\nvar BBox = exports.BBox = function BBox() {\n var x = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;\n var y = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;\n var width = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;\n var height = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 0;\n\n _classCallCheck(this, BBox);\n\n this.x = x;\n this.y = y;\n this.width = width;\n this.height = height;\n};\n\nfunction appendRectText(g, x, y, text, pad) {\n if (typeof text === 'string' && text.length > MAX_LEN) {\n text = text.substr(0, MAX_LEN) + '...';\n }\n\n var rect = g.append('rect').attr('class', getClassName(text));\n\n var t = g.append('text').text(text);\n\n var b = t.node().getBBox();\n var w = Math.ceil(b.width);\n var h = Math.ceil(b.height);\n\n t.attr('x', x + pad).attr('y', y + pad + h);\n\n var bbox = new BBox(x, y, w + pad * 2, h + pad * 2);\n\n rect.attr('x', bbox.x).attr('y', bbox.y).attr('width', bbox.width).attr('height', bbox.height);\n\n return bbox;\n}\n\nfunction getClassName(d) {\n var name = '';\n var type = typeof d === 'undefined' ? 'undefined' : _typeof(d);\n\n if (d === null) {\n return 'null-text';\n } else if (type === 'string') {\n name = 'string-text';\n } else if (type === 'number') {\n name = 'number-text';\n } else if (type === 'boolean') {\n name = 'boolean-text';\n } else {\n name = 'unknown-text';\n }\n\n return name;\n}\n\n//# sourceURL=webpack://VTree/./src/node/util.js?"); /***/ }), /***/ "./src/reader/object.js": /*!******************************!*\ !*** ./src/reader/object.js ***! \******************************/ /*! no static exports found */ /***/ (function(module, exports, __webpack_require__) { "use strict"; eval("\n\nObject.defineProperty(exports, \"__esModule\", {\n value: true\n});\n\nvar _typeof = typeof Symbol === \"function\" && typeof Symbol.iterator === \"symbol\" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === \"function\" && obj.constructor === Symbol && obj !== Symbol.prototype ? \"symbol\" : typeof obj; };\n\nvar _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if (\"value\" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();\n\nvar _string = __webpack_require__(/*! ../node/string.js */ \"./src/node/string.js\");\n\nvar _string2 = _interopRequireDefault(_string);\n\nvar _array = __webpack_require__(/*! ../node/array.js */ \"./src/node/array.js\");\n\nvar _array2 = _interopRequireDefault(_array);\n\nvar _table = __webpack_require__(/*! ../node/table.js */ \"./src/node/table.js\");\n\nvar _table2 = _interopRequireDefault(_table);\n\nvar _dummy = __webpack_require__(/*! ../node/dummy.js */ \"./src/node/dummy.js\");\n\nvar _dummy2 = _interopRequireDefault(_dummy);\n\nvar _linkName = __webpack_require__(/*! ../decorator/linkName.js */ \"./src/decorator/linkName.js\");\n\nvar _linkName2 = _interopRequireDefault(_linkName);\n\nfunction _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }\n\nfunction _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError(\"Cannot call a class as a function\"); } }\n\nvar ObjectReader = function () {\n function ObjectReader() {\n _classCallCheck(this, ObjectReader);\n }\n\n _createClass(ObjectReader, [{\n key: 'read',\n value: function read(obj) {\n return obj2node(obj, '');\n }\n }]);\n\n return ObjectReader;\n}();\n\nexports.default = ObjectReader;\n\n\nfunction obj2node(obj, linkName) {\n var node;\n\n if (isPrimitive(obj)) {\n node = new _string2.default(obj);\n } else if (Array.isArray(obj)) {\n var nodes = [];\n\n obj.forEach(function (item, i) {\n if (Array.isArray(item)) {\n node = new _dummy2.default(obj2node(item, ''));\n node.decorators.push(new _linkName2.default(linkName + '[' + i + ']'));\n nodes.push(node);\n } else {\n node = obj2node(item, linkName + '[' + i + ']');\n nodes.push(node);\n }\n });\n\n // empty array\n if (nodes.length === 0) {\n node = obj2node(null, linkName + '[]');\n nodes.push(node);\n }\n\n node = new _array2.default(nodes);\n } else {\n var name;\n var tbl = [];\n var children = [];\n\n for (name in obj) {\n if (!Object.prototype.hasOwnProperty.call(obj, name)) {\n continue;\n }\n\n var data = obj[name];\n\n if (isPrimitive(data)) {\n tbl.push([name, data]);\n } else {\n children.push(obj2node(data, name));\n }\n }\n\n if (tbl.length === 0) {\n node = new _table2.default([[' ', ' ']], children);\n } else {\n node = new _table2.default(tbl, children);\n }\n }\n\n if (linkName !== '' && node.constructor !== _array2.default) {\n node.decorators.push(new _linkName2.default(linkName));\n }\n\n return node;\n}\n\nfunction isPrimitive(d) {\n var type = typeof d === 'undefined' ? 'undefined' : _typeof(d);\n\n if (d === null || type === 'string' || type === 'number' || type === 'boolean') {\n return true;\n }\n\n return false;\n}\n\n//# sourceURL=webpack://VTree/./src/reader/object.js?"); /***/ }), /***/ "./src/util.js": /*!*********************!*\ !*** ./src/util.js ***! \*********************/ /*! no static exports found */ /***/ (function(module, exports, __webpack_require__) { "use strict"; eval("\n\nObject.defineProperty(exports, \"__esModule\", {\n value: true\n});\nexports.visitBefore = visitBefore;\nexports.visitAfter = visitAfter;\nfunction visitBefore(node, callback) {\n var nodes = [node];\n\n while (nodes.length !== 0) {\n node = nodes.pop();\n\n callback(node);\n\n var children = node.children;\n\n if (children) {\n var n = children.length;\n\n while (--n >= 0) {\n nodes.push(children[n]);\n }\n }\n }\n}\n\nfunction visitAfter(node, callback) {\n var nodes = [node];\n var nodes2 = [];\n\n while (nodes.length !== 0) {\n node = nodes.pop();\n\n nodes2.push(node);\n\n var children = node.children;\n\n if (children) {\n var i = -1;\n var n = children.length;\n\n while (++i < n) {\n nodes.push(children[i]);\n }\n }\n }\n\n while (nodes2.length !== 0) {\n node = nodes2.pop();\n\n callback(node);\n }\n}\n\n//# sourceURL=webpack://VTree/./src/util.js?"); /***/ }), /***/ "./src/vtree.js": /*!**********************!*\ !*** ./src/vtree.js ***! \**********************/ /*! no static exports found */ /***/ (function(module, exports, __webpack_require__) { "use strict"; eval("\n\nvar _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if (\"value\" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); /* global d3 */\n\nvar _node = __webpack_require__(/*! ./node/node.js */ \"./src/node/node.js\");\n\nvar _node2 = _interopRequireDefault(_node);\n\nvar _string = __webpack_require__(/*! ./node/string.js */ \"./src/node/string.js\");\n\nvar _string2 = _interopRequireDefault(_string);\n\nvar _table = __webpack_require__(/*! ./node/table.js */ \"./src/node/table.js\");\n\nvar _table2 = _interopRequireDefault(_table);\n\nvar _array = __webpack_require__(/*! ./node/array.js */ \"./src/node/array.js\");\n\nvar _array2 = _interopRequireDefault(_array);\n\nvar _tree = __webpack_require__(/*! ./layout/tree.js */ \"./src/layout/tree.js\");\n\nvar _tree2 = _interopRequireDefault(_tree);\n\nvar _array3 = __webpack_require__(/*! ./layout/array.js */ \"./src/layout/array.js\");\n\nvar _array4 = _interopRequireDefault(_array3);\n\nvar _object = __webpack_require__(/*! ./reader/object.js */ \"./src/reader/object.js\");\n\nvar _object2 = _interopRequireDefault(_object);\n\nvar _util = __webpack_require__(/*! ./util.js */ \"./src/util.js\");\n\nfunction _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }\n\nfunction _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError(\"Cannot call a class as a function\"); } }\n\nvar WIDTH = 960;\nvar HEIGHT = 800;\nvar MARGIN = 20;\n\nvar DEFAULT_TREE_LAYOUT_HEIGHT = 50;\nvar DEBUG_TREE_LAYOUT_HEIGHT = 100;\n\nvar style = '\\n.vtree-node text { font: 14px sans-serif; }\\n.vtree-link { fill: none; stroke: #888; stroke-width: 2px; }\\n.vtree-table { stroke-width: 2px; stroke: steelblue; }\\npath.vtree-table { fill: white; }\\ng.vtree-node rect { fill: white; stroke: black; stroke-width: 1px; }\\ng.vtree-node rect.number-text { fill: #d8f0ed; }\\ng.vtree-node rect.string-text { fill: #e7f0db; }\\ng.vtree-node rect.boolean-text { fill: #e1d8f0; }\\ng.vtree-node rect.null-text { fill: #888; }\\n';\n\nvar VTree = function () {\n function VTree(container) {\n var _this = this;\n\n _classCallCheck(this, VTree);\n\n this.root = new _array2.default([], new _array4.default({ hideLinks: true }));\n this.defaultLayout = new _tree2.default({ height: DEFAULT_TREE_LAYOUT_HEIGHT });\n this.container = container;\n this._width = WIDTH;\n this._height = HEIGHT;\n this._debug = false;\n\n this.d3 = {};\n\n this.d3.container = d3.select(this.container);\n\n this.d3.zoomListener = d3.behavior.zoom().scaleExtent([0.1, 10]).on('zoom', function () {\n var e = d3.event;\n\n if (_this.d3.g) {\n _this.d3.g.attr('transform', 'translate(' + e.translate + ')scale(' + e.scale + ')');\n }\n });\n\n this.d3.svg = this.d3.container.append('svg').attr('class', 'vtree').attr('width', this._width).attr('height', this._height).call(this.d3.zoomListener);\n }\n\n _createClass(VTree, [{\n key: 'width',\n value: function width(_width) {\n if (arguments.length === 0) {\n return this._width;\n }\n\n this._width = _width;\n\n this.d3.container.select('svg').attr('width', _width);\n\n return this;\n }\n }, {\n key: 'height',\n value: function height(_height) {\n if (arguments.length === 0) {\n return this._height;\n }\n\n this._height = _height;\n\n this.d3.container.select('svg').attr('height', _height);\n\n return this;\n }\n }, {\n key: 'debug',\n value: function debug(_debug) {\n if (arguments.length === 0) {\n return this._debug;\n }\n\n if (_debug) {\n this.defaultLayout.height = DEBUG_TREE_LAYOUT_HEIGHT;\n } else {\n this.defaultLayout.height = DEFAULT_TREE_LAYOUT_HEIGHT;\n }\n\n this._debug = _debug;\n\n return this;\n }\n }, {\n key: 'data',\n value: function data(_data) {\n if (Array.isArray(_data)) {\n this.root.children = _data;\n } else {\n this.root.children = [_data];\n }\n\n return this;\n }\n }, {\n key: 'createSvgString',\n value: function createSvgString() {\n var svg = this.d3.svg.node();\n var serializer = new XMLSerializer();\n var source = serializer.serializeToString(svg);\n\n if (!source.match(/^]+xmlns=\"http:\\/\\/www\\.w3\\.org\\/2000\\/svg\"/)) {\n source = source.replace(/^]+\"http:\\/\\/www\\.w3\\.org\\/1999\\/xlink\"/)) {\n source = source.replace(/^ Online JSON to Tree Diagram Converter

Online JSON to Tree Diagram Converter

If you want to export the diagram as a svg file, copy the following text into a text editor and save the file with a .svg extension.

Created Date: 2014-07-27, Modified Date: 2018-11-01
================================================ FILE: package.json ================================================ { "name": "vtree", "version": "0.3.0", "description": "display json data in a tree diagram.", "main": "build/vtree.js", "scripts": { "build": "webpack" }, "repository": { "type": "git", "url": "git+https://github.com/ivan111/vtree.git" }, "keywords": [ "json", "tree" ], "author": "ivan111", "license": "Apache License Version 2", "bugs": { "url": "https://github.com/ivan111/vtree/issues" }, "homepage": "https://github.com/ivan111/vtree#readme", "devDependencies": { "babel-core": "^6.23.1", "babel-loader": "^7", "babel-preset-es2015": "^6.22.0", "eslint": "^5.8.0", "eslint-loader": "^2.1.1", "webpack": "^4.23.1", "webpack-cli": "^3.1.2" } } ================================================ FILE: src/decorator/linkName.js ================================================ const pad = 4; export default class LinkNameDecorator { constructor(linkName) { this.linkName = linkName; } render(g, oldG, width) { const t = g.append('text') .text(this.linkName); const b = t.node().getBBox(); const textW = Math.ceil(b.width); const textH = Math.ceil(b.height); const textTotalW = textW + pad * 2; var newW = width; if (textTotalW > width) { newW = textTotalW; } const textTotalH = textH + pad; const dw = newW - width; const dh = textTotalH; const dx = Math.round(dw / 2); const dy = textTotalH; t .attr('x', Math.round(newW / 2)) .attr('y', textH) .attr('text-anchor', 'middle'); return { dx, dy, dw, dh }; } } ================================================ FILE: src/layout/array.js ================================================ /* global d3 */ const DURATION = 1000; const diagonal = d3.svg.diagonal(); export default class ArrayLayout { constructor(options={}) { this.hideLinks = options.hideLinks; } layout(node) { calcChildrenWidth(node); calcTotalSize(node); layout(node); } renderLinks(node) { if (!this.hideLinks) { renderLinks(node); } } } function layout(node) { if (node.children.length === 0) { return; } var x = -Math.round(node.childrenWidth / 2); const y = 0; node.children.forEach((child) => { child.x = x + Math.round(child.totalWidth / 2) - Math.round(child.width / 2); child.y = y; child.g .transition() .duration(DURATION) .attr('transform', `translate(${child.x},${child.y})`); x += child.totalWidth + node.margin; }); } function renderLinks(node) { if (node.children.length === 0) { return; } var h = node.children[0].linkY; node.children.forEach((child) => { h = Math.min(h, child.linkY); }); const orig = { x: 0, y: 0 }; var i, src, dst; for (i = 0; i < node.children.length; i++) { if (i === 0) { continue; } const prev = node.children[i - 1]; const child = node.children[i]; src = { x: prev.x + prev.linkX, y: prev.y + h }; dst = { x: child.x + child.linkX, y: child.y + h }; const link = node.g.insert('path', ':first-child') .attr('class', 'vtree-link') .attr('d', function () { return diagonal({ source: orig, target: orig }); }); link .transition() .duration(DURATION) .attr('d', function () { return diagonal({ source: src, target: dst }); }); } } function calcChildrenWidth(node) { if (node.children.length === 0) { node.childrenWidth = 0; return; } var w = 0; node.children.forEach((child) => { w += child.totalWidth; }); w += (node.children.length - 1) * node.margin; node.childrenWidth = w; } function calcTotalSize(node) { if (node.children.length === 0) { node.totalWidth = node.width; node.totalHeight = node.height; return; } node.totalWidth = Math.max(node.width, node.childrenWidth); var maxChildH = 0; node.children.forEach((child) => { maxChildH = Math.max(maxChildH, child.totalHeight); }); node.totalHeight = maxChildH; } ================================================ FILE: src/layout/tree.js ================================================ /* global d3 */ import ArrayNode from '../node/array.js'; const MARGIN = 10; const HEIGHT = 50; const DURATION = 1000; const diagonal = d3.svg.diagonal(); export default class TreeLayout { constructor(options={}) { this.height = options.height || HEIGHT; } layout(node) { calcChildrenWidth(node); calcTotalSize(node, this.height); layout(node, this.height); } renderLinks(node) { renderLinks(node); } } function layout(node, height) { if (node.children.length === 0) { return; } var x = Math.round(node.width / 2) - Math.round(node.childrenWidth / 2); const y = node.height + height; node.children.forEach((child) => { child.x = x + Math.round(child.totalWidth / 2) - Math.round(child.width / 2); child.y = y; child.g .transition() .duration(DURATION) .attr('transform', `translate(${child.x},${child.y})`); x += child.totalWidth + MARGIN; }); } function renderLinks(node) { const src = { x: node.linkX, y: node.linkY }; node.children.forEach((child) => { const dst = { x: child.x + child.linkX, y: child.y + child.linkY }; if (child.constructor === ArrayNode) { if (child.children.length !== 0) { const gc = child.children[0] dst.x += gc.x + gc.linkX; dst.y += gc.y + gc.linkY; } } const link = node.g.insert('path', ':first-child') .attr('class', 'vtree-link') .attr('d', function () { return diagonal({ source: src, target: src }); }); link .transition() .duration(DURATION) .attr('d', function () { return diagonal({ source: src, target: dst }); }); }); } function calcChildrenWidth(node) { if (node.children.length === 0) { node.childrenWidth = 0; return; } var w = 0; node.children.forEach((child) => { w += child.totalWidth; }); w += (node.children.length - 1) * MARGIN; node.childrenWidth = w; } function calcTotalSize(node, height) { if (node.children.length === 0) { node.totalWidth = node.width; node.totalHeight = node.height; return; } node.totalWidth = Math.max(node.width, node.childrenWidth); var maxChildH = 0; node.children.forEach((child) => { maxChildH = Math.max(maxChildH, child.totalHeight); }); node.totalHeight = node.height + height + maxChildH; } ================================================ FILE: src/node/README.md ================================================ # How to make a custom node 1. extend the Node class ```js export default class MyNode extends Node { ... } ``` 2. create the constructor functio and call super in it. ```js constructor(data, children) { super(data, children); ... } ``` 3. write a custom \_render function. And set this.width, this.height, this.linkX and this.linkY in it. (linkX, linkY) is a link coord. ```js _render(g) { ... this.width = ...; this.height = ...; this.linkX = ...; this.linkY = ...; } ``` ================================================ FILE: src/node/array.js ================================================ import Node from './node.js'; import ArrayLayout from '../layout/array.js'; const defaultLayout = new ArrayLayout(); const MARGIN = 10; export default class ArrayNode extends Node { constructor(nodes, layout, options={}) { if (!layout) { layout = defaultLayout; } super(null, nodes, layout); this.width = 0; this.height = 0; this.linkX = 0; this.linkY = 0; this.margin = options.margin || MARGIN; } _render() { } } ================================================ FILE: src/node/dummy.js ================================================ import Node from './node.js'; export default class DummyNode extends Node { constructor(child) { super(null, [child]); this.r = 4; } _render(g) { g.append('circle') .attr('cx', this.r) .attr('cy', this.r) .attr('r', this.r); this.width = this.r * 2; this.height = this.r * 2; this.linkX = this.r; this.linkY = this.r; } } ================================================ FILE: src/node/node.js ================================================ /** * Node * * A tree consists of nodes and links. * A node consists of the following fields: * * - id : auto increment ID. * - data : data which may be displayed. * The data format is determined by the renderer. * - children : children of the node */ var curMaxId = 0; export default class Node { constructor(data, children, layout) { this.id = ++curMaxId; this.data = data; this.children = children; if (layout) { this.layout = layout; } this.width = 0; this.height = 0; this.decorators = []; } render(g) { if (this.decorators.length === 0) { this._render(g); return; } var prevG = g.append('g'); this._render(prevG); this.decorators.forEach((decorator) => { const newG = g.append('g'); const dbbox = decorator.render(newG, prevG, this.width, this.height); if (dbbox.dw || dbbox.dh) { this.width += dbbox.dw; this.height += dbbox.dh; } if (dbbox.dx || dbbox.dy) { prevG.attr('transform', `translate(${dbbox.dx},${dbbox.dy})`); this.linkX += dbbox.dx; this.linkY += dbbox.dy; } prevG = newG; }); } _render() { throw new Error('[no overwride errror] _render is not implemented.'); } } ================================================ FILE: src/node/string.js ================================================ import Node from './node.js'; import { appendRectText } from './util.js'; export default class StringNode extends Node { constructor(data) { super(data, []); this.textPad = 4; } _render(g) { const bbox = appendRectText(g, 0, 0, this.data, this.textPad); this.width = bbox.width; this.height = bbox.height; this.linkX = Math.round(this.width / 2); this.linkY = Math.round(this.height / 2); } } ================================================ FILE: src/node/table.js ================================================ import Node from './node.js'; import { appendRectText } from './util.js'; export default class TableNode extends Node { constructor(data, children) { super(data, children); this.textPad = 4; } _render(g) { if (this.data.length === 0 || this.data[0].length === 0) { return; } const tbl = this.renderCells(g); const size = this.layoutCells(tbl); this.width = size.width; this.height = size.height; this.linkX = Math.round(this.width / 2); this.linkY = Math.round(this.height / 2); } renderCells(g) { const tbl = []; this.data.forEach((row) => { const tblRow = []; row.forEach((col) => { const colG = g.append('g'); const bbox = appendRectText(colG, 0, 0, col, this.textPad); tblRow.push({ g: colG, bbox: bbox }); }); tbl.push(tblRow); }); return tbl; } layoutCells(tbl) { const maxW = this.calcMaxWidths(tbl); const maxH = this.calcMaxHeights(tbl); var x = 0; var y = 0; tbl.forEach((row, rowI) => { x = 0; row.forEach((col, colI) => { col.g.attr('transform', `translate(${x},${y})`); col.g.select('rect') .attr('width', maxW[colI]) .attr('height', maxH[rowI]); x += maxW[colI]; }); y += maxH[rowI]; }); return { width: x, height: y }; } calcMaxWidths(tbl) { const maxW = []; var colI, rowI; for (colI = 0; colI < tbl[0].length; colI++) { var w = 0; for (rowI = 0; rowI < tbl.length; rowI++) { w = Math.max(w, tbl[rowI][colI].bbox.width); } maxW.push(w); } return maxW; } calcMaxHeights(tbl) { const maxH = []; tbl.forEach((row) => { var h = 0; row.forEach((col) => { h = Math.max(h, col.bbox.height); }); maxH.push(h); }); return maxH; } } ================================================ FILE: src/node/util.js ================================================ const MAX_LEN = 32; export class BBox { constructor(x=0, y=0, width=0, height=0) { this.x = x; this.y = y; this.width = width; this.height = height; } } export function appendRectText(g, x, y, text, pad) { if (typeof text === 'string' && text.length > MAX_LEN) { text = text.substr(0, MAX_LEN) + '...'; } const rect = g.append('rect') .attr('class', getClassName(text)); const t = g.append('text') .text(text); const b = t.node().getBBox(); const w = Math.ceil(b.width); const h = Math.ceil(b.height); t .attr('x', x + pad) .attr('y', y + pad + h); const bbox = new BBox(x, y, w + pad * 2, h + pad * 2); rect .attr('x', bbox.x) .attr('y', bbox.y) .attr('width', bbox.width) .attr('height', bbox.height); return bbox; } function getClassName(d) { var name = ''; const type = typeof d; if (d === null) { return 'null-text'; } else if (type === 'string') { name = 'string-text'; } else if (type === 'number') { name = 'number-text'; } else if (type === 'boolean') { name = 'boolean-text'; } else { name = 'unknown-text'; } return name; } ================================================ FILE: src/reader/object.js ================================================ import StringNode from '../node/string.js'; import ArrayNode from '../node/array.js'; import TableNode from '../node/table.js'; import DummyNode from '../node/dummy.js'; import LinkNameDecorator from '../decorator/linkName.js'; export default class ObjectReader { constructor() { } read(obj) { return obj2node(obj, ''); } } function obj2node(obj, linkName) { var node; if (isPrimitive(obj)) { node = new StringNode(obj); } else if (Array.isArray(obj)) { const nodes = []; obj.forEach((item, i) => { if (Array.isArray(item)) { node = new DummyNode(obj2node(item, '')); node.decorators.push(new LinkNameDecorator(`${linkName}[${i}]`)); nodes.push(node); } else { node = obj2node(item, `${linkName}[${i}]`); nodes.push(node); } }); // empty array if (nodes.length === 0) { node = obj2node(null, `${linkName}[]`); nodes.push(node); } node = new ArrayNode(nodes); } else { var name; var tbl = []; var children = []; for (name in obj) { if (!Object.prototype.hasOwnProperty.call(obj, name)) { continue; } var data = obj[name]; if (isPrimitive(data)) { tbl.push([name, data]); } else { children.push(obj2node(data, name)); } } if (tbl.length === 0) { node = new TableNode([[' ', ' ']], children); } else { node = new TableNode(tbl, children); } } if (linkName !== '' && node.constructor !== ArrayNode) { node.decorators.push(new LinkNameDecorator(linkName)); } return node; } function isPrimitive(d) { const type = typeof d; if (d === null || type === 'string' || type === 'number' || type === 'boolean') { return true; } return false; } ================================================ FILE: src/reader/test/object_reader_test.js ================================================ /* eslint-disable no-undef */ import Reader from '../object.js'; import StringNode from '../../node/string.js'; import ArrayNode from '../../node/array.js'; import TableNode from '../../node/table.js'; import DummyNode from '../../node/dummy.js'; import LinkNameDecorator from '../../decorator/linkName.js'; describe('test ObjectReader', function () { var r; beforeEach(function () { r = new Reader(); }); // read a primitive object it ('read a null', function () { var d = r.read(null); expect(d).to.be.instanceof(StringNode); expect(d.data).to.equal(null); expect(d.children).to.have.lengthOf(0); }); it ('read a boolean', function () { var d = r.read(true); expect(d).to.be.instanceof(StringNode); expect(d.data).to.equal(true); expect(d.children).to.have.lengthOf(0); d = r.read(false); expect(d).to.be.instanceof(StringNode); expect(d.data).to.equal(false); expect(d.children).to.have.lengthOf(0); }); it ('read a number', function () { var d = r.read(666); expect(d).to.be.instanceof(StringNode); expect(d.data).to.equal(666); expect(d.children).to.have.lengthOf(0); d = r.read(3.14); expect(d).to.be.instanceof(StringNode); expect(d.data).to.equal(3.14); expect(d.children).to.have.lengthOf(0); }); it ('read a string', function () { var d = r.read('JK business'); expect(d).to.be.instanceof(StringNode); expect(d.data).to.equal('JK business'); expect(d.children).to.have.lengthOf(0); d = r.read('お茶漬け'); expect(d).to.be.instanceof(StringNode); expect(d.data).to.equal('お茶漬け'); expect(d.children).to.have.lengthOf(0); }); // read a complex object it ('read an array', function () { var d = r.read([1, 2]); expect(d.data).to.equal(null); expect(d).to.be.instanceof(ArrayNode); expect(d.children).to.have.lengthOf(2); var c = d.children[0]; expect(c).to.be.instanceof(StringNode); expect(c.data).to.equal(1); expect(c.children).to.have.lengthOf(0); c = d.children[1]; expect(c).to.be.instanceof(StringNode); expect(c.data).to.equal(2); expect(c.children).to.have.lengthOf(0); }); it ('read a nested array', function () { var d = r.read([1, [2, 3]]); expect(d.data).to.equal(null); expect(d).to.be.instanceof(ArrayNode); expect(d.children).to.have.lengthOf(2); var c = d.children[0]; expect(c).to.be.instanceof(StringNode); expect(c.data).to.equal(1); expect(c.children).to.have.lengthOf(0); c = d.children[1]; expect(c).to.be.instanceof(DummyNode); expect(c.data).to.eql(null); expect(c.children).to.have.lengthOf(1); d = c.children[0]; expect(d).to.be.instanceof(ArrayNode); expect(d.data).to.equal(null); expect(d.children).to.have.lengthOf(2); c = d.children[0]; expect(c).to.be.instanceof(StringNode); expect(c.data).to.equal(2); expect(c.children).to.have.lengthOf(0); c = d.children[1]; expect(c).to.be.instanceof(StringNode); expect(c.data).to.equal(3); expect(c.children).to.have.lengthOf(0); }); it ('read an object', function () { var d = r.read({ x: 1, y: 2 }); expect(d).to.be.instanceof(TableNode); expect(d.data).to.eql([['x', 1], ['y', 2]]); expect(d.children).to.have.lengthOf(0); }); it ('read a nested object', function () { var d = r.read({ name: { first: 'Bob', last: 'Bach' }, age: 24 }); expect(d).to.be.instanceof(TableNode); expect(d.data).to.eql([['age', 24]]); expect(d.children).to.have.lengthOf(1); var c = d.children[0]; expect(c).to.be.instanceof(TableNode); expect(c.data).to.eql([['first', 'Bob'], ['last', 'Bach']]); expect(c.children).to.have.lengthOf(0); var deco = c.decorators[0]; expect(deco).to.be.instanceof(LinkNameDecorator); expect(deco.linkName).to.equal('name'); }); it ('read a nested object with no parent data', function () { var d = r.read({ name: { first: 'Bob', last: 'Bach' } }); expect(d).to.be.instanceof(TableNode); expect(d.data).to.eql([[' ', ' ']]); expect(d.children).to.have.lengthOf(1); var c = d.children[0]; expect(c).to.be.instanceof(TableNode); expect(c.data).to.eql([['first', 'Bob'], ['last', 'Bach']]); expect(c.children).to.have.lengthOf(0); var deco = c.decorators[0]; expect(deco).to.be.instanceof(LinkNameDecorator); expect(deco.linkName).to.equal('name'); }); it ('read an array containing an object', function () { var d = r.read([1, {val: 4}]); expect(d.data).to.equal(null); expect(d).to.be.instanceof(ArrayNode); expect(d.children).to.have.lengthOf(2); var c = d.children[0]; expect(c).to.be.instanceof(StringNode); expect(c.data).to.equal(1); expect(c.children).to.have.lengthOf(0); c = d.children[1]; expect(c).to.be.instanceof(TableNode); expect(c.data).to.eql([['val', 4]]); expect(c.children).to.have.lengthOf(0); }); it ('read an object containing an array', function () { var d = r.read({ name: 'Mike', vals: [2, 4] }); expect(d).to.be.instanceof(TableNode); expect(d.data).to.eql([['name', 'Mike']]); expect(d.children).to.have.lengthOf(1); var c = d.children[0]; expect(c).to.be.instanceof(ArrayNode); expect(c.data).to.equal(null); expect(c.children).to.have.lengthOf(2); d = c.children[0]; expect(d).to.be.instanceof(StringNode); expect(d.data).to.equal(2); expect(d.children).to.have.lengthOf(0); var deco = d.decorators[0]; expect(deco).to.be.instanceof(LinkNameDecorator); expect(deco.linkName).to.equal('vals[0]'); d = c.children[1]; expect(d).to.be.instanceof(StringNode); expect(d.data).to.equal(4); expect(d.children).to.have.lengthOf(0); deco = d.decorators[0]; expect(deco).to.be.instanceof(LinkNameDecorator); expect(deco.linkName).to.equal('vals[1]'); }); }); ================================================ FILE: src/util.js ================================================ export function visitBefore(node, callback) { const nodes = [node]; while (nodes.length !== 0) { node = nodes.pop(); callback(node); const children = node.children; if (children) { var n = children.length; while (--n >= 0) { nodes.push(children[n]); } } } } export function visitAfter(node, callback) { const nodes = [node]; const nodes2 = []; while (nodes.length !== 0) { node = nodes.pop(); nodes2.push(node); const children = node.children; if (children) { var i = -1; const n = children.length; while (++i < n) { nodes.push(children[i]); } } } while (nodes2.length !== 0) { node = nodes2.pop(); callback(node); } } ================================================ FILE: src/vtree.js ================================================ /* global d3 */ import Node from './node/node.js'; import StringNode from './node/string.js'; import TableNode from './node/table.js'; import ArrayNode from './node/array.js'; import TreeLayout from './layout/tree.js'; import ArrayLayout from './layout/array.js'; import ObjectReader from './reader/object.js'; import { visitAfter } from './util.js'; const WIDTH = 960; const HEIGHT = 800; const MARGIN = 20; const DEFAULT_TREE_LAYOUT_HEIGHT = 50; const DEBUG_TREE_LAYOUT_HEIGHT = 100; const style = ` .vtree-node text { font: 14px sans-serif; } .vtree-link { fill: none; stroke: #888; stroke-width: 2px; } .vtree-table { stroke-width: 2px; stroke: steelblue; } path.vtree-table { fill: white; } g.vtree-node rect { fill: white; stroke: black; stroke-width: 1px; } g.vtree-node rect.number-text { fill: #d8f0ed; } g.vtree-node rect.string-text { fill: #e7f0db; } g.vtree-node rect.boolean-text { fill: #e1d8f0; } g.vtree-node rect.null-text { fill: #888; } `; class VTree { constructor(container) { this.root = new ArrayNode([], new ArrayLayout({ hideLinks: true })); this.defaultLayout = new TreeLayout({height: DEFAULT_TREE_LAYOUT_HEIGHT}); this.container = container; this._width = WIDTH; this._height = HEIGHT; this._debug = false; this.d3 = {}; this.d3.container = d3.select(this.container); this.d3.zoomListener = d3.behavior.zoom() .scaleExtent([0.1, 10]) .on('zoom', () => { const e = d3.event; if (this.d3.g) { this.d3.g.attr('transform', `translate(${e.translate})scale(${e.scale})`); } }); this.d3.svg = this.d3.container.append('svg') .attr('class', 'vtree') .attr('width', this._width) .attr('height', this._height) .call(this.d3.zoomListener); } width(width) { if (arguments.length === 0) { return this._width; } this._width = width; this.d3.container.select('svg') .attr('width', width); return this; } height(height) { if (arguments.length === 0) { return this._height; } this._height = height; this.d3.container.select('svg') .attr('height', height); return this; } debug(debug) { if (arguments.length === 0) { return this._debug; } if (debug) { this.defaultLayout.height = DEBUG_TREE_LAYOUT_HEIGHT; } else { this.defaultLayout.height = DEFAULT_TREE_LAYOUT_HEIGHT; } this._debug = debug; return this; } data(data) { if (Array.isArray(data)) { this.root.children = data; } else { this.root.children = [data]; } return this; } createSvgString() { var svg = this.d3.svg.node(); var serializer = new XMLSerializer(); var source = serializer.serializeToString(svg); if (!source.match(/^]+xmlns="http:\/\/www\.w3\.org\/2000\/svg"/)) { source = source.replace(/^]+"http:\/\/www\.w3\.org\/1999\/xlink"/)) { source = source.replace(/^ { node.render(node.g); const layout = node.layout || this.defaultLayout; layout.layout(node); if (layout.renderLinks) { layout.renderLinks(node); } }); if (this._debug) { visitAfter(this.root, (node) => { this._debugDrawNodeInfo(node); }); } this.setRootPos(); return this; } setRootPos() { this.root.x = Math.round((this._width - this.root.width) / 2); this.root.y = Math.round((this._height - this.root.totalHeight) / 2); if (this.root.y < MARGIN) { this.root.y = MARGIN; } this.root.g.attr('transform', `translate(${this.root.x},${this.root.y})`); } _debugGetG() { if (!this._debug) { return; } var g = this.d3.svg.select('g.debug-info'); if (!g.empty()) { return g; } return this.d3.svg.append('g') .attr('class', 'debug-info'); } _debugDrawGrid() { if (!this._debug) { return; } const g = this._debugGetG(); g.append('line') .style('stroke', 'red') .attr('x1', this._width / 2) .attr('y1', 0) .attr('x2', this._width / 2) .attr('y2', this._height); g.append('line') .style('stroke', 'red') .attr('x1', 0) .attr('y1', this._height / 2) .attr('x2', this._width) .attr('y2', this._height / 2); } _debugDrawNodeInfo(node) { if (node.constructor === ArrayNode) { return; } // node rect node.g.append('rect') .style('fill', 'none') .style('stroke', 'tomato') .attr('x', -1) .attr('y', -1) .attr('width', node.width + 2) .attr('height', node.height + 2); // node total rect node.g.append('rect') .style('fill', 'none') .style('stroke', 'mediumpurple') .attr('x', (node.width - node.totalWidth) / 2) .attr('y', 0) .attr('width', node.totalWidth) .attr('height', node.totalHeight); // x, y const xy = node.g.append('text') .text(`x=${node.x} y=${node.y}`); const bbox = xy.node().getBBox(); var x = node.width / 2; var y = node.height + bbox.height + 2; xy .attr('x', x) .attr('y', y) .attr('text-anchor', 'middle'); y += bbox.height + 2; // width, height node.g.append('text') .text(`w=${node.width} h=${node.height}`) .attr('x', x) .attr('y', y) .attr('text-anchor', 'middle'); y += bbox.height + 2; // totalWidth, totalHeight node.g.append('text') .text(`tw=${node.totalWidth} th=${node.totalHeight}`) .attr('x', x) .attr('y', y) .attr('text-anchor', 'middle'); y += bbox.height + 2; // childrenWidth node.g.append('text') .text(`cw=${node.childrenWidth}`) .attr('x', x) .attr('y', y) .attr('text-anchor', 'middle'); } } VTree.node = {}; VTree.node.Node = Node; VTree.node.String = StringNode; VTree.node.Table = TableNode; VTree.node.Array = ArrayNode; VTree.layout = {}; VTree.layout.Tree = TreeLayout; VTree.layout.Array = ArrayLayout; VTree.reader = {}; VTree.reader.Object = ObjectReader; module.exports = VTree; ================================================ FILE: test.html ================================================ vtree tests

vtree tests

null
true
123
"黄 飛鴻"
[1234.56789, "ABCDE"]
{ "key": "value" }
{"name": "=" ,"children": [{"name": "id=range"},{"name": "class=range"}]}
{ "array": [ 1, 2, 3 ], "boolean": true, "null": null, "number": 123, "object": { "a": "b", "c": "d", "e": "f" }, "string": "Hello World" }
{ "type": "Feature", "geometry": { "type": "Point", "coordinates": [125.6, 10.1] }, "properties": { "name": "Dinagat Islands" } }
{"employees":[ {"firstName":"John", "lastName":"Doe"}, {"firstName":"Anna", "lastName":"Smith"}, {"firstName":"Peter", "lastName":"Jones"} ]}
{ "items": [ { "index": 1, "index_start_at": 56, "integer": 29, "float": 16.8278, "name": "Milton", "surname": "Jensen", "fullname": "Sheryl Winters", "email": "denise@weiss.na", "bool": false } ] }
[1, [2, [3, 4]]]
================================================ FILE: webpack.config.js ================================================ const path = require('path') const config = { mode: 'development', entry: './src/vtree.js', output: { path: path.join(__dirname, 'dist'), filename: 'vtree.js', library: 'VTree' }, module: { rules: [ { enforce: 'pre', test: /\.js$/, exclude: /node_modules/, loader: 'eslint-loader' }, { test: /\.js$/, exclude: /node_modules/, loader: 'babel-loader', query: { presets: ['es2015'] } } ] } } module.exports = config