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.

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