GoJS的学习使用

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Process Flow</title>
<meta name="description"
content="A simple process flow or SCADA diagram editor, simulating equipment monitoring and control." />
<!-- Copyright 1998-2017 by Northwoods Software Corporation. -->
<meta charset="UTF-8">
<script src="js/go-debug1.js"></script>
<script id="code">
function init() {
if (window.goSamples)
goSamples(); // init for these samples -- you don't need to call this
var $ = go.GraphObject.make; // for conciseness in defining templates


myDiagram = $(go.Diagram, "myDiagramDiv", // must name or refer to the DIV HTML element
{
/* grid : $(go.Panel, "Grid", $(go.Shape, "LineH", {
stroke : "lightgray",
strokeWidth : 0.5
}), $(go.Shape, "LineH", {
stroke : "gray",
strokeWidth : 0.5,
interval : 10
}), $(go.Shape, "LineV", {
stroke : "lightgray",
strokeWidth : 0.5
}), $(go.Shape, "LineV", {
stroke : "gray",
strokeWidth : 0.5,
interval : 10
})), */
allowDrop : true, // must be true to accept drops from the Palette 右边的面板允许防止图形
"draggingTool.dragsLink" : true,
"draggingTool.isGridSnapEnabled" : true,
"linkingTool.isUnconnectedLinkValid" : true,
"linkingTool.portGravity" : 20,
"relinkingTool.isUnconnectedLinkValid" : true,
"relinkingTool.portGravity" : 20,
//连接线的起始点小菱形
"relinkingTool.fromHandleArchetype" : $(go.Shape, "Diamond", {
segmentIndex : 0,
cursor : "pointer",
desiredSize : new go.Size(8, 8),
fill : "tomato",
stroke : "darkred"
}),
//连接线终点的小菱形
"relinkingTool.toHandleArchetype" : $(go.Shape, "Diamond", {
segmentIndex : -1,
cursor : "pointer",
desiredSize : new go.Size(8, 8),
fill : "darkred",
stroke : "tomato"
}),
//连接线中间点的小菱形
"linkReshapingTool.handleArchetype" : $(go.Shape, "Diamond", {
desiredSize : new go.Size(7, 7),
fill : "lightblue",
stroke : "deepskyblue"
}),
rotatingTool : $(TopRotatingTool), // defined below
"rotatingTool.snapAngleMultiple" : 15,
"rotatingTool.snapAngleEpsilon" : 15,
"undoManager.isEnabled" : true
});


// when the document is modified, add a "*" to the title and enable the "Save" button
//DiagramListener实时监听,当画板被更改时isModified为true,在title上加上*,并且使保存按钮生效。
//监听器触发:myDiagram.isModified的值发生改变时触发此函数
myDiagram.addDiagramListener("Modified", function(e) {
//alert("isModified1"+myDiagram.isModified);
var button = document.getElementById("SaveButton");
if (button)
button.disabled = !myDiagram.isModified; //当监听到isModified为true画布被修改时,button生效
var idx = document.title.indexOf("*"); //当执行完save()时,isModified被修改为false,button失效
if (myDiagram.isModified) {
//alert("isModified2"+myDiagram.isModified);
if (idx < 0)
document.title += "*";
} else {
if (idx >= 0)
document.title = document.title.substr(0, idx);
}
});


// Define a function for creating a "port" that is normally transparent.
// The "name" is used as the GraphObject.portId, the "spot" is used to control how links connect
// and where the port is positioned on the node, and the boolean "output" and "input" arguments
// control whether the user can draw links from or to the port.
//创建一个port,ID为name,spot控制其怎么被连接,放置于node的什么位置,output/input决定其哪里可以from和to
function makePort(name, spot, output, input) {
// the port is basically just a small transparent square
return $(go.Shape, "Circle", {
fill : null, // not seen, by default; set to a translucent gray by showSmallPorts, defined below
stroke : null,
desiredSize : new go.Size(7, 7),
alignment : spot, // align the port on the main Shape
alignmentFocus : spot, // just inside the Shape
portId : name, // declare this object to be a "port"
fromSpot : spot,
toSpot : spot, // declare where links may connect at this port
fromLinkable : output,
toLinkable : input, // declare whether the user may draw links to/from here
cursor : "pointer" // show a different cursor to indicate potential link point
});
}


//选中修饰
var nodeSelectionAdornmentTemplate = $(go.Adornment, "Auto", $(
go.Shape, {
fill : null,
stroke : "deepskyblue",
strokeWidth : 1.5,
strokeDashArray : [ 4, 2 ]
}), $(go.Placeholder));
//改变大小修饰
var nodeResizeAdornmentTemplate = $(go.Adornment, "Spot", {
locationSpot : go.Spot.Right
}, $(go.Placeholder), $(go.Shape, { //左上
alignment : go.Spot.TopLeft,
cursor : "nw-resize",
desiredSize : new go.Size(6, 6),
fill : "lightblue",
stroke : "deepskyblue"
}), $(go.Shape, { //上
alignment : go.Spot.Top,
cursor : "n-resize",
desiredSize : new go.Size(6, 6),
fill : "lightblue",
stroke : "deepskyblue"
}), $(go.Shape, { //右上
alignment : go.Spot.TopRight,
cursor : "ne-resize",
desiredSize : new go.Size(6, 6),
fill : "lightblue",
stroke : "deepskyblue"
}),


$(go.Shape, { //左中
alignment : go.Spot.Left,
cursor : "w-resize",
desiredSize : new go.Size(6, 6),
fill : "lightblue",
stroke : "deepskyblue"
}), $(go.Shape, { //右中
alignment : go.Spot.Right,
cursor : "e-resize",
desiredSize : new go.Size(6, 6),
fill : "lightblue",
stroke : "deepskyblue"
}),


$(go.Shape, { //下左
alignment : go.Spot.BottomLeft,
cursor : "se-resize",
desiredSize : new go.Size(6, 6),
fill : "lightblue",
stroke : "deepskyblue"
}), $(go.Shape, { //下
alignment : go.Spot.Bottom,
cursor : "s-resize",
desiredSize : new go.Size(6, 6),
fill : "lightblue",
stroke : "deepskyblue"
}), $(go.Shape, { //下右
alignment : go.Spot.BottomRight,
cursor : "sw-resize",
desiredSize : new go.Size(6, 6),
fill : "lightblue",
stroke : "deepskyblue"
}));

//旋转修饰
var nodeRotateAdornmentTemplate = $(go.Adornment, {
locationSpot : go.Spot.Center,
locationObjectName : "CIRCLE"
}, $(go.Shape, "Circle", { //旋转小圆点
name : "CIRCLE",
cursor : "pointer",
desiredSize : new go.Size(7, 7),
fill : "lightblue",
stroke : "deepskyblue"
}), $(go.Shape, { //旋转小圆点狭下面的线
geometryString : "M3.5 7 L3.5 30",
isGeometryPositioned : true,
stroke : "lightblue",
strokeWidth : 1.5,
strokeDashArray : [ 4, 2 ]
}));

//节点模板
myDiagram.nodeTemplate = 
$(go.Node, "Spot", {locationSpot : go.Spot.Center}, 
new go.Binding("location", "loc", go.Point.parse).makeTwoWay(go.Point.stringify), 
{//设置其可选择
selectable : true,
selectionAdornmentTemplate : nodeSelectionAdornmentTemplate
}, {//设置其可改变大小
resizable : true,
resizeObjectName : "PANEL",
resizeAdornmentTemplate : nodeResizeAdornmentTemplate
}, {//设置其可旋转
rotatable : true,
rotateAdornmentTemplate : nodeRotateAdornmentTemplate
},
new go.Binding("angle").makeTwoWay(),

// the main object is a Panel that surrounds a TextBlock with a Shape ~图形:Panel包围着TextBlock
$(go.Panel, "Auto", {
name : "PANEL"
}, 
new go.Binding("desiredSize", "size", go.Size.parse).makeTwoWay(go.Size.stringify), 

$(go.Shape, "Rectangle", // default figure 默认的形状
{
portId : "", // the default port: if no spot on link data, use closest side
fromLinkable : true,
toLinkable : true,
cursor : "pointer",
fill : "white", // default color
strokeWidth : 2
}, 
new go.Binding("figure"), new go.Binding("fill")), 

$(go.TextBlock, {
font : "bold 11pt Helvetica, Arial, sans-serif",
margin : 8,
maxSize : new go.Size(160, NaN),
wrap : go.TextBlock.WrapFit,
editable : true
}, 
new go.Binding("text").makeTwoWay())),

// four small named ports, one on each side: 设置可被链接的上下左右四个点
makePort("T", go.Spot.Top, false, true), 
makePort("L", go.Spot.Left,true, true), 
makePort("R", go.Spot.Right, true, true),
makePort("B", go.Spot.Bottom, true, false),
{ // handle mouse enter/leave events to show/hide the ports 使得鼠标离开或者进入图形时,显示四个圆点
mouseEnter : function(e, node) {
showSmallPorts(node, true);
},
mouseLeave : function(e, node) {
showSmallPorts(node, false);
}
});


function showSmallPorts(node, show) {
node.ports.each(function(port) {
if (port.portId !== "") { // don't change the default port, which is the big shape 不改变port默认的大shape
port.fill = show ? "rgba(0,0,0,.3)" : null;
}
});
}


//连接线装饰模板
var linkSelectionAdornmentTemplate = $(go.Adornment, "Link", $(
go.Shape,
// isPanelMain declares that this Shape shares the Link.geometry
{
isPanelMain : true,
fill : null,
stroke : "deepskyblue",
strokeWidth : 0
}) // use selection object's strokeWidth
);

//连接线模板
myDiagram.linkTemplate = 
$(go.Link, // the whole link panel
{
selectable : true,
selectionAdornmentTemplate : linkSelectionAdornmentTemplate
}, {
relinkableFrom : true,
relinkableTo : true,
reshapable : true
}, {
routing : go.Link.AvoidsNodes,
curve : go.Link.JumpOver,
corner : 5,
toShortLength : 4
},
new go.Binding("points").makeTwoWay(), 

$(go.Shape, // the link path shape 连接线形状
{
isPanelMain : true,
strokeWidth : 2
}),

$(go.Shape, // the arrowhead 箭头头
{
toArrow : "Standard",
stroke : null
}), 

$(go.Panel, "Auto", new go.Binding("visible", "isSelected")
.ofObject(), $(go.Shape, "RoundedRectangle", // the link shape
{ //连接线上的块块
fill : "#F8F8F8",
stroke : null
}), 
$(go.TextBlock, {//连接线上的text文字
textAlign : "center",
font : "10pt helvetica, arial, sans-serif",
stroke : "#919191",
margin : 2,
minSize : new go.Size(10, NaN),
editable : true
},
new go.Binding("text").makeTwoWay()))
);
load(); // load an initial diagram from some JSON text


//初始化左边的模板框  initialize the Palette that is on the left side of the page 
myPalette = $(go.Palette, "myPaletteDiv", // must name or refer to the DIV HTML element
{
maxSelectionCount : 1,
nodeTemplateMap : myDiagram.nodeTemplateMap, // share the templates used by myDiagram
linkTemplate : // simplify the link template, just in this Palette
//设置连接线属性,在左侧模板框中,简化连接线
$(go.Link, 
{ // because the GridLayout.alignment is Location and the nodes have locationSpot == Spot.Center,
// to line up the Link in the same manner we have to pretend the Link has the same location spot
locationSpot : go.Spot.Center,
selectionAdornmentTemplate : 
$(go.Adornment, "Link", {
locationSpot : go.Spot.Center
}, 
$(go.Shape, {
isPanelMain : true,
fill : null,
stroke : "deepskyblue",
strokeWidth : 0
}), 
$(go.Shape, // the arrowhead
{
toArrow : "Standard",
stroke : null
}))
}, 
{
routing : go.Link.AvoidsNodes,
curve : go.Link.JumpOver,
corner : 5,
toShortLength : 4
}, 
new go.Binding("points"), 
$(go.Shape, // the link path shape
{
isPanelMain : true,
strokeWidth : 2
}), 
$(go.Shape, // the arrowhead
{
toArrow : "Standard",
stroke : null
})
),
model : new go.GraphLinksModel([ // specify the contents of the Palette
{
text : "Start",
figure : "Circle",
fill : "#00AD5F"
}, {
text : "Step"
}, {
text : "DB",
figure : "Database",
fill : "lightgray"
}, {
text : "???",
figure : "Diamond",
fill : "lightskyblue"
}, {
text : "End",
figure : "Circle",
fill : "#CE0620"
}, {
text : "Comment",
figure : "RoundedRectangle",
fill : "lightyellow"
} ], [
// the Palette also has a disconnected Link, which the user can drag-and-drop
{
points : new go.List(go.Point).addAll([ new go.Point(0, 0),
new go.Point(30, 0), new go.Point(30, 40),
new go.Point(60, 40) ])
} ])
});
}


function TopRotatingTool() {
go.RotatingTool.call(this);
}
go.Diagram.inherit(TopRotatingTool, go.RotatingTool);


/** @override */
TopRotatingTool.prototype.updateAdornments = function(part) {
go.RotatingTool.prototype.updateAdornments.call(this, part);
var adornment = part.findAdornment("Rotating");
if (adornment !== null) {
adornment.location = part.rotateObject
.getDocumentPoint(new go.Spot(0.5, 0, 0, -30)); // above middle top
}
};


/** @override */
TopRotatingTool.prototype.rotate = function(newangle) {
go.RotatingTool.prototype.rotate.call(this, newangle + 90);
};
// end of TopRotatingTool class


// Show the diagram's model in JSON format that the user may edit
//将画板中图形对应json加载至mySavedModel框(内存)中
function save() {
saveDiagramProperties(); // do this first, before writing to JSON 将myDiagram画板中的参数信息存储至至myDiagram.model中
document.getElementById("mySavedModel").value = myDiagram.model.toJson();  //将myDiagram.model中信息存储至mySavedModel框中
myDiagram.isModified = false; //修改标识设置为false
}

//将myDiagram.model中信息存储至mySavedModel框中
  function saveDiagramProperties() {
myDiagram.model.modelData.position = go.Point
.stringify(myDiagram.position);


//按照mySavedModel框中json来显示画板
function load() {
myDiagram.model = go.Model.fromJson(document
.getElementById("mySavedModel").value); //将mySavedModel中信息赋值给至myDiagram.model
loadDiagramProperties(); // do this after the Model.modelData has been brought into memory 将myDiagram.model中的信息展示在画板上
}


//将myDiagram.model中的信息展示在画板上
function loadDiagramProperties(e) {
// set Diagram.initialPosition, not Diagram.position, to handle initialization side-effects
var pos = myDiagram.model.modelData.position;
if (pos)
myDiagram.initialPosition = go.Point.parse(pos);
//alert(11111111111);
}
</script>


</head>
<body οnlοad="init()">
<div id="sample">
<div
style="width: 100%; display: flex; justify-content: space-between">
<div id="myPaletteDiv"
style="width: 105px; margin-right: 2px; background-color: whitesmoke; border: solid 1px black"></div>
<div id="myDiagramDiv"
style="flex-grow: 1; height: 620px; border: solid 1px black"></div>
</div>
<p>This sample demonstrates the ability for the user to drag
around a Link as if it were a Node. When either end of the link
passes over a valid port, the port is highlighted.</p>
<p>
The link-dragging functionality is enabled by setting some or all of
the following properties: <a>DraggingTool.dragsLink</a>, <a>LinkingTool.isUnconnectedLinkValid</a>,
and <a>RelinkingTool.isUnconnectedLinkValid</a>.
</p>
<p>
Note that a Link is present in the <a>Palette</a> so that it too can
be dragged out and onto the main Diagram. Because links are not
automatically routed when either end is not connected with a Node,
the route is provided explicitly when that Palette item is defined.
</p>
<p>
This also demonstrates several custom Adornments: <a>Part.selectionAdornmentTemplate</a>,
<a>Part.resizeAdornmentTemplate</a>, and <a>Part.rotateAdornmentTemplate</a>.
</p>
<p>
Finally this sample demonstrates saving and restoring the <a>Diagram.position</a>
as a property on the <a>Model.modelData</a> object that is
automatically saved and restored when calling <a>Model.toJson</a> and
<a>Model.fromJson</a>.
</p>
<div>
<div>
<button id="SaveButton" οnclick="save()">Save</button>
<button οnclick="load()">Load</button>
Diagram Model saved in JSON format:
</div>
<textarea id="mySavedModel" style="width: 100%; height: 300px">{ "class": "go.GraphLinksModel",
  "linkFromPortIdProperty": "fromPort",
  "linkToPortIdProperty": "toPort",
  "nodeDataArray": [
 ],
  "linkDataArray": [
 ]}
    </textarea>
</div>
</div>
</body>
</html>
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值