Reusable Accessible Mapping Platform

API Docs for: 3.0.0
Show:

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

  1. /* global define */
  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 {Number} layerIndex Index value of a layer inside Rest-end url (optional)
  42. * @returns {Object} Returns a JSON object with following properties
  43. * .data = {}
  44. * .config = <global config object>
  45. * .lyr = <global config object . featurelayers [parameter index] >
  46. * .fn = object with helper functions assigned to it.
  47. *
  48. */
  49. dataBuilder: function (data, layerUrl) {
  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 = GlobalStorage.config;
  61.  
  62. if (layerUrl != null) {
  63. //get configuration from the layer with layerIndex
  64. dataWrapper.lyr = Ramp.getLayerConfig(layerUrl);
  65. }
  66.  
  67. dataWrapper.fn = TmplUtil;
  68. return dataWrapper;
  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. * @param {Number} layerIndex Index value of a layer inside Rest-end url (optional)
  75. * @returns {Object} Returns a JSON object with following properties
  76. * .data = {}
  77. * .config = <global config object>
  78. * .lyr = <global config object . featurelayers [parameter index] >
  79. * .fn = object with helper functions assigned to it.
  80. *
  81. */
  82. genericDataBuilder: function (data) {
  83. var dataWrapperPrototype = {
  84. data: null,
  85. config: null,
  86. str: null,
  87. fn: null
  88. },
  89. dataWrapper = Object.create(dataWrapperPrototype);
  90.  
  91. dataWrapper.data = data;
  92. dataWrapper.config = GlobalStorage.config;
  93.  
  94. dataWrapper.fn = TmplUtil;
  95. return dataWrapper;
  96. },
  97.  
  98. /*
  99. * strips comments from json template
  100. * @method stringifyTemplate
  101. * @param {String} template A template in JSON format
  102. * @returns {String} A JSON template without comments
  103. *
  104. */
  105. stringifyTemplate: function (template) {
  106. return template
  107. // strip comments from the template
  108. .replace(/`(?:\\.|[^`])*`|'(?:\\.|[^'])*'|"(?:\\.|[^"])*"|\/\*[^]*?\*\/|\/\/.*\n?/g,
  109. function (s) {
  110. if (s.charAt(0) === '/') {
  111. return '';
  112. } else {
  113. return s;
  114. }
  115. })
  116. // remove hard breaks and tabs
  117. .replace(/[\n\r\t]/g, "")
  118. .replace(/>\s*?</g, "><");
  119. }
  120. };
  121. });