NavigationHelpButton.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. import buildModuleUrl from "../../Core/buildModuleUrl.js";
  2. import defaultValue from "../../Core/defaultValue.js";
  3. import defined from "../../Core/defined.js";
  4. import destroyObject from "../../Core/destroyObject.js";
  5. import DeveloperError from "../../Core/DeveloperError.js";
  6. import FeatureDetection from "../../Core/FeatureDetection.js";
  7. import knockout from "../../ThirdParty/knockout.js";
  8. import getElement from "../getElement.js";
  9. import NavigationHelpButtonViewModel from "./NavigationHelpButtonViewModel.js";
  10. /**
  11. * <p>The NavigationHelpButton is a single button widget for displaying instructions for
  12. * navigating the globe with the mouse.</p><p style="clear: both;"></p><br/>
  13. *
  14. * @alias NavigationHelpButton
  15. * @constructor
  16. *
  17. * @param {Object} options Object with the following properties:
  18. * @param {Element|String} options.container The DOM element or ID that will contain the widget.
  19. * @param {Boolean} [options.instructionsInitiallyVisible=false] True if the navigation instructions should initially be visible; otherwise, false.
  20. *
  21. * @exception {DeveloperError} Element with id "container" does not exist in the document.
  22. *
  23. * @example
  24. * // In HTML head, include a link to the NavigationHelpButton.css stylesheet,
  25. * // and in the body, include: <div id="navigationHelpButtonContainer"></div>
  26. *
  27. * var navigationHelpButton = new Cesium.NavigationHelpButton({
  28. * container : 'navigationHelpButtonContainer'
  29. * });
  30. */
  31. function NavigationHelpButton(options) {
  32. //>>includeStart('debug', pragmas.debug);
  33. if (!defined(options) || !defined(options.container)) {
  34. throw new DeveloperError("options.container is required.");
  35. }
  36. //>>includeEnd('debug');
  37. var container = getElement(options.container);
  38. var viewModel = new NavigationHelpButtonViewModel();
  39. var showInsructionsDefault = defaultValue(
  40. options.instructionsInitiallyVisible,
  41. false
  42. );
  43. viewModel.showInstructions = showInsructionsDefault;
  44. viewModel._svgPath =
  45. "M16,1.466C7.973,1.466,1.466,7.973,1.466,16c0,8.027,6.507,14.534,14.534,14.534c8.027,0,14.534-6.507,14.534-14.534C30.534,7.973,24.027,1.466,16,1.466z M17.328,24.371h-2.707v-2.596h2.707V24.371zM17.328,19.003v0.858h-2.707v-1.057c0-3.19,3.63-3.696,3.63-5.963c0-1.034-0.924-1.826-2.134-1.826c-1.254,0-2.354,0.924-2.354,0.924l-1.541-1.915c0,0,1.519-1.584,4.137-1.584c2.487,0,4.796,1.54,4.796,4.136C21.156,16.208,17.328,16.627,17.328,19.003z";
  46. var wrapper = document.createElement("span");
  47. wrapper.className = "cesium-navigationHelpButton-wrapper";
  48. container.appendChild(wrapper);
  49. var button = document.createElement("button");
  50. button.type = "button";
  51. button.className =
  52. "cesium-button cesium-toolbar-button cesium-navigation-help-button";
  53. button.setAttribute(
  54. "data-bind",
  55. "\
  56. attr: { title: tooltip },\
  57. click: command,\
  58. cesiumSvgPath: { path: _svgPath, width: 32, height: 32 }"
  59. );
  60. wrapper.appendChild(button);
  61. var instructionContainer = document.createElement("div");
  62. instructionContainer.className = "cesium-navigation-help";
  63. instructionContainer.setAttribute(
  64. "data-bind",
  65. 'css: { "cesium-navigation-help-visible" : showInstructions}'
  66. );
  67. wrapper.appendChild(instructionContainer);
  68. var mouseButton = document.createElement("button");
  69. mouseButton.type = "button";
  70. mouseButton.className =
  71. "cesium-navigation-button cesium-navigation-button-left";
  72. mouseButton.setAttribute(
  73. "data-bind",
  74. 'click: showClick, css: {"cesium-navigation-button-selected": !_touch, "cesium-navigation-button-unselected": _touch}'
  75. );
  76. var mouseIcon = document.createElement("img");
  77. mouseIcon.src = buildModuleUrl("Widgets/Images/NavigationHelp/Mouse.svg");
  78. mouseIcon.className = "cesium-navigation-button-icon";
  79. mouseIcon.style.width = "25px";
  80. mouseIcon.style.height = "25px";
  81. mouseButton.appendChild(mouseIcon);
  82. mouseButton.appendChild(document.createTextNode("Mouse"));
  83. var touchButton = document.createElement("button");
  84. touchButton.type = "button";
  85. touchButton.className =
  86. "cesium-navigation-button cesium-navigation-button-right";
  87. touchButton.setAttribute(
  88. "data-bind",
  89. 'click: showTouch, css: {"cesium-navigation-button-selected": _touch, "cesium-navigation-button-unselected": !_touch}'
  90. );
  91. var touchIcon = document.createElement("img");
  92. touchIcon.src = buildModuleUrl("Widgets/Images/NavigationHelp/Touch.svg");
  93. touchIcon.className = "cesium-navigation-button-icon";
  94. touchIcon.style.width = "25px";
  95. touchIcon.style.height = "25px";
  96. touchButton.appendChild(touchIcon);
  97. touchButton.appendChild(document.createTextNode("Touch"));
  98. instructionContainer.appendChild(mouseButton);
  99. instructionContainer.appendChild(touchButton);
  100. var clickInstructions = document.createElement("div");
  101. clickInstructions.className =
  102. "cesium-click-navigation-help cesium-navigation-help-instructions";
  103. clickInstructions.setAttribute(
  104. "data-bind",
  105. 'css: { "cesium-click-navigation-help-visible" : !_touch}'
  106. );
  107. clickInstructions.innerHTML =
  108. '\
  109. <table>\
  110. <tr>\
  111. <td><img src="' +
  112. buildModuleUrl("Widgets/Images/NavigationHelp/MouseLeft.svg") +
  113. '" width="48" height="48" /></td>\
  114. <td>\
  115. <div class="cesium-navigation-help-pan">Pan view</div>\
  116. <div class="cesium-navigation-help-details">Left click + drag</div>\
  117. </td>\
  118. </tr>\
  119. <tr>\
  120. <td><img src="' +
  121. buildModuleUrl("Widgets/Images/NavigationHelp/MouseRight.svg") +
  122. '" width="48" height="48" /></td>\
  123. <td>\
  124. <div class="cesium-navigation-help-zoom">Zoom view</div>\
  125. <div class="cesium-navigation-help-details">Right click + drag, or</div>\
  126. <div class="cesium-navigation-help-details">Mouse wheel scroll</div>\
  127. </td>\
  128. </tr>\
  129. <tr>\
  130. <td><img src="' +
  131. buildModuleUrl("Widgets/Images/NavigationHelp/MouseMiddle.svg") +
  132. '" width="48" height="48" /></td>\
  133. <td>\
  134. <div class="cesium-navigation-help-rotate">Rotate view</div>\
  135. <div class="cesium-navigation-help-details">Middle click + drag, or</div>\
  136. <div class="cesium-navigation-help-details">CTRL + Left/Right click + drag</div>\
  137. </td>\
  138. </tr>\
  139. </table>';
  140. instructionContainer.appendChild(clickInstructions);
  141. var touchInstructions = document.createElement("div");
  142. touchInstructions.className =
  143. "cesium-touch-navigation-help cesium-navigation-help-instructions";
  144. touchInstructions.setAttribute(
  145. "data-bind",
  146. 'css: { "cesium-touch-navigation-help-visible" : _touch}'
  147. );
  148. touchInstructions.innerHTML =
  149. '\
  150. <table>\
  151. <tr>\
  152. <td><img src="' +
  153. buildModuleUrl("Widgets/Images/NavigationHelp/TouchDrag.svg") +
  154. '" width="70" height="48" /></td>\
  155. <td>\
  156. <div class="cesium-navigation-help-pan">Pan view</div>\
  157. <div class="cesium-navigation-help-details">One finger drag</div>\
  158. </td>\
  159. </tr>\
  160. <tr>\
  161. <td><img src="' +
  162. buildModuleUrl("Widgets/Images/NavigationHelp/TouchZoom.svg") +
  163. '" width="70" height="48" /></td>\
  164. <td>\
  165. <div class="cesium-navigation-help-zoom">Zoom view</div>\
  166. <div class="cesium-navigation-help-details">Two finger pinch</div>\
  167. </td>\
  168. </tr>\
  169. <tr>\
  170. <td><img src="' +
  171. buildModuleUrl("Widgets/Images/NavigationHelp/TouchTilt.svg") +
  172. '" width="70" height="48" /></td>\
  173. <td>\
  174. <div class="cesium-navigation-help-rotate">Tilt view</div>\
  175. <div class="cesium-navigation-help-details">Two finger drag, same direction</div>\
  176. </td>\
  177. </tr>\
  178. <tr>\
  179. <td><img src="' +
  180. buildModuleUrl("Widgets/Images/NavigationHelp/TouchRotate.svg") +
  181. '" width="70" height="48" /></td>\
  182. <td>\
  183. <div class="cesium-navigation-help-tilt">Rotate view</div>\
  184. <div class="cesium-navigation-help-details">Two finger drag, opposite direction</div>\
  185. </td>\
  186. </tr>\
  187. </table>';
  188. instructionContainer.appendChild(touchInstructions);
  189. knockout.applyBindings(viewModel, wrapper);
  190. this._container = container;
  191. this._viewModel = viewModel;
  192. this._wrapper = wrapper;
  193. this._closeInstructions = function (e) {
  194. if (!wrapper.contains(e.target)) {
  195. viewModel.showInstructions = false;
  196. }
  197. };
  198. if (FeatureDetection.supportsPointerEvents()) {
  199. document.addEventListener("pointerdown", this._closeInstructions, true);
  200. } else {
  201. document.addEventListener("mousedown", this._closeInstructions, true);
  202. document.addEventListener("touchstart", this._closeInstructions, true);
  203. }
  204. }
  205. Object.defineProperties(NavigationHelpButton.prototype, {
  206. /**
  207. * Gets the parent container.
  208. * @memberof NavigationHelpButton.prototype
  209. *
  210. * @type {Element}
  211. */
  212. container: {
  213. get: function () {
  214. return this._container;
  215. },
  216. },
  217. /**
  218. * Gets the view model.
  219. * @memberof NavigationHelpButton.prototype
  220. *
  221. * @type {NavigationHelpButtonViewModel}
  222. */
  223. viewModel: {
  224. get: function () {
  225. return this._viewModel;
  226. },
  227. },
  228. });
  229. /**
  230. * @returns {Boolean} true if the object has been destroyed, false otherwise.
  231. */
  232. NavigationHelpButton.prototype.isDestroyed = function () {
  233. return false;
  234. };
  235. /**
  236. * Destroys the widget. Should be called if permanently
  237. * removing the widget from layout.
  238. */
  239. NavigationHelpButton.prototype.destroy = function () {
  240. if (FeatureDetection.supportsPointerEvents()) {
  241. document.removeEventListener("pointerdown", this._closeInstructions, true);
  242. } else {
  243. document.removeEventListener("mousedown", this._closeInstructions, true);
  244. document.removeEventListener("touchstart", this._closeInstructions, true);
  245. }
  246. knockout.cleanNode(this._wrapper);
  247. this._container.removeChild(this._wrapper);
  248. return destroyObject(this);
  249. };
  250. export default NavigationHelpButton;