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.

262 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. } else if (typeof title === "function") {
  126. title = title.call(this.customTime);
  127. }
  128. this.bar.style.left = x + 'px';
  129. this.bar.title = title;
  130. return false;
  131. };
  132. /**
  133. * Remove the CustomTime from the DOM
  134. */
  135. CustomTime.prototype.hide = function () {
  136. // remove the line from the DOM
  137. if (this.bar.parentNode) {
  138. this.bar.parentNode.removeChild(this.bar);
  139. }
  140. };
  141. /**
  142. * Set custom time.
  143. * @param {Date | number | string} time
  144. */
  145. CustomTime.prototype.setCustomTime = function(time) {
  146. this.customTime = util.convert(time, 'Date');
  147. this.redraw();
  148. };
  149. /**
  150. * Retrieve the current custom time.
  151. * @return {Date} customTime
  152. */
  153. CustomTime.prototype.getCustomTime = function() {
  154. return new Date(this.customTime.valueOf());
  155. };
  156. /**
  157. * Set custom title.
  158. * @param {Date | number | string} title
  159. */
  160. CustomTime.prototype.setCustomTitle = function(title) {
  161. this.options.title = title;
  162. };
  163. /**
  164. * Start moving horizontally
  165. * @param {Event} event
  166. * @private
  167. */
  168. CustomTime.prototype._onDragStart = function(event) {
  169. this.eventParams.dragging = true;
  170. this.eventParams.customTime = this.customTime;
  171. event.stopPropagation();
  172. };
  173. /**
  174. * Perform moving operating.
  175. * @param {Event} event
  176. * @private
  177. */
  178. CustomTime.prototype._onDrag = function (event) {
  179. if (!this.eventParams.dragging) return;
  180. var x = this.body.util.toScreen(this.eventParams.customTime) + event.deltaX;
  181. var time = this.body.util.toTime(x);
  182. this.setCustomTime(time);
  183. // fire a timechange event
  184. this.body.emitter.emit('timechange', {
  185. id: this.options.id,
  186. time: new Date(this.customTime.valueOf()),
  187. event: util.elementsCensor(event)
  188. });
  189. event.stopPropagation();
  190. };
  191. /**
  192. * Stop moving operating.
  193. * @param {Event} event
  194. * @private
  195. */
  196. CustomTime.prototype._onDragEnd = function (event) {
  197. if (!this.eventParams.dragging) return;
  198. // fire a timechanged event
  199. this.body.emitter.emit('timechanged', {
  200. id: this.options.id,
  201. time: new Date(this.customTime.valueOf()),
  202. event: util.elementsCensor(event)
  203. });
  204. event.stopPropagation();
  205. };
  206. /**
  207. * Find a custom time from an event target:
  208. * searches for the attribute 'custom-time' in the event target's element tree
  209. * @param {Event} event
  210. * @return {CustomTime | null} customTime
  211. */
  212. CustomTime.customTimeFromTarget = function(event) {
  213. var target = event.target;
  214. while (target) {
  215. if (target.hasOwnProperty('custom-time')) {
  216. return target['custom-time'];
  217. }
  218. target = target.parentNode;
  219. }
  220. return null;
  221. };
  222. module.exports = CustomTime;