Reusable Accessible Mapping Platform

API Docs for: 4.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. */
  42.  
  43. require([
  44. /* Dojo */
  45. "dojo/parser", "dojo/on", "dojo/topic",
  46. "dojo/request/script",
  47. "dojo/request/xhr", "dojo/_base/array",
  48. "esri/config",
  49.  
  50. /* RAMP */
  51. "ramp/map", "ramp/basemapSelector", "ramp/maptips", "ramp/datagrid",
  52. "ramp/navigation", "ramp/filterManager", "ramp/bookmarkLink",
  53. "utils/url", "ramp/featureHighlighter",
  54. "ramp/ramp", "ramp/globalStorage", "ramp/gui", "ramp/eventManager",
  55. "ramp/advancedToolbar",
  56. "ramp/theme",
  57.  
  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,
  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.ALL_LAYERS_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. // Added current level so slider will know how to adjust the position
  118. var currentLevel = (RampMap.getMap().__LOD.level) ? RampMap.getMap().__LOD.level : 0;
  119.  
  120. NavWidget.init(currentLevel);
  121. FeatureHighlighter.init();
  122. Maptips.init();
  123.  
  124. //Apply listeners for basemap gallery
  125. BasemapSelector.init();
  126.  
  127. //initialize the filter
  128. FilterManager.init();
  129.  
  130. // Initialize the advanced toolbar and tools.
  131. if (RAMP.config.advancedToolbar.enabled) {
  132. AdvancedToolbar.init();
  133. }
  134.  
  135. Datagrid.init();
  136. theme.tooltipster();
  137. });
  138.  
  139. RampMap.init();
  140. NavWidget.construct();
  141.  
  142. // 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
  143. theme.tooltipster();
  144.  
  145. /* End - RAMP Events */
  146. }
  147. /* End - Bootstrapper functions */
  148.  
  149. // Check to make sure the console exists, redefines it to the no-op function
  150. // if it does not (e.g. in IE when the debugger is not on)
  151. UtilMisc.checkConsole();
  152.  
  153. // Once all of our modules are loaded and the DOM is ready:
  154.  
  155. // call the parser to create the dijit layout dijits
  156. parser.parse();
  157.  
  158. //To hold values from RAMP service
  159.  
  160. var lang = $("html").attr("lang"),
  161. configFile,
  162. defJson;
  163.  
  164. if (lang !== "en" && lang !== "fr") {
  165. lang = "en";
  166. }
  167.  
  168. i18n.init(
  169. {
  170. lng: lang + "-CA",
  171. load: "current",
  172. fallbackLng: false
  173. });
  174.  
  175. //loading config object from JSON file
  176. configFile = (lang === "fr") ? "config.fr.json" : "config.en.json";
  177.  
  178. // Request the JSON config file
  179. defJson = xhr(configFile, {
  180. handleAs: "json"
  181. });
  182.  
  183.  
  184. defJson.then(
  185. function (fileConfig) {
  186. //there is no need to convert the result to an object. it comes through pre-parsed
  187. if (!RAMP.configServiceURL) {
  188. //no config service. we just use the file provided
  189. configReady(fileConfig);
  190. } else {
  191. //get additional config stuff from the config service. mash it into our primary object
  192.  
  193. // pull smallkeys from URL
  194. var siteURL = new Url(require.toUrl(document.location)),
  195. smallkeys = siteURL.queryObject.keys;
  196.  
  197. if (!smallkeys || smallkeys === "") {
  198. //no keys. no point hitting the service. jump to next step
  199. configReady(fileConfig);
  200. } else {
  201.  
  202. //TODO verify endpoint is correct
  203. var serviceUrl = RAMP.configServiceURL + "docs/" + $("html").attr("lang") + "/" + smallkeys,
  204. defService = requestScript.get(serviceUrl, { jsonp:'callback', timeout: 2000 });
  205.  
  206. //Request the JSON snippets from the RAMP Config Service
  207.  
  208. //NOTE: XHR cannot be used here for cross domain purposes (primarily when running thru visual studio).
  209. // we use request/script instead to get the config as jsonp
  210. // we may consider looking into ways to mitiate the cross domain issue (Aly had some ideas)
  211.  
  212. defService.then(
  213. function (serviceContent) {
  214. console.log(serviceContent);
  215.  
  216. //we are expecting an array of JSON config fragments
  217. //merge each fragment into the file config
  218.  
  219. dojoArray.forEach(serviceContent, function (configFragment) {
  220. UtilMisc.mergeRecursive(fileConfig, configFragment);
  221. });
  222.  
  223. //fragments are now in fileConfig. carry on.
  224. configReady(fileConfig);
  225. },
  226. function (error) {
  227. console.log("An error occurred: " + error);
  228. }
  229. );
  230.  
  231. }
  232.  
  233. }
  234. },
  235. function (error) {
  236. console.log("An error occurred when retrieving the JSON Config: " + error);
  237. }
  238. );
  239.  
  240. function configReady(configObject) {
  241. var pluginConfig,
  242. advancedToolbarToggle = $("li.map-toolbar-item #advanced-toggle").parent();
  243.  
  244. console.log("Bootstrapper: config loaded");
  245.  
  246. globalStorage.init(configObject);
  247.  
  248. esriConfig.defaults.io.proxyUrl = RAMP.config.proxyUrl;// "/proxy/proxy.ashx";
  249. // try to avoid the proxy if possible, but this will cause network errors if CORS is not allowed by the target server
  250. esriConfig.defaults.io.corsDetection = true;
  251.  
  252. // Show or remove advanced toolbar toggle based on the config value
  253. if (RAMP.config.advancedToolbar.enabled) {
  254. advancedToolbarToggle.removeClass("wb-invisible");
  255. } else {
  256. advancedToolbarToggle.remove();
  257. }
  258.  
  259. pluginConfig = RAMP.config.plugins;
  260. if (pluginConfig) {
  261. dojoArray.map(pluginConfig, function (pName) {
  262. loadPlugin(pName);
  263. });
  264. }
  265. // Modify the config based on the url
  266. // needs to do this before the gui loads because the gui module
  267. // also reads from the config
  268. BookmarkLink.updateConfig(window.location.pathname.split("/").last());
  269.  
  270. // Initialize the map only after the gui loads
  271. // if we do it beforehand, the map extent may get messed up since
  272. // the available screen size may still be changing (e.g. due to fullscreen
  273. // or subpanel closing)
  274. topic.subscribe(EventManager.GUI.UPDATE_COMPLETE, function () {
  275. initializeMap();
  276. });
  277.  
  278. gui.load(null, null, function () { });
  279.  
  280. // Create the panel that the bookmark link sits in
  281. // can only do this after the gui loads
  282. BookmarkLink.createUI();
  283.  
  284. Ramp.loadStrings();
  285. }
  286. });