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.

193 lines
4.6 KiB

  1. var Hammer = require('../../module/hammer');
  2. var util = require('../../util');
  3. var Component = require('./Component');
  4. var moment = require('../../module/moment');
  5. var locales = require('../locales');
  6. /**
  7. * A custom time bar
  8. * @param {{range: Range, dom: Object}} body
  9. * @param {Object} [options] Available parameters:
  10. * {Boolean} [showCustomTime]
  11. * @constructor CustomTime
  12. * @extends Component
  13. */
  14. function CustomTime (body, options) {
  15. this.body = body;
  16. // default options
  17. this.defaultOptions = {
  18. showCustomTime: false,
  19. locales: locales,
  20. locale: 'en'
  21. };
  22. this.options = util.extend({}, this.defaultOptions);
  23. this.customTime = new Date();
  24. this.eventParams = {}; // stores state parameters while dragging the bar
  25. // create the DOM
  26. this._create();
  27. this.setOptions(options);
  28. }
  29. CustomTime.prototype = new Component();
  30. /**
  31. * Set options for the component. Options will be merged in current options.
  32. * @param {Object} options Available parameters:
  33. * {boolean} [showCustomTime]
  34. */
  35. CustomTime.prototype.setOptions = function(options) {
  36. if (options) {
  37. // copy all options that we know
  38. util.selectiveExtend(['showCustomTime', 'locale', 'locales'], this.options, options);
  39. }
  40. };
  41. /**
  42. * Create the DOM for the custom time
  43. * @private
  44. */
  45. CustomTime.prototype._create = function() {
  46. var bar = document.createElement('div');
  47. bar.className = 'customtime';
  48. bar.style.position = 'absolute';
  49. bar.style.top = '0px';
  50. bar.style.height = '100%';
  51. this.bar = bar;
  52. var drag = document.createElement('div');
  53. drag.style.position = 'relative';
  54. drag.style.top = '0px';
  55. drag.style.left = '-10px';
  56. drag.style.height = '100%';
  57. drag.style.width = '20px';
  58. bar.appendChild(drag);
  59. // attach event listeners
  60. this.hammer = new Hammer(drag);
  61. this.hammer.on('panstart', this._onDragStart.bind(this));
  62. this.hammer.on('panmove', this._onDrag.bind(this));
  63. this.hammer.on('panend', this._onDragEnd.bind(this));
  64. };
  65. /**
  66. * Destroy the CustomTime bar
  67. */
  68. CustomTime.prototype.destroy = function () {
  69. this.options.showCustomTime = false;
  70. this.redraw(); // will remove the bar from the DOM
  71. this.hammer.enable(false);
  72. this.hammer = null;
  73. this.body = null;
  74. };
  75. /**
  76. * Repaint the component
  77. * @return {boolean} Returns true if the component is resized
  78. */
  79. CustomTime.prototype.redraw = function () {
  80. if (this.options.showCustomTime) {
  81. var parent = this.body.dom.backgroundVertical;
  82. if (this.bar.parentNode != parent) {
  83. // attach to the dom
  84. if (this.bar.parentNode) {
  85. this.bar.parentNode.removeChild(this.bar);
  86. }
  87. parent.appendChild(this.bar);
  88. }
  89. var x = this.body.util.toScreen(this.customTime);
  90. var locale = this.options.locales[this.options.locale];
  91. var title = locale.time + ': ' + moment(this.customTime).format('dddd, MMMM Do YYYY, H:mm:ss');
  92. title = title.charAt(0).toUpperCase() + title.substring(1);
  93. this.bar.style.left = x + 'px';
  94. this.bar.title = title;
  95. }
  96. else {
  97. // remove the line from the DOM
  98. if (this.bar.parentNode) {
  99. this.bar.parentNode.removeChild(this.bar);
  100. }
  101. }
  102. return false;
  103. };
  104. /**
  105. * Set custom time.
  106. * @param {Date | number | string} time
  107. */
  108. CustomTime.prototype.setCustomTime = function(time) {
  109. this.customTime = util.convert(time, 'Date');
  110. this.redraw();
  111. };
  112. /**
  113. * Retrieve the current custom time.
  114. * @return {Date} customTime
  115. */
  116. CustomTime.prototype.getCustomTime = function() {
  117. return new Date(this.customTime.valueOf());
  118. };
  119. /**
  120. * Start moving horizontally
  121. * @param {Event} event
  122. * @private
  123. */
  124. CustomTime.prototype._onDragStart = function(event) {
  125. this.eventParams.dragging = true;
  126. this.eventParams.customTime = this.customTime;
  127. //event.stopPropagation();
  128. event.preventDefault();
  129. };
  130. /**
  131. * Perform moving operating.
  132. * @param {Event} event
  133. * @private
  134. */
  135. CustomTime.prototype._onDrag = function (event) {
  136. if (!this.eventParams.dragging) return;
  137. var x = this.body.util.toScreen(this.eventParams.customTime) + event.deltaX;
  138. var time = this.body.util.toTime(x);
  139. this.setCustomTime(time);
  140. // fire a timechange event
  141. this.body.emitter.emit('timechange', {
  142. time: new Date(this.customTime.valueOf())
  143. });
  144. //event.stopPropagation();
  145. event.preventDefault();
  146. };
  147. /**
  148. * Stop moving operating.
  149. * @param {Event} event
  150. * @private
  151. */
  152. CustomTime.prototype._onDragEnd = function (event) {
  153. if (!this.eventParams.dragging) return;
  154. // fire a timechanged event
  155. this.body.emitter.emit('timechanged', {
  156. time: new Date(this.customTime.valueOf())
  157. });
  158. //event.stopPropagation();
  159. event.preventDefault();
  160. };
  161. module.exports = CustomTime;