Reusable Accessible Mapping Platform

API Docs for: 5.0.0
Show:

File: src\js\RAMP\Modules\layerGroup.js

  1. /* global define, tmpl, $, console */
  2.  
  3. /**
  4. * @module RAMP
  5. * @submodule FilterManager
  6. * @main FilterManager
  7. */
  8.  
  9. /**
  10. *
  11. * ####Imports RAMP Modules:
  12. * {{#crossLink "TmplHelper"}}{{/crossLink}}
  13. * {{#crossLink "Array"}}{{/crossLink}}
  14. * {{#crossLink "LayerItem"}}{{/crossLink}}
  15. *
  16. * ####Uses RAMP Templates:
  17. * {{#crossLink "templates/layer_selector_template.json"}}{{/crossLink}}
  18. *
  19. * @class LayerGroup
  20. * @constructor
  21. * @uses dojo/Evented
  22. * @uses dojo/_base/declare
  23. * @uses dojo/_base/lang
  24. * @uses dojo/_base/array
  25. *
  26. * @param {Array} layers an array of layer config definitions to be added to the group
  27. * @param {Object} [options] Additional options
  28. *
  29. * @param {String} [options.groupType] Specifies type of this LayerGroup and the name of the layer group template to use
  30. * @param {String} [options.layerState] Specifies the initial state of any LyerItem added to this group; must be one of the `LayerItem.state` defaults
  31. * @param {String} [options.layerType] Specifies type of any LayerItem added to this group and the name of the layer item template to use
  32. *
  33. * @param {Object} [options.stateMatrix] additional state matrix records to be mixed into the default
  34. * @param {Object} [options.transitionMatrix] additional state transition matrix records to be mixed into the default
  35.  
  36. *
  37. * @return {LayerGroup} A control object representing a group of layers allowing to dynamically change their states.
  38. */
  39.  
  40. define([
  41. "dojo/Evented", "dojo/_base/declare", "dojo/_base/lang", "dojo/_base/array",
  42.  
  43. /* Text */
  44. "dojo/text!./templates/layer_selector_template.json",
  45.  
  46. /* Util */
  47. "utils/tmplHelper", "utils/array", "utils/dictionary",
  48.  
  49. /* RAMP */
  50. "ramp/layerItem"
  51. ],
  52. function (
  53. Evented, declare, lang, dojoArray,
  54. layer_selector_template,
  55. TmplHelper, UtilArray, UtilDict,
  56. LayerItem) {
  57. "use strict";
  58.  
  59. return declare([Evented], {
  60. constructor: function (layers, options) {
  61. var that = this;
  62.  
  63. // declare individual properties inside the constructor: http://dojotoolkit.org/reference-guide/1.9/dojo/_base/declare.html#id6
  64. lang.mixin(this,
  65. {
  66. /**
  67. * A node of the LayerGroup.
  68. *
  69. * @property node
  70. * @type JObject
  71. * @default null
  72. */
  73. node: null,
  74.  
  75. /**
  76. * A node of the list in the LayerGroup.
  77. *
  78. * @property node
  79. * @private
  80. * @type JObject
  81. * @default null
  82. */
  83. _listNode: null,
  84.  
  85. /**
  86. * Templates to be used in construction of the layer nodes.
  87. *
  88. * @property templates
  89. * @type Object
  90. * @default layer_selector_template.json
  91. */
  92. templates: JSON.parse(TmplHelper.stringifyTemplate(layer_selector_template)),
  93.  
  94. /**
  95. * Specifies type of this LayerGroup and the name of the layer group template to use; is set by `groupType` value;
  96. *
  97. * @property groupType
  98. * @type String
  99. * @default "layer_group"
  100. */
  101. groupType: "layer_group",
  102.  
  103. /**
  104. * Specifies type of any LayerItem added to this group during initialization and the name of the layer item template to use; is set by `layerType` value;; can be overwritten when adding individual layers by `options.type`.
  105. *
  106. * @property type
  107. * @type String
  108. * @default null
  109. */
  110. layerType: null,
  111.  
  112. /**
  113. * State of any LayerItem added to this group during its initialization; is set by `layerSate` value; can be overwritten when adding individual layers by `options.state`.
  114. *
  115. * @property state
  116. * @type String
  117. * @default LayerItem.state.DEFAULT
  118. */
  119. layerState: LayerItem.state.DEFAULT,
  120.  
  121. /**
  122. * An array of layer config definitions to be added to the group during initialization; is set to `layers` value.
  123. *
  124. * @property layers
  125. * @type Array
  126. * @default []
  127. */
  128. layers: [],
  129.  
  130. /**
  131. * An array of resulting LayerItem objects.
  132. *
  133. * @property layerItems
  134. * @type Array
  135. * @default []
  136. */
  137. layerItems: []
  138. },
  139. options,
  140. {
  141. layers: layers
  142. }
  143. );
  144.  
  145. // create group node from the template
  146. this.node = $(this._template(this.groupType));
  147. this._listNode = this.node.find("ul");
  148.  
  149. console.debug(LayerItem.state);
  150.  
  151. this.layers.forEach(function (layer) {
  152. that.addLayerItem(layer);
  153. });
  154. },
  155.  
  156. /**
  157. * Constructs and adds a LayerItem to the LayerGroup.
  158. *
  159. * @param {Object} layer config of the layer to be added
  160. * @param {Object} options additional options allowing for customization
  161. * @method addLayerItem
  162. * @return {Object} the item that was added
  163. */
  164. addLayerItem: function (layer, options) {
  165. var layerItem,
  166. layerItemOptions = {
  167. stateMatrix: this._constructStateMatrix(layer)
  168. };
  169.  
  170. lang.mixin(layerItemOptions,
  171. {
  172. state: this.layerState,
  173. type: this.layerType
  174. },
  175. options
  176. );
  177.  
  178. layerItem = new LayerItem(layer, layerItemOptions);
  179.  
  180. this.layerItems.push(layerItem);
  181. this._listNode.prepend(layerItem.node);
  182.  
  183. if (this.layerItems.length === 1) {
  184. this.node.show();
  185. }
  186.  
  187. return layerItem;
  188. },
  189.  
  190. /**
  191. * Removes the specified LayerItem from the LayerGroup.
  192. *
  193. * @param {String} layerId id of the layer to be removed
  194. * @method removeLayerItem
  195. * @return {Object} this LayerGroup for chaining
  196. */
  197. removeLayerItem: function (layerId) {
  198. var layerItem = this.getLayerItem(layerId);
  199.  
  200. // remove layerItem from DOM
  201. layerItem.node.remove();
  202.  
  203. // remove layerItem from the list
  204. UtilArray.remove(this.layerItems, layerItem, function (l) {
  205. return l.id === layerItem.id;
  206. });
  207.  
  208. if (this.layerItems.length === 0) {
  209. this.node.hide();
  210. }
  211.  
  212. return this;
  213. },
  214.  
  215. /**
  216. * Modifies the state matrix of the layer to accommodate types of layers that might not use/have all the default controls or have extra controls.
  217. *
  218. * @param {Object} layerConfig layer config
  219. * @method _constructStateMatrix
  220. * @private
  221. * @return {Object} modified layer state matrix
  222. */
  223. _constructStateMatrix: function (layerConfig) {
  224. var stateMatrix = lang.clone(LayerItem.stateMatrix);
  225.  
  226. if (!layerConfig.settings.panelEnabled) {
  227. this._removeStateMatrixPart(stateMatrix, "controls", LayerItem.controls.SETTINGS);
  228. }
  229.  
  230. // remove bounding box toggle if there is no layer extent property - layer is a wms layer
  231. if (!layerConfig.layerExtent || layerConfig.isStatic) {
  232. this._removeStateMatrixPart(stateMatrix, "toggles", LayerItem.toggles.BOX);
  233. this._addStateMatrixPart(stateMatrix, "toggles", LayerItem.toggles.PLACEHOLDER);
  234. }
  235.  
  236. return stateMatrix;
  237. },
  238.  
  239. /**
  240. * Modifies the state matrix by adding specified partKey to the specified partType collection
  241. *
  242. * @param {Object} stateMatrix matrix to modify
  243. * @param {String} partType type of the parts to modify: `controls`, `toggles`, `notices`
  244. * @param {String} partKey part key to be inserted into the collection
  245. * @method _addStateMatrixPart
  246. * @private
  247. */
  248. _addStateMatrixPart: function (stateMatrix, partType, partKey) {
  249. UtilDict.forEachEntry(stateMatrix, function (state, data) {
  250. data[partType].push(partKey);
  251. });
  252. },
  253.  
  254. /**
  255. * Modifies the state matrix by removing specified partKey to the specified partType collection
  256. *
  257. * @param {Object} stateMatrix matrix to modify
  258. * @param {String} partType type of the parts to modify: `controls`, `toggles`, `notices`
  259. * @param {String} partKey part key to be removed into the collection
  260. * @method _addStateMatrixPart
  261. * @private
  262. */
  263. _removeStateMatrixPart: function (stateMatrix, partType, partKey) {
  264. UtilDict.forEachEntry(stateMatrix, function (state, data) {
  265. UtilArray.remove(data[partType], partKey);
  266. });
  267. },
  268.  
  269. /**
  270. * Constructs and adds a LayerItem to the LayerGroup.
  271. *
  272. * @param {String} layerId an id of the LayerItem to set the state on
  273. * @param {String} state state to be set; must be one of the `LayerItem.state` defaults
  274. * @param {Object} options additional options allowing for customization
  275. * @method setState
  276. */
  277. setState: function (layerId, state, options) {
  278. var layerItem = this.getLayerItem(layerId);
  279.  
  280. if (layerItem) {
  281. layerItem.setState(state, options);
  282. }
  283. },
  284.  
  285. /**
  286. * Finds and returns a LayerItem object with the specified id. If none found, returns null.
  287. *
  288. * @param {String} layerId an id of the LayerItem to return
  289. * @return {LayerItem} a LayerItem with the specified id
  290. * @method getLayerItem
  291. */
  292. getLayerItem: function (layerId) {
  293. var layerItem;
  294.  
  295. layerItem = UtilArray.find(this.layerItems, function (li) {
  296. return li.id === layerId;
  297. });
  298.  
  299. return layerItem;
  300. },
  301.  
  302. /**
  303. * Populates a template specified by the key with the supplied data.
  304. *
  305. * @param {String} key template name
  306. * @param {Object} data data to be inserted into the template
  307. * @method _template
  308. * @private
  309. * @return {String} a string template filled with supplied data
  310. */
  311. _template: function (key, data) {
  312. tmpl.cache = {};
  313. tmpl.templates = this.templates;
  314.  
  315. data = data || {};
  316.  
  317. return tmpl(key, data);
  318. }
  319. });
  320. });