ProjectionPicker.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import defined from '../../Core/defined.js';
  2. import defineProperties from '../../Core/defineProperties.js';
  3. import destroyObject from '../../Core/destroyObject.js';
  4. import DeveloperError from '../../Core/DeveloperError.js';
  5. import FeatureDetection from '../../Core/FeatureDetection.js';
  6. import knockout from '../../ThirdParty/knockout.js';
  7. import getElement from '../getElement.js';
  8. import ProjectionPickerViewModel from './ProjectionPickerViewModel.js';
  9. var perspectivePath = 'M 28.15625,10.4375 9.125,13.21875 13.75,43.25 41.75,55.09375 50.8125,37 54.5,11.9375 z m 0.125,3 19.976451,0.394265 L 43.03125,16.875 22.6875,14.28125 z M 50.971746,15.705477 47.90625,36.03125 42.53125,46 44.84375,19.3125 z M 12.625,16.03125 l 29.15625,3.6875 -2.65625,31 L 16.4375,41.125 z';
  10. var orthographicPath = 'm 31.560594,6.5254438 -20.75,12.4687502 0.1875,24.5625 22.28125,11.8125 19.5,-12 0.65625,-0.375 0,-0.75 0.0312,-23.21875 z m 0.0625,3.125 16.65625,9.5000002 -16.125,10.28125 -17.34375,-9.71875 z m 18.96875,11.1875002 0.15625,20.65625 -17.46875,10.59375 0.15625,-20.28125 z m -37.0625,1.25 17.21875,9.625 -0.15625,19.21875 -16.9375,-9 z';
  11. /**
  12. * The ProjectionPicker is a single button widget for switching between perspective and orthographic projections.
  13. *
  14. * @alias ProjectionPicker
  15. * @constructor
  16. *
  17. * @param {Element|String} container The DOM element or ID that will contain the widget.
  18. * @param {Scene} scene The Scene instance to use.
  19. *
  20. * @exception {DeveloperError} Element with id "container" does not exist in the document.
  21. *
  22. * @example
  23. * // In HTML head, include a link to the ProjectionPicker.css stylesheet,
  24. * // and in the body, include: <div id="projectionPickerContainer"></div>
  25. * // Note: This code assumes you already have a Scene instance.
  26. *
  27. * var projectionPicker = new Cesium.ProjectionPicker('projectionPickerContainer', scene);
  28. */
  29. function ProjectionPicker(container, scene) {
  30. //>>includeStart('debug', pragmas.debug);
  31. if (!defined(container)) {
  32. throw new DeveloperError('container is required.');
  33. }
  34. if (!defined(scene)) {
  35. throw new DeveloperError('scene is required.');
  36. }
  37. //>>includeEnd('debug');
  38. container = getElement(container);
  39. var viewModel = new ProjectionPickerViewModel(scene);
  40. viewModel._perspectivePath = perspectivePath;
  41. viewModel._orthographicPath = orthographicPath;
  42. var wrapper = document.createElement('span');
  43. wrapper.className = 'cesium-projectionPicker-wrapper cesium-toolbar-button';
  44. container.appendChild(wrapper);
  45. var button = document.createElement('button');
  46. button.type = 'button';
  47. button.className = 'cesium-button cesium-toolbar-button';
  48. button.setAttribute('data-bind', '\
  49. css: { "cesium-projectionPicker-buttonPerspective": !_orthographic,\
  50. "cesium-projectionPicker-buttonOrthographic": _orthographic,\
  51. "cesium-button-disabled" : sceneMode === _sceneMode.SCENE2D || _flightInProgress, \
  52. "cesium-projectionPicker-selected": dropDownVisible },\
  53. attr: { title: selectedTooltip },\
  54. click: toggleDropDown');
  55. button.innerHTML = '\
  56. <!-- ko cesiumSvgPath: { path: _perspectivePath, width: 64, height: 64, css: "cesium-projectionPicker-iconPerspective" } --><!-- /ko -->\
  57. <!-- ko cesiumSvgPath: { path: _orthographicPath, width: 64, height: 64, css: "cesium-projectionPicker-iconOrthographic" } --><!-- /ko -->';
  58. wrapper.appendChild(button);
  59. var perspectiveButton = document.createElement('button');
  60. perspectiveButton.type = 'button';
  61. perspectiveButton.className = 'cesium-button cesium-toolbar-button cesium-projectionPicker-dropDown-icon';
  62. perspectiveButton.setAttribute('data-bind', '\
  63. css: { "cesium-projectionPicker-visible" : (dropDownVisible && _orthographic),\
  64. "cesium-projectionPicker-none" : !_orthographic,\
  65. "cesium-projectionPicker-hidden" : !dropDownVisible },\
  66. attr: { title: tooltipPerspective },\
  67. click: switchToPerspective,\
  68. cesiumSvgPath: { path: _perspectivePath, width: 64, height: 64 }');
  69. wrapper.appendChild(perspectiveButton);
  70. var orthographicButton = document.createElement('button');
  71. orthographicButton.type = 'button';
  72. orthographicButton.className = 'cesium-button cesium-toolbar-button cesium-projectionPicker-dropDown-icon';
  73. orthographicButton.setAttribute('data-bind', '\
  74. css: { "cesium-projectionPicker-visible" : (dropDownVisible && !_orthographic),\
  75. "cesium-projectionPicker-none" : _orthographic,\
  76. "cesium-projectionPicker-hidden" : !dropDownVisible},\
  77. attr: { title: tooltipOrthographic },\
  78. click: switchToOrthographic,\
  79. cesiumSvgPath: { path: _orthographicPath, width: 64, height: 64 }');
  80. wrapper.appendChild(orthographicButton);
  81. knockout.applyBindings(viewModel, wrapper);
  82. this._viewModel = viewModel;
  83. this._container = container;
  84. this._wrapper = wrapper;
  85. this._closeDropDown = function(e) {
  86. if (!wrapper.contains(e.target)) {
  87. viewModel.dropDownVisible = false;
  88. }
  89. };
  90. if (FeatureDetection.supportsPointerEvents()) {
  91. document.addEventListener('pointerdown', this._closeDropDown, true);
  92. } else {
  93. document.addEventListener('mousedown', this._closeDropDown, true);
  94. document.addEventListener('touchstart', this._closeDropDown, true);
  95. }
  96. }
  97. defineProperties(ProjectionPicker.prototype, {
  98. /**
  99. * Gets the parent container.
  100. * @memberof ProjectionPicker.prototype
  101. *
  102. * @type {Element}
  103. */
  104. container : {
  105. get : function() {
  106. return this._container;
  107. }
  108. },
  109. /**
  110. * Gets the view model.
  111. * @memberof ProjectionPicker.prototype
  112. *
  113. * @type {ProjectionPickerViewModel}
  114. */
  115. viewModel : {
  116. get : function() {
  117. return this._viewModel;
  118. }
  119. }
  120. });
  121. /**
  122. * @returns {Boolean} true if the object has been destroyed, false otherwise.
  123. */
  124. ProjectionPicker.prototype.isDestroyed = function() {
  125. return false;
  126. };
  127. /**
  128. * Destroys the widget. Should be called if permanently
  129. * removing the widget from layout.
  130. */
  131. ProjectionPicker.prototype.destroy = function() {
  132. this._viewModel.destroy();
  133. if (FeatureDetection.supportsPointerEvents()) {
  134. document.removeEventListener('pointerdown', this._closeDropDown, true);
  135. } else {
  136. document.removeEventListener('mousedown', this._closeDropDown, true);
  137. document.removeEventListener('touchstart', this._closeDropDown, true);
  138. }
  139. knockout.cleanNode(this._wrapper);
  140. this._container.removeChild(this._wrapper);
  141. return destroyObject(this);
  142. };
  143. export default ProjectionPicker;