Reusable Accessible Mapping Platform

API Docs for: 5.0.0
Show:

File: src\js\RAMP\bootstrapper.js

  1. /*global require, window, dojoConfig, i18n, document, $, console, RAMP */
  2.  
  3. /**
  4. * Ramp module
  5. *
  6. * @module RAMP
  7. * @main RAMP
  8. */
  9.  
  10. /**
  11. * Bootstrapper class.
  12. * Starting point of RAMP, RAMP modules are loaded here and mapped to a function parameter
  13. * Phase X?: For mobile support, there can be a different mobileBootstrapper with only the mobile modules loaded
  14. *
  15. * @class Bootstrapper
  16. * @static
  17. *
  18. * @uses dojo/parser
  19. * @uses dojo/on
  20. * @uses dojo/topic
  21. * @uses dojo/request/script
  22. * @uses dojo/request/xhr
  23. *
  24. * @uses Map
  25. * @uses Basemapselector
  26. * @uses Maptips
  27. * @uses Datagrid
  28. * @uses Navigation
  29. * @uses FilterManager
  30. * @uses BookmarkLink
  31. * @uses Url
  32. * @uses FeatureHighlighter
  33. * @uses Ramp
  34. * @uses GlobalStorage
  35. * @uses GUI
  36. * @uses EventManager
  37. * @uses AdvancedToolbar
  38. * @uses Util
  39. * @uses Prototype
  40. * @uses FunctionMangler
  41. * @uses LayerLoader
  42. */
  43.  
  44. require([
  45. /* Dojo */
  46. "dojo/parser", "dojo/on", "dojo/topic",
  47. "dojo/request/script",
  48. "dojo/request/xhr", "dojo/_base/array",
  49. "esri/config",
  50.  
  51. /* RAMP */
  52. "ramp/map", "ramp/basemapSelector", "ramp/maptips", "ramp/datagrid",
  53. "ramp/navigation", "ramp/filterManager", "ramp/bookmarkLink",
  54. "utils/url", "ramp/featureHighlighter",
  55. "ramp/ramp", "ramp/globalStorage", "ramp/gui", "ramp/eventManager",
  56. "ramp/advancedToolbar",
  57. "ramp/theme", "ramp/layerLoader", "ramp/dataLoaderGui", "ramp/dataLoader",
  58. /* Utils */
  59. "utils/util",
  60.  
  61. /* Plugins */
  62. "utils/prototype!", "utils/functionMangler!"],
  63.  
  64. //"dojo/domReady!"],
  65.  
  66. function (
  67. /* Dojo */
  68. parser, dojoOn, topic, requestScript, xhr, dojoArray,
  69. esriConfig,
  70.  
  71. /* RAMP */
  72. RampMap, BasemapSelector, Maptips, Datagrid, NavWidget, FilterManager,
  73. BookmarkLink, Url, FeatureHighlighter,
  74. Ramp, GlobalStorage, gui, EventManager, AdvancedToolbar, theme, LayerLoader, DataLoadedGui, DataLoader,
  75.  
  76. /* Utils */
  77. UtilMisc
  78. ) {
  79. "use strict";
  80.  
  81. /**
  82. * loadPlugin takes a plugin file and loads it into the DOM.
  83. * Creates a dynamic script tag to load the script at runtime.
  84. *
  85. * @method loadPlugin
  86. * @private
  87. * @param {String} pluginName, the file name of the plugin to be loaded (should be in the plugins folder)
  88. */
  89. function loadPlugin(pluginName) {
  90. var head = document.getElementsByTagName('head')[0],
  91. script = document.createElement('script');
  92. script.type = 'text/javascript';
  93. script.src = dojoConfig.fullPluginPath + pluginName;
  94. console.log('loading plugin: ' + script.src);
  95. head.appendChild(script);
  96. }
  97.  
  98. function initializeMap() {
  99. /* Start - RAMP Events, after map is loaded */
  100.  
  101. topic.subscribe(EventManager.Map.INITIAL_BASEMAP_LOADED, function () {
  102. console.log("map - >> first update-end; init the rest");
  103.  
  104. // Only initialize the bookmark link after all the UI events of all other modules have
  105. // finished loading
  106. // IMPORTANT: for now, only basemapselector and filtermanager have a UI complete event
  107. // but in the future, if other modules start publishing their own UI complete events, it needs
  108. // to be subscribe to here so BookmarkLink will not attempt to call the module before its GUI
  109. // has finished rendering
  110. UtilMisc.subscribeAll(
  111. [
  112. EventManager.BasemapSelector.UI_COMPLETE,
  113. EventManager.FilterManager.UI_COMPLETE
  114. ], function () {
  115. BookmarkLink.subscribeAndUpdate();
  116.  
  117. //RampMap.zoomToLayerScale();
  118. });
  119. // Added current level so slider will know how to adjust the position
  120. var currentLevel = (RampMap.getMap().__LOD.level) ? RampMap.getMap().__LOD.level : 0;
  121.  
  122. NavWidget.init(currentLevel);
  123. FeatureHighlighter.init();
  124.  
  125. Maptips.init();
  126.  
  127. //Apply listeners for basemap gallery
  128. BasemapSelector.init();
  129.  
  130. //initialize the filter
  131. FilterManager.init();
  132.  
  133. DataLoadedGui.init();
  134.  
  135. // Initialize the advanced toolbar and tools.
  136. if (RAMP.config.advancedToolbar.enabled) {
  137. AdvancedToolbar.init();
  138. }
  139.  
  140. Datagrid.init();
  141. theme.tooltipster();
  142.  
  143. //start loading the layers
  144. dojoArray.forEach(RAMP.startupLayers, function (layer) {
  145. LayerLoader.loadLayer(layer);
  146. });
  147. });
  148.  
  149. RampMap.init();
  150. NavWidget.construct();
  151.  
  152. // a workaround for bug#3460; ideally each module's ui component would call tooltipster on its own; probably a good idea would to implement this when working on mobile view
  153. theme.tooltipster();
  154.  
  155. /* End - RAMP Events */
  156. }
  157. /* End - Bootstrapper functions */
  158.  
  159. // Check to make sure the console exists, redefines it to the no-op function
  160. // if it does not (e.g. in IE when the debugger is not on)
  161. UtilMisc.checkConsole();
  162.  
  163. // Once all of our modules are loaded and the DOM is ready:
  164.  
  165. // call the parser to create the dijit layout dijits
  166. parser.parse();
  167.  
  168. //To hold values from RAMP service
  169.  
  170. var lang = $("html").attr("lang"),
  171. configFile,
  172. defJson;
  173.  
  174. if (lang !== "en" && lang !== "fr") {
  175. lang = "en";
  176. }
  177.  
  178. RAMP.locale = lang;
  179.  
  180. i18n.init(
  181. {
  182. lng: lang + "-CA",
  183. load: "current",
  184. fallbackLng: false
  185. });
  186.  
  187. //loading config object from JSON file
  188. configFile = (lang === "fr") ? "config.fr.json" : "config.en.json";
  189.  
  190. // Request the JSON config file
  191. defJson = xhr(configFile, {
  192. handleAs: "json"
  193. });
  194.  
  195. defJson.then(
  196. function (fileConfig) {
  197. //there is no need to convert the result to an object. it comes through pre-parsed
  198. if (!RAMP.configServiceURL) {
  199. //no config service. we just use the file provided
  200. configReady(fileConfig);
  201. } else {
  202. //get additional config stuff from the config service. mash it into our primary object
  203.  
  204. // pull smallkeys from URL
  205. var siteURL = new Url(require.toUrl(document.location)),
  206. smallkeys = siteURL.queryObject.keys;
  207.  
  208. if (!smallkeys || smallkeys === "") {
  209. //no keys. no point hitting the service. jump to next step
  210. configReady(fileConfig);
  211. } else {
  212. //TODO verify endpoint is correct
  213. var serviceUrl = RAMP.configServiceURL + "docs/" + $("html").attr("lang") + "/" + smallkeys,
  214. defService = requestScript.get(serviceUrl, { jsonp: 'callback', timeout: 2000 });
  215.  
  216. //Request the JSON snippets from the RAMP Config Service
  217.  
  218. //NOTE: XHR cannot be used here for cross domain purposes (primarily when running thru visual studio).
  219. // we use request/script instead to get the config as jsonp
  220. // we may consider looking into ways to mitiate the cross domain issue (Aly had some ideas)
  221.  
  222. defService.then(
  223. function (serviceContent) {
  224. console.log(serviceContent);
  225.  
  226. //we are expecting an array of JSON config fragments
  227. //merge each fragment into the file config
  228.  
  229. dojoArray.forEach(serviceContent, function (configFragment) {
  230. UtilMisc.mergeRecursive(fileConfig, configFragment);
  231. });
  232.  
  233. //fragments are now in fileConfig. carry on.
  234. configReady(fileConfig);
  235. },
  236. function (error) {
  237. console.log("An error occurred: " + error);
  238. }
  239. );
  240. }
  241. }
  242. },
  243. function (error) {
  244. console.log("An error occurred when retrieving the JSON Config: " + error);
  245. }
  246. );
  247.  
  248. /**
  249. * once the config file has been retrieved, proceed with the loading of the site
  250. *
  251. * @method configReady
  252. * @private
  253. * @param {Object} configObject the configuration object
  254. */
  255. function configReady(configObject) {
  256. var pluginConfig,
  257. advancedToolbarToggle = $("li.map-toolbar-item #advanced-toggle").parent();
  258.  
  259. console.log("Bootstrapper: config loaded");
  260.  
  261. GlobalStorage.init(configObject);
  262. GlobalStorage.defineProjections(window.proj4);
  263.  
  264. esriConfig.defaults.io.proxyUrl = RAMP.config.proxyUrl;// "/proxy/proxy.ashx";
  265. // try to avoid the proxy if possible, but this will cause network errors if CORS is not allowed by the target server
  266. esriConfig.defaults.io.corsDetection = true;
  267.  
  268. // Show or remove advanced toolbar toggle based on the config value
  269. if (RAMP.config.advancedToolbar.enabled) {
  270. advancedToolbarToggle.removeClass("wb-invisible");
  271. } else {
  272. advancedToolbarToggle.remove();
  273. }
  274.  
  275. pluginConfig = RAMP.config.plugins;
  276. if (pluginConfig) {
  277. dojoArray.map(pluginConfig, function (pName) {
  278. loadPlugin(pName);
  279. });
  280. }
  281.  
  282. // apply defaulting of extents (must be done prior to bookmark link updates)
  283. RampMap.applyExtentDefaulting();
  284.  
  285. // Modify the config based on the url
  286. // needs to do this before the gui loads because the gui module
  287. // also reads from the config
  288. BookmarkLink.updateConfig(window.location.pathname.split("/").last());
  289.  
  290. //other initilizations must wait until our extents have been projected to our active basemap
  291. topic.subscribe(EventManager.Map.EXTENTS_REPROJECTED, function () {
  292. // Initialize the map only after the gui loads
  293. // if we do it beforehand, the map extent may get messed up since
  294. // the available screen size may still be changing (e.g. due to fullscreen
  295. // or subpanel closing)
  296. topic.subscribe(EventManager.GUI.UPDATE_COMPLETE, function () {
  297. // Create the panel that the bookmark link sits in
  298. // can only do this after the gui loads
  299. BookmarkLink.createUI();
  300.  
  301. LayerLoader.init();
  302. initializeMap();
  303. });
  304.  
  305. gui.load(null, null, function () { });
  306. Ramp.loadStrings();
  307. });
  308.  
  309. //project extents to basemap
  310. RampMap.projectConfigExtents();
  311. }
  312. });
  313.