Reusable Accessible Mapping Platform

API Docs for: 4.0.0
Show:

File: src\js\RAMP\Modules\mapClickHandler.js

  1. /* global define, RAMP, console, $ */
  2.  
  3. /**
  4. * @module RAMP
  5. * @submodule Map
  6. */
  7.  
  8. /**
  9. * Map click handler class.
  10. *
  11. * The mapClickHandler registers WMS layers for combined getFeatureInfo request. It makes a
  12. * single subscription to Map.CLICK and triggers a set of requests and joins the results together.
  13. *
  14. * @class MapClickHandler
  15. * @static
  16. * @uses EventManager
  17. * @uses esri/request
  18. * @uses dojo/promise/all
  19. * @uses dojo/_base/array
  20. * @uses dojo/topic
  21. */
  22.  
  23. define([
  24. /* RAMP */
  25. "ramp/eventManager",
  26.  
  27. /* Dojo */
  28. "esri/request", "dojo/promise/all", "dojo/_base/array", "dojo/topic"
  29. ],
  30.  
  31. function (
  32. /* RAMP */
  33. EventManager,
  34.  
  35. /* Dojo */
  36. EsriRequest, all, dojoArray, topic
  37. ) {
  38.  
  39. "use strict";
  40. var wmsClickQueue = [], // the queue of WMS layers registered to trigger on click
  41. esriMap; // a local reference to the map object (for pull extent and dimensions)
  42.  
  43. return {
  44.  
  45. /**
  46. * This function should be called after the map has been created. It will subscribe to the Map.CLICK event
  47. * and trigger GUI.SUBPANEL_OPEN events for displaying the response data.
  48. *
  49. * @method registerWMSClick
  50. * @param {Object} map an EsriMap instance
  51. */
  52. init: function (map) {
  53. esriMap = map;
  54. topic.subscribe(EventManager.Map.CLICK, function (evt) {
  55. var visibleLayers = [],
  56. rqPromises = [];
  57.  
  58. // filter only currently visible layers
  59. visibleLayers = dojoArray.filter(wmsClickQueue, function (wmsData) {
  60. return wmsData.wmsLayer.visible;
  61. });
  62.  
  63. // if no visible layers return early and do not open the panel
  64. if (visibleLayers.length === 0) {
  65. return;
  66. }
  67.  
  68. topic.publish(EventManager.GUI.SUBPANEL_OPEN, {
  69. panelName: "WMS Click Results",
  70. title: "WMS Click Results",
  71. content: null,
  72. origin: "wmsFeatureInfo",
  73. target: $("#map-div"),
  74. guid: 'wms-guid'
  75. });
  76.  
  77. // create an EsriRequest for each WMS layer (these follow the promise API)
  78. rqPromises = dojoArray.map(visibleLayers, function (wmsData) {
  79. var req = {};
  80. if (wmsData.wmsLayer.version === "1.3" || wmsData.wmsLayer.version === "1.3.0") {
  81. req = { CRS: "EPSG:" + wmsData.wmsLayer.spatialReference.wkid, I: evt.layerX, J: evt.layerY };
  82. } else {
  83. req = { SRS: "EPSG:" + wmsData.wmsLayer.spatialReference.wkid, X: evt.layerX, Y: evt.layerY };
  84. }
  85. $.extend(req, {
  86. SERVICE: "WMS",
  87. REQUEST: "GetFeatureInfo",
  88. VERSION: wmsData.wmsLayer.version,
  89. BBOX: esriMap.extent.xmin + "," + esriMap.extent.ymin + "," + esriMap.extent.xmax + "," + esriMap.extent.ymax,
  90. WIDTH: esriMap.width,
  91. HEIGHT: esriMap.height,
  92. QUERY_LAYERS: wmsData.layerConfig.layerName,
  93. LAYERS: wmsData.layerConfig.layerName,
  94. INFO_FORMAT: wmsData.layerConfig.featureInfo.mimeType
  95. });
  96. return new EsriRequest({
  97. url: wmsData.wmsLayer.url.split('?')[0],
  98. content: req,
  99. handleAs: "text"
  100. });
  101.  
  102. });
  103.  
  104. topic.publish(EventManager.GUI.SUBPANEL_OPEN, {
  105. content: "",
  106. origin: "wmsFeatureInfo",
  107. update: true,
  108. guid: 'wms-guid'
  109. });
  110.  
  111. // wait for all success or any failure in the requests
  112. all(rqPromises).then(function (results) {
  113. console.log('all success');
  114. console.log(results);
  115.  
  116. var strings = dojoArray.map(results, function (response, index) {
  117. var res = "<h5 class='margin-top-none'>" + visibleLayers[index].layerConfig.displayName + "</h5>" +
  118. RAMP.plugins.featureInfoParser[visibleLayers[index].layerConfig.featureInfo.parser](response);
  119. return res;
  120. });
  121.  
  122. topic.publish(EventManager.GUI.SUBPANEL_OPEN, {
  123. content: strings.join(''),
  124. origin: "wmsFeatureInfo",
  125. update: true,
  126. guid: 'wms-guid'
  127. });
  128.  
  129. }, function (errors) {
  130. console.log('wms errors');
  131. console.log(errors);
  132.  
  133. topic.publish(EventManager.GUI.SUBPANEL_OPEN, {
  134. content: ':(', // FIXME
  135. origin: "wmsFeatureInfo",
  136. update: true,
  137. guid: 'wms-guid'
  138. });
  139.  
  140. });
  141.  
  142. });
  143.  
  144. },
  145.  
  146. /**
  147. * This function is called to register a WMS layer for feature info click events. The parameter wmsData
  148. * should include wmsLayer (an instance of an ESRI WMSLayer) and layerConfig (a reference to the configuration
  149. * node for the WMS layer).
  150. *
  151. * @method registerWMSClick
  152. * @param {Object} wmsData
  153. */
  154. registerWMSClick: function (wmsData) {
  155. wmsClickQueue.push(wmsData);
  156. }
  157.  
  158. };
  159. });