<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=7, IE=9" />
<meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no"/>
<title>
Maps Toolbar
</title>
<link rel="stylesheet" type="text/css" href="http://serverapi.arcgisonline.com/jsapi/arcgis/2.4/js/dojo/dijit/themes/claro/claro.css">
<style type="text/css">
html, body {
height: 100%; width: 100%; margin: 0; padding: 0;
}
</style>
<script type="text/javascript">
dojoConfig = {
parseOnLoad: true
}
</script>
<script type="text/javascript" src="http://serverapi.arcgisonline.com/jsapi/arcgis/?v=2.4">
</script>
<script type="text/javascript">
dojo.require("esri.map");
dojo.require("dijit.dijit"); // optimize: load dijit layer
dojo.require("dijit.layout.BorderContainer");
dojo.require("dijit.layout.ContentPane");
dojo.require("esri.toolbars.edit");
dojo.require("dijit.form.Button");
dojo.require("dijit.Menu");
dojo.require("esri.undoManager");
var map, editToolbar, undoManager;
var oldGraphic, currentGraphic;
function init() {
var startExtent = new esri.geometry.Extent({"xmin":-10527519,"ymin":3459600,"xmax":11408073,"ymax":13047861,"spatialReference":{"wkid":102100}});
map = new esri.Map("map", {
extent: startExtent,
wrapAround180:true
});
dojo.connect(map, "onLoad", createToolbar);
var basemap = new esri.layers.ArcGISTiledMapServiceLayer("http://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer");
map.addLayer(basemap);
undoManager = new esri.UndoManager();
buildOperations();
}
function buildOperations(){
dojo.declare('operations.graphics.applyEdit', esri.OperationBase, {
label: 'Apply Edit',
constructor: function (parameters) {
},
performUndo: function () {
var test = map.graphics.graphics;
for (var i = 0; i < test.length; i++) {
if (test.geometry == currentGraphic.geometry) {
test.setGeometry(oldGraphic.geometry);
editToolbar.refresh();
}
}
},
performRedo: function () {
for (var i = 0; i < map.graphics.length; i++) {
if (map.graphics.geometry == oldGraphic.geometry) {
map.graphics.setGeometry(currentGraphic.geometry);
editToolbar.refresh();
}
}
}
});
}
function createToolbar(map) {
dojo.connect(dijit.byId('map'), 'resize', resizeMap);
addGraphics();
editToolbar = new esri.toolbars.Edit(map);
//Activate the toolbar when you click on a graphic
dojo.connect(map.graphics, "onClick", function(evt) {
dojo.stopEvent(evt);
activateToolbar(evt.graphic);
});
//deactivate the toolbar when you click outside a graphic
dojo.connect(map,"onClick", function(evt){
editToolbar.deactivate();
});
}
function addGraphics() {
//add pre-defined geometries to map
var polygonSymbol = new esri.symbol.SimpleFillSymbol(esri.symbol.SimpleFillSymbol.STYLE_SOLID, new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_DASHDOT, new
dojo.Color([255, 0, 0]), 2), new dojo.Color([255, 255, 0, 0.25]));
var polylineSymbol = new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color([255, 0, 0]), 3);
var polyline = new
esri.geometry.Polyline({"paths":[[[-12484306.95575803,7244028.536719631],[-7318386.8361340165,10061803.147423638],[-3013453.403114006,10727111.04161764]]],"spatialReference":{"wkid":102100}});
var polygon = new esri.geometry.Polygon({
"rings": [
[
[-4226661.916056009, 8496372.808143634],
[-3835304.3312360067, 8731187.359035634],
[-2269873.991956003, 9005137.668409634],
[-1213208.5129420012, 8613780.083589634],
[-1017529.7205320001, 8065879.464841632],
[-1213208.5129420012, 7478843.087611631],
[-2230738.233474003, 6891806.710381631],
[-2935181.8861500043, 6735263.6764536295],
[-3522218.263380006, 6891806.710381631],
[-3952711.606682008, 7165757.01975563],
[-4265797.674538009, 7283164.295201631],
[-4304933.433020009, 7635386.121539632],
[-4304933.433020009, 7674521.880021632],
[-4226661.916056009, 8496372.808143634]
]
],
"spatialReference": {
"wkid": 102100
}
});
var triangle = new esri.geometry.Polygon({
"rings": [
[
[2426417.02588401, 8535508.566625634],
[4304933.433020014, 12292541.380897645],
[6183449.840156019, 8535508.566625634],
[2426417.02588401, 8535508.566625634]
]
],
"spatialReference": {
"wkid": 102100
}
});
map.graphics.add(new esri.Graphic(polyline, polylineSymbol));
map.graphics.add(new esri.Graphic(polygon, polygonSymbol));
map.graphics.add(new esri.Graphic(triangle, polygonSymbol));
}
function activateToolbar(graphic) {
var tool = 0;
if (dijit.byId("tool_move").checked) {
tool = tool | esri.toolbars.Edit.MOVE;
}
if (dijit.byId("tool_scale").checked) {
tool = tool | esri.toolbars.Edit.SCALE;
}
if (dijit.byId("tool_rotate").checked) {
tool = tool | esri.toolbars.Edit.ROTATE;
}
editToolbar.activate(tool, graphic);
dojo.connect(map, 'onClick', dojo.hitch(this, this.handleMapClick));
dojo.connect(editToolbar, 'onGraphicFirstMove', handleGraphicEdit);
dojo.connect(editToolbar, 'onScaleFirstMove', handleGraphicEdit);
dojo.connect(editToolbar, 'onRotateFirstMove', handleGraphicEdit);
dojo.connect(editToolbar, 'onGraphicMoveStop', handleGraphicEditStop);
dojo.connect(editToolbar, 'onScaleStop', handleGraphicEditStop);
dojo.connect(editToolbar, 'onRotateStop', handleGraphicEditStop);
}
function handleGraphicEdit(graphic){
oldGraphic = new esri.Graphic(graphic.geometry, graphic.symbol);
}
function handleGraphicEditStop(graphic){
currentGraphic = new esri.Graphic(graphic.geometry, graphic.symbol);
var operation = new operations.graphics.applyEdit({
graphics: map.graphics,
currentGraphic: currentGraphic,
oldGraphic: oldGraphic
});
undoManager.add(operation);
}
function resizeMap() {
//resize the map when the browser resizes - view the 'Resizing and repositioning the map' section in
//the following help topic for more details http://help.esri.com/EN/webapi/javascript/arcgis/help/jshelp_start.htm#jshelp/inside_guidelines.htm
var resizeTimer;
clearTimeout(resizeTimer);
resizeTimer = setTimeout(function() {
map.resize();
map.reposition();
}, 500);
}
dojo.addOnLoad(init);
</script>
</head>
<body class="claro">
<div id="mainWindow" data-dojo-type="dijit.layout.BorderContainer" data-dojo-props="design:'headline', gutters:'false'" style="width:100%; height:100%;">
<div id="header" data-dojo-type="dijit.layout.ContentPane" data-dojo-props="region:'top'" style="height:58px;text-align:left;font-weight:bold;font-size:14px;color:#400D12;">
<span>Specify editing options then click a graphic to edit the shape. Click outside the graphic to deactivate the toolbar.</span><br />
<div id="tool_move" data-dojo-type="dijit.form.ToggleButton" data-dojo-props="checked:'true', iconClass:'dijitCheckBoxIcon'">Move</div>
<div id="tool_scale" data-dojo-type="dijit.form.ToggleButton" data-dojo-props="checked:'true', iconClass:'dijitCheckBoxIcon'">Scale</div>
<div id="tool_rotate" data-dojo-type="dijit.form.ToggleButton" data-dojo-props="checked:'true', iconClass:'dijitCheckBoxIcon'">Rotate</div>
<button data-dojo-type="dijit.form.Button" data-dojo-props="onClick:function(){undoManager.undo()}">Undo</button>
<button data-dojo-type="dijit.form.Button" data-dojo-props="onClick:function(){undoManager.redo()}">Redo</button>
</div>
<div id="map" data-dojo-type="dijit.layout.ContentPane" style="border:solid 2px cornflowerblue;margin:5px;" data-dojo-props="region:'center'">
</div>
</div>
</body>
</html>