vis.js is a dynamic, browser-based visualization library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
2.9 KiB

  1. /**
  2. * A current time bar
  3. * @param {{range: Range, dom: Object, domProps: Object}} body
  4. * @param {Object} [options] Available parameters:
  5. * {Boolean} [showCurrentTime]
  6. * @constructor CurrentTime
  7. * @extends Component
  8. */
  9. function CurrentTime (body, options) {
  10. this.body = body;
  11. // default options
  12. this.defaultOptions = {
  13. showCurrentTime: true
  14. };
  15. this.options = util.extend({}, this.defaultOptions);
  16. this._create();
  17. this.setOptions(options);
  18. }
  19. CurrentTime.prototype = new Component();
  20. /**
  21. * Create the HTML DOM for the current time bar
  22. * @private
  23. */
  24. CurrentTime.prototype._create = function() {
  25. var bar = document.createElement('div');
  26. bar.className = 'currenttime';
  27. bar.style.position = 'absolute';
  28. bar.style.top = '0px';
  29. bar.style.height = '100%';
  30. this.bar = bar;
  31. };
  32. /**
  33. * Destroy the CurrentTime bar
  34. */
  35. CurrentTime.prototype.destroy = function () {
  36. this.options.showCurrentTime = false;
  37. this.redraw(); // will remove the bar from the DOM and stop refreshing
  38. this.body = null;
  39. };
  40. /**
  41. * Set options for the component. Options will be merged in current options.
  42. * @param {Object} options Available parameters:
  43. * {boolean} [showCurrentTime]
  44. */
  45. CurrentTime.prototype.setOptions = function(options) {
  46. if (options) {
  47. // copy all options that we know
  48. util.selectiveExtend(['showCurrentTime'], this.options, options);
  49. }
  50. };
  51. /**
  52. * Repaint the component
  53. * @return {boolean} Returns true if the component is resized
  54. */
  55. CurrentTime.prototype.redraw = function() {
  56. if (this.options.showCurrentTime) {
  57. var parent = this.body.dom.backgroundVertical;
  58. if (this.bar.parentNode != parent) {
  59. // attach to the dom
  60. if (this.bar.parentNode) {
  61. this.bar.parentNode.removeChild(this.bar);
  62. }
  63. parent.appendChild(this.bar);
  64. this.start();
  65. }
  66. var now = new Date();
  67. var x = this.body.util.toScreen(now);
  68. this.bar.style.left = x + 'px';
  69. this.bar.title = 'Current time: ' + now;
  70. }
  71. else {
  72. // remove the line from the DOM
  73. if (this.bar.parentNode) {
  74. this.bar.parentNode.removeChild(this.bar);
  75. }
  76. this.stop();
  77. }
  78. return false;
  79. };
  80. /**
  81. * Start auto refreshing the current time bar
  82. */
  83. CurrentTime.prototype.start = function() {
  84. var me = this;
  85. function update () {
  86. me.stop();
  87. // determine interval to refresh
  88. var scale = me.body.range.conversion(me.body.domProps.center.width).scale;
  89. var interval = 1 / scale / 10;
  90. if (interval < 30) interval = 30;
  91. if (interval > 1000) interval = 1000;
  92. me.redraw();
  93. // start a timer to adjust for the new time
  94. me.currentTimeTimer = setTimeout(update, interval);
  95. }
  96. update();
  97. };
  98. /**
  99. * Stop auto refreshing the current time bar
  100. */
  101. CurrentTime.prototype.stop = function() {
  102. if (this.currentTimeTimer !== undefined) {
  103. clearTimeout(this.currentTimeTimer);
  104. delete this.currentTimeTimer;
  105. }
  106. };