undefined

API Docs for: 5.4.1
Show:

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

  1. /* global define, RAMP, console, i18n, $, document, window */
  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. * ####Imports RAMP Modules:
  15. * {{#crossLink "EventManager"}}{{/crossLink}}
  16. *
  17. * @class MapClickHandler
  18. * @static
  19. * @uses esri/request
  20. * @uses dojo/promise/all
  21. * @uses dojo/_base/array
  22. * @uses dojo/topic
  23. */
  24.  
  25. define([
  26. /* RAMP */
  27. "ramp/eventManager",
  28.  
  29. /* Dojo */
  30. "esri/request", "dojo/promise/all", "dojo/topic"
  31. ],
  32.  
  33. function (
  34. /* RAMP */
  35. EventManager,
  36.  
  37. /* Dojo */
  38. EsriRequest, all, topic
  39. ) {
  40.  
  41. "use strict";
  42. var wmsClickQueue = [], // the queue of WMS layers registered to trigger on click
  43. esriMap; // a local reference to the map object (for pull extent and dimensions)
  44.  
  45. return {
  46.  
  47. /**
  48. * This function should be called after the map has been created. It will subscribe to the Map.CLICK event
  49. * and trigger GUI.SUBPANEL_OPEN events for displaying the response data. Tests RAMP.config.ui.mapQueryToggle.show to
  50. * determine if it should process the query or not.
  51. *
  52. * @method registerWMSClick
  53. * @param {Object} map an EsriMap instance
  54. */
  55. init: function (map) {
  56.  
  57. var modalHeader;
  58.  
  59. esriMap = map;
  60. topic.subscribe(EventManager.Map.CLICK, function (evt) {
  61. var visibleLayers = [],
  62. rqPromises = [];
  63.  
  64. // construct modal header on the first call to prevent potential race condition when locale resources didn't have time to load just yet
  65. if (!modalHeader) {
  66. modalHeader = '<header class="modal-header"><h2 class="modal-title">{0}</h2></header>'.format(i18n.t('mapClickHandler.getFiPanelTitle'));
  67. }
  68.  
  69. if (!RAMP.config.ui.mapQueryToggle.show) {
  70. return;
  71. }
  72.  
  73. console.log(RAMP.layerRegistry);
  74. // filter only currently visible layers
  75. visibleLayers = wmsClickQueue.filter(function (wmsLayer) {
  76. console.log(wmsLayer);
  77. return wmsLayer.visible &&
  78. wmsLayer.id in RAMP.layerRegistry &&
  79. RAMP.layerRegistry[wmsLayer.id] &&
  80. wmsLayer.ramp.loadOk !== false && // can't use loadOk directly because nothing sets it to true
  81. wmsLayer.ramp.state.queryEnabled === true; // check if wmsQuery toggle is set to true
  82. });
  83.  
  84. // if no visible layers return early and do not open the panel
  85. if (visibleLayers.length === 0) {
  86. return;
  87. }
  88.  
  89. topic.publish(EventManager.GUI.SUBPANEL_OPEN, {
  90. panelName: i18n.t('mapClickHandler.getFiPanelName'),
  91. title: i18n.t('mapClickHandler.getFiPanelTitle'),
  92. content: null,
  93. origin: "wmsFeatureInfo",
  94. target: $("#map-div"),
  95. guid: 'wms-guid'
  96. });
  97.  
  98. // create an EsriRequest for each WMS layer (these follow the promise API)
  99. rqPromises = visibleLayers.map(function (wmsLayer) {
  100. try {
  101. var req = {}, wkid, mapSR, srList;
  102. mapSR = wmsLayer.getMap().spatialReference;
  103. srList = wmsLayer.spatialReferences;
  104.  
  105. if (srList && srList.length > 1) {
  106. wkid = srList[0];
  107. } else if (mapSR.wkid) {
  108. wkid = mapSR.wkid;
  109. }
  110. if (wmsLayer.version === "1.3" || wmsLayer.version === "1.3.0") {
  111. req = { CRS: "EPSG:" + wkid, I: evt.screenPoint.x, J: evt.screenPoint.y };
  112. } else {
  113. req = { SRS: "EPSG:" + wkid, X: evt.screenPoint.x, Y: evt.screenPoint.y };
  114. }
  115. $.extend(req, {
  116. SERVICE: "WMS",
  117. REQUEST: "GetFeatureInfo",
  118. VERSION: wmsLayer.version,
  119. BBOX: esriMap.extent.xmin + "," + esriMap.extent.ymin + "," + esriMap.extent.xmax + "," + esriMap.extent.ymax,
  120. WIDTH: esriMap.width,
  121. HEIGHT: esriMap.height,
  122. QUERY_LAYERS: wmsLayer.ramp.config.layerName,
  123. LAYERS: wmsLayer.ramp.config.layerName,
  124. INFO_FORMAT: wmsLayer.ramp.config.featureInfo.mimeType
  125. });
  126. //console.log('BBOX: ' + esriMap.extent.xmin + "," + esriMap.extent.ymin + "," + esriMap.extent.xmax + "," + esriMap.extent.ymax);
  127. //console.log('Clicked at (map units): ' + evt.mapPoint.x + ',' + evt.mapPoint.y);
  128. //console.log('Clicked at (pixels): ' + evt.screenPoint.x + ',' + evt.screenPoint.y);
  129. //console.log('Clicked at (pixels): ' + evt.layerX + ',' + evt.layerY);
  130. return new EsriRequest({
  131. url: wmsLayer.url.split('?')[0],
  132. content: req,
  133. handleAs: "text"
  134. });
  135.  
  136. } catch (exception) {
  137. console.log("WMS Error: " + exception);
  138. }
  139.  
  140. });
  141.  
  142. topic.publish(EventManager.GUI.SUBPANEL_OPEN, {
  143. content: "",
  144. origin: "wmsFeatureInfo",
  145. update: true,
  146. guid: 'wms-guid'
  147. });
  148.  
  149. // wait for all success or any failure in the requests
  150. all(rqPromises).then(function (results) {
  151. console.log('all success');
  152. console.log(results);
  153.  
  154. var strings = results.map(function (response, index) {
  155. var res = "<h5 class='margin-top-none'>" + visibleLayers[index].ramp.config.displayName + "</h5>" +
  156. RAMP.plugins.featureInfoParser[visibleLayers[index].ramp.config.featureInfo.parser](response,visibleLayers[index].id);
  157. return res;
  158. }).join(''), modalBox = '<section id="wms-results-large" class="mfp-hide modal-dialog modal-content overlay-def">{0}<div class="modal-body">{1}</div></section>'.format(modalHeader,strings);
  159.  
  160. $('.sub-panel').on('click', '#wms-expand', function () {
  161. $(document).trigger('open.wb-lbx', [{ src: '#wms-results-large', type: 'inline' }]);
  162. $('#wms-results-large').css({
  163. width: Math.round(window.innerWidth * 0.9) + 'px',
  164. 'max-height': Math.round(window.innerHeight * 0.75) + 'px'
  165. });
  166. });
  167.  
  168. topic.publish(EventManager.GUI.SUBPANEL_OPEN, {
  169. content: '{0}<a id="wms-expand" href="#wms-results-large" role="button" aria-controls="wms-results-large">{2}</a>{1}'.format(modalBox,strings,i18n.t('gui.actions.expand')),
  170. origin: "wmsFeatureInfo",
  171. update: true,
  172. guid: 'wms-guid'
  173. });
  174.  
  175. }, function (errors) {
  176. console.log('wms errors');
  177. console.log(errors);
  178.  
  179. topic.publish(EventManager.GUI.SUBPANEL_OPEN, {
  180. content: String.format("<em>{0}</em>",i18n.t('mapClickHandler.getFiFailed')),
  181. origin: "wmsFeatureInfo",
  182. update: true,
  183. guid: 'wms-guid'
  184. });
  185.  
  186. console.log('subpanel open done');
  187.  
  188. });
  189.  
  190. });
  191.  
  192. },
  193.  
  194. /**
  195. * This function is called to register a WMS layer for feature info click events. The parameter wmsData
  196. * should include wmsLayer (an instance of an ESRI WMSLayer) and layerConfig (a reference to the configuration
  197. * node for the WMS layer).
  198. *
  199. * @method registerWMSClick
  200. * @param {Object} wmsData
  201. */
  202. registerWMSClick: function (wmsData) {
  203. wmsClickQueue.push(wmsData);
  204. }
  205.  
  206. };
  207. });
  208.