NavigationHelpButton.js 11 KB

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