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.

132 lines
3.0 KiB

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