Editor layout with data from ArcGIS.com

View live sample

Description

This sample shows how to display and edit data from an ArcGIS.com map using the Dojo layout widgets. The application layout includes the editor widget and requires that the map contain data from a feature service.

The data for the sample application comes from an ArcGIS.com map. You can use ArcGIS.com to discover existing maps or create your own. After creating the map, you can use this sample layout to build an application with little or no development required.

The application has the url parameters listed below. If no url parameters are specified, the sample uses hardcoded values from within the application.

For information on working with the Dojo layout widgets see the Build Application Layouts help document.

Note: In the sample application the CSS and JavaScript are inlined in the page to make it easier to view the code. However in the real world the CSS and JavaScript are typically contained in external files. Click here to download a zipped version of the sample which uses external files for the JavaScript and CSS.

Code

<!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">
    <title>
    </title>
    <link rel="stylesheet" type="text/css" href="http://serverapi.arcgisonline.com/jsapi/arcgis/2.1/js/dojo/dijit/themes/claro/claro.css">
    <style type="text/css">
      html,body {
      height:100%;
      width:100%;
      margin:0;
      padding:0;
      }

      body {
      background-color:#FFF;
      color:#333;
      font-size:14px;
      overflow:hidden;
      font-family:Helvetica, "Trebuchet MS", Arial, sans-serif;
      }

      #header {
      border:solid 1px #224a54;
      height:80px;
      margin:2px;
      }

      #title {
      color:#369;
      font-size:30px;
      text-align:left;
      font-weight:bolder;
      padding-top:10px;
      padding-left:15px;
      }

      #snippet {
      font-size:14px;
      color:#444;
      padding-left:15px;
      }

      #leftPane {
      border:solid 1px #224a54;
      width:28%;
      margin:5px;
      }

      #footer {
      border:solid 1px #224a54;
      background-color:#ecefe4;
      color:#3f3f3f;
      font-size:10pt;
      text-align:center;
      height:40px;
      margin:2px;
      }

      #editorDiv {
      width:90%;
      border:0;
      height:90%;
      }

      #map {
      border:solid 1px #224a54;
      margin:5px;
      }
    </style>
    <script type="text/javascript">
      var djConfig = {
        parseOnLoad: true
      };
    </script>
    <script type="text/javascript" src="http://serverapi.arcgisonline.com/jsapi/arcgis/?v=2.1">
    </script>
    <script type="text/javascript">
      dojo.require("dijit.dijit"); // optimize: load dijit layer
      dojo.require("dijit.layout.BorderContainer");
      dojo.require("dijit.layout.ContentPane");
      dojo.require("esri.dijit.editing.Editor-all");
      dojo.require("esri.arcgis.utils");
      dojo.require("dijit.layout.StackContainer");
      dojo.require("dijit.form.Button");
      dojo.require("dijit.Menu");

      var map;
      var geometryService = "http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer";

      function init() {
        esri.config.defaults.io.proxyUrl = "/arcgisserver/apis/javascript/proxy/proxy.ashx";
        var urlObject = esri.urlToObject(document.location.href);
        var agoId;
        var bingMapsKey;
        if (urlObject.query) {
          agoId = urlObject.query.webmap;
          bingMapsKey = urlObject.query.bingMapsKey;
        }
        else {
          agoId = "a193c5459e6e4fd99ebf09d893d65cf0"
        }
        
        var itemDeferred = esri.arcgis.utils.getItem(agoId);

        itemDeferred.addCallback(function(itemInfo) {
          dojo.byId("title").innerHTML = itemInfo.item.title;
          dojo.byId("snippet").innerHTML = itemInfo.item.snippet;
          dojo.byId("dataSource").innerHTML = "<a target='_blank' href='http://www.arcgis.com/home/item.html?id=" + agoId + "'>View data details</a>";

          var mapDeferred = esri.arcgis.utils.createMap(itemInfo, "map", {
            mapOptions: {
              slider: true
            },
            geometryServiceURL: geometryService
          });

          mapDeferred.addCallback(function(response) {
            map = response.map;
            dojo.connect(dijit.byId('map'), 'resize', resizeMap);

            //initialize the editor
            var layerInfo = [];
            var templateInfo = [];
            dojo.forEach(map.graphicsLayerIds, function(id) {
              var layer = map.getLayer(id);
              if (layer.type === 'Feature Layer') {
                layerInfo.push({
                  'featureLayer': layer
                });
                templateInfo.push(layer);
              }
            });
            var templatePicker = new esri.dijit.editing.TemplatePicker({
              featureLayers: templateInfo,
              grouping: true,
              rows: 'auto',
              columns: 3
            }, 'editorDiv');
            templatePicker.startup();
            var settings = {
              map: map,
              templatePicker: templatePicker,
              geometryService: new esri.tasks.GeometryService(geometryService),
              layerInfos: layerInfo
            };

            var params = {
              settings: settings
            };

            var editorWidget = new esri.dijit.editing.Editor(params);
            editorWidget.startup();
          });

          mapDeferred.addErrback(function(error) {
            console.log("CreateMap failed: ", dojo.toJson(error));
          });
        });

        itemDeferred.addErrback(function(error) {
          console.log("getItem failed: ", dojo.toJson(error));
        });

      }


      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);
      }
      //show map on load
      dojo.addOnLoad(init);
    </script>
  </head>
  
  <body class="claro">
    <div id="mainWindow" dojotype="dijit.layout.BorderContainer" design="headline"
    gutters="false" style="width:100%; height:100%;">
      <div id="header" dojotype="dijit.layout.ContentPane" region="top">
        <div id="title">
        </div>
        <div id="snippet">
        </div>
      </div>
      <div dojotype="dijit.layout.ContentPane" id="leftPane" region="left">
        <div id="editorDiv">
        </div>
      </div>
      <div id="map" dojotype="dijit.layout.ContentPane" region="center">
      </div>
      <div id="footer" dojotype="dijit.layout.ContentPane" region="bottom">
        <div id="dataSource"></div>
      </div>
    </div>
  </body>

</html>