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.

417 lines
12 KiB

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