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.

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