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.

260 lines
6.5 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. * {number | string} id
  11. * {string} locales
  12. * {string} locale
  13. * @constructor CustomTime
  14. * @extends Component
  15. */
  16. function CustomTime (body, options) {
  17. this.body = body;
  18. // default options
  19. this.defaultOptions = {
  20. moment: moment,
  21. locales: locales,
  22. locale: 'en',
  23. id: undefined,
  24. title: undefined
  25. };
  26. this.options = util.extend({}, this.defaultOptions);
  27. if (options && options.time) {
  28. this.customTime = options.time;
  29. } else {
  30. this.customTime = new Date();
  31. }
  32. this.eventParams = {}; // stores state parameters while dragging the bar
  33. this.setOptions(options);
  34. // create the DOM
  35. this._create();
  36. }
  37. CustomTime.prototype = new Component();
  38. /**
  39. * Set options for the component. Options will be merged in current options.
  40. * @param {Object} options Available parameters:
  41. * {number | string} id
  42. * {string} locales
  43. * {string} locale
  44. */
  45. CustomTime.prototype.setOptions = function(options) {
  46. if (options) {
  47. // copy all options that we know
  48. util.selectiveExtend(['moment', 'locale', 'locales', 'id'], this.options, options);
  49. }
  50. };
  51. /**
  52. * Create the DOM for the custom time
  53. * @private
  54. */
  55. CustomTime.prototype._create = function() {
  56. var bar = document.createElement('div');
  57. bar['custom-time'] = this;
  58. bar.className = 'vis-custom-time ' + (this.options.id || '');
  59. bar.style.position = 'absolute';
  60. bar.style.top = '0px';
  61. bar.style.height = '100%';
  62. this.bar = bar;
  63. var drag = document.createElement('div');
  64. drag.style.position = 'relative';
  65. drag.style.top = '0px';
  66. drag.style.left = '-10px';
  67. drag.style.height = '100%';
  68. drag.style.width = '20px';
  69. function onMouseWheel (e) {
  70. this.body.range._onMouseWheel(e);
  71. }
  72. if (drag.addEventListener) {
  73. // IE9, Chrome, Safari, Opera
  74. drag.addEventListener("mousewheel", onMouseWheel.bind(this), false);
  75. // Firefox
  76. drag.addEventListener("DOMMouseScroll", onMouseWheel.bind(this), false);
  77. } else {
  78. // IE 6/7/8
  79. drag.attachEvent("onmousewheel", onMouseWheel.bind(this));
  80. }
  81. bar.appendChild(drag);
  82. // attach event listeners
  83. this.hammer = new Hammer(drag);
  84. this.hammer.on('panstart', this._onDragStart.bind(this));
  85. this.hammer.on('panmove', this._onDrag.bind(this));
  86. this.hammer.on('panend', this._onDragEnd.bind(this));
  87. this.hammer.get('pan').set({threshold:5, direction: Hammer.DIRECTION_HORIZONTAL});
  88. };
  89. /**
  90. * Destroy the CustomTime bar
  91. */
  92. CustomTime.prototype.destroy = function () {
  93. this.hide();
  94. this.hammer.destroy();
  95. this.hammer = null;
  96. this.body = null;
  97. };
  98. /**
  99. * Repaint the component
  100. * @return {boolean} Returns true if the component is resized
  101. */
  102. CustomTime.prototype.redraw = function () {
  103. var parent = this.body.dom.backgroundVertical;
  104. if (this.bar.parentNode != parent) {
  105. // attach to the dom
  106. if (this.bar.parentNode) {
  107. this.bar.parentNode.removeChild(this.bar);
  108. }
  109. parent.appendChild(this.bar);
  110. }
  111. var x = this.body.util.toScreen(this.customTime);
  112. var locale = this.options.locales[this.options.locale];
  113. if (!locale) {
  114. if (!this.warned) {
  115. console.log('WARNING: options.locales[\'' + this.options.locale + '\'] not found. See http://visjs.org/docs/timeline/#Localization');
  116. this.warned = true;
  117. }
  118. locale = this.options.locales['en']; // fall back on english when not available
  119. }
  120. var title = this.options.title;
  121. // To hide the title completely use empty string ''.
  122. if (title === undefined) {
  123. title = locale.time + ': ' + this.options.moment(this.customTime).format('dddd, MMMM Do YYYY, H:mm:ss');
  124. title = title.charAt(0).toUpperCase() + title.substring(1);
  125. }
  126. this.bar.style.left = x + 'px';
  127. this.bar.title = title;
  128. return false;
  129. };
  130. /**
  131. * Remove the CustomTime from the DOM
  132. */
  133. CustomTime.prototype.hide = function () {
  134. // remove the line from the DOM
  135. if (this.bar.parentNode) {
  136. this.bar.parentNode.removeChild(this.bar);
  137. }
  138. };
  139. /**
  140. * Set custom time.
  141. * @param {Date | number | string} time
  142. */
  143. CustomTime.prototype.setCustomTime = function(time) {
  144. this.customTime = util.convert(time, 'Date');
  145. this.redraw();
  146. };
  147. /**
  148. * Retrieve the current custom time.
  149. * @return {Date} customTime
  150. */
  151. CustomTime.prototype.getCustomTime = function() {
  152. return new Date(this.customTime.valueOf());
  153. };
  154. /**
  155. * Set custom title.
  156. * @param {Date | number | string} title
  157. */
  158. CustomTime.prototype.setCustomTitle = function(title) {
  159. this.options.title = title;
  160. };
  161. /**
  162. * Start moving horizontally
  163. * @param {Event} event
  164. * @private
  165. */
  166. CustomTime.prototype._onDragStart = function(event) {
  167. this.eventParams.dragging = true;
  168. this.eventParams.customTime = this.customTime;
  169. event.stopPropagation();
  170. };
  171. /**
  172. * Perform moving operating.
  173. * @param {Event} event
  174. * @private
  175. */
  176. CustomTime.prototype._onDrag = function (event) {
  177. if (!this.eventParams.dragging) return;
  178. var x = this.body.util.toScreen(this.eventParams.customTime) + event.deltaX;
  179. var time = this.body.util.toTime(x);
  180. this.setCustomTime(time);
  181. // fire a timechange event
  182. this.body.emitter.emit('timechange', {
  183. id: this.options.id,
  184. time: new Date(this.customTime.valueOf()),
  185. event: util.elementsCensor(event)
  186. });
  187. event.stopPropagation();
  188. };
  189. /**
  190. * Stop moving operating.
  191. * @param {Event} event
  192. * @private
  193. */
  194. CustomTime.prototype._onDragEnd = function (event) {
  195. if (!this.eventParams.dragging) return;
  196. // fire a timechanged event
  197. this.body.emitter.emit('timechanged', {
  198. id: this.options.id,
  199. time: new Date(this.customTime.valueOf()),
  200. event: util.elementsCensor(event)
  201. });
  202. event.stopPropagation();
  203. };
  204. /**
  205. * Find a custom time from an event target:
  206. * searches for the attribute 'custom-time' in the event target's element tree
  207. * @param {Event} event
  208. * @return {CustomTime | null} customTime
  209. */
  210. CustomTime.customTimeFromTarget = function(event) {
  211. var target = event.target;
  212. while (target) {
  213. if (target.hasOwnProperty('custom-time')) {
  214. return target['custom-time'];
  215. }
  216. target = target.parentNode;
  217. }
  218. return null;
  219. };
  220. module.exports = CustomTime;