Reusable Accessible Mapping Platform

API Docs for: 5.0.0
Show:

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

  1. /* global define, RAMP */
  2. /* jshint bitwise:false */
  3.  
  4. /**
  5. * Utility module containing useful static classes.
  6. *
  7. * @module Utils
  8. * @submodule TmplUtil
  9. */
  10.  
  11. /**
  12. * A set of functions used to support and standardize the use of templating.
  13. *
  14. *
  15. * @class TmplHelper
  16. * @static
  17. * @uses dojo/_base/lang
  18. * @uses GlobalStorage
  19. * @uses RAMP
  20. * @uses TmplUtil
  21. */
  22.  
  23. define([
  24. /* Dojo */
  25. "dojo/_base/lang",
  26.  
  27. /* Ramp Modules */
  28. "ramp/globalStorage",
  29. "ramp/ramp",
  30. "utils/tmplUtil"
  31.  
  32. ],
  33. function (dojoLang, GlobalStorage, Ramp, TmplUtil) {
  34. "use strict";
  35.  
  36. return {
  37. /*
  38. * Create a data wrapper with properties: data, config, str, lyr, fn
  39. * @method dataBuilder
  40. * @param {Object} data A json object passing over.
  41. * @param {String} layerConfig config object for specific layer (optional)
  42. * @returns {Object} Returns a JSON object with following properties
  43. * .data = {}
  44. * .config = <global config object>
  45. * .lyr = <global config object . layers.feature [parameter index] >
  46. * .fn = object with helper functions assigned to it.
  47. *
  48. */
  49. dataBuilder: function (data, layerConfig) {
  50. var dataWrapperPrototype = {
  51. data: null,
  52. config: null,
  53. str: null,
  54. lyr: null,
  55. fn: null
  56. },
  57. dataWrapper = Object.create(dataWrapperPrototype);
  58.  
  59. dataWrapper.data = data;
  60. dataWrapper.config = RAMP.config;
  61.  
  62. if (layerConfig != null) {
  63. dataWrapper.lyr = layerConfig;
  64. }
  65.  
  66. dataWrapper.fn = TmplUtil;
  67. return dataWrapper;
  68. },
  69.  
  70. /*
  71. * Create a data wrapper with properties: data, config, str, fn
  72. * @method genericDataBuilder
  73. * @param {Object} data A json object passing over.
  74. * @returns {Object} Returns a JSON object with following properties
  75. * .data = {}
  76. * .config = <global config object>
  77. * .lyr = <global config object . featurelayers [parameter index] >
  78. * .fn = object with helper functions assigned to it.
  79. *
  80. */
  81. genericDataBuilder: function (data) {
  82. var dataWrapperPrototype = {
  83. data: null,
  84. config: null,
  85. str: null,
  86. fn: null
  87. },
  88. dataWrapper = Object.create(dataWrapperPrototype);
  89.  
  90. dataWrapper.data = data;
  91. dataWrapper.config = RAMP.config;
  92.  
  93. dataWrapper.fn = TmplUtil;
  94. return dataWrapper;
  95. },
  96.  
  97. /*
  98. * strips comments from json template
  99. * @method stringifyTemplate
  100. * @param {String} template A template in JSON format
  101. * @returns {String} A JSON template without comments
  102. *
  103. */
  104. stringifyTemplate: function (template) {
  105. return template
  106. // strip comments from the template
  107. .replace(/`(?:\\.|[^`])*`|'(?:\\.|[^'])*'|"(?:\\.|[^"])*"|\/\*[^]*?\*\/|\/\/.*\n?/g,
  108. function (s) {
  109. if (s.charAt(0) === '/') {
  110. return '';
  111. } else {
  112. return s;
  113. }
  114. })
  115. // remove hard breaks and tabs
  116. .replace(/[\n\r\t]/g, "")
  117. .replace(/>\s*?</g, "><")
  118.  
  119. // strip spaces between html and other tags
  120. .replace(/%}\s*?</g, "%}<")
  121. .replace(/>\s*?{%/g, ">{%")
  122.  
  123. .replace(/"\s*?</g, '"<')
  124. .replace(/>\s*?"/g, '>"');
  125. }
  126. };
  127. });