Viewer.js 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391
  1. /*global define*/
  2. define([
  3. '../../Core/Cartesian3',
  4. '../../Core/defaultValue',
  5. '../../Core/defined',
  6. '../../Core/defineProperties',
  7. '../../Core/destroyObject',
  8. '../../Core/DeveloperError',
  9. '../../Core/EventHelper',
  10. '../../Core/ScreenSpaceEventType',
  11. '../../DataSources/ConstantPositionProperty',
  12. '../../DataSources/DataSourceCollection',
  13. '../../DataSources/DataSourceDisplay',
  14. '../../DataSources/Entity',
  15. '../../DataSources/EntityView',
  16. '../../Scene/SceneMode',
  17. '../../ThirdParty/knockout',
  18. '../../ThirdParty/when',
  19. '../Animation/Animation',
  20. '../Animation/AnimationViewModel',
  21. '../BaseLayerPicker/BaseLayerPicker',
  22. '../BaseLayerPicker/createDefaultImageryProviderViewModels',
  23. '../BaseLayerPicker/createDefaultTerrainProviderViewModels',
  24. '../CesiumWidget/CesiumWidget',
  25. '../ClockViewModel',
  26. '../FullscreenButton/FullscreenButton',
  27. '../Geocoder/Geocoder',
  28. '../getElement',
  29. '../HomeButton/HomeButton',
  30. '../InfoBox/InfoBox',
  31. '../NavigationHelpButton/NavigationHelpButton',
  32. '../SceneModePicker/SceneModePicker',
  33. '../SelectionIndicator/SelectionIndicator',
  34. '../subscribeAndEvaluate',
  35. '../Timeline/Timeline'
  36. ], function(
  37. Cartesian3,
  38. defaultValue,
  39. defined,
  40. defineProperties,
  41. destroyObject,
  42. DeveloperError,
  43. EventHelper,
  44. ScreenSpaceEventType,
  45. ConstantPositionProperty,
  46. DataSourceCollection,
  47. DataSourceDisplay,
  48. Entity,
  49. EntityView,
  50. SceneMode,
  51. knockout,
  52. when,
  53. Animation,
  54. AnimationViewModel,
  55. BaseLayerPicker,
  56. createDefaultImageryProviderViewModels,
  57. createDefaultTerrainProviderViewModels,
  58. CesiumWidget,
  59. ClockViewModel,
  60. FullscreenButton,
  61. Geocoder,
  62. getElement,
  63. HomeButton,
  64. InfoBox,
  65. NavigationHelpButton,
  66. SceneModePicker,
  67. SelectionIndicator,
  68. subscribeAndEvaluate,
  69. Timeline) {
  70. "use strict";
  71. function onTimelineScrubfunction(e) {
  72. var clock = e.clock;
  73. clock.currentTime = e.timeJulian;
  74. clock.shouldAnimate = false;
  75. }
  76. function pickEntity(viewer, e) {
  77. var picked = viewer.scene.pick(e.position);
  78. if (defined(picked)) {
  79. var id = defaultValue(picked.id, picked.primitive.id);
  80. if (id instanceof Entity) {
  81. return id;
  82. }
  83. }
  84. // No regular entity picked. Try picking features from imagery layers.
  85. return pickImageryLayerFeature(viewer, e.position);
  86. }
  87. function trackDataSourceClock(timeline, clock, dataSource) {
  88. if (defined(dataSource)) {
  89. var dataSourceClock = dataSource.clock;
  90. if (defined(dataSourceClock)) {
  91. dataSourceClock.getValue(clock);
  92. if (defined(timeline)) {
  93. timeline.updateFromClock();
  94. timeline.zoomTo(dataSourceClock.startTime, dataSourceClock.stopTime);
  95. }
  96. }
  97. }
  98. }
  99. var cartesian3Scratch = new Cartesian3();
  100. function pickImageryLayerFeature(viewer, windowPosition) {
  101. var scene = viewer.scene;
  102. var pickRay = scene.camera.getPickRay(windowPosition);
  103. var imageryLayerFeaturePromise = scene.imageryLayers.pickImageryLayerFeatures(pickRay, scene);
  104. if (!defined(imageryLayerFeaturePromise)) {
  105. return;
  106. }
  107. // Imagery layer feature picking is asynchronous, so put up a message while loading.
  108. var loadingMessage = new Entity('Loading...');
  109. loadingMessage.description = {
  110. getValue : function() {
  111. return 'Loading feature information...';
  112. }
  113. };
  114. when(imageryLayerFeaturePromise, function(features) {
  115. // Has this async pick been superseded by a later one?
  116. if (viewer.selectedEntity !== loadingMessage) {
  117. return;
  118. }
  119. if (!defined(features) || features.length === 0) {
  120. viewer.selectedEntity = createNoFeaturesEntity();
  121. return;
  122. }
  123. // Select the first feature.
  124. var feature = features[0];
  125. var entity = new Entity(feature.name);
  126. entity.description = {
  127. getValue : function() {
  128. return feature.description;
  129. }
  130. };
  131. if (defined(feature.position)) {
  132. var ecfPosition = viewer.scene.globe.ellipsoid.cartographicToCartesian(feature.position, cartesian3Scratch);
  133. entity.position = new ConstantPositionProperty(ecfPosition);
  134. }
  135. viewer.selectedEntity = entity;
  136. }, function() {
  137. // Has this async pick been superseded by a later one?
  138. if (viewer.selectedEntity !== loadingMessage) {
  139. return;
  140. }
  141. var entity = new Entity('None');
  142. entity.description = {
  143. getValue : function() {
  144. return 'No features found.';
  145. }
  146. };
  147. viewer.selectedEntity = createNoFeaturesEntity();
  148. });
  149. return loadingMessage;
  150. }
  151. function createNoFeaturesEntity() {
  152. var entity = new Entity('None');
  153. entity.description = {
  154. getValue : function() {
  155. return 'No features found.';
  156. }
  157. };
  158. return entity;
  159. }
  160. /**
  161. * A base widget for building applications. It composites all of the standard Cesium widgets into one reusable package.
  162. * The widget can always be extended by using mixins, which add functionality useful for a variety of applications.
  163. *
  164. * @alias Viewer
  165. * @constructor
  166. *
  167. * @param {Element|String} container The DOM element or ID that will contain the widget.
  168. * @param {Object} [options] Object with the following properties:
  169. * @param {Boolean} [options.animation=true] If set to false, the Animation widget will not be created.
  170. * @param {Boolean} [options.baseLayerPicker=true] If set to false, the BaseLayerPicker widget will not be created.
  171. * @param {Boolean} [options.fullscreenButton=true] If set to false, the FullscreenButton widget will not be created.
  172. * @param {Boolean} [options.geocoder=true] If set to false, the Geocoder widget will not be created.
  173. * @param {Boolean} [options.homeButton=true] If set to false, the HomeButton widget will not be created.
  174. * @param {Boolean} [options.infoBox=true] If set to false, the InfoBox widget will not be created.
  175. * @param {Boolean} [options.sceneModePicker=true] If set to false, the SceneModePicker widget will not be created.
  176. * @param {Boolean} [options.selectionIndicator=true] If set to false, the SelectionIndicator widget will not be created.
  177. * @param {Boolean} [options.timeline=true] If set to false, the Timeline widget will not be created.
  178. * @param {Boolean} [options.navigationHelpButton=true] If set to the false, the navigation help button will not be created.
  179. * @param {Boolean} [options.navigationInstructionsInitiallyVisible=true] True if the navigation instructions should initially be visible, or false if the should not be shown until the user explicitly clicks the button.
  180. * @param {Boolean} [options.scene3DOnly=false] When <code>true</code>, each geometry instance will only be rendered in 3D to save GPU memory.
  181. * @param {Clock} [options.clock=new Clock()] The clock to use to control current time.
  182. * @param {ProviderViewModel} [options.selectedImageryProviderViewModel] The view model for the current base imagery layer, if not supplied the first available base layer is used. This value is only valid if options.baseLayerPicker is set to true.
  183. * @param {ProviderViewModel[]} [options.imageryProviderViewModels=createDefaultImageryProviderViewModels()] The array of ProviderViewModels to be selectable from the BaseLayerPicker. This value is only valid if options.baseLayerPicker is set to true.
  184. * @param {ProviderViewModel} [options.selectedTerrainProviderViewModel] The view model for the current base terrain layer, if not supplied the first available base layer is used. This value is only valid if options.baseLayerPicker is set to true.
  185. * @param {ProviderViewModel[]} [options.terrainProviderViewModels=createDefaultTerrainProviderViewModels()] The array of ProviderViewModels to be selectable from the BaseLayerPicker. This value is only valid if options.baseLayerPicker is set to true.
  186. * @param {ImageryProvider} [options.imageryProvider=new BingMapsImageryProvider()] The imagery provider to use. This value is only valid if options.baseLayerPicker is set to false.
  187. * @param {TerrainProvider} [options.terrainProvider=new EllipsoidTerrainProvider()] The terrain provider to use
  188. * @param {SkyBox} [options.skyBox] The skybox used to render the stars. When <code>undefined</code>, the default stars are used.
  189. * @param {Element|String} [options.fullscreenElement=document.body] The element or id to be placed into fullscreen mode when the full screen button is pressed.
  190. * @param {Boolean} [options.useDefaultRenderLoop=true] True if this widget should control the render loop, false otherwise.
  191. * @param {Number} [options.targetFrameRate] The target frame rate when using the default render loop.
  192. * @param {Boolean} [options.showRenderLoopErrors=true] If true, this widget will automatically display an HTML panel to the user containing the error, if a render loop error occurs.
  193. * @param {Boolean} [options.automaticallyTrackDataSourceClocks=true] If true, this widget will automatically track the clock settings of newly added DataSources, updating if the DataSource's clock changes. Set this to false if you want to configure the clock independently.
  194. * @param {Object} [options.contextOptions] Context and WebGL creation properties corresponding to <code>options</code> passed to {@link Scene}.
  195. * @param {SceneMode} [options.sceneMode=SceneMode.SCENE3D] The initial scene mode.
  196. * @param {MapProjection} [options.mapProjection=new GeographicProjection()] The map projection to use in 2D and Columbus View modes.
  197. * @param {Boolean} [options.orderIndependentTranslucency=true] If true and the configuration supports it, use order independent translucency.
  198. * @param {Element|String} [options.creditContainer] The DOM element or ID that will contain the {@link CreditDisplay}. If not specified, the credits are added to the bottom of the widget itself.
  199. * @param {DataSourceCollection} [options.dataSources=new DataSourceCollection()] The collection of data sources visualized by the widget. If this parameter is provided,
  200. the instance is assumed to be owned by the caller and will not be destroyed when the viewer is destroyed.
  201. *
  202. * @exception {DeveloperError} Element with id "container" does not exist in the document.
  203. * @exception {DeveloperError} options.imageryProvider is not available when using the BaseLayerPicker widget, specify options.selectedImageryProviderViewModel instead.
  204. * @exception {DeveloperError} options.terrainProvider is not available when using the BaseLayerPicker widget, specify options.selectedTerrainProviderViewModel instead.
  205. * @exception {DeveloperError} options.selectedImageryProviderViewModel is not available when not using the BaseLayerPicker widget, specify options.imageryProvider instead.
  206. * @exception {DeveloperError} options.selectedTerrainProviderViewModel is not available when not using the BaseLayerPicker widget, specify options.terrainProvider instead.
  207. *
  208. * @see Animation
  209. * @see BaseLayerPicker
  210. * @see CesiumWidget
  211. * @see FullscreenButton
  212. * @see HomeButton
  213. * @see SceneModePicker
  214. * @see Timeline
  215. * @see viewerDragDropMixin
  216. *
  217. * @demo {@link http://cesiumjs.org/Cesium/Apps/Sandcastle/index.html?src=Hello%20World.html|Cesium Sandcastle Hello World Demo}
  218. *
  219. * @example
  220. * //Initialize the viewer widget with several custom options and mixins.
  221. * var viewer = new Cesium.Viewer('cesiumContainer', {
  222. * //Start in Columbus Viewer
  223. * sceneMode : Cesium.SceneMode.COLUMBUS_VIEW,
  224. * //Use standard Cesium terrain
  225. * terrainProvider : new Cesium.CesiumTerrainProvider({
  226. * url : '//cesiumjs.org/smallterrain',
  227. * credit : 'Terrain data courtesy Analytical Graphics, Inc.'
  228. * }),
  229. * //Hide the base layer picker
  230. * baseLayerPicker : false,
  231. * //Use OpenStreetMaps
  232. * imageryProvider : new Cesium.OpenStreetMapImageryProvider({
  233. * url : '//a.tile.openstreetmap.org/'
  234. * }),
  235. * // Use high-res stars downloaded from https://github.com/AnalyticalGraphicsInc/cesium-assets
  236. * skyBox : new Cesium.SkyBox({
  237. * sources : {
  238. * positiveX : 'stars/TychoSkymapII.t3_08192x04096_80_px.jpg',
  239. * negativeX : 'stars/TychoSkymapII.t3_08192x04096_80_mx.jpg',
  240. * positiveY : 'stars/TychoSkymapII.t3_08192x04096_80_py.jpg',
  241. * negativeY : 'stars/TychoSkymapII.t3_08192x04096_80_my.jpg',
  242. * positiveZ : 'stars/TychoSkymapII.t3_08192x04096_80_pz.jpg',
  243. * negativeZ : 'stars/TychoSkymapII.t3_08192x04096_80_mz.jpg'
  244. * }
  245. * }),
  246. * // Show Columbus View map with Web Mercator projection
  247. * mapProjection : new Cesium.WebMercatorProjection()
  248. * });
  249. *
  250. * //Add basic drag and drop functionality
  251. * viewer.extend(Cesium.viewerDragDropMixin);
  252. *
  253. * //Show a pop-up alert if we encounter an error when processing a dropped file
  254. * viewer.dropError.addEventListener(function(dropHandler, name, error) {
  255. * console.log(error);
  256. * window.alert(error);
  257. * });
  258. */
  259. var Viewer = function(container, options) {
  260. //>>includeStart('debug', pragmas.debug);
  261. if (!defined(container)) {
  262. throw new DeveloperError('container is required.');
  263. }
  264. //>>includeEnd('debug');
  265. container = getElement(container);
  266. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  267. var createBaseLayerPicker = !defined(options.baseLayerPicker) || options.baseLayerPicker !== false;
  268. //>>includeStart('debug', pragmas.debug);
  269. // If using BaseLayerPicker, imageryProvider is an invalid option
  270. if (createBaseLayerPicker && defined(options.imageryProvider)) {
  271. throw new DeveloperError('options.imageryProvider is not available when using the BaseLayerPicker widget. \
  272. Either specify options.selectedImageryProviderViewModel instead or set options.baseLayerPicker to false.');
  273. }
  274. // If not using BaseLayerPicker, selectedImageryProviderViewModel is an invalid option
  275. if (!createBaseLayerPicker && defined(options.selectedImageryProviderViewModel)) {
  276. throw new DeveloperError('options.selectedImageryProviderViewModel is not available when not using the BaseLayerPicker widget. \
  277. Either specify options.imageryProvider instead or set options.baseLayerPicker to true.');
  278. }
  279. // If using BaseLayerPicker, terrainProvider is an invalid option
  280. if (createBaseLayerPicker && defined(options.terrainProvider)) {
  281. throw new DeveloperError('options.terrainProvider is not available when using the BaseLayerPicker widget. \
  282. Either specify options.selectedTerrainProviderViewModel instead or set options.baseLayerPicker to false.');
  283. }
  284. // If not using BaseLayerPicker, selectedTerrainProviderViewModel is an invalid option
  285. if (!createBaseLayerPicker && defined(options.selectedTerrainProviderViewModel)) {
  286. throw new DeveloperError('options.selectedTerrainProviderViewModel is not available when not using the BaseLayerPicker widget. \
  287. Either specify options.terrainProvider instead or set options.baseLayerPicker to true.');
  288. }
  289. //>>includeEnd('debug')
  290. var viewerContainer = document.createElement('div');
  291. viewerContainer.className = 'cesium-viewer';
  292. container.appendChild(viewerContainer);
  293. // Cesium widget container
  294. var cesiumWidgetContainer = document.createElement('div');
  295. cesiumWidgetContainer.className = 'cesium-viewer-cesiumWidgetContainer';
  296. viewerContainer.appendChild(cesiumWidgetContainer);
  297. // Bottom container
  298. var bottomContainer = document.createElement('div');
  299. bottomContainer.className = 'cesium-viewer-bottom';
  300. viewerContainer.appendChild(bottomContainer);
  301. var scene3DOnly = defaultValue(options.scene3DOnly, false);
  302. // Cesium widget
  303. var cesiumWidget = new CesiumWidget(cesiumWidgetContainer, {
  304. terrainProvider : options.terrainProvider,
  305. imageryProvider : createBaseLayerPicker ? false : options.imageryProvider,
  306. clock : options.clock,
  307. skyBox : options.skyBox,
  308. sceneMode : options.sceneMode,
  309. mapProjection : options.mapProjection,
  310. orderIndependentTranslucency : options.orderIndependentTranslucency,
  311. contextOptions : options.contextOptions,
  312. useDefaultRenderLoop : options.useDefaultRenderLoop,
  313. targetFrameRate : options.targetFrameRate,
  314. showRenderLoopErrors : options.showRenderLoopErrors,
  315. creditContainer : defined(options.creditContainer) ? options.creditContainer : bottomContainer,
  316. scene3DOnly : scene3DOnly
  317. });
  318. var dataSourceCollection = options.dataSources;
  319. var destroyDataSourceCollection = false;
  320. if (!defined(dataSourceCollection)) {
  321. dataSourceCollection = new DataSourceCollection();
  322. destroyDataSourceCollection = true;
  323. }
  324. var dataSourceDisplay = new DataSourceDisplay({
  325. scene : cesiumWidget.scene,
  326. dataSourceCollection : dataSourceCollection
  327. });
  328. var clock = cesiumWidget.clock;
  329. var clockViewModel = new ClockViewModel(clock);
  330. var eventHelper = new EventHelper();
  331. eventHelper.add(clock.onTick, Viewer.prototype._onTick, this);
  332. // Selection Indicator
  333. var selectionIndicator;
  334. if (!defined(options.selectionIndicator) || options.selectionIndicator !== false) {
  335. var selectionIndicatorContainer = document.createElement('div');
  336. selectionIndicatorContainer.className = 'cesium-viewer-selectionIndicatorContainer';
  337. viewerContainer.appendChild(selectionIndicatorContainer);
  338. selectionIndicator = new SelectionIndicator(selectionIndicatorContainer, cesiumWidget.scene);
  339. }
  340. // Info Box
  341. var infoBox;
  342. if (!defined(options.infoBox) || options.infoBox !== false) {
  343. var infoBoxContainer = document.createElement('div');
  344. infoBoxContainer.className = 'cesium-viewer-infoBoxContainer';
  345. viewerContainer.appendChild(infoBoxContainer);
  346. infoBox = new InfoBox(infoBoxContainer);
  347. var infoBoxViewModel = infoBox.viewModel;
  348. eventHelper.add(infoBoxViewModel.cameraClicked, Viewer.prototype._trackSelectedEntity, this);
  349. eventHelper.add(infoBoxViewModel.closeClicked, Viewer.prototype._clearSelectedEntity, this);
  350. }
  351. // Main Toolbar
  352. var toolbar = document.createElement('div');
  353. toolbar.className = 'cesium-viewer-toolbar';
  354. viewerContainer.appendChild(toolbar);
  355. // Geocoder
  356. var geocoder;
  357. if (!defined(options.geocoder) || options.geocoder !== false) {
  358. var geocoderContainer = document.createElement('div');
  359. geocoderContainer.className = 'cesium-viewer-geocoderContainer';
  360. toolbar.appendChild(geocoderContainer);
  361. geocoder = new Geocoder({
  362. container : geocoderContainer,
  363. scene : cesiumWidget.scene
  364. });
  365. // Subscribe to search so that we can clear the trackedEntity when it is clicked.
  366. eventHelper.add(geocoder.viewModel.search.beforeExecute, Viewer.prototype._clearObjects, this);
  367. }
  368. // HomeButton
  369. var homeButton;
  370. if (!defined(options.homeButton) || options.homeButton !== false) {
  371. homeButton = new HomeButton(toolbar, cesiumWidget.scene);
  372. if (defined(geocoder)) {
  373. eventHelper.add(homeButton.viewModel.command.afterExecute, function() {
  374. var viewModel = geocoder.viewModel;
  375. viewModel.searchText = '';
  376. if (viewModel.isSearchInProgress) {
  377. viewModel.search();
  378. }
  379. });
  380. }
  381. // Subscribe to the home button beforeExecute event so that we can clear the trackedEntity.
  382. eventHelper.add(homeButton.viewModel.command.beforeExecute, Viewer.prototype._clearTrackedObject, this);
  383. }
  384. // SceneModePicker
  385. // By default, we silently disable the scene mode picker if scene3DOnly is true,
  386. // but if sceneModePicker is explicitly set to true, throw an error.
  387. if ((options.sceneModePicker === true) && scene3DOnly) {
  388. throw new DeveloperError('options.sceneModePicker is not available when options.scene3DOnly is set to true.');
  389. }
  390. var sceneModePicker;
  391. if (!scene3DOnly && (!defined(options.sceneModePicker) || options.sceneModePicker !== false)) {
  392. sceneModePicker = new SceneModePicker(toolbar, cesiumWidget.scene);
  393. }
  394. // BaseLayerPicker
  395. var baseLayerPicker;
  396. var baseLayerPickerDropDown;
  397. if (createBaseLayerPicker) {
  398. var imageryProviderViewModels = defaultValue(options.imageryProviderViewModels, createDefaultImageryProviderViewModels());
  399. var terrainProviderViewModels = defaultValue(options.terrainProviderViewModels, createDefaultTerrainProviderViewModels());
  400. baseLayerPicker = new BaseLayerPicker(toolbar, {
  401. globe : cesiumWidget.scene.globe,
  402. imageryProviderViewModels : imageryProviderViewModels,
  403. selectedImageryProviderViewModel : options.selectedImageryProviderViewModel,
  404. terrainProviderViewModels : terrainProviderViewModels,
  405. selectedTerrainProviderViewModel : options.selectedTerrainProviderViewModel
  406. });
  407. //Grab the dropdown for resize code.
  408. var elements = toolbar.getElementsByClassName('cesium-baseLayerPicker-dropDown');
  409. baseLayerPickerDropDown = elements[0];
  410. }
  411. // Navigation Help Button
  412. var navigationHelpButton;
  413. if (!defined(options.navigationHelpButton) || options.navigationHelpButton !== false) {
  414. var showNavHelp = true;
  415. if (defined(window.localStorage)) {
  416. var hasSeenNavHelp = window.localStorage.getItem('cesium-hasSeenNavHelp');
  417. if (defined(hasSeenNavHelp) && Boolean(hasSeenNavHelp)) {
  418. showNavHelp = false;
  419. } else {
  420. window.localStorage.setItem('cesium-hasSeenNavHelp', 'true');
  421. }
  422. }
  423. navigationHelpButton = new NavigationHelpButton({
  424. container : toolbar,
  425. instructionsInitiallyVisible : defaultValue(options.navigationInstructionsInitiallyVisible, showNavHelp)
  426. });
  427. }
  428. // Animation
  429. var animation;
  430. if (!defined(options.animation) || options.animation !== false) {
  431. var animationContainer = document.createElement('div');
  432. animationContainer.className = 'cesium-viewer-animationContainer';
  433. viewerContainer.appendChild(animationContainer);
  434. animation = new Animation(animationContainer, new AnimationViewModel(clockViewModel));
  435. }
  436. // Timeline
  437. var timeline;
  438. if (!defined(options.timeline) || options.timeline !== false) {
  439. var timelineContainer = document.createElement('div');
  440. timelineContainer.className = 'cesium-viewer-timelineContainer';
  441. viewerContainer.appendChild(timelineContainer);
  442. timeline = new Timeline(timelineContainer, clock);
  443. timeline.addEventListener('settime', onTimelineScrubfunction, false);
  444. timeline.zoomTo(clock.startTime, clock.stopTime);
  445. }
  446. // Fullscreen
  447. var fullscreenButton;
  448. var fullscreenSubscription;
  449. if (!defined(options.fullscreenButton) || options.fullscreenButton !== false) {
  450. var fullscreenContainer = document.createElement('div');
  451. fullscreenContainer.className = 'cesium-viewer-fullscreenContainer';
  452. viewerContainer.appendChild(fullscreenContainer);
  453. fullscreenButton = new FullscreenButton(fullscreenContainer, options.fullscreenElement);
  454. //Subscribe to fullscreenButton.viewModel.isFullscreenEnabled so
  455. //that we can hide/show the button as well as size the timeline.
  456. fullscreenSubscription = subscribeAndEvaluate(fullscreenButton.viewModel, 'isFullscreenEnabled', function(isFullscreenEnabled) {
  457. fullscreenContainer.style.display = isFullscreenEnabled ? 'block' : 'none';
  458. if (defined(timeline)) {
  459. timeline.container.style.right = fullscreenContainer.clientWidth + 'px';
  460. timeline.resize();
  461. }
  462. });
  463. } else if (defined(timeline)) {
  464. timeline.container.style.right = 0;
  465. }
  466. //Assign all properties to this instance. No "this" assignments should
  467. //take place above this line.
  468. this._baseLayerPickerDropDown = baseLayerPickerDropDown;
  469. this._fullscreenSubscription = fullscreenSubscription;
  470. this._dataSourceChangedListeners = {};
  471. this._automaticallyTrackDataSourceClocks = defaultValue(options.automaticallyTrackDataSourceClocks, true);
  472. this._container = container;
  473. this._bottomContainer = bottomContainer;
  474. this._element = viewerContainer;
  475. this._cesiumWidget = cesiumWidget;
  476. this._selectionIndicator = selectionIndicator;
  477. this._infoBox = infoBox;
  478. this._dataSourceCollection = dataSourceCollection;
  479. this._destroyDataSourceCollection = destroyDataSourceCollection;
  480. this._dataSourceDisplay = dataSourceDisplay;
  481. this._clockViewModel = clockViewModel;
  482. this._toolbar = toolbar;
  483. this._homeButton = homeButton;
  484. this._sceneModePicker = sceneModePicker;
  485. this._baseLayerPicker = baseLayerPicker;
  486. this._animation = animation;
  487. this._timeline = timeline;
  488. this._fullscreenButton = fullscreenButton;
  489. this._geocoder = geocoder;
  490. this._eventHelper = eventHelper;
  491. this._lastWidth = 0;
  492. this._lastHeight = 0;
  493. this._allowDataSourcesToSuspendAnimation = true;
  494. this._entityView = undefined;
  495. this._enableInfoOrSelection = defined(infoBox) || defined(selectionIndicator);
  496. this._clockTrackedDataSource = undefined;
  497. this._trackedEntity = undefined;
  498. this._selectedEntity = undefined;
  499. this._clockTrackedDataSource = undefined;
  500. this._forceResize = false;
  501. knockout.track(this, ['_trackedEntity', '_selectedEntity', '_clockTrackedDataSource']);
  502. //Listen to data source events in order to track clock changes.
  503. eventHelper.add(dataSourceCollection.dataSourceAdded, Viewer.prototype._onDataSourceAdded, this);
  504. eventHelper.add(dataSourceCollection.dataSourceRemoved, Viewer.prototype._onDataSourceRemoved, this);
  505. // Prior to each render, check if anything needs to be resized.
  506. eventHelper.add(cesiumWidget.scene.preRender, Viewer.prototype.resize, this);
  507. // We need to subscribe to the data sources and collections so that we can clear the
  508. // tracked object when it is removed from the scene.
  509. // Subscribe to current data sources
  510. var dataSourceLength = dataSourceCollection.length;
  511. for (var i = 0; i < dataSourceLength; i++) {
  512. this._dataSourceAdded(dataSourceCollection, dataSourceCollection.get(i));
  513. }
  514. // Hook up events so that we can subscribe to future sources.
  515. eventHelper.add(dataSourceCollection.dataSourceAdded, Viewer.prototype._dataSourceAdded, this);
  516. eventHelper.add(dataSourceCollection.dataSourceRemoved, Viewer.prototype._dataSourceRemoved, this);
  517. var that = this;
  518. // Subscribe to left clicks and zoom to the picked object.
  519. function pickAndTrackObject(e) {
  520. var entity = pickEntity(that, e);
  521. if (defined(entity) && defined(entity.position)) {
  522. that.trackedEntity = entity;
  523. }
  524. }
  525. function pickAndSelectObject(e) {
  526. that.selectedEntity = pickEntity(that, e);
  527. }
  528. cesiumWidget.screenSpaceEventHandler.setInputAction(pickAndSelectObject, ScreenSpaceEventType.LEFT_CLICK);
  529. cesiumWidget.screenSpaceEventHandler.setInputAction(pickAndTrackObject, ScreenSpaceEventType.LEFT_DOUBLE_CLICK);
  530. };
  531. defineProperties(Viewer.prototype, {
  532. /**
  533. * Gets the parent container.
  534. * @memberof Viewer.prototype
  535. * @type {Element}
  536. */
  537. container : {
  538. get : function() {
  539. return this._container;
  540. }
  541. },
  542. /**
  543. * Gets the DOM element for the area at the bottom of the window containing the
  544. * {@link CreditDisplay} and potentially other things.
  545. * @memberof Viewer.prototype
  546. * @type {Element}
  547. */
  548. bottomContainer : {
  549. get : function() {
  550. return this._bottomContainer;
  551. }
  552. },
  553. /**
  554. * Gets the CesiumWidget.
  555. * @memberof Viewer.prototype
  556. * @type {CesiumWidget}
  557. */
  558. cesiumWidget : {
  559. get : function() {
  560. return this._cesiumWidget;
  561. }
  562. },
  563. /**
  564. * Gets the selection indicator.
  565. * @memberof Viewer.prototype
  566. * @type {SelectionIndicator}
  567. */
  568. selectionIndicator : {
  569. get : function() {
  570. return this._selectionIndicator;
  571. }
  572. },
  573. /**
  574. * Gets the info box.
  575. * @memberof Viewer.prototype
  576. * @type {InfoBox}
  577. */
  578. infoBox : {
  579. get : function() {
  580. return this._infoBox;
  581. }
  582. },
  583. /**
  584. * Gets the Geocoder.
  585. * @memberof Viewer.prototype
  586. * @type {Geocoder}
  587. */
  588. geocoder : {
  589. get : function() {
  590. return this._geocoder;
  591. }
  592. },
  593. /**
  594. * Gets the HomeButton.
  595. * @memberof Viewer.prototype
  596. * @type {HomeButton}
  597. */
  598. homeButton : {
  599. get : function() {
  600. return this._homeButton;
  601. }
  602. },
  603. /**
  604. * Gets the SceneModePicker.
  605. * @memberof Viewer.prototype
  606. * @type {SceneModePicker}
  607. */
  608. sceneModePicker : {
  609. get : function() {
  610. return this._sceneModePicker;
  611. }
  612. },
  613. /**
  614. * Gets the BaseLayerPicker.
  615. * @memberof Viewer.prototype
  616. * @type {BaseLayerPicker}
  617. */
  618. baseLayerPicker : {
  619. get : function() {
  620. return this._baseLayerPicker;
  621. }
  622. },
  623. /**
  624. * Gets the Animation widget.
  625. * @memberof Viewer.prototype
  626. * @type {Animation}
  627. */
  628. animation : {
  629. get : function() {
  630. return this._animation;
  631. }
  632. },
  633. /**
  634. * Gets the Timeline widget.
  635. * @memberof Viewer.prototype
  636. * @type {Timeline}
  637. */
  638. timeline : {
  639. get : function() {
  640. return this._timeline;
  641. }
  642. },
  643. /**
  644. * Gets the FullscreenButton.
  645. * @memberof Viewer.prototype
  646. * @type {FullscreenButton}
  647. */
  648. fullscreenButton : {
  649. get : function() {
  650. return this._fullscreenButton;
  651. }
  652. },
  653. /**
  654. * Gets the display used for {@link DataSource} visualization.
  655. * @memberof Viewer.prototype
  656. * @type {DataSourceDisplay}
  657. */
  658. dataSourceDisplay : {
  659. get : function() {
  660. return this._dataSourceDisplay;
  661. }
  662. },
  663. /**
  664. * Gets the set of {@link DataSource} instances to be visualized.
  665. * @memberof Viewer.prototype
  666. * @type {DataSourceCollection}
  667. */
  668. dataSources : {
  669. get : function() {
  670. return this._dataSourceCollection;
  671. }
  672. },
  673. /**
  674. * Gets the canvas.
  675. * @memberof Viewer.prototype
  676. * @type {Canvas}
  677. */
  678. canvas : {
  679. get : function() {
  680. return this._cesiumWidget.canvas;
  681. }
  682. },
  683. /**
  684. * Gets the Cesium logo element.
  685. * @memberof Viewer.prototype
  686. * @type {Element}
  687. */
  688. cesiumLogo : {
  689. get : function() {
  690. return this._cesiumWidget.cesiumLogo;
  691. }
  692. },
  693. /**
  694. * Gets the scene.
  695. * @memberof Viewer.prototype
  696. * @type {Scene}
  697. */
  698. scene : {
  699. get : function() {
  700. return this._cesiumWidget.scene;
  701. }
  702. },
  703. /**
  704. * Gets the collection of image layers that will be rendered on the globe.
  705. * @memberof Viewer.prototype
  706. *
  707. * @type {ImageryLayerCollection}
  708. * @readonly
  709. */
  710. imageryLayers : {
  711. get : function() {
  712. return this.scene.imageryLayers;
  713. }
  714. },
  715. /**
  716. * The terrain provider providing surface geometry for the globe.
  717. * @memberof Viewer.prototype
  718. *
  719. * @type {TerrainProvider}
  720. */
  721. terrainProvider : {
  722. get : function() {
  723. return this.scene.terrainProvider;
  724. },
  725. set : function(terrainProvider) {
  726. this.scene.terrainProvider = terrainProvider;
  727. }
  728. },
  729. /**
  730. * Gets the camera.
  731. * @memberof Viewer.prototype
  732. *
  733. * @type {Camera}
  734. * @readonly
  735. */
  736. camera : {
  737. get : function() {
  738. return this.scene.camera;
  739. }
  740. },
  741. /**
  742. * Gets the clock.
  743. * @memberof Viewer.prototype
  744. * @type {Clock}
  745. */
  746. clock : {
  747. get : function() {
  748. return this._cesiumWidget.clock;
  749. }
  750. },
  751. /**
  752. * Gets the screen space event handler.
  753. * @memberof Viewer.prototype
  754. * @type {ScreenSpaceEventHandler}
  755. */
  756. screenSpaceEventHandler : {
  757. get : function() {
  758. return this._cesiumWidget.screenSpaceEventHandler;
  759. }
  760. },
  761. /**
  762. * Gets or sets the target frame rate of the widget when <code>useDefaultRenderLoop</code>
  763. * is true. If undefined, the browser's {@link requestAnimationFrame} implementation
  764. * determines the frame rate. This value must be greater than 0 and a value higher than
  765. * the underlying requestAnimationFrame implementatin will have no effect.
  766. * @memberof Viewer.prototype
  767. *
  768. * @type {Number}
  769. */
  770. targetFrameRate : {
  771. get : function() {
  772. return this._cesiumWidget.targetFrameRate;
  773. },
  774. set : function(value) {
  775. this._cesiumWidget.targetFrameRate = value;
  776. }
  777. },
  778. /**
  779. * Gets or sets whether or not this widget should control the render loop.
  780. * If set to true the widget will use {@link requestAnimationFrame} to
  781. * perform rendering and resizing of the widget, as well as drive the
  782. * simulation clock. If set to false, you must manually call the
  783. * <code>resize</code>, <code>render</code> methods
  784. * as part of a custom render loop. If an error occurs during rendering, {@link Scene}'s
  785. * <code>renderError</code> event will be raised and this property
  786. * will be set to false. It must be set back to true to continue rendering
  787. * after the error.
  788. * @memberof Viewer.prototype
  789. *
  790. * @type {Boolean}
  791. */
  792. useDefaultRenderLoop : {
  793. get : function() {
  794. return this._cesiumWidget.useDefaultRenderLoop;
  795. },
  796. set : function(value) {
  797. this._cesiumWidget.useDefaultRenderLoop = value;
  798. }
  799. },
  800. /**
  801. * Gets or sets a scaling factor for rendering resolution. Values less than 1.0 can improve
  802. * performance on less powerful devices while values greater than 1.0 will render at a higher
  803. * resolution and then scale down, resulting in improved visual fidelity.
  804. * For example, if the widget is laid out at a size of 640x480, setting this value to 0.5
  805. * will cause the scene to be rendered at 320x240 and then scaled up while setting
  806. * it to 2.0 will cause the scene to be rendered at 1280x960 and then scaled down.
  807. * @memberof Viewer.prototype
  808. *
  809. * @type {Number}
  810. * @default 1.0
  811. */
  812. resolutionScale : {
  813. get : function() {
  814. return this._cesiumWidget.resolutionScale;
  815. },
  816. set : function(value) {
  817. this._cesiumWidget.resolutionScale = value;
  818. this._forceResize = true;
  819. }
  820. },
  821. /**
  822. * Gets or sets whether or not data sources can temporarily pause
  823. * animation in order to avoid showing an incomplete picture to the user.
  824. * For example, if asynchronous primitives are being processed in the
  825. * background, the clock will not advance until the geometry is ready.
  826. *
  827. * @memberof Viewer.prototype
  828. *
  829. * @type {Boolean}
  830. */
  831. allowDataSourcesToSuspendAnimation : {
  832. get : function() {
  833. return this._allowDataSourcesToSuspendAnimation;
  834. },
  835. set : function(value) {
  836. this._allowDataSourcesToSuspendAnimation = value;
  837. }
  838. },
  839. /**
  840. * Gets or sets the Entity instance currently being tracked by the camera.
  841. * @memberof Viewer.prototype
  842. * @type {Entity}
  843. */
  844. trackedEntity : {
  845. get : function() {
  846. return this._trackedEntity;
  847. },
  848. set : function(value) {
  849. if (this._trackedEntity !== value) {
  850. this._trackedEntity = value;
  851. var scene = this.scene;
  852. var sceneMode = scene.mode;
  853. var isTracking = defined(value);
  854. if (sceneMode === SceneMode.COLUMBUS_VIEW || sceneMode === SceneMode.SCENE2D) {
  855. scene.screenSpaceCameraController.enableTranslate = !isTracking;
  856. }
  857. if (sceneMode === SceneMode.COLUMBUS_VIEW || sceneMode === SceneMode.SCENE3D) {
  858. scene.screenSpaceCameraController.enableTilt = !isTracking;
  859. }
  860. if (isTracking && defined(value.position)) {
  861. this._entityView = new EntityView(value, scene, this.scene.globe.ellipsoid);
  862. } else {
  863. this._entityView = undefined;
  864. }
  865. }
  866. }
  867. },
  868. /**
  869. * Gets or sets the object instance for which to display a selection indicator.
  870. * @memberof Viewer.prototype
  871. * @type {Entity}
  872. */
  873. selectedEntity : {
  874. get : function() {
  875. return this._selectedEntity;
  876. },
  877. set : function(value) {
  878. if (this._selectedEntity !== value) {
  879. this._selectedEntity = value;
  880. var selectionIndicatorViewModel = defined(this._selectionIndicator) ? this._selectionIndicator.viewModel : undefined;
  881. if (defined(value)) {
  882. var infoBoxViewModel = defined(this._infoBox) ? this._infoBox.viewModel : undefined;
  883. if (defined(infoBoxViewModel)) {
  884. infoBoxViewModel.titleText = defined(value.name) ? value.name : value.id;
  885. }
  886. if (defined(selectionIndicatorViewModel)) {
  887. selectionIndicatorViewModel.animateAppear();
  888. }
  889. } else {
  890. // Leave the info text in place here, it is needed during the exit animation.
  891. if (defined(selectionIndicatorViewModel)) {
  892. selectionIndicatorViewModel.animateDepart();
  893. }
  894. }
  895. }
  896. }
  897. },
  898. /**
  899. * Gets or sets the data source to track with the viewer's clock.
  900. * @type {DataSource}
  901. */
  902. clockTrackedDataSource : {
  903. get : function() {
  904. return this._clockTrackedDataSource;
  905. },
  906. set : function(value) {
  907. if (this._clockTrackedDataSource !== value) {
  908. this._clockTrackedDataSource = value;
  909. trackDataSourceClock(this._timeline, this.clock, value);
  910. }
  911. }
  912. }
  913. });
  914. /**
  915. * Extends the base viewer functionality with the provided mixin.
  916. * A mixin may add additional properties, functions, or other behavior
  917. * to the provided viewer instance.
  918. *
  919. * @param {Viewer~ViewerMixin} mixin The Viewer mixin to add to this instance.
  920. * @param {Object} options The options object to be passed to the mixin function.
  921. *
  922. * @see viewerDragDropMixin
  923. */
  924. Viewer.prototype.extend = function(mixin, options) {
  925. //>>includeStart('debug', pragmas.debug);
  926. if (!defined(mixin)) {
  927. throw new DeveloperError('mixin is required.');
  928. }
  929. //>>includeEnd('debug')
  930. mixin(this, options);
  931. };
  932. /**
  933. * Resizes the widget to match the container size.
  934. * This function is called automatically as needed unless
  935. * <code>useDefaultRenderLoop</code> is set to false.
  936. */
  937. Viewer.prototype.resize = function() {
  938. var cesiumWidget = this._cesiumWidget;
  939. var container = this._container;
  940. var width = container.clientWidth;
  941. var height = container.clientHeight;
  942. var animationExists = defined(this._animation);
  943. var timelineExists = defined(this._timeline);
  944. if (!this._forceResize && width === this._lastWidth && height === this._lastHeight) {
  945. return;
  946. }
  947. cesiumWidget.resize();
  948. this._forceResize = false;
  949. var panelMaxHeight = height - 125;
  950. var baseLayerPickerDropDown = this._baseLayerPickerDropDown;
  951. if (defined(baseLayerPickerDropDown)) {
  952. baseLayerPickerDropDown.style.maxHeight = panelMaxHeight + 'px';
  953. }
  954. if (defined(this._infoBox)) {
  955. this._infoBox.viewModel.maxHeight = panelMaxHeight;
  956. }
  957. var timeline = this._timeline;
  958. var animationContainer;
  959. var animationWidth = 0;
  960. var creditLeft = 0;
  961. var creditBottom = 0;
  962. if (animationExists && window.getComputedStyle(this._animation.container).visibility !== 'hidden') {
  963. var lastWidth = this._lastWidth;
  964. animationContainer = this._animation.container;
  965. if (width > 900) {
  966. animationWidth = 169;
  967. if (lastWidth <= 900) {
  968. animationContainer.style.width = '169px';
  969. animationContainer.style.height = '112px';
  970. this._animation.resize();
  971. }
  972. } else if (width >= 600) {
  973. animationWidth = 136;
  974. if (lastWidth < 600 || lastWidth > 900) {
  975. animationContainer.style.width = '136px';
  976. animationContainer.style.height = '90px';
  977. this._animation.resize();
  978. }
  979. } else {
  980. animationWidth = 106;
  981. if (lastWidth > 600 || lastWidth === 0) {
  982. animationContainer.style.width = '106px';
  983. animationContainer.style.height = '70px';
  984. this._animation.resize();
  985. }
  986. }
  987. creditLeft = animationWidth + 5;
  988. }
  989. if (timelineExists && window.getComputedStyle(this._timeline.container).visibility !== 'hidden') {
  990. var fullscreenButton = this._fullscreenButton;
  991. var timelineContainer = timeline.container;
  992. var timelineStyle = timelineContainer.style;
  993. creditBottom = timelineContainer.clientHeight + 3;
  994. timelineStyle.left = animationWidth + 'px';
  995. if (defined(fullscreenButton)) {
  996. timelineStyle.right = fullscreenButton.container.clientWidth + 'px';
  997. }
  998. timeline.resize();
  999. }
  1000. this._bottomContainer.style.left = creditLeft + 'px';
  1001. this._bottomContainer.style.bottom = creditBottom + 'px';
  1002. this._lastWidth = width;
  1003. this._lastHeight = height;
  1004. };
  1005. /**
  1006. * This forces the widget to re-think its layout, including
  1007. * widget sizes and credit placement.
  1008. */
  1009. Viewer.prototype.forceResize = function() {
  1010. this._lastWidth = 0;
  1011. this.resize();
  1012. };
  1013. /**
  1014. * Renders the scene. This function is called automatically
  1015. * unless <code>useDefaultRenderLoop</code> is set to false;
  1016. */
  1017. Viewer.prototype.render = function() {
  1018. this._cesiumWidget.render();
  1019. };
  1020. /**
  1021. * @returns {Boolean} true if the object has been destroyed, false otherwise.
  1022. */
  1023. Viewer.prototype.isDestroyed = function() {
  1024. return false;
  1025. };
  1026. /**
  1027. * Destroys the widget. Should be called if permanently
  1028. * removing the widget from layout.
  1029. */
  1030. Viewer.prototype.destroy = function() {
  1031. var i;
  1032. this.screenSpaceEventHandler.removeInputAction(ScreenSpaceEventType.LEFT_CLICK);
  1033. this.screenSpaceEventHandler.removeInputAction(ScreenSpaceEventType.LEFT_DOUBLE_CLICK);
  1034. // Unsubscribe from data sources
  1035. var dataSources = this.dataSources;
  1036. var dataSourceLength = dataSources.length;
  1037. for (i = 0; i < dataSourceLength; i++) {
  1038. this._dataSourceRemoved(dataSources, dataSources.get(i));
  1039. }
  1040. this._container.removeChild(this._element);
  1041. this._element.removeChild(this._toolbar);
  1042. this._eventHelper.removeAll();
  1043. if (defined(this._geocoder)) {
  1044. this._geocoder = this._geocoder.destroy();
  1045. }
  1046. if (defined(this._homeButton)) {
  1047. this._homeButton = this._homeButton.destroy();
  1048. }
  1049. if (defined(this._sceneModePicker)) {
  1050. this._sceneModePicker = this._sceneModePicker.destroy();
  1051. }
  1052. if (defined(this._baseLayerPicker)) {
  1053. this._baseLayerPicker = this._baseLayerPicker.destroy();
  1054. }
  1055. if (defined(this._animation)) {
  1056. this._element.removeChild(this._animation.container);
  1057. this._animation = this._animation.destroy();
  1058. }
  1059. if (defined(this._timeline)) {
  1060. this._timeline.removeEventListener('settime', onTimelineScrubfunction, false);
  1061. this._element.removeChild(this._timeline.container);
  1062. this._timeline = this._timeline.destroy();
  1063. }
  1064. if (defined(this._fullscreenButton)) {
  1065. this._fullscreenSubscription.dispose();
  1066. this._element.removeChild(this._fullscreenButton.container);
  1067. this._fullscreenButton = this._fullscreenButton.destroy();
  1068. }
  1069. if (defined(this._infoBox)) {
  1070. this._element.removeChild(this._infoBox.container);
  1071. this._infoBox = this._infoBox.destroy();
  1072. }
  1073. if (defined(this._selectionIndicator)) {
  1074. this._element.removeChild(this._selectionIndicator.container);
  1075. this._selectionIndicator = this._selectionIndicator.destroy();
  1076. }
  1077. this._clockViewModel = this._clockViewModel.destroy();
  1078. this._dataSourceDisplay = this._dataSourceDisplay.destroy();
  1079. this._cesiumWidget = this._cesiumWidget.destroy();
  1080. if (this._destroyDataSourceCollection) {
  1081. this._dataSourceCollection = this._dataSourceCollection.destroy();
  1082. }
  1083. return destroyObject(this);
  1084. };
  1085. /**
  1086. * @private
  1087. */
  1088. Viewer.prototype._dataSourceAdded = function(dataSourceCollection, dataSource) {
  1089. var entityCollection = dataSource.entities;
  1090. entityCollection.collectionChanged.addEventListener(Viewer.prototype._onEntityCollectionChanged, this);
  1091. };
  1092. /**
  1093. * @private
  1094. */
  1095. Viewer.prototype._dataSourceRemoved = function(dataSourceCollection, dataSource) {
  1096. var entityCollection = dataSource.entities;
  1097. entityCollection.collectionChanged.removeEventListener(Viewer.prototype._onEntityCollectionChanged, this);
  1098. if (defined(this.trackedEntity)) {
  1099. if (entityCollection.getById(this.trackedEntity.id) === this.trackedEntity) {
  1100. this.homeButton.viewModel.command();
  1101. }
  1102. }
  1103. if (defined(this.selectedEntity)) {
  1104. if (entityCollection.getById(this.selectedEntity.id) === this.selectedEntity) {
  1105. this.selectedEntity = undefined;
  1106. }
  1107. }
  1108. };
  1109. /**
  1110. * @private
  1111. */
  1112. Viewer.prototype._onTick = function(clock) {
  1113. var time = clock.currentTime;
  1114. var entityView = this._entityView;
  1115. var infoBoxViewModel = defined(this._infoBox) ? this._infoBox.viewModel : undefined;
  1116. var selectionIndicatorViewModel = this._selectionIndicator.viewModel;
  1117. var isUpdated = this._dataSourceDisplay.update(time);
  1118. if (this._allowDataSourcesToSuspendAnimation) {
  1119. this._clockViewModel.canAnimate = isUpdated;
  1120. }
  1121. if (defined(entityView)) {
  1122. entityView.update(time);
  1123. }
  1124. var selectedEntity = this.selectedEntity;
  1125. var showSelection = defined(selectedEntity) && this._enableInfoOrSelection;
  1126. if (showSelection) {
  1127. var oldPosition = defined(selectionIndicatorViewModel) ? selectionIndicatorViewModel.position : undefined;
  1128. var position;
  1129. var enableCamera = false;
  1130. if (selectedEntity.isAvailable(time)) {
  1131. if (defined(selectedEntity.position)) {
  1132. position = selectedEntity.position.getValue(time, oldPosition);
  1133. enableCamera = defined(position) && (this.trackedEntity !== this.selectedEntity);
  1134. }
  1135. // else "position" is undefined and "enableCamera" is false.
  1136. }
  1137. // else "position" is undefined and "enableCamera" is false.
  1138. if (defined(selectionIndicatorViewModel)) {
  1139. selectionIndicatorViewModel.position = position;
  1140. }
  1141. if (defined(infoBoxViewModel)) {
  1142. infoBoxViewModel.enableCamera = enableCamera;
  1143. infoBoxViewModel.isCameraTracking = (this.trackedEntity === this.selectedEntity);
  1144. if (defined(selectedEntity.description)) {
  1145. infoBoxViewModel.descriptionRawHtml = defaultValue(selectedEntity.description.getValue(time), '');
  1146. } else {
  1147. infoBoxViewModel.descriptionRawHtml = '';
  1148. }
  1149. }
  1150. }
  1151. if (defined(selectionIndicatorViewModel)) {
  1152. selectionIndicatorViewModel.showSelection = showSelection;
  1153. selectionIndicatorViewModel.update();
  1154. }
  1155. if (defined(infoBoxViewModel)) {
  1156. infoBoxViewModel.showInfo = showSelection;
  1157. }
  1158. };
  1159. /**
  1160. * @private
  1161. */
  1162. Viewer.prototype._onEntityCollectionChanged = function(collection, added, removed) {
  1163. var length = removed.length;
  1164. for (var i = 0; i < length; i++) {
  1165. var removedObject = removed[i];
  1166. if (this.trackedEntity === removedObject) {
  1167. this.homeButton.viewModel.command();
  1168. }
  1169. if (this.selectedEntity === removedObject) {
  1170. this.selectedEntity = undefined;
  1171. }
  1172. }
  1173. };
  1174. /**
  1175. * @private
  1176. */
  1177. Viewer.prototype._trackSelectedEntity = function() {
  1178. this.trackedEntity = this.selectedEntity;
  1179. };
  1180. /**
  1181. * @private
  1182. */
  1183. Viewer.prototype._clearTrackedObject = function() {
  1184. this.trackedEntity = undefined;
  1185. };
  1186. /**
  1187. * @private
  1188. */
  1189. Viewer.prototype._clearSelectedEntity = function() {
  1190. this.selectedEntity = undefined;
  1191. };
  1192. /**
  1193. * @private
  1194. */
  1195. Viewer.prototype._clearObjects = function() {
  1196. this.trackedEntity = undefined;
  1197. this.selectedEntity = undefined;
  1198. };
  1199. /**
  1200. * @private
  1201. */
  1202. Viewer.prototype._onDataSourceChanged = function(dataSource) {
  1203. if (this.clockTrackedDataSource === dataSource) {
  1204. trackDataSourceClock(this.timeline, this.clock, dataSource);
  1205. }
  1206. };
  1207. /**
  1208. * @private
  1209. */
  1210. Viewer.prototype._onDataSourceAdded = function(dataSourceCollection, dataSource) {
  1211. if (this._automaticallyTrackDataSourceClocks) {
  1212. this.clockTrackedDataSource = dataSource;
  1213. }
  1214. var id = dataSource.entities.id;
  1215. var removalFunc = this._eventHelper.add(dataSource.changedEvent, Viewer.prototype._onDataSourceChanged, this);
  1216. this._dataSourceChangedListeners[id] = removalFunc;
  1217. };
  1218. /**
  1219. * @private
  1220. */
  1221. Viewer.prototype._onDataSourceRemoved = function(dataSourceCollection, dataSource) {
  1222. var resetClock = (this.clockTrackedDataSource === dataSource);
  1223. var id = dataSource.entities.id;
  1224. this._dataSourceChangedListeners[id]();
  1225. this._dataSourceChangedListeners[id] = undefined;
  1226. if (resetClock) {
  1227. var numDataSources = dataSourceCollection.length;
  1228. if (this._automaticallyTrackDataSourceClocks && numDataSources > 0) {
  1229. this.clockTrackedDataSource = dataSourceCollection.get(numDataSources - 1);
  1230. } else {
  1231. this.clockTrackedDataSource = undefined;
  1232. }
  1233. }
  1234. };
  1235. /**
  1236. * A function that augments a Viewer instance with additional functionality.
  1237. * @callback Viewer~ViewerMixin
  1238. * @param {Viewer} viewer The viewer instance.
  1239. * @param {Object} options Options object to be passed to the mixin function.
  1240. *
  1241. * @see Viewer#extend
  1242. */
  1243. return Viewer;
  1244. });