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.

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