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.

141 lines
3.4 KiB

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