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.

216 lines
5.2 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. if (!locale) {
  103. if (!this.warned) {
  104. console.log('WARNING: locale "' + this.options.locale + '" not found in vis.js');
  105. this.warned = true;
  106. }
  107. locale = this.options.locales['en']; // fall back on english when not available
  108. }
  109. var title = locale.time + ': ' + moment(this.customTime).format('dddd, MMMM Do YYYY, H:mm:ss');
  110. title = title.charAt(0).toUpperCase() + title.substring(1);
  111. this.bar.style.left = x + 'px';
  112. this.bar.title = title;
  113. }
  114. else {
  115. // remove the line from the DOM
  116. if (this.bar.parentNode) {
  117. this.bar.parentNode.removeChild(this.bar);
  118. }
  119. }
  120. return false;
  121. };
  122. /**
  123. * Set custom time.
  124. * @param {Date | number | string} time
  125. */
  126. CustomTime.prototype.setCustomTime = function(time) {
  127. this.customTime = util.convert(time, 'Date');
  128. this.redraw();
  129. };
  130. /**
  131. * Retrieve the current custom time.
  132. * @return {Date} customTime
  133. */
  134. CustomTime.prototype.getCustomTime = function() {
  135. return new Date(this.customTime.valueOf());
  136. };
  137. /**
  138. * Start moving horizontally
  139. * @param {Event} event
  140. * @private
  141. */
  142. CustomTime.prototype._onDragStart = function(event) {
  143. this.eventParams.dragging = true;
  144. this.eventParams.customTime = this.customTime;
  145. event.stopPropagation();
  146. event.preventDefault();
  147. };
  148. /**
  149. * Perform moving operating.
  150. * @param {Event} event
  151. * @private
  152. */
  153. CustomTime.prototype._onDrag = function (event) {
  154. if (!this.eventParams.dragging) return;
  155. var deltaX = event.gesture.deltaX,
  156. x = this.body.util.toScreen(this.eventParams.customTime) + deltaX,
  157. time = this.body.util.toTime(x);
  158. this.setCustomTime(time);
  159. // fire a timechange event
  160. this.body.emitter.emit('timechange', {
  161. id: this.options.id,
  162. time: new Date(this.customTime.valueOf())
  163. });
  164. event.stopPropagation();
  165. event.preventDefault();
  166. };
  167. /**
  168. * Stop moving operating.
  169. * @param {event} event
  170. * @private
  171. */
  172. CustomTime.prototype._onDragEnd = function (event) {
  173. if (!this.eventParams.dragging) return;
  174. // fire a timechanged event
  175. this.body.emitter.emit('timechanged', {
  176. id: this.options.id,
  177. time: new Date(this.customTime.valueOf())
  178. });
  179. event.stopPropagation();
  180. event.preventDefault();
  181. };
  182. module.exports = CustomTime;