Search code examples
draw2d-js

Save and Restore rectangles with connections in Draw2D.js touch via JSON


How do I create rectangles with 4 ports (each side) in a correct way, so I can save and restore them via JSON?

I tried this one, but only the rectangles are been saved. The connections and labels got lost.

JSFiddle: http://jsfiddle.net/xfvf4/36/

  1. Create two elements (Add) - move them and connect them
  2. Write: This gives the content as JSON-Array
  3. Read: Should make the grafic out of the JSON-Array

The last point doesn't work.

JS:

var LabelRectangle = draw2d.shape.basic.Rectangle.extend({

    NAME: "draw2d.shape.basic.Rectangle",

    init: function (attr) {
        this._super(attr);
        this.label = new draw2d.shape.basic.Label({
            text: "Text",
            fontColor: "#0d0d0d",
            stroke: 0
        });
        this.add(this.label, new draw2d.layout.locator.CenterLocator(this));
        this.label.installEditor(new draw2d.ui.LabelInplaceEditor());
        this.createPort("hybrid", new draw2d.layout.locator.BottomLocator(this));
    },
    getPersistentAttributes: function () {
        var memento = this._super();

        memento.labels = [];

        var ports = [];
        ports = this.getPorts();

        memento.ports = [];

        console.log(ports);
        this.children.each(function (i, e) {
          console.log(e);
            memento.labels.push({
                id: e.figure.getId(),
                label: e.figure.getText(),
                locator: e.locator.NAME
            });
            ports.each(function (i, e) {
              memento.ports.push({
                  //id: e.id,
                  name: e.name,
                  locator: e.locator.NAME
              });
            });

        });

        return memento;
    },
    setPersistentAttributes: function (memento) {
        this._super(memento);

        this.resetChildren();

        $.each(memento.labels, $.proxy(function (i, e) {
            var label = new draw2d.shape.basic.Label(e.label);
            var locator = eval("new " + e.locator + "()");
            locator.setParent(this);
            this.add(label, locator);
        }, this));
    }
});


$(window).load(function () {
    var canvas = new draw2d.Canvas("gfx_holder");

    $("#add").click(function (e) { // Add a new rectangle
        var rect = new LabelRectangle({
            width: 200,
            height: 40,
            radius: 3,
            bgColor: '#ffffff',
            stroke: 0
        });
        rect.createPort("hybrid", new draw2d.layout.locator.OutputPortLocator(rect));
        rect.createPort("hybrid", new draw2d.layout.locator.InputPortLocator(rect));
        rect.createPort("hybrid", new draw2d.layout.locator.TopLocator(rect));
        canvas.add(rect, 150, 200);
    });

    $("#write").click(function (e) { // Write to pre-Element (JSON)
      var writer = new draw2d.io.json.Writer();
      writer.marshal(canvas, function(json){
          $("#json").text(JSON.stringify(json,null,2));
          $('#gfx_holder').empty();
      });

    });

    $("#read").click(function (e) { // Read from pre-Element (JSON)
      var canvas = new draw2d.Canvas("gfx_holder");
      var jsonDocument = $('#json').text();
      var reader = new draw2d.io.json.Reader();
      reader.unmarshal(canvas, jsonDocument);
    });

});

HTML:

<ul class="toolbar">
  <li><a href="javascript:void(0);" id="add" title="Add">Add</a></li>
  <li><a href="javascript:void(0);" id="write" title="Write">Write</a></li>
  <li><a href="javascript:void(0);" id="read" title="Read">Read</a></li>

</ul>

<div id="container" class="boxed">
  <div onselectstart="javascript:/*IE8 hack*/return false" id="gfx_holder" style="width:100%; height:100%; ">
  </div>

  <pre id="json" style="overflow:auto;position:absolute; top:10px; right:10px; width:350; height:500;background:white;border:1px solid gray">
  </pre>

</div>


Solution

  • Just use the write.js and Reader.js in the "json"-Folder of Draw2D.js 5.0.4 and this code:

    $(window).load(function () {
    
        var canvas = new draw2d.Canvas("gfx_holder");
    
    
        // unmarshal the JSON document into the canvas
        // (load)
        var reader = new draw2d.io.json.Reader();
        reader.unmarshal(canvas, jsonDocument);
    
        // display the JSON document in the preview DIV
        //
        displayJSON(canvas);
    
    
        // add an event listener to the Canvas for change notifications.
        // We just dump the current canvas document into the DIV
        //
        canvas.getCommandStack().addEventListener(function(e){
            if(e.isPostChangeEvent()){
                displayJSON(canvas);
            }
        });
    
    });
    
    function displayJSON(canvas){
        var writer = new draw2d.io.json.Writer();
        writer.marshal(canvas,function(json){
            $("#json").text(JSON.stringify(json, null, 2));
        });
    }