FullscreenButtonViewModel.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import defaultValue from '../../Core/defaultValue.js';
  2. import defineProperties from '../../Core/defineProperties.js';
  3. import destroyObject from '../../Core/destroyObject.js';
  4. import DeveloperError from '../../Core/DeveloperError.js';
  5. import Fullscreen from '../../Core/Fullscreen.js';
  6. import knockout from '../../ThirdParty/knockout.js';
  7. import createCommand from '../createCommand.js';
  8. import getElement from '../getElement.js';
  9. /**
  10. * The view model for {@link FullscreenButton}.
  11. * @alias FullscreenButtonViewModel
  12. * @constructor
  13. *
  14. * @param {Element|String} [fullscreenElement=document.body] The element or id to be placed into fullscreen mode.
  15. */
  16. function FullscreenButtonViewModel(fullscreenElement) {
  17. var that = this;
  18. var tmpIsFullscreen = knockout.observable(Fullscreen.fullscreen);
  19. var tmpIsEnabled = knockout.observable(Fullscreen.enabled);
  20. /**
  21. * Gets whether or not fullscreen mode is active. This property is observable.
  22. *
  23. * @type {Boolean}
  24. */
  25. this.isFullscreen = undefined;
  26. knockout.defineProperty(this, 'isFullscreen', {
  27. get : function() {
  28. return tmpIsFullscreen();
  29. }
  30. });
  31. /**
  32. * Gets or sets whether or not fullscreen functionality should be enabled. This property is observable.
  33. *
  34. * @type {Boolean}
  35. * @see Fullscreen.enabled
  36. */
  37. this.isFullscreenEnabled = undefined;
  38. knockout.defineProperty(this, 'isFullscreenEnabled', {
  39. get : function() {
  40. return tmpIsEnabled();
  41. },
  42. set : function(value) {
  43. tmpIsEnabled(value && Fullscreen.enabled);
  44. }
  45. });
  46. /**
  47. * Gets the tooltip. This property is observable.
  48. *
  49. * @type {String}
  50. */
  51. this.tooltip = undefined;
  52. knockout.defineProperty(this, 'tooltip', function() {
  53. if (!this.isFullscreenEnabled) {
  54. return 'Full screen unavailable';
  55. }
  56. return tmpIsFullscreen() ? 'Exit full screen' : 'Full screen';
  57. });
  58. this._command = createCommand(function() {
  59. if (Fullscreen.fullscreen) {
  60. Fullscreen.exitFullscreen();
  61. } else {
  62. Fullscreen.requestFullscreen(that._fullscreenElement);
  63. }
  64. }, knockout.getObservable(this, 'isFullscreenEnabled'));
  65. this._fullscreenElement = defaultValue(getElement(fullscreenElement), document.body);
  66. this._callback = function() {
  67. tmpIsFullscreen(Fullscreen.fullscreen);
  68. };
  69. document.addEventListener(Fullscreen.changeEventName, this._callback);
  70. }
  71. defineProperties(FullscreenButtonViewModel.prototype, {
  72. /**
  73. * Gets or sets the HTML element to place into fullscreen mode when the
  74. * corresponding button is pressed.
  75. * @memberof FullscreenButtonViewModel.prototype
  76. *
  77. * @type {Element}
  78. */
  79. fullscreenElement : {
  80. //TODO:@exception {DeveloperError} value must be a valid HTML Element.
  81. get : function() {
  82. return this._fullscreenElement;
  83. },
  84. set : function(value) {
  85. //>>includeStart('debug', pragmas.debug);
  86. if (!(value instanceof Element)) {
  87. throw new DeveloperError('value must be a valid Element.');
  88. }
  89. //>>includeEnd('debug');
  90. this._fullscreenElement = value;
  91. }
  92. },
  93. /**
  94. * Gets the Command to toggle fullscreen mode.
  95. * @memberof FullscreenButtonViewModel.prototype
  96. *
  97. * @type {Command}
  98. */
  99. command : {
  100. get : function() {
  101. return this._command;
  102. }
  103. }
  104. });
  105. /**
  106. * @returns {Boolean} true if the object has been destroyed, false otherwise.
  107. */
  108. FullscreenButtonViewModel.prototype.isDestroyed = function() {
  109. return false;
  110. };
  111. /**
  112. * Destroys the view model. Should be called to
  113. * properly clean up the view model when it is no longer needed.
  114. */
  115. FullscreenButtonViewModel.prototype.destroy = function() {
  116. document.removeEventListener(Fullscreen.changeEventName, this._callback);
  117. destroyObject(this);
  118. };
  119. export default FullscreenButtonViewModel;