{"size": 1547, "ext": "js", "max_stars_repo_path": "node.js", "max_stars_repo_name": "juenanfeng/checkbox-tree", "max_stars_repo_stars_event_min_datetime": "2019-02-18T04:46:15.000Z", "max_stars_repo_stars_event_max_datetime": "2019-02-18T04:49:54.000Z", "max_issues_repo_path": "node.js", "max_issues_repo_name": "juenanfeng/checkbox-tree", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "node.js", "max_forks_repo_name": "juenanfeng/checkbox-tree", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 2, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 23.8, "max_line_length": 77, "alphanum_fraction": 0.4731738849}
let nodeIdSeed = 0

export default class Node {
    constructor(options) {
        this.id = nodeIdSeed++
        this.text = null
        this.checked = false
        this.data = null
        this.expanded = false
        this.parent = null

        for (let name in options) {
            if (options.hasOwnProperty(name)) {
                this[name] = options[name]
            }
        }

        // internal
        this.level = 0;
        this.childNodes = []

        if (this.parent) {
            this.level = this.parent.level + 1
        }

        if (this.data) {
            this.checked = this.data.checked || false
            this.expanded = this.data.expanded || false
            this.setData(this.data)
        }
    }

    setData(data) {

        this.data = data;
        this.childNodes = [];

        let children;
        if (this.level === 0 && this.data instanceof Array) {
            children = this.data;
        } else {
            children = this.data.children || [];
        }

        for (let i = 0, j = children.length; i < j; i++) {
            this.insertChild({
                data: children[i]
            });
        }
    }

    insertChild(child) {
        if (!child) throw new Error('insertChild error: child is required.');

        if (!(child instanceof Node)) {
            //遍历时保存父节点
            Object.assign(child, {
                parent: this
            })
            child = new Node(child);
        }
        child.level = this.level + 1;
        this.childNodes.push(child);
    }
}