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.

158 lines
4.9 KiB

  1. /**
  2. * Created by Alex on 10/3/2014.
  3. */
  4. var moment = require('../module/moment');
  5. exports.convertHiddenOptions = function(timeline) {
  6. var hiddenTimes = timeline.options.hide;
  7. if (Array.isArray(hiddenTimes) == true) {
  8. for (var i = 0; i < hiddenTimes.length; i++) {
  9. var dateItem = {};
  10. dateItem.start = moment(hiddenTimes[i].start).toDate().valueOf();
  11. dateItem.end = moment(hiddenTimes[i].end).toDate().valueOf();
  12. timeline.body.hiddenDates.push(dateItem);
  13. }
  14. timeline.body.hiddenDates.sort(function(a,b) {return a.start - b.start;}); // sort by start time
  15. }
  16. else {
  17. timeline.body.hiddenDates = [{
  18. start:moment(hiddenTimes.start).toDate().valueOf(),
  19. end:moment(hiddenTimes.end).toDate().valueOf()
  20. }
  21. ];
  22. }
  23. }
  24. exports.stepOverHiddenDates = function(timeStep, previousTime) {
  25. var stepInHidden = false;
  26. var currentValue = timeStep.current.valueOf();
  27. for (var i = 0; i < timeStep.hiddenDates.length; i++) {
  28. var startDate = timeStep.hiddenDates[i].start;
  29. var endDate = timeStep.hiddenDates[i].end;
  30. if (currentValue >= startDate && currentValue < endDate) {
  31. stepInHidden = true;
  32. break;
  33. }
  34. }
  35. if (stepInHidden == true && currentValue < timeStep._end.valueOf() && currentValue != previousTime) {
  36. timeStep.current = moment(endDate).toDate();
  37. }
  38. }
  39. exports.toScreen = function(timeline, time, width) {
  40. var hidden = exports.isHidden(time, timeline.body.hiddenDates)
  41. if (hidden.hidden == true) {
  42. time = hidden.startDate;
  43. }
  44. var res = exports.correctTimeForDuration(timeline.body.hiddenDates, timeline.range, time);
  45. var duration = res.duration;
  46. time = res.time;
  47. var conversion = timeline.range.conversion(width, duration);
  48. return (time.valueOf() - conversion.offset) * conversion.scale;
  49. }
  50. exports.toTime = function(body, range, x, width) {
  51. var duration = exports.getHiddenDuration(body.hiddenDates, range);
  52. var conversion = range.conversion(width, duration);
  53. var time = new Date(x / conversion.scale + conversion.offset);
  54. //var hidden = exports.isHidden(time, timeline.body.hiddenDates)
  55. //if (hidden.hidden == true) {
  56. // time = hidden.startDate;
  57. //}
  58. //time = exports.correctTimeForDuration(body.hiddenDates, range, time).time;
  59. return time;
  60. }
  61. exports.getHiddenDuration = function(hiddenTimes, range) {
  62. var duration = 0;
  63. for (var i = 0; i < hiddenTimes.length; i++) {
  64. var startDate = hiddenTimes[i].start;
  65. var endDate = hiddenTimes[i].end;
  66. // if time after the cutout, and the
  67. if (startDate >= range.start && endDate < range.end) {
  68. duration += endDate - startDate;
  69. }
  70. }
  71. return duration;
  72. }
  73. exports.correctTimeForDuration = function(hiddenTimes, range, time) {
  74. var duration = 0;
  75. var timeOffset = 0;
  76. time = moment(time).toDate().valueOf()
  77. for (var i = 0; i < hiddenTimes.length; i++) {
  78. var startDate = hiddenTimes[i].start;
  79. var endDate = hiddenTimes[i].end;
  80. // if time after the cutout, and the
  81. if (startDate >= range.start && endDate < range.end) {
  82. duration += (endDate - startDate);
  83. if (time >= endDate) {
  84. timeOffset += (endDate - startDate);
  85. }
  86. }
  87. }
  88. time -= timeOffset;
  89. return {duration: duration, time:time, offset: timeOffset};
  90. }
  91. exports.snapAwayFromHidden = function(hiddenTimes, range, start, end, delta, zoom) {
  92. zoom = zoom || false;
  93. var newStart = start;
  94. var newEnd = end;
  95. for (var i = 0; i < hiddenTimes.length; i++) {
  96. var startDate = hiddenTimes[i].start;
  97. var endDate = hiddenTimes[i].end;
  98. if (start >= startDate && start < endDate) { // if the start is entering a hidden zone
  99. range.deltaDifference += delta;
  100. if (range.previousDelta - delta > 0 && zoom == false || zoom == true && range.previousDelta - delta < 0) { // from the left
  101. console.log("start from left, snap to right")
  102. newStart = endDate + 1;
  103. }
  104. else { // from the right
  105. console.log("start from right, snap to left")
  106. newStart = startDate - 1;
  107. }
  108. return {newStart: newStart, newEnd: newEnd};
  109. }
  110. else if (end >= startDate && end < endDate) { // if the start is entering a hidden zone
  111. range.deltaDifference += delta;
  112. if (range.previousDelta - delta < 0) { // from the right
  113. console.log("end from right, snap to left")
  114. newEnd = startDate - 1;
  115. }
  116. else { // from the left
  117. console.log("end from left, snap to right")
  118. newEnd = endDate + 1;
  119. }
  120. return {newStart: newStart, newEnd: newEnd};
  121. }
  122. }
  123. return false;
  124. }
  125. exports.isHidden = function(time, hiddenTimes) {
  126. var isHidden = false;
  127. for (var i = 0; i < hiddenTimes.length; i++) {
  128. var startDate = hiddenTimes[i].start;
  129. var endDate = hiddenTimes[i].end;
  130. if (time >= startDate && time < endDate) { // if the start is entering a hidden zone
  131. isHidden = true;
  132. break;
  133. }
  134. }
  135. return {hidden: isHidden, startDate: startDate, endDate: endDate};
  136. }