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.

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