NavigationHelpButton.js 11 KB

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