All files / widgets/Source/ProjectionPicker ProjectionPickerViewModel.js

92% Statements 46/50
71.42% Branches 10/14
92.85% Functions 13/14
92% Lines 46/50

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202                                        13x 1x       12x 12x 12x             12x             12x             12x           12x           12x   12x                 12x 12x 14x 3x   11x     12x 1x       1x     12x 12x     2x 2x         12x       12x 2x       2x 2x 2x     12x 2x       2x 2x 2x       12x     1x               1x                       8x                       9x                       9x                       5x               1x 1x           1x 12x 12x      
import {
  defined,
  destroyObject,
  DeveloperError,
  EventHelper,
  OrthographicFrustum,
  SceneMode,
} from "@cesium/engine";
import knockout from "../ThirdParty/knockout.js";
import createCommand from "../createCommand.js";
 
/**
 * The view model for {@link ProjectionPicker}.
 * @alias ProjectionPickerViewModel
 * @constructor
 *
 * @param {Scene} scene The Scene to switch projections.
 */
function ProjectionPickerViewModel(scene) {
  //>>includeStart('debug', pragmas.debug);
  if (!defined(scene)) {
    throw new DeveloperError("scene is required.");
  }
  //>>includeEnd('debug');
 
  this._scene = scene;
  this._orthographic = scene.camera.frustum instanceof OrthographicFrustum;
  this._flightInProgress = false;
 
  /**
   * Gets or sets whether the button drop-down is currently visible.  This property is observable.
   * @type {boolean}
   * @default false
   */
  this.dropDownVisible = false;
 
  /**
   * Gets or sets the perspective projection tooltip.  This property is observable.
   * @type {string}
   * @default 'Perspective Projection'
   */
  this.tooltipPerspective = "Perspective Projection";
 
  /**
   * Gets or sets the orthographic projection tooltip.  This property is observable.
   * @type {string}
   * @default 'Orthographic Projection'
   */
  this.tooltipOrthographic = "Orthographic Projection";
 
  /**
   * Gets the currently active tooltip.  This property is observable.
   * @type {string}
   */
  this.selectedTooltip = undefined;
 
  /**
   * Gets or sets the current SceneMode.  This property is observable.
   * @type {SceneMode}
   */
  this.sceneMode = scene.mode;
 
  knockout.track(this, [
    "_orthographic",
    "_flightInProgress",
    "sceneMode",
    "dropDownVisible",
    "tooltipPerspective",
    "tooltipOrthographic",
  ]);
 
  const that = this;
  knockout.defineProperty(this, "selectedTooltip", function () {
    if (that._orthographic) {
      return that.tooltipOrthographic;
    }
    return that.tooltipPerspective;
  });
 
  this._toggleDropDown = createCommand(function () {
    Iif (that.sceneMode === SceneMode.SCENE2D || that._flightInProgress) {
      return;
    }
 
    that.dropDownVisible = !that.dropDownVisible;
  });
 
  this._eventHelper = new EventHelper();
  this._eventHelper.add(
    scene.morphComplete,
    function (transitioner, oldMode, newMode, isMorphing) {
      that.sceneMode = newMode;
      that._orthographic =
        newMode === SceneMode.SCENE2D ||
        that._scene.camera.frustum instanceof OrthographicFrustum;
    },
  );
  this._eventHelper.add(scene.preRender, function () {
    that._flightInProgress = defined(scene.camera._currentFlight);
  });
 
  this._switchToPerspective = createCommand(function () {
    Iif (that.sceneMode === SceneMode.SCENE2D) {
      return;
    }
 
    that._scene.camera.switchToPerspectiveFrustum();
    that._orthographic = false;
    that.dropDownVisible = false;
  });
 
  this._switchToOrthographic = createCommand(function () {
    Iif (that.sceneMode === SceneMode.SCENE2D) {
      return;
    }
 
    that._scene.camera.switchToOrthographicFrustum();
    that._orthographic = true;
    that.dropDownVisible = false;
  });
 
  //Used by knockout
  this._sceneMode = SceneMode;
}
 
Object.defineProperties(ProjectionPickerViewModel.prototype, {
  /**
   * Gets the scene
   * @memberof ProjectionPickerViewModel.prototype
   * @type {Scene}
   */
  scene: {
    get: function () {
      return this._scene;
    },
  },
 
  /**
   * Gets the command to toggle the drop down box.
   * @memberof ProjectionPickerViewModel.prototype
   *
   * @type {Command}
   */
  toggleDropDown: {
    get: function () {
      return this._toggleDropDown;
    },
  },
 
  /**
   * Gets the command to switch to a perspective projection.
   * @memberof ProjectionPickerViewModel.prototype
   *
   * @type {Command}
   */
  switchToPerspective: {
    get: function () {
      return this._switchToPerspective;
    },
  },
 
  /**
   * Gets the command to switch to orthographic projection.
   * @memberof ProjectionPickerViewModel.prototype
   *
   * @type {Command}
   */
  switchToOrthographic: {
    get: function () {
      return this._switchToOrthographic;
    },
  },
 
  /**
   * Gets whether the scene is currently using an orthographic projection.
   * @memberof ProjectionPickerViewModel.prototype
   *
   * @type {Command}
   */
  isOrthographicProjection: {
    get: function () {
      return this._orthographic;
    },
  },
});
 
/**
 * @returns {boolean} true if the object has been destroyed, false otherwise.
 */
ProjectionPickerViewModel.prototype.isDestroyed = function () {
  return false;
};
 
/**
 * Destroys the view model.
 */
ProjectionPickerViewModel.prototype.destroy = function () {
  this._eventHelper.removeAll();
  destroyObject(this);
};
export default ProjectionPickerViewModel;