Reusable Accessible Mapping Platform

API Docs for: 3.0.0
Show:

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

  1. /*global define, $, TimelineLite, TweenLite, require, tmpl, console */
  2.  
  3. /**
  4. * Tools module. Contains tools accessible through Advanced Toolbar.
  5. *
  6. * Contains the advanced tools as a popup.
  7. *
  8. * @module Tools
  9. * @main Tools
  10. */
  11.  
  12. /**
  13. * AdvancedToolbar class.
  14. *
  15. * @class AdvancedToolbar
  16. * @static
  17. * @uses dojo/_base/lang
  18. * @uses dojo/_base/array
  19. * @uses dojo/topic
  20. * @uses dojo/Deferred
  21. * @uses ramp/eventManager
  22. * @uses ramp/map
  23. * @uses ramp/globalStorage
  24. * @uses PopupManager
  25. */
  26.  
  27. define([
  28. // Dojo
  29. "dojo/_base/lang", "dojo/_base/array", "dojo/topic", "dojo/Deferred",
  30. // Ramp
  31. "ramp/eventManager", "ramp/map", "ramp/globalStorage",
  32. // Util
  33. "utils/util", "utils/dictionary", "utils/popupManager",
  34. "utils/tmplHelper",
  35. // Text
  36. "dojo/text!./templates/advanced_toolbar_template.json"
  37. ],
  38.  
  39. function (
  40. // Dojo
  41. dojoLang, dojoArray, topic, Deferred,
  42. // Ramp
  43. EventManager, RampMap, globalStorage,
  44. // Util
  45. UtilMisc, UtilDict, PopupManager, TmplHelper,
  46. // Text
  47. advanced_toolbar_template_json) {
  48. "use strict";
  49.  
  50. var advancedToggle,
  51. advancedSectionContainer,
  52. advancedToolbarList,
  53.  
  54. cssButtonPressedClass = "button-pressed",
  55.  
  56. transitionDuration = 0.4,
  57.  
  58. map,
  59.  
  60. tools = [
  61. // name,
  62. // selector,
  63. // enabled,
  64. // module
  65. ],
  66.  
  67. ui = {
  68. /**
  69. * Initiates additional UI components of the widget, setting listeners and other stuff.
  70. *
  71. * @method ui.init
  72. * @private
  73. */
  74. init: function () {
  75. var viewport = $(".viewport"),
  76. subPanelContainer,
  77. panelToggle = viewport.find("#panel-toggle"),
  78.  
  79. advancedToolbarTimeLine = new TimelineLite({
  80. paused: true,
  81. onComplete: function () { },
  82. onReverseComplete: function () { }
  83. });
  84.  
  85. advancedToggle = viewport.find("#advanced-toggle");
  86. advancedSectionContainer = viewport.find("#advanced-toolbar");
  87.  
  88. // create html code for advancedToolbar
  89. tmpl.cache = {};
  90. tmpl.templates = JSON.parse(TmplHelper.stringifyTemplate(advanced_toolbar_template_json));
  91. advancedSectionContainer.append(tmpl("at_main"));
  92. advancedToolbarList = advancedSectionContainer.find("#advanced-toolbar-list");
  93.  
  94. advancedToolbarTimeLine
  95. .set(advancedSectionContainer, { display: "block" }, 0)
  96. .set(viewport, { className: "+=advanced-toolbar-mode" }, 0)
  97. .fromTo(advancedToolbarList, transitionDuration, { top: -32 }, { top: 0, ease: "easeOutCirc" }, 0)
  98. .to(panelToggle, transitionDuration, { top: "+=32", ease: "easeOutCirc" }, 0);
  99.  
  100. // register the popup for the advanced toolbar
  101. PopupManager.registerPopup(advancedToggle, "click",
  102. function (d) {
  103. topic.publish(EventManager.GUI.TOOLBAR_SECTION_OPEN, { id: "advanced-toolbar" });
  104.  
  105. // close this panel if any other panel is opened
  106. UtilMisc.subscribeOnce(EventManager.GUI.TOOLBAR_SECTION_OPEN, dojoLang.hitch(this,
  107. function (evt) {
  108. if (evt.id !== "advanced-toolbar" && this.isOpen()) {
  109. this.close();
  110. }
  111. })
  112. );
  113.  
  114. // perform transitions
  115. subPanelContainer = viewport.find(".sub-panel-container");
  116. TweenLite.fromTo(subPanelContainer, transitionDuration,
  117. { "margin-top": 0 },
  118. { "margin-top": 32, ease: "easeOutCirc" });
  119.  
  120. advancedToolbarTimeLine.eventCallback("onComplete", d.resolve, [], this);
  121. advancedToolbarTimeLine.play();
  122. },
  123. {
  124. activeClass: cssButtonPressedClass,
  125. setClassBefore: true,
  126. target: advancedSectionContainer,
  127. closeHandler: function (d) {
  128. topic.publish(EventManager.GUI.TOOLBAR_SECTION_CLOSE, { id: "advanced-toolbar" });
  129.  
  130. // deactivate all the tools
  131. deactivateAll();
  132.  
  133. // perform transitions
  134. subPanelContainer = viewport.find(".sub-panel-container");
  135. TweenLite.fromTo(subPanelContainer, transitionDuration,
  136. { "margin-top": 32 },
  137. { "margin-top": 0, ease: "easeInCirc" });
  138.  
  139. advancedToolbarTimeLine.eventCallback("onReverseComplete", d.resolve, [], this);
  140. advancedToolbarTimeLine.reverse();
  141. }
  142. }
  143. );
  144.  
  145. map = RampMap.getMap();
  146. },
  147.  
  148. addTool: function (tool) {
  149. advancedToolbarList.append(tool.module.node);
  150. }
  151. };
  152.  
  153. /**
  154. * Deactivates all the tools. Used when closing the Advanced toolbar or when another tool is being activated.
  155. *
  156. * @method deactivateAll
  157. * @param {Tool} except A tool module that should not be deactivated.
  158. * @private
  159. */
  160. function deactivateAll(except) {
  161. // deactivate all the tools except "except" tool
  162. tools.forEach(function (tool) {
  163. if ((!except || except.name !== tool.name) && tool.module) {
  164. tool.module.deactivate();
  165. }
  166. });
  167. }
  168.  
  169. return {
  170. init: function () {
  171. var toolsRequire;
  172.  
  173. ui.init();
  174.  
  175. tools = globalStorage.config.advancedToolbar.tools;
  176. toolsRequire = tools
  177. .filter(function (tool) { return tool.enabled; })
  178. .map(function (tool) {
  179. return "tools/" + tool.name;
  180. });
  181.  
  182. // load all the tools in one go
  183. console.log("toolbar : loading tools", toolsRequire);
  184. require(toolsRequire, function () {
  185. var deferredList = [],
  186. deferred,
  187. args = Array.prototype.slice.call(arguments);
  188.  
  189. args.forEach(function () {
  190. deferredList.push(new Deferred());
  191. });
  192.  
  193. UtilMisc.afterAll(deferredList, function () {
  194. // insert tool buttons into the toolbar
  195. tools.forEach(ui.addTool);
  196. });
  197.  
  198. args.forEach(function (arg, index) {
  199. deferred = new Deferred();
  200.  
  201. deferred.then(function (module) {
  202. tools[index].module = module;
  203. deferredList[index].resolve();
  204. });
  205.  
  206. arg
  207. .init(tools[index].selector, deferred)
  208. .on(arg.event.ACTIVATE, function () {
  209. console.log(arg.name, ": is activated");
  210. deactivateAll(arg);
  211. });
  212. });
  213.  
  214. console.log(args);
  215. });
  216. }
  217. };
  218. });