UNPKG

7.26 kBJavaScriptView Raw
1/*
2* Copyright (C) 1998-2021 by Northwoods Software Corporation. All Rights Reserved.
3*/
4(function (factory) {
5 if (typeof module === "object" && typeof module.exports === "object") {
6 var v = factory(require, exports);
7 if (v !== undefined) module.exports = v;
8 }
9 else if (typeof define === "function" && define.amd) {
10 define(["require", "exports", "../release/go.js", "./GeometryReshapingTool.js", "./PolygonDrawingTool.js"], factory);
11 }
12})(function (require, exports) {
13 "use strict";
14 Object.defineProperty(exports, "__esModule", { value: true });
15 exports.allowRotating = exports.allowReshaping = exports.allowResizing = exports.cancelDrawing = exports.finishDrawing = exports.drawPolyline = exports.drawPolygon = exports.select = exports.load = exports.save = exports.updateAllAdornments = exports.undo = exports.finish = exports.mode = exports.init = void 0;
16 /*
17 * This is an extension and not part of the main GoJS library.
18 * Note that the API for this class may change with any version, even point releases.
19 * If you intend to use an extension in production, you should copy the code to your own source directory.
20 * Extensions can be found in the GoJS kit under the extensions or extensionsTS folders.
21 * See the Extensions intro page (https://gojs.net/latest/intro/extensions.html) for more information.
22 */
23 var go = require("../release/go.js");
24 var GeometryReshapingTool_js_1 = require("./GeometryReshapingTool.js");
25 var PolygonDrawingTool_js_1 = require("./PolygonDrawingTool.js");
26 var myDiagram;
27 function init() {
28 if (window.goSamples)
29 window.goSamples(); // init for these samples -- you don't need to call this
30 var $ = go.GraphObject.make;
31 myDiagram =
32 $(go.Diagram, 'myDiagramDiv');
33 myDiagram.toolManager.mouseDownTools.insertAt(3, new GeometryReshapingTool_js_1.GeometryReshapingTool());
34 myDiagram.nodeTemplateMap.add('PolygonDrawing', $(go.Node, { locationSpot: go.Spot.Center }, // to support rotation about the center
35 new go.Binding('location', 'loc', go.Point.parse).makeTwoWay(go.Point.stringify), {
36 selectionAdorned: true, selectionObjectName: 'SHAPE',
37 selectionAdornmentTemplate: // custom selection adornment: a blue rectangle
38 $(go.Adornment, 'Auto', $(go.Shape, { stroke: 'dodgerblue', fill: null }), $(go.Placeholder, { margin: -1 }))
39 }, { resizable: true, resizeObjectName: 'SHAPE' }, { rotatable: true, rotateObjectName: 'SHAPE' }, { reshapable: true }, // GeometryReshapingTool assumes nonexistent Part.reshapeObjectName would be "SHAPE"
40 $(go.Shape, { name: 'SHAPE', fill: 'lightgray', strokeWidth: 1.5 }, new go.Binding('desiredSize', 'size', go.Size.parse).makeTwoWay(go.Size.stringify), new go.Binding('angle').makeTwoWay(), new go.Binding('geometryString', 'geo').makeTwoWay(), new go.Binding('fill'), new go.Binding('stroke'), new go.Binding('strokeWidth'))));
41 // create polygon drawing tool for myDiagram, defined in PolygonDrawingTool.js
42 var tool = new PolygonDrawingTool_js_1.PolygonDrawingTool();
43 // provide the default JavaScript object for a new polygon in the model
44 tool.archetypePartData = { fill: 'yellow', stroke: 'blue', strokeWidth: 3, category: 'PolygonDrawing' };
45 tool.isPolygon = true; // for a polyline drawing tool set this property to false
46 tool.isEnabled = false;
47 // install as first mouse-down-tool
48 myDiagram.toolManager.mouseDownTools.insertAt(0, tool);
49 load(); // load a simple diagram from the textarea
50 }
51 exports.init = init;
52 function mode(draw, polygon) {
53 // assume PolygonDrawingTool is the first tool in the mouse-down-tools list
54 var tool = myDiagram.toolManager.mouseDownTools.elt(0);
55 tool.isEnabled = draw;
56 tool.isPolygon = polygon;
57 tool.archetypePartData.fill = (polygon ? 'yellow' : null);
58 tool.temporaryShape.fill = (polygon ? 'yellow' : null);
59 if (draw)
60 myDiagram.currentTool = tool;
61 }
62 exports.mode = mode;
63 // this command ends the PolygonDrawingTool
64 function finish(commit) {
65 var tool = myDiagram.currentTool;
66 if (commit && tool instanceof PolygonDrawingTool_js_1.PolygonDrawingTool) {
67 var lastInput = myDiagram.lastInput;
68 if (lastInput.event instanceof MouseEvent)
69 tool.removeLastPoint(); // remove point from last mouse-down
70 tool.finishShape();
71 }
72 else {
73 tool.doCancel();
74 }
75 }
76 exports.finish = finish;
77 // this command removes the last clicked point from the temporary Shape
78 function undo() {
79 var tool = myDiagram.currentTool;
80 if (tool instanceof PolygonDrawingTool_js_1.PolygonDrawingTool) {
81 var lastInput = myDiagram.lastInput;
82 if (lastInput.event instanceof MouseEvent)
83 tool.removeLastPoint(); // remove point from last mouse-down
84 tool.undo();
85 }
86 }
87 exports.undo = undo;
88 function updateAllAdornments() {
89 myDiagram.selection.each(function (p) { p.updateAdornments(); });
90 }
91 exports.updateAllAdornments = updateAllAdornments;
92 // save a model to and load a model from Json text, displayed below the Diagram
93 function save() {
94 var str = '{ "position": "' + go.Point.stringify(myDiagram.position) + '",\n "model": ' + myDiagram.model.toJson() + ' }';
95 document.getElementById('mySavedDiagram').value = str;
96 myDiagram.isModified = false;
97 }
98 exports.save = save;
99 function load() {
100 var str = document.getElementById('mySavedDiagram').value;
101 try {
102 var json = JSON.parse(str);
103 myDiagram.initialPosition = go.Point.parse(json.position || '0 0');
104 myDiagram.model = go.Model.fromJson(json.model);
105 myDiagram.model.undoManager.isEnabled = true;
106 }
107 catch (ex) {
108 alert(ex);
109 }
110 }
111 exports.load = load;
112 function select() {
113 mode(false, false);
114 }
115 exports.select = select;
116 function drawPolygon() {
117 mode(true, true);
118 }
119 exports.drawPolygon = drawPolygon;
120 function drawPolyline() {
121 mode(true, false);
122 }
123 exports.drawPolyline = drawPolyline;
124 function finishDrawing() {
125 finish(true);
126 }
127 exports.finishDrawing = finishDrawing;
128 function cancelDrawing() {
129 finish(false);
130 }
131 exports.cancelDrawing = cancelDrawing;
132 function allowResizing() {
133 myDiagram.allowResize = !myDiagram.allowResize;
134 updateAllAdornments();
135 }
136 exports.allowResizing = allowResizing;
137 function allowReshaping() {
138 myDiagram.allowReshape = !myDiagram.allowReshape;
139 updateAllAdornments();
140 }
141 exports.allowReshaping = allowReshaping;
142 function allowRotating() {
143 myDiagram.allowRotate = !myDiagram.allowRotate;
144 updateAllAdornments();
145 }
146 exports.allowRotating = allowRotating;
147});