Palette drop on Layer

Hello,

I am creating multiple layers, where only one may be visible at a time. I would like to directly drop palette items onto the visible layer. Also, the links created between the nodes on the visible layer should also get the visible layer assigned to them. How can I do that?

When the graph is generated using "diagram.model.toJson() and later re-loaded via model.fromJson() will the layers previously created get recreated?

Thanks,

I was able to assign newly dropped nodes to the default layer, using

diagram.addDiagramListener("ExternalObjectsDropped", function(evt) {
        var node = diagram.selection.first();
        node.layerName = select_layer;
    });

Is there an easier way to do this?

Thanks,

That seems pretty easy to me.

However, a couple points:

The Diagram.selection is a collection – unless you have explicitly disallowed multiple selection, you have to assume there are multiple Parts in the selection.

Is select_layer in your code a Layer? The value of Part.layerName must be a string.

So I would write it as:

diagram.addDiagramListener("ExternalObjectsDropped", function(e) {
    e.diagram.selection.each(function(part) {
        part.layerName = select_layer.name;
      });
  });

Thanks for the quick reply.

I did not think that more than one node could be dropped at the same time. I’ll make that change. Also, selected_layer is a string.

The problem is that all the nodes are drawn in the default layer when the JSON is read back in. The layers are not automatically created. I am trying to implement that now.

Thanks

Immediatly after adding and selecting a new layer. I get many “failure to validate parts” messages in the console. Once I select a different layer and come back, it works fine… I am creating the layers with the following code:

function newLayer(layer_name) {
    diagram.startTransaction("addLayer");
    diagram.addLayerBefore(go.GraphObject.make(go.Layer, {name: layer_name}), diagram.findLayer("Foreground"));
    diagram.layers.each(function(layer) {
            if(layer.name.match(/Grid|Background|Foreground|Adornment|Tool/)) return;
            if(layer.name == layer_name) {
                layer.visible = true;
                diagram.selected_layer = layer.name;
            } else {
                layer.visible = false;
            }
        });

    diagram.commitTransaction("addLayer");
}

I am missing something?

Thanks,

I just tried this in a test app that I created starting with the code in the http://gojs.net/latest/intro/layers.html page with the blue/green/orange rectangles, and adding a Palette.

          "ExternalObjectsDropped": function(e) {
            var diagram = e.diagram;
            diagram.startTransaction();
            var name = diagram.selection.first().data.color;
            if (diagram.findLayer(name) === null) {
              diagram.addLayerBefore(go.GraphObject.make(go.Layer, { name: name }), diagram.findLayer("Foreground"));
            }
            diagram.layers.each(function(layer) {
              if (layer.name.match(/Grid|Background|Foreground|Adornment|Tool/)) return;
              if (layer.name === name) {
                layer.visible = true;
                diagram.selected_layer = name;
              } else {
                layer.visible = false;
              }
            });
            diagram.commitTransaction("change layers");
          },

I never encountered any errors or warnings.

What version are you using?

What specific functionality are you trying to implement? It appears that your code is not related to what you said earlier about automatically creating layers from the model. If I were trying to do that, I would add an Array of all of the additional layer names in the Model.modelData object, which will exist and be written by Model.toJson even if there are no nodes or links in your model.

Hello,

I can not get it to happen again. I think that some of the links were pointing to nodes in another layer, when I started pushing nodes between layers. I noticed that links layers are computed automatically, but if one node ends up in a different layer then the graph is inconsistent…

I was not aware of Model.modelData, however, I want the layers to disappear if there are no nodes in them, so processing the model data is working as desired.

Sorry for wasting your time, but I am sure that there is a ton of stuff that I am still familiar with and will be probably be bugging you again shortly.

Thanks for all your help. GoJS is a wonderful library.