Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Details

Privacy

Go PRO Window blinds lowered to protect code. Code Editor with window blinds (raised) and a light blub turned on.

Keep it secret; keep it safe.

Private Pens are hidden everywhere on CodePen, except to you. You can still share them and other people can see them, they just can't find them through searching or browsing.

Upgrade to PRO

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

Template

Make Template?

Templates are Pens that can be used to start other Pens quickly from the create menu. The new Pen will copy all the code and settings from the template and make a new Pen (that is not a fork). You can view all of your templates, or learn more in the documentation.

Template URL

Any Pen can act as a template (even if you don't flip the toggle above) with a special URL you can use yourself or share with others. Here's this Pen's template URL:

Screenshot

Screenshot or Custom Thumbnail

Screenshots of Pens are shown in mobile browsers, RSS feeds, to users who chose images instead of iframes, and in social media sharing.

This Pen is using the default Screenshot, generated by CodePen. Upgrade to PRO to upload your own thumbnail that will be displayed on previews of this pen throughout the site and when sharing to social media.

Upgrade to PRO

HTML

              
                <script src="https://gojs.net/beta/release/go-debug.js"></script>
<body onload="init()">
  <div id="sample">
    <div style="width:100%; white-space:nowrap;">
      <div style="display: inline-block; vertical-align: top; padding: 5px; width:150px">
      <div id="myTreeView" style="border: solid 1px black; height: 500px"></div>
    </div>
    <div style="display: inline-block; vertical-align: top; padding: 5px; width:60%">
      <div id="myDiagram" style="border: solid 1px black; height: 500px"></div>
    </div>
      		<div id="right-column" style="vertical-align: top; width:20%; float:right;">
			<div id="mainPalette" style="height: 150px;"></div>
		</div>
  </div>
  </div>
</body>
              
            
!

CSS

              
                
              
            
!

JS

              
                 function init() {
   var $ = go.GraphObject.make;

   myDiagram =
     $(go.Diagram, "myDiagram", {
       // what to do when a drag-drop occurs in the Diagram's background
       allowDrop: true,
       mouseDrop: function(e) {
         finishDrop(e, null);
       },
       layout: // Diagram has simple horizontal layout
         $(go.GridLayout, {
         wrappingWidth: Infinity,
         alignment: go.GridLayout.Position,
         cellSize: new go.Size(1, 1)
       }),
       initialContentAlignment: go.Spot.Center,
       "commandHandler.archetypeGroupData": {
         isGroup: true,
         category: "OfNodes"
       },
       "undoManager.isEnabled": true,
       // when a node is selected in the main Diagram, select the corresponding tree node
       "ChangedSelection": function(e) {
         if (myChangingSelection) return;
         myChangingSelection = true;
         var diagnodes = new go.Set();
         myDiagram.selection.each(function(n) {
           diagnodes.add(myTreeView.findNodeForData(n.data));
         });
         myTreeView.clearSelection();
         myTreeView.selectCollection(diagnodes);
         myChangingSelection = false;
       }
     });

   var myChangingSelection = false; // to protect against recursive selection changes

   // when the document is modified, add a "*" to the title and enable the "Save" button
   myDiagram.addDiagramListener("Modified", function(e) {
     var button = document.getElementById("saveModel");
     if (button) button.disabled = !myDiagram.isModified;
     var idx = document.title.indexOf("*");
     if (myDiagram.isModified) {
       if (idx < 0) document.title += "*";
     } else {
       if (idx >= 0) document.title = document.title.substr(0, idx);
     }
   });

   // There are two templates for Groups, "OfGroups" and "OfNodes".

   // this function is used to highlight a Group that the selection may be dropped into
   function highlightGroup(e, grp, show) {
     if (!grp) return;
     e.handled = true;
     if (show) {
       // cannot depend on the grp.diagram.selection in the case of external drag-and-drops;
       // instead depend on the DraggingTool.draggedParts or .copiedParts
       var tool = grp.diagram.toolManager.draggingTool;
       var map = tool.draggedParts || tool.copiedParts; // this is a Map
       // now we can check to see if the Group will accept membership of the dragged Parts
       if (grp.canAddMembers(map.toKeySet())) {
         grp.isHighlighted = true;
         return;
       }
     }
     grp.isHighlighted = false;
   }

   // Upon a drop onto a Group, we try to add the selection as members of the Group.
   // Upon a drop onto the background, or onto a top-level Node, make selection top-level.
   // If this is OK, we're done; otherwise we cancel the operation to rollback everything.
   function finishDrop(e, grp) {
     var ok = (grp !== null ? grp.addMembers(grp.diagram.selection, true) : e.diagram.commandHandler.addTopLevelParts(e.diagram.selection, true));
     if (!ok) e.diagram.currentTool.doCancel();
   }

   myDiagram.groupTemplateMap.add("OfGroups",
     $(go.Group, go.Panel.Auto, {
         background: "transparent",
         // highlight when dragging into the Group
         mouseDragEnter: function(e, grp, prev) {
           highlightGroup(e, grp, true);
         },
         mouseDragLeave: function(e, grp, next) {
           highlightGroup(e, grp, false);
         },
         computesBoundsAfterDrag: true,
         // when the selection is dropped into a Group, add the selected Parts into that Group;
         // if it fails, cancel the tool, rolling back any changes
         mouseDrop: finishDrop,
         //???handlesDragDropForMembers: true,  // don't need to define handlers on member Nodes and Links
         // Groups containing Groups lay out their members horizontally
         layout: $(go.GridLayout, {
           wrappingWidth: Infinity,
           alignment: go.GridLayout.Position,
           cellSize: new go.Size(1, 1),
           spacing: new go.Size(4, 4)
         })
       },
       new go.Binding("background", "isHighlighted", function(h) {
         return h ? "rgba(255,0,0,0.2)" : "transparent";
       }).ofObject(),
       $(go.Shape, "Rectangle", {
         fill: null,
         stroke: "#E69900",
         strokeWidth: 2
       }),
       $(go.Panel, go.Panel.Vertical, // title above Placeholder
         $(go.Panel, go.Panel.Horizontal, // button next to TextBlock
           {
             stretch: go.GraphObject.Horizontal,
             background: "#FFDD33",
             margin: 1
           },
           $("SubGraphExpanderButton", {
             alignment: go.Spot.Right,
             margin: 5
           }),
           $(go.TextBlock, {
               alignment: go.Spot.Left,
               editable: true,
               margin: 5,
               font: "bold 18px sans-serif",
               stroke: "#9A6600"
             },
             new go.Binding("text", "text").makeTwoWay())
         ), // end Horizontal Panel
         $(go.Placeholder, {
           padding: 5,
           alignment: go.Spot.TopLeft
         })
       ) // end Vertical Panel
     )); // end Group and call to add to template Map

   myDiagram.groupTemplateMap.add("OfNodes",
     $(go.Group, go.Panel.Auto, {
         background: "transparent",
         ungroupable: true,
         // highlight when dragging into the Group
         mouseDragEnter: function(e, grp, prev) {
           highlightGroup(e, grp, true);
         },
         mouseDragLeave: function(e, grp, next) {
           highlightGroup(e, grp, false);
         },
         computesBoundsAfterDrag: true,
         // when the selection is dropped into a Group, add the selected Parts into that Group;
         // if it fails, cancel the tool, rolling back any changes
         mouseDrop: finishDrop,
         //???handlesDragDropForMembers: true,  // don't need to define handlers on member Nodes and Links
         // Groups containing Nodes lay out their members vertically
         layout: $(go.GridLayout, {
           wrappingColumn: 1,
           alignment: go.GridLayout.Position,
           cellSize: new go.Size(1, 1),
           spacing: new go.Size(4, 4)
         })
       },
       new go.Binding("background", "isHighlighted", function(h) {
         return h ? "rgba(255,0,0,0.2)" : "transparent";
       }).ofObject(),
       $(go.Shape, "Rectangle", {
         fill: null,
         stroke: "#0099CC",
         strokeWidth: 2
       }),
       $(go.Panel, go.Panel.Vertical, // title above Placeholder
         $(go.Panel, go.Panel.Horizontal, // button next to TextBlock
           {
             stretch: go.GraphObject.Horizontal,
             background: "#33D3E5",
             margin: 1
           },
           $("SubGraphExpanderButton", {
             alignment: go.Spot.Right,
             margin: 5
           }),
           $(go.TextBlock, {
               alignment: go.Spot.Left,
               editable: true,
               margin: 5,
               font: "bold 16px sans-serif",
               stroke: "#006080"
             },
             new go.Binding("text", "text").makeTwoWay())
         ), // end Horizontal Panel
         $(go.Placeholder, {
           padding: 5,
           alignment: go.Spot.TopLeft
         })
       ) // end Vertical Panel
     )); // end Group and call to add to template Map

   // Nodes have a trivial definition
   myDiagram.nodeTemplate =
     $(go.Node, go.Panel.Auto, { // dropping on a Node is the same as dropping on its containing Group, even if it's top-level
         mouseDrop: function(e, nod) {
           finishDrop(e, nod.containingGroup);
         }
       },
       $(go.Shape, "Rectangle", {
           fill: "#ACE600",
           stroke: "#558000",
           strokeWidth: 2
         },
         new go.Binding("fill", "color")),
       $(go.TextBlock, {
           margin: 5,
           editable: true,
           font: "bold 13px sans-serif",
           stroke: "#446700"
         },
         new go.Binding("text", "text").makeTwoWay())
     );

   // setup the tree view; will be initialized with data by the load() function
   myTreeView =
     $(go.Diagram, "myTreeView", {
       allowMove: false, // don't let users mess up the tree
       allowCopy: true, // but you might want this to be false
       "commandHandler.copiesTree": true,
       "commandHandler.copiesParentKey": true,
       allowDelete: true, // but you might want this to be false
       "commandHandler.deletesTree": true,
       allowHorizontalScroll: false,
       layout: $(go.TreeLayout, {
         alignment: go.TreeLayout.AlignmentStart,
         angle: 0,
         compaction: go.TreeLayout.CompactionNone,
         layerSpacing: 16,
         layerSpacingParentOverlap: 1,
         nodeIndent: 2,
         nodeIndentPastParent: 0.88,
         nodeSpacing: 0,
         setsPortSpot: false,
         setsChildPortSpot: false,
         arrangementSpacing: new go.Size(0, 0)
       }),
       // when a node is selected in the tree, select the corresponding node in the main diagram
       "ChangedSelection": function(e) {
         if (myChangingSelection) return;
         myChangingSelection = true;
         var diagnodes = new go.Set();
         myTreeView.selection.each(function(n) {
           diagnodes.add(myDiagram.findNodeForData(n.data));
         });
         myDiagram.clearSelection();
         myDiagram.selectCollection(diagnodes);
         myChangingSelection = false;
       }
     });

   myTreeView.nodeTemplate =
     $(go.Node,
       // no Adornment: instead change panel background color by binding to Node.isSelected
       {
         selectionAdorned: false
       },
       $("TreeExpanderButton", {
         width: 14,
         "ButtonBorder.fill": "white",
         "ButtonBorder.stroke": null,
         "_buttonFillOver": "rgba(0,128,255,0.25)",
         "_buttonStrokeOver": null
       }),
       $(go.Panel, "Horizontal", {
           position: new go.Point(16, 0)
         },
         new go.Binding("background", "isSelected", function(s) {
           return (s ? "lightblue" : "white");
         }).ofObject(),
         $(go.TextBlock, {
             editable: true
           },
           new go.Binding("text").makeTwoWay())
       ) // end Horizontal Panel
     ); // end Node

   // without lines
   myTreeView.linkTemplate = $(go.Link);

   // cannot share the model itself, but can share all of the node data from the main Diagram,
   // pretending the "group" relationship is the "tree parent" relationship
   myTreeView.model = $(go.TreeModel, {
     nodeParentKeyProperty: "group"
   });
   myDiagram.model.nodeDataArray = [{
     "key": 1,
     "text": "Main 1",
     "isGroup": true,
     "category": "OfGroups"
   }, {
     "key": 2,
     "text": "Main 2",
     "isGroup": true,
     "category": "OfGroups"
   }, {
     "key": 3,
     "text": "Group A",
     "isGroup": true,
     "category": "OfNodes",
     "group": 1
   }, {
     "key": 4,
     "text": "Group B",
     "isGroup": true,
     "category": "OfNodes",
     "group": 1
   }, {
     "key": 5,
     "text": "Group C",
     "isGroup": true,
     "category": "OfNodes",
     "group": 2
   }, {
     "key": 6,
     "text": "Group D",
     "isGroup": true,
     "category": "OfNodes",
     "group": 2
   }, {
     "key": 7,
     "text": "Group E",
     "isGroup": true,
     "category": "OfNodes",
     "group": 6
   }, {
     "text": "first A",
     "group": 3,
     "key": -7
   }, {
     "text": "second A",
     "group": 3,
     "key": -8
   }, {
     "text": "first B",
     "group": 4,
     "key": -9
   }, {
     "text": "second B",
     "group": 4,
     "key": -10
   }, {
     "text": "third B",
     "group": 4,
     "key": -11
   }, {
     "text": "first C",
     "group": 5,
     "key": -12
   }, {
     "text": "second C",
     "group": 5,
     "key": -13
   }, {
     "text": "first D",
     "group": 6,
     "key": -14
   }, {
     "text": "first E",
     "group": 7,
     "key": -15
   }];
   
   myDiagram.model.addChangedListener(function(e) {
      // don't need to start/commit a transaction because the UndoManager is shared with myTreeView
      if (e.modelChange === "nodeGroupKey" || e.modelChange === "nodeParentKey") {
        // handle structural change: group memberships
        var treenode = myTreeView.findNodeForData(e.object);
        if (treenode !== null) treenode.updateRelationshipsFromData();
      } else if (e.change === go.ChangedEvent.Property) {
        var treenode = myTreeView.findNodeForData(e.object);
        if (treenode !== null) treenode.updateTargetBindings();
      } else if (e.change === go.ChangedEvent.Insert && e.propertyName === "nodeDataArray") {
        // pretend the new data isn't already in the nodeDataArray for myTreeView
        //console.log(myDiagram.currentTool); //Returns ToolManager...
        myTreeView.model.nodeDataArray.splice(e.newParam, 1);
        // now add to the myTreeView model using the normal mechanisms
        myTreeView.model.addNodeData(e.newValue);
      } else if (e.change === go.ChangedEvent.Remove && e.propertyName === "nodeDataArray") {
        // remove the corresponding node from myTreeView
        var treenode = myTreeView.findNodeForData(e.oldValue);
        if (treenode !== null) myTreeView.remove(treenode);
      }
    });
 
   // share all of the data with the tree view
   myTreeView.model.nodeDataArray = myDiagram.model.nodeDataArray;

   // share the UndoManager too!
   myTreeView.model.undoManager = myDiagram.model.undoManager;

   myTreeView.model.addChangedListener(function(e) {
     // don't need to start/commit a transaction because the UndoManager is shared with myDiagram
     if (e.modelChange === "nodeGroupKey" || e.modelChange === "nodeParentKey") {
       // handle structural change: tree parent/children
       var node = myDiagram.findNodeForData(e.object);
       if (node !== null) node.updateRelationshipsFromData();
     } else if (e.change === go.ChangedEvent.Property) {
       // propagate simple data property changes back to the main Diagram
       var node = myDiagram.findNodeForData(e.object);
       if (node !== null) node.updateTargetBindings();
     } else if (e.change === go.ChangedEvent.Insert && e.propertyName === "nodeDataArray") {
       // pretend the new data isn't already in the nodeDataArray for the main Diagram model
       myDiagram.model.nodeDataArray.splice(e.newParam, 1);
       // now add to the myDiagram model using the normal mechanisms
       myDiagram.model.addNodeData(e.newValue);
     } else if (e.change === go.ChangedEvent.Remove && e.propertyName === "nodeDataArray") {
       // remove the corresponding node from the main Diagram
       var node = myDiagram.findNodeForData(e.oldValue);
       if (node !== null) myDiagram.remove(node);
     }
   });
   
   
   mainPalette = $(go.Palette, "mainPalette", { layout: $(go.TreeLayout) });
		mainPalette.nodeTemplate =
			$(go.Node, "Horizontal",
				$(go.Shape,
					{ width: 14, height: 14, fill: "white" },
					new go.Binding("fill", "color")),
				$(go.TextBlock,
					new go.Binding("text", "label"))
			);
		mainPalette.groupTemplate =
		$(go.Group, "Horizontal", { isSubGraphExpanded: false },
			$(go.Shape,
				{ width: 14, height: 14, fill: "red" },
				new go.Binding("fill", "color")),
			$(go.TextBlock,
				new go.Binding("text", "text"))
		);
		mainPalette.model.nodeDataArray = [
			{ key: "1", label: "Object 1", text: "Object 1", color: "pink" },
			{ key: "1", label: "Object 2", text: "Empty Object", color: "red" },
			{ key: "-1.", text: "2 object group", color: "aquamarine", isGroup: true },
			{ key: "-1.", text: "Object", color: "turquoise", group: "-1." },
			{ key: "-1.", text: "Object", color: "turquoise",  group: "-1." },
			{ key: "-2.", text: "3 object group", color: "aquamarine", isGroup: true },
			{ key: "-2.", text: "Object", color: "turquoise",  group: "-2." },
			{ key: "-2.", text: "Object", color: "turquoise",  group: "-2." },
			{ key: "-2.", text: "Object", color: "turquoise",  group: "-2." }
		];

 }
              
            
!
999px
How would you React if I said I love Vue?

Console