Reusable Accessible Mapping Platform

API Docs for: 3.0.0
Show:

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

  1. /*global define, $, esri, tmpl */
  2. /*jslint white: true */
  3.  
  4. /**
  5. *
  6. * @module RAMP
  7. */
  8.  
  9. /**
  10. * Populates the BasemapGallery widget located in the maps toolbar with items found in the application configuration.
  11. * This module also handles all the event needed to change the map basemap and update the UI
  12. *
  13. *
  14. * @class BaseMapSelector
  15. * @static
  16. * @uses dojo/_base/array
  17. * @uses dojo/_base/lang
  18. * @uses dojo/dom-attr
  19. * @uses dojo/query
  20. * @uses dojo/topic
  21. * @uses templates/basemap_selector_template.json
  22. * @uses GlobalStorage
  23. * @uses Map
  24. * @uses EventManager
  25. * @uses esri/dijit/BasemapGallery
  26. * @uses utils/popupManager
  27. * @uses utils/thmplHelper
  28. */
  29.  
  30. define([
  31. // Dojo
  32. "dojo/_base/array", "dojo/_base/lang", "dojo/dom-attr", "dojo/query", "dojo/topic",
  33. // Templates
  34. "dojo/text!./templates/basemap_selector_template.json",
  35. // Ramp
  36. "ramp/globalStorage", "ramp/map", "ramp/eventManager",
  37. // Esri
  38. "esri/dijit/BasemapGallery",
  39. // Util
  40. "utils/popupManager", "utils/tmplHelper", "utils/array"],
  41.  
  42. function (
  43. // Dojo
  44. dojoArray, dojoLang, domAttr, query, topic,
  45. // Templates
  46. basemapselectorTemplate,
  47. // Ramp
  48. GlobalStorage, RampMap, EventManager,
  49. // Esri
  50. BasemapGallery,
  51. // Util
  52. PopupManager, TmplHelper, UtilArray) {
  53. "use strict";
  54.  
  55. var basemapGallery,
  56. basemaps = [],
  57. config,
  58.  
  59. placementAnchorId = "basemapGallery",
  60.  
  61. baseMapControls,
  62. baseMapToggle,
  63. basemapGalleryNode,
  64.  
  65. cssButtonPressedClass = "button-pressed",
  66.  
  67. ui = {
  68. /**
  69. * Initiates additional UI components of the widget, setting listeners and registering the popup functionality
  70. *
  71. * @method init
  72. * @private
  73. * @return {object} itself
  74. *
  75. */
  76. init: function () {
  77. baseMapControls = $("#basemapControls");
  78. baseMapToggle = $("#baseMapToggle");
  79. basemapGalleryNode = $("#basemapGallery").attr("role", "listbox");
  80.  
  81. // Set alt text for selector thumbnails
  82. dojoArray.forEach(config.basemaps, function (basemap) {
  83. domAttr.set(query(String.format("#galleryNode_{0} img", basemap.id))[0], "alt", basemap.altText);
  84. });
  85.  
  86. // load JSON templates for basemap and skin every node under the basemap selector
  87. tmpl.templates = JSON.parse(TmplHelper.stringifyTemplate(basemapselectorTemplate));
  88. dojoArray.forEach($(".esriBasemapGalleryNode"), function (node, i) {
  89. $(node).html(tmpl(config.siteTemplate.basemapTemplate, TmplHelper.dataBuilder(config.basemaps[i])));
  90. });
  91.  
  92. // turn on the opening and closing of the basemap selector section
  93. PopupManager.registerPopup(baseMapControls, "hoverIntent",
  94. function (d) {
  95. basemapGalleryNode.slideDown("fast", function () { d.resolve(); });
  96. },
  97. {
  98. activeClass: cssButtonPressedClass,
  99. target: basemapGalleryNode,
  100. closeHandler: function (d) {
  101. basemapGalleryNode.slideUp("fast", function () { d.resolve(); });
  102. },
  103. timeout: 500
  104. }
  105. );
  106.  
  107. topic.publish(EventManager.BasemapSelector.UI_COMPLETE, { title: basemaps[0].title });
  108.  
  109. return this;
  110. },
  111. /*
  112. * Changes the text shown on the toolbar to match the currently selected basemap's title
  113. * @method updateToggleLabel
  114. * @private
  115. *
  116. */
  117. updateToggleLabel: function () {
  118. baseMapToggle.find("span:first").text(basemapGallery.getSelected().title);
  119. }
  120. };
  121.  
  122. /**
  123. * Initializes functions that publish events.
  124. *
  125. * @method initTopics
  126. * @private
  127. */
  128. function initTopics() {
  129. /* PUBLISH */
  130. basemapGallery.on("selection-change", function () {
  131. var basemap = basemapGallery.getSelected();
  132.  
  133. ui.updateToggleLabel();
  134. topic.publish(EventManager.BasemapSelector.BASEMAP_CHANGED, {
  135. id: basemap.id,
  136. title: basemap.title,
  137. cssStyle: basemap.scaleCssClass
  138. });
  139. });
  140. }
  141.  
  142. /**
  143. * Initializes class listeners.
  144. *
  145. * @method initListeners
  146. * @private
  147. */
  148. function initListeners() {
  149. /* SUBSCRIBE */
  150. topic.subscribe(EventManager.BasemapSelector.TOGGLE, function (eventArg) {
  151. basemapGallery.select(eventArg.id);
  152. });
  153. }
  154.  
  155. return {
  156. /*
  157. * Adds all of the basemaps specified in the application configuration to the basemap selector widget and then calls function to initializes event handling
  158. * @method init
  159. * @constructor
  160. *
  161. */
  162. init: function () {
  163. config = GlobalStorage.config;
  164.  
  165. dojoArray.forEach(config.basemaps, function (basemap) {
  166. var layerDijit, basemapDijit;
  167.  
  168. layerDijit = new esri.dijit.BasemapLayer({ url: basemap.url });
  169. basemapDijit = new esri.dijit.Basemap({
  170. id: basemap.id,
  171. layers: [layerDijit],
  172. title: String.format("{0} ({1})", basemap.name, basemap.type),
  173. thumbnailUrl: basemap.thumbnail
  174. });
  175. basemapDijit.scaleCssClass = basemap.scaleCssClass;
  176.  
  177. basemaps.push(basemapDijit);
  178. });
  179.  
  180. //Create and start the selector
  181. basemapGallery = new BasemapGallery({
  182. showArcGISBasemaps: false,
  183. basemaps: basemaps,
  184. map: RampMap.getMap()
  185. }, placementAnchorId);
  186.  
  187. basemapGallery.startup();
  188.  
  189. var startId = UtilArray.find(GlobalStorage.config.basemaps, function (basemap) {
  190. return basemap.showOnInit;
  191. }).id;
  192.  
  193. basemapGallery.select(startId);
  194.  
  195. //take over the click
  196. $(".esriBasemapGalleryNode").on("mousedown keyup", function (evt) {
  197. if (evt.which === 1 || evt.which === 13 || evt.which === 32) {
  198. var curr_id = basemapGallery.getSelected().id,
  199. selected_node = evt.currentTarget.id,
  200. selected_id = selected_node.slice(selected_node.indexOf("_") + 1);
  201.  
  202. if (curr_id === selected_id) {
  203. return false; //prevent the basemap from changing
  204. } else { //didn't select the same basemap
  205. basemapGallery.select(selected_id);
  206. }
  207. }
  208. });
  209.  
  210. initTopics();
  211. initListeners();
  212.  
  213. ui
  214. .init()
  215. .updateToggleLabel();
  216. }
  217. };
  218. });