Reusable Accessible Mapping Platform

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