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.

426 lines
12 KiB

10 years ago
10 years ago
10 years ago
10 years ago
  1. var util = require('../../util');
  2. var stack = require('../Stack');
  3. var ItemRange = require('./item/ItemRange');
  4. /**
  5. * @constructor Group
  6. * @param {Number | String} groupId
  7. * @param {Object} data
  8. * @param {ItemSet} itemSet
  9. */
  10. function Group (groupId, data, itemSet) {
  11. this.groupId = groupId;
  12. this.itemSet = itemSet;
  13. this.dom = {};
  14. this.props = {
  15. label: {
  16. width: 0,
  17. height: 0
  18. }
  19. };
  20. this.className = null;
  21. this.items = {}; // items filtered by groupId of this group
  22. this.visibleItems = []; // items currently visible in window
  23. this.orderedItems = { // items sorted by start and by end
  24. byStart: [],
  25. byEnd: []
  26. };
  27. this._create();
  28. this.setData(data);
  29. }
  30. /**
  31. * Create DOM elements for the group
  32. * @private
  33. */
  34. Group.prototype._create = function() {
  35. var label = document.createElement('div');
  36. label.className = 'vlabel';
  37. this.dom.label = label;
  38. var inner = document.createElement('div');
  39. inner.className = 'inner';
  40. label.appendChild(inner);
  41. this.dom.inner = inner;
  42. var foreground = document.createElement('div');
  43. foreground.className = 'group';
  44. foreground['timeline-group'] = this;
  45. this.dom.foreground = foreground;
  46. this.dom.background = document.createElement('div');
  47. this.dom.background.className = 'group';
  48. this.dom.axis = document.createElement('div');
  49. this.dom.axis.className = 'group';
  50. // create a hidden marker to detect when the Timelines container is attached
  51. // to the DOM, or the style of a parent of the Timeline is changed from
  52. // display:none is changed to visible.
  53. this.dom.marker = document.createElement('div');
  54. this.dom.marker.style.visibility = 'hidden';
  55. this.dom.marker.innerHTML = '?';
  56. this.dom.background.appendChild(this.dom.marker);
  57. };
  58. /**
  59. * Set the group data for this group
  60. * @param {Object} data Group data, can contain properties content and className
  61. */
  62. Group.prototype.setData = function(data) {
  63. // update contents
  64. var content = data && data.content;
  65. if (content instanceof Element) {
  66. this.dom.inner.appendChild(content);
  67. }
  68. else if (content != undefined) {
  69. this.dom.inner.innerHTML = content;
  70. }
  71. else {
  72. this.dom.inner.innerHTML = this.groupId;
  73. }
  74. // update title
  75. this.dom.label.title = data && data.title || '';
  76. // Mark hidden when empty
  77. if (!this.dom.inner.firstChild) {
  78. util.addClassName(this.dom.inner, 'hidden');
  79. }
  80. else {
  81. util.removeClassName(this.dom.inner, 'hidden');
  82. }
  83. // update className
  84. var className = data && data.className || null;
  85. if (className != this.className) {
  86. if (this.className) {
  87. util.removeClassName(this.dom.label, className);
  88. util.removeClassName(this.dom.foreground, className);
  89. util.removeClassName(this.dom.background, className);
  90. util.removeClassName(this.dom.axis, className);
  91. }
  92. util.addClassName(this.dom.label, className);
  93. util.addClassName(this.dom.foreground, className);
  94. util.addClassName(this.dom.background, className);
  95. util.addClassName(this.dom.axis, className);
  96. // TODO: addClassName to contents as well
  97. }
  98. // TODO: be able to remove className
  99. };
  100. /**
  101. * Get the width of the group label
  102. * @return {number} width
  103. */
  104. Group.prototype.getLabelWidth = function() {
  105. return this.props.label.width;
  106. };
  107. /**
  108. * Repaint this group
  109. * @param {{start: number, end: number}} range
  110. * @param {{item: number, axis: number}} margin
  111. * @param {boolean} [restack=false] Force restacking of all items
  112. * @return {boolean} Returns true if the group is resized
  113. */
  114. Group.prototype.redraw = function(range, margin, restack) {
  115. var resized = false;
  116. this.visibleItems = this._updateVisibleItems(this.orderedItems, this.visibleItems, range);
  117. // force recalculation of the height of the items when the marker height changed
  118. // (due to the Timeline being attached to the DOM or changed from display:none to visible)
  119. var markerHeight = this.dom.marker.clientHeight;
  120. if (markerHeight != this.lastMarkerHeight) {
  121. this.lastMarkerHeight = markerHeight;
  122. util.forEach(this.items, function (item) {
  123. item.dirty = true;
  124. if (item.displayed) item.redraw();
  125. });
  126. restack = true;
  127. }
  128. // reposition visible items vertically
  129. if (this.itemSet.options.stack) { // TODO: ugly way to access options...
  130. stack.stack(this.visibleItems, margin, restack);
  131. }
  132. else { // no stacking
  133. stack.nostack(this.visibleItems, margin);
  134. }
  135. // recalculate the height of the group
  136. var height;
  137. var visibleItems = this.visibleItems;
  138. if (visibleItems.length) {
  139. var min = visibleItems[0].top;
  140. var max = visibleItems[0].top + visibleItems[0].height;
  141. util.forEach(visibleItems, function (item) {
  142. min = Math.min(min, item.top);
  143. max = Math.max(max, (item.top + item.height));
  144. });
  145. if (min > margin.axis) {
  146. // there is an empty gap between the lowest item and the axis
  147. var offset = min - margin.axis;
  148. max -= offset;
  149. util.forEach(visibleItems, function (item) {
  150. item.top -= offset;
  151. });
  152. }
  153. height = max + margin.item / 2;
  154. }
  155. else {
  156. height = margin.axis + margin.item;
  157. }
  158. height = Math.max(height, this.props.label.height);
  159. // calculate actual size and position
  160. var foreground = this.dom.foreground;
  161. this.top = this.itemSet.getGroupTop(this.groupId); // top is determined by the ItemSet, from the heights of previous groups
  162. this.left = foreground.offsetLeft;
  163. this.width = foreground.offsetWidth;
  164. resized = util.updateProperty(this, 'height', height) || resized;
  165. // recalculate size of label
  166. resized = util.updateProperty(this.props.label, 'width', this.dom.inner.clientWidth) || resized;
  167. resized = util.updateProperty(this.props.label, 'height', this.dom.inner.clientHeight) || resized;
  168. // apply new height
  169. this.dom.background.style.height = height + 'px';
  170. this.dom.foreground.style.height = height + 'px';
  171. this.dom.label.style.height = height + 'px';
  172. // update vertical position of items after they are re-stacked and the height of the group is calculated
  173. for (var i = 0, ii = this.visibleItems.length; i < ii; i++) {
  174. var item = this.visibleItems[i];
  175. item.repositionY();
  176. }
  177. return resized;
  178. };
  179. /**
  180. * Show this group: attach to the DOM
  181. */
  182. Group.prototype.show = function() {
  183. if (!this.dom.label.parentNode) {
  184. this.itemSet.dom.labelSet.appendChild(this.dom.label);
  185. }
  186. if (!this.dom.foreground.parentNode) {
  187. this.itemSet.dom.foreground.appendChild(this.dom.foreground);
  188. }
  189. if (!this.dom.background.parentNode) {
  190. this.itemSet.dom.background.appendChild(this.dom.background);
  191. }
  192. if (!this.dom.axis.parentNode) {
  193. this.itemSet.dom.axis.appendChild(this.dom.axis);
  194. }
  195. };
  196. /**
  197. * Hide this group: remove from the DOM
  198. */
  199. Group.prototype.hide = function() {
  200. var label = this.dom.label;
  201. if (label.parentNode) {
  202. label.parentNode.removeChild(label);
  203. }
  204. var foreground = this.dom.foreground;
  205. if (foreground.parentNode) {
  206. foreground.parentNode.removeChild(foreground);
  207. }
  208. var background = this.dom.background;
  209. if (background.parentNode) {
  210. background.parentNode.removeChild(background);
  211. }
  212. var axis = this.dom.axis;
  213. if (axis.parentNode) {
  214. axis.parentNode.removeChild(axis);
  215. }
  216. };
  217. /**
  218. * Add an item to the group
  219. * @param {Item} item
  220. */
  221. Group.prototype.add = function(item) {
  222. this.items[item.id] = item;
  223. item.setParent(this);
  224. if (item instanceof ItemRange && this.visibleItems.indexOf(item) == -1) {
  225. var range = this.itemSet.body.range; // TODO: not nice accessing the range like this
  226. this._checkIfVisible(item, this.visibleItems, range);
  227. }
  228. };
  229. /**
  230. * Remove an item from the group
  231. * @param {Item} item
  232. */
  233. Group.prototype.remove = function(item) {
  234. delete this.items[item.id];
  235. item.setParent(this.itemSet);
  236. // remove from visible items
  237. var index = this.visibleItems.indexOf(item);
  238. if (index != -1) this.visibleItems.splice(index, 1);
  239. // TODO: also remove from ordered items?
  240. };
  241. /**
  242. * Remove an item from the corresponding DataSet
  243. * @param {Item} item
  244. */
  245. Group.prototype.removeFromDataSet = function(item) {
  246. this.itemSet.removeItem(item.id);
  247. };
  248. /**
  249. * Reorder the items
  250. */
  251. Group.prototype.order = function() {
  252. var array = util.toArray(this.items);
  253. this.orderedItems.byStart = array;
  254. this.orderedItems.byEnd = this._constructByEndArray(array);
  255. stack.orderByStart(this.orderedItems.byStart);
  256. stack.orderByEnd(this.orderedItems.byEnd);
  257. };
  258. /**
  259. * Create an array containing all items being a range (having an end date)
  260. * @param {Item[]} array
  261. * @returns {ItemRange[]}
  262. * @private
  263. */
  264. Group.prototype._constructByEndArray = function(array) {
  265. var endArray = [];
  266. for (var i = 0; i < array.length; i++) {
  267. if (array[i] instanceof ItemRange) {
  268. endArray.push(array[i]);
  269. }
  270. }
  271. return endArray;
  272. };
  273. /**
  274. * Update the visible items
  275. * @param {{byStart: Item[], byEnd: Item[]}} orderedItems All items ordered by start date and by end date
  276. * @param {Item[]} visibleItems The previously visible items.
  277. * @param {{start: number, end: number}} range Visible range
  278. * @return {Item[]} visibleItems The new visible items.
  279. * @private
  280. */
  281. Group.prototype._updateVisibleItems = function(orderedItems, visibleItems, range) {
  282. var initialPosByStart,
  283. newVisibleItems = [],
  284. i;
  285. // first check if the items that were in view previously are still in view.
  286. // this handles the case for the ItemRange that is both before and after the current one.
  287. if (visibleItems.length > 0) {
  288. for (i = 0; i < visibleItems.length; i++) {
  289. this._checkIfVisible(visibleItems[i], newVisibleItems, range);
  290. }
  291. }
  292. // If there were no visible items previously, use binarySearch to find a visible ItemPoint or ItemRange (based on startTime)
  293. if (newVisibleItems.length == 0) {
  294. initialPosByStart = util.binarySearch(orderedItems.byStart, range, 'data','start');
  295. }
  296. else {
  297. initialPosByStart = orderedItems.byStart.indexOf(newVisibleItems[0]);
  298. }
  299. // use visible search to find a visible ItemRange (only based on endTime)
  300. var initialPosByEnd = util.binarySearch(orderedItems.byEnd, range, 'data','end');
  301. // if we found a initial ID to use, trace it up and down until we meet an invisible item.
  302. if (initialPosByStart != -1) {
  303. for (i = initialPosByStart; i >= 0; i--) {
  304. if (this._checkIfInvisible(orderedItems.byStart[i], newVisibleItems, range)) {break;}
  305. }
  306. for (i = initialPosByStart + 1; i < orderedItems.byStart.length; i++) {
  307. if (this._checkIfInvisible(orderedItems.byStart[i], newVisibleItems, range)) {break;}
  308. }
  309. }
  310. // if we found a initial ID to use, trace it up and down until we meet an invisible item.
  311. if (initialPosByEnd != -1) {
  312. for (i = initialPosByEnd; i >= 0; i--) {
  313. if (this._checkIfInvisible(orderedItems.byEnd[i], newVisibleItems, range)) {break;}
  314. }
  315. for (i = initialPosByEnd + 1; i < orderedItems.byEnd.length; i++) {
  316. if (this._checkIfInvisible(orderedItems.byEnd[i], newVisibleItems, range)) {break;}
  317. }
  318. }
  319. return newVisibleItems;
  320. };
  321. /**
  322. * this function checks if an item is invisible. If it is NOT we make it visible
  323. * and add it to the global visible items. If it is, return true.
  324. *
  325. * @param {Item} item
  326. * @param {Item[]} visibleItems
  327. * @param {{start:number, end:number}} range
  328. * @returns {boolean}
  329. * @private
  330. */
  331. Group.prototype._checkIfInvisible = function(item, visibleItems, range) {
  332. if (item.isVisible(range)) {
  333. if (!item.displayed) item.show();
  334. item.repositionX();
  335. if (visibleItems.indexOf(item) == -1) {
  336. visibleItems.push(item);
  337. }
  338. return false;
  339. }
  340. else {
  341. return true;
  342. }
  343. };
  344. /**
  345. * this function is very similar to the _checkIfInvisible() but it does not
  346. * return booleans, hides the item if it should not be seen and always adds to
  347. * the visibleItems.
  348. * this one is for brute forcing and hiding.
  349. *
  350. * @param {Item} item
  351. * @param {Array} visibleItems
  352. * @param {{start:number, end:number}} range
  353. * @private
  354. */
  355. Group.prototype._checkIfVisible = function(item, visibleItems, range) {
  356. if (item.isVisible(range)) {
  357. if (!item.displayed) item.show();
  358. // reposition item horizontally
  359. item.repositionX();
  360. visibleItems.push(item);
  361. }
  362. else {
  363. if (item.displayed) item.hide();
  364. }
  365. };
  366. module.exports = Group;