Reusable Accessible Mapping Platform

API Docs for: 4.0.0
Show:

File: src\js\RAMP\Utils\tmplUtil.js

  1. /* global define, console */
  2.  
  3. /**
  4. * Utility module containing useful static classes.
  5. *
  6. * @module Utils
  7. */
  8.  
  9. /**
  10. * A set of functions that can be accessed within templates
  11. *
  12. *
  13. * @class TmplUtil
  14. * @static
  15. */
  16.  
  17. define(["ramp/globalStorage"],
  18. function (GlobalStorage) {
  19. "use strict";
  20.  
  21. return {
  22. /**
  23. * Given a feature object or a graphic object (or any object that has a getLayer method and an
  24. * attributes field) return the image URL for that feature/graphic object.
  25. *
  26. * NOTE: all dependent functions should be written as nested functions inside the caller function, otherwise TmplEx templating library won't identify
  27. *
  28. * @method getGraphicIcon
  29. * @param {Graphic} graphic
  30. * @param {Object} layerConfig
  31. * @return {String} imageUrl Url to the features symbology image
  32. */
  33. getGraphicIcon: function (graphic, layerConfig) {
  34. var i, symbolConfig = layerConfig.symbology, img = "";
  35.  
  36. switch (symbolConfig.type) {
  37. case "simple":
  38. return symbolConfig.imageUrl;
  39.  
  40. case "uniqueValue":
  41. //make a key value for the graphic in question, using comma-space delimiter if multiple fields
  42. var graphicKey = graphic.attributes[symbolConfig.field1];
  43. if (symbolConfig.field2) {
  44. graphicKey = graphicKey + ", " + graphic.attributes[symbolConfig.field2];
  45. if (symbolConfig.field3) {
  46. graphicKey = graphicKey + ", " + graphic.attributes[symbolConfig.field3];
  47. }
  48. }
  49.  
  50. //search the value maps for a matching entry. if no match found, use the default image
  51. for (i = 0; i < symbolConfig.valueMaps.length; i++) {
  52. if (symbolConfig.valueMaps[i].value === graphicKey) {
  53. img = symbolConfig.valueMaps[i].imageUrl;
  54. break;
  55. }
  56. }
  57.  
  58. if (img === "") {
  59. img = symbolConfig.defaultImageUrl;
  60. }
  61.  
  62. return img;
  63.  
  64. case "classBreaks":
  65.  
  66. var gVal, lower, upper;
  67. gVal = graphic.attributes[symbolConfig.field];
  68.  
  69. //find where the value exists in the range
  70. lower = symbolConfig.minValue;
  71.  
  72. if (gVal < lower) {
  73. img = symbolConfig.defaultImageUrl;
  74. } else {
  75. // a trick to prime the first loop iteration
  76. // the first low value is inclusive. every other low value is exclusive.
  77. // if we have entered this else bracket, we know we are not below the first lower limit.
  78. // so we reduce lower by 1 to make the first exclusive test inclusive
  79. upper = lower - 1;
  80.  
  81. for (i = 0; i < symbolConfig.rangeMaps.length; i++) {
  82. lower = upper;
  83. upper = symbolConfig.rangeMaps[i].maxValue;
  84. if ((gVal > lower) && (gVal <= upper)) {
  85. img = symbolConfig.rangeMaps[i].imageUrl;
  86. break;
  87. }
  88. }
  89.  
  90. if (img === "") {
  91. //no match in defined ranges.
  92. img = symbolConfig.defaultImageUrl;
  93. }
  94. }
  95.  
  96. return img;
  97.  
  98. default:
  99. return symbolConfig.icons["default"].imageUrl;
  100. }
  101. },
  102.  
  103. /**
  104. * Given a feature object or a graphic object (or any object that has a getLayer method and an
  105. * attributes field) return the attribute value for its designed "name" field
  106. *
  107. * NOTE: all dependent functions should be written as nested functions inside the caller function, otherwise TmplEx templating library won't identify
  108. *
  109. * @method getFeatureName
  110. * @param {Graphic} graphic
  111. * @param {Object} layerConfig
  112. * @return {String} imageUrl Url to the features symbology image
  113. */
  114. getFeatureName: function (graphic, layerConfig) {
  115. return graphic.attributes[layerConfig.nameField];
  116. },
  117.  
  118. /**
  119. * Given a feature object return the objectid for that item.
  120. * This will likely fail on a non-feature object (e.g. a plain graphic)
  121. *
  122. * NOTE: all dependent functions should be written as nested functions inside the caller function, otherwise TmplEx templating library won't identify
  123. *
  124. * @method getObjectId
  125. * @param {Graphic} graphic
  126. * @return {Integer} objectId
  127. */
  128. getObjectId: function (graphic) {
  129. return graphic.attributes[graphic.getLayer().objectIdField];
  130. },
  131. /*
  132. * Helper function, get attribute value by field name
  133. *
  134. * @method getAttributeValueByName
  135. * @param {Object} graphic ?
  136. * @param {String} fieldName ?
  137. */
  138. getAttributeValueByName: function (graphic, fieldName) {
  139. return graphic.attributes[fieldName];
  140. },
  141.  
  142. /* Helper function used by filterManager.*/
  143. /*
  144. * generate visibility legend object
  145. * @param o
  146. */
  147. generateVisibilityLegend: function (o) {
  148. var attr = "",
  149. visibilityLegendLabel = {
  150. for: "filterGroup_" + o.data[o.idx].id,
  151. attr: attr,
  152. value: o.data[o.idx].id,
  153. checked: "checked",
  154. label: o.data[o.idx].layerConfig.displayName,
  155. class: "eye checked",
  156. layerId: o.data[o.idx].id
  157. };
  158. return visibilityLegendLabel;
  159. },
  160. /*
  161. * generate visibility legend object
  162. * @param o
  163. */
  164. generateBoundingBoxLegend: function (o) {
  165. // adding flag for the generated o object
  166. // o.disabled will indicate the bounding checkbox is to be disabled.
  167. var checkboxDisabled = false,
  168. attr = "",
  169. boundingLegendLabel;
  170.  
  171. // determine if given layer is static or WMS
  172. checkboxDisabled = Boolean(o.data[o.idx].ramp.type === GlobalStorage.layerType.Static ||
  173. o.data[o.idx].ramp.type === GlobalStorage.layerType.WMS);
  174.  
  175. boundingLegendLabel = {
  176. for: "filterGroup_" + o.data[o.idx].id + "1",
  177. attr: attr + "1",
  178. value: o.data[o.idx].id,
  179. checked: "checked",
  180. label: o.data[o.idx].layerConfig.displayName,
  181. class: "box checked",
  182. disabled: checkboxDisabled,
  183. layerId: o.data[o.idx].id
  184. };
  185.  
  186. return boundingLegendLabel;
  187. },
  188.  
  189. /*
  190. * Generate settings toggle object.
  191. *
  192. * @method generateSettingsToggle
  193. * @param o
  194. */
  195. generateSettingsToggle: function (o) {
  196. var //attr = "",
  197. boundingLegendLabel = {
  198. str: o.str,
  199. layerId: o.data[o.idx].id,
  200. settings: o.data[o.idx].layerConfig.settings
  201. };
  202.  
  203. return boundingLegendLabel;
  204. },
  205.  
  206. /**
  207. * Gets an array of symbology images to display in the layer selector
  208. * @method getSymbolForLayer
  209. * @param {Object} layerConfig A layer's config object
  210. * @returns {icon} The array of icon(s) to use in layer selector
  211. */
  212. getSymbolForLayer: function (layerConfig) {
  213. //will take a symbol list that has 1 or more entries. will return first 3. if fewer than 3, will duplicate values
  214. function pick3(symbolList) {
  215. var num = symbolList.length, indexes;
  216.  
  217. if (num > 2) {
  218. //pick first 3
  219. indexes = [0, 1, 2];
  220. } else if (num === 2) {
  221. //duplicate the first
  222. indexes = [0, 1, 0];
  223. } else if (num === 1) {
  224. //triple whammy
  225. indexes = [0, 0, 0];
  226. } else {
  227. //something is ruined
  228. return ["", "", ""];
  229. }
  230.  
  231. //return images in an array
  232. return [symbolList[indexes[0]].imageUrl, symbolList[indexes[1]].imageUrl, symbolList[indexes[2]].imageUrl];
  233. }
  234.  
  235. if (layerConfig.symbology) {
  236. //feature layer. make an array for the appropriate renderer
  237.  
  238. var symbNode = layerConfig.symbology;
  239. switch (symbNode.type) {
  240. case "simple":
  241. return [symbNode.imageUrl];
  242. case "uniqueValue":
  243. return pick3(symbNode.valueMaps);
  244. case "classBreaks":
  245. return pick3(symbNode.rangeMaps);
  246. default:
  247. //we have an unknown renderer type. at this point, something else would have failed most likely. write out a screech to the console just incase
  248. console.log('unknown renderer encountered: ' + symbNode.type);
  249. return [""];
  250. }
  251. } else {
  252. //no symbology defined, assume a WMS
  253.  
  254. if (layerConfig.imageUrl) {
  255. return [layerConfig.imageUrl];
  256. } else {
  257. //catch-all in the case that things are really messed up
  258. console.log('layer with no image info for layer selector');
  259. return [""];
  260. }
  261. }
  262. }
  263. };
  264. });