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.

209 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 = Hammer(bar, {
  70. prevent_default: true
  71. });
  72. this.hammer.on('dragstart', this._onDragStart.bind(this));
  73. this.hammer.on('drag', this._onDrag.bind(this));
  74. this.hammer.on('dragend', this._onDragEnd.bind(this));
  75. };
  76. /**
  77. * Destroy the CustomTime bar
  78. */
  79. CustomTime.prototype.destroy = function () {
  80. this.options.showCustomTime = false;
  81. this.redraw(); // will remove the bar from the DOM
  82. this.hammer.enable(false);
  83. this.hammer = null;
  84. this.body = null;
  85. };
  86. /**
  87. * Repaint the component
  88. * @return {boolean} Returns true if the component is resized
  89. */
  90. CustomTime.prototype.redraw = function () {
  91. if (this.options.showCustomTime) {
  92. var parent = this.body.dom.backgroundVertical;
  93. if (this.bar.parentNode != parent) {
  94. // attach to the dom
  95. if (this.bar.parentNode) {
  96. this.bar.parentNode.removeChild(this.bar);
  97. }
  98. parent.appendChild(this.bar);
  99. }
  100. var x = this.body.util.toScreen(this.customTime);
  101. var locale = this.options.locales[this.options.locale];
  102. var title = locale.time + ': ' + moment(this.customTime).format('dddd, MMMM Do YYYY, H:mm:ss');
  103. title = title.charAt(0).toUpperCase() + title.substring(1);
  104. this.bar.style.left = x + 'px';
  105. this.bar.title = title;
  106. }
  107. else {
  108. // remove the line from the DOM
  109. if (this.bar.parentNode) {
  110. this.bar.parentNode.removeChild(this.bar);
  111. }
  112. }
  113. return false;
  114. };
  115. /**
  116. * Set custom time.
  117. * @param {Date | number | string} time
  118. */
  119. CustomTime.prototype.setCustomTime = function(time) {
  120. this.customTime = util.convert(time, 'Date');
  121. this.redraw();
  122. };
  123. /**
  124. * Retrieve the current custom time.
  125. * @return {Date} customTime
  126. */
  127. CustomTime.prototype.getCustomTime = function() {
  128. return new Date(this.customTime.valueOf());
  129. };
  130. /**
  131. * Start moving horizontally
  132. * @param {Event} event
  133. * @private
  134. */
  135. CustomTime.prototype._onDragStart = function(event) {
  136. this.eventParams.dragging = true;
  137. this.eventParams.customTime = this.customTime;
  138. event.stopPropagation();
  139. event.preventDefault();
  140. };
  141. /**
  142. * Perform moving operating.
  143. * @param {Event} event
  144. * @private
  145. */
  146. CustomTime.prototype._onDrag = function (event) {
  147. if (!this.eventParams.dragging) return;
  148. var deltaX = event.gesture.deltaX,
  149. x = this.body.util.toScreen(this.eventParams.customTime) + deltaX,
  150. 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. event.preventDefault();
  159. };
  160. /**
  161. * Stop moving operating.
  162. * @param {event} event
  163. * @private
  164. */
  165. CustomTime.prototype._onDragEnd = function (event) {
  166. if (!this.eventParams.dragging) return;
  167. // fire a timechanged event
  168. this.body.emitter.emit('timechanged', {
  169. id: this.options.id,
  170. time: new Date(this.customTime.valueOf())
  171. });
  172. event.stopPropagation();
  173. event.preventDefault();
  174. };
  175. module.exports = CustomTime;