Reusable Accessible Mapping Platform

API Docs for: 5.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. try {
  80. var req = {}, wkid, mapSR, srList;
  81. mapSR = wmsData.wmsLayer.getMap().spatialReference;
  82. srList = wmsData.wmsLayer.spatialReferences;
  83.  
  84. if (srList && srList.length > 1) {
  85. wkid = srList[0];
  86. } else if (mapSR.wkid) {
  87. wkid = mapSR.wkid;
  88. }
  89. if (wmsData.wmsLayer.version === "1.3" || wmsData.wmsLayer.version === "1.3.0") {
  90. req = { CRS: "EPSG:" + wkid, I: evt.layerX, J: evt.layerY };
  91. } else {
  92. req = { SRS: "EPSG:" + wkid, X: evt.layerX, Y: evt.layerY };
  93. }
  94. $.extend(req, {
  95. SERVICE: "WMS",
  96. REQUEST: "GetFeatureInfo",
  97. VERSION: wmsData.wmsLayer.version,
  98. BBOX: esriMap.extent.xmin + "," + esriMap.extent.ymin + "," + esriMap.extent.xmax + "," + esriMap.extent.ymax,
  99. WIDTH: esriMap.width,
  100. HEIGHT: esriMap.height,
  101. QUERY_LAYERS: wmsData.layerConfig.layerName,
  102. LAYERS: wmsData.layerConfig.layerName,
  103. INFO_FORMAT: wmsData.layerConfig.featureInfo.mimeType
  104. });
  105. return new EsriRequest({
  106. url: wmsData.wmsLayer.url.split('?')[0],
  107. content: req,
  108. handleAs: "text"
  109. });
  110.  
  111. } catch (exception) {
  112. console.log("WMS Error: " + exception);
  113. }
  114.  
  115. });
  116.  
  117. topic.publish(EventManager.GUI.SUBPANEL_OPEN, {
  118. content: "",
  119. origin: "wmsFeatureInfo",
  120. update: true,
  121. guid: 'wms-guid'
  122. });
  123.  
  124. // wait for all success or any failure in the requests
  125. all(rqPromises).then(function (results) {
  126. console.log('all success');
  127. console.log(results);
  128.  
  129. var strings = dojoArray.map(results, function (response, index) {
  130. var res = "<h5 class='margin-top-none'>" + visibleLayers[index].layerConfig.displayName + "</h5>" +
  131. RAMP.plugins.featureInfoParser[visibleLayers[index].layerConfig.featureInfo.parser](response);
  132. return res;
  133. });
  134.  
  135. topic.publish(EventManager.GUI.SUBPANEL_OPEN, {
  136. content: strings.join(''),
  137. origin: "wmsFeatureInfo",
  138. update: true,
  139. guid: 'wms-guid'
  140. });
  141.  
  142. }, function (errors) {
  143. console.log('wms errors');
  144. console.log(errors);
  145.  
  146. topic.publish(EventManager.GUI.SUBPANEL_OPEN, {
  147. content: ':(', // FIXME
  148. origin: "wmsFeatureInfo",
  149. update: true,
  150. guid: 'wms-guid'
  151. });
  152.  
  153. });
  154.  
  155. });
  156.  
  157. },
  158.  
  159. /**
  160. * This function is called to register a WMS layer for feature info click events. The parameter wmsData
  161. * should include wmsLayer (an instance of an ESRI WMSLayer) and layerConfig (a reference to the configuration
  162. * node for the WMS layer).
  163. *
  164. * @method registerWMSClick
  165. * @param {Object} wmsData
  166. */
  167. registerWMSClick: function (wmsData) {
  168. wmsClickQueue.push(wmsData);
  169. }
  170.  
  171. };
  172. });