PerformanceWatchdog.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 knockout from '../../ThirdParty/knockout.js';
  6. import getElement from '../getElement.js';
  7. import PerformanceWatchdogViewModel from './PerformanceWatchdogViewModel.js';
  8. /**
  9. * Monitors performance of the application and displays a message if poor performance is detected.
  10. *
  11. * @alias PerformanceWatchdog
  12. * @constructor
  13. *
  14. * @param {Object} [options] Object with the following properties:
  15. * @param {Element|String} options.container The DOM element or ID that will contain the widget.
  16. * @param {Scene} options.scene The {@link Scene} for which to monitor performance.
  17. * @param {String} [options.lowFrameRateMessage='This application appears to be performing poorly on your system. Please try using a different web browser or updating your video drivers.'] The
  18. * message to display when a low frame rate is detected. The message is interpeted as HTML, so make sure
  19. * it comes from a trusted source so that your application is not vulnerable to cross-site scripting attacks.
  20. */
  21. function PerformanceWatchdog(options) {
  22. //>>includeStart('debug', pragmas.debug);
  23. if (!defined(options) || !defined(options.container)) {
  24. throw new DeveloperError('options.container is required.');
  25. }
  26. if (!defined(options.scene)) {
  27. throw new DeveloperError('options.scene is required.');
  28. }
  29. //>>includeEnd('debug');
  30. var container = getElement(options.container);
  31. var viewModel = new PerformanceWatchdogViewModel(options);
  32. var element = document.createElement('div');
  33. element.className = 'cesium-performance-watchdog-message-area';
  34. element.setAttribute('data-bind', 'visible: showingLowFrameRateMessage');
  35. var dismissButton = document.createElement('button');
  36. dismissButton.setAttribute('type', 'button');
  37. dismissButton.className = 'cesium-performance-watchdog-message-dismiss';
  38. dismissButton.innerHTML = '×';
  39. dismissButton.setAttribute('data-bind', 'click: dismissMessage');
  40. element.appendChild(dismissButton);
  41. var message = document.createElement('div');
  42. message.className = 'cesium-performance-watchdog-message';
  43. message.setAttribute('data-bind', 'html: lowFrameRateMessage');
  44. element.appendChild(message);
  45. container.appendChild(element);
  46. knockout.applyBindings(viewModel, element);
  47. this._container = container;
  48. this._viewModel = viewModel;
  49. this._element = element;
  50. }
  51. defineProperties(PerformanceWatchdog.prototype, {
  52. /**
  53. * Gets the parent container.
  54. * @memberof PerformanceWatchdog.prototype
  55. *
  56. * @type {Element}
  57. */
  58. container : {
  59. get : function() {
  60. return this._container;
  61. }
  62. },
  63. /**
  64. * Gets the view model.
  65. * @memberof PerformanceWatchdog.prototype
  66. *
  67. * @type {PerformanceWatchdogViewModel}
  68. */
  69. viewModel : {
  70. get : function() {
  71. return this._viewModel;
  72. }
  73. }
  74. });
  75. /**
  76. * @memberof PerformanceWatchdog
  77. * @returns {Boolean} true if the object has been destroyed, false otherwise.
  78. */
  79. PerformanceWatchdog.prototype.isDestroyed = function() {
  80. return false;
  81. };
  82. /**
  83. * Destroys the widget. Should be called if permanently
  84. * removing the widget from layout.
  85. * @memberof PerformanceWatchdog
  86. */
  87. PerformanceWatchdog.prototype.destroy = function() {
  88. this._viewModel.destroy();
  89. knockout.cleanNode(this._element);
  90. this._container.removeChild(this._element);
  91. return destroyObject(this);
  92. };
  93. export default PerformanceWatchdog;