freegroup / draw2d

Create Visio like drawings, diagrams or workflows with JavaScript and HTML5
https://freegroup.github.io/draw2d/#/examples
MIT License
738 stars 227 forks source link

Children of nodes are not serialized to JSON #180

Open rudolphi opened 3 years ago

rudolphi commented 3 years ago

Proposed fix in draw2d/src/shape/node/Node.js: getPersistentAttributes: function () { let memento = this._super()

// ...

memento.children = []
this.children.each(function (i, child) { // getChildren() misses the locator
  memento.children.push(extend(child.figure.getPersistentAttributes(), {
      // no child.figure.getName()
      child: child.figure.NAME,
      locator: child.locator.NAME,
      locatorAttr: child.locator.attr()
    }))
  })

return memento

},

// ... setPersistentAttributes: function (memento) { this._super(memento)

// ...

// remove all children created in the init method
//
this.resetChildren()

// and restore all children of the JSON document instead.
//
memento.children.forEach((e) => {
    let locator = eval("new " + e.locator + "()")
    if(e.locatorAttr) {
      locator.attr(e.locatorAttr)
    }

    let child = eval("new " + e.child + "()")
    child.setPersistentAttributes(e)
    this.add(child, locator)
})

return this

}