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.

466 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. var index = 0;
  248. if (item.data.subgroup !== undefined) {
  249. if (this.subgroups[item.data.subgroup] === undefined) {
  250. this.subgroups[item.data.subgroup] = {height:0, visible: false, index:index};
  251. index++;
  252. }
  253. }
  254. if (this.visibleItems.indexOf(item) == -1) {
  255. var range = this.itemSet.body.range; // TODO: not nice accessing the range like this
  256. this._checkIfVisible(item, this.visibleItems, range);
  257. }
  258. };
  259. Group.prototype.resetSubgroups = function() {
  260. for (var subgroup in this.subgroups) {
  261. if (this.subgroups.hasOwnProperty(subgroup)) {
  262. this.subgroups[subgroup].visible = false;
  263. }
  264. }
  265. }
  266. /**
  267. * Remove an item from the group
  268. * @param {Item} item
  269. */
  270. Group.prototype.remove = function(item) {
  271. delete this.items[item.id];
  272. item.setParent(this.itemSet);
  273. // remove from visible items
  274. var index = this.visibleItems.indexOf(item);
  275. if (index != -1) this.visibleItems.splice(index, 1);
  276. // TODO: also remove from ordered items?
  277. };
  278. /**
  279. * Remove an item from the corresponding DataSet
  280. * @param {Item} item
  281. */
  282. Group.prototype.removeFromDataSet = function(item) {
  283. this.itemSet.removeItem(item.id);
  284. };
  285. /**
  286. * Reorder the items
  287. */
  288. Group.prototype.order = function() {
  289. var array = util.toArray(this.items);
  290. this.orderedItems.byStart = array;
  291. this.orderedItems.byEnd = this._constructByEndArray(array);
  292. stack.orderByStart(this.orderedItems.byStart);
  293. stack.orderByEnd(this.orderedItems.byEnd);
  294. };
  295. /**
  296. * Create an array containing all items being a range (having an end date)
  297. * @param {Item[]} array
  298. * @returns {RangeItem[]}
  299. * @private
  300. */
  301. Group.prototype._constructByEndArray = function(array) {
  302. var endArray = [];
  303. for (var i = 0; i < array.length; i++) {
  304. if (array[i] instanceof RangeItem) {
  305. endArray.push(array[i]);
  306. }
  307. }
  308. return endArray;
  309. };
  310. /**
  311. * Update the visible items
  312. * @param {{byStart: Item[], byEnd: Item[]}} orderedItems All items ordered by start date and by end date
  313. * @param {Item[]} visibleItems The previously visible items.
  314. * @param {{start: number, end: number}} range Visible range
  315. * @return {Item[]} visibleItems The new visible items.
  316. * @private
  317. */
  318. Group.prototype._updateVisibleItems = function(orderedItems, visibleItems, range) {
  319. var initialPosByStart,
  320. newVisibleItems = [],
  321. i;
  322. // first check if the items that were in view previously are still in view.
  323. // this handles the case for the RangeItem that is both before and after the current one.
  324. if (visibleItems.length > 0) {
  325. for (i = 0; i < visibleItems.length; i++) {
  326. this._checkIfVisible(visibleItems[i], newVisibleItems, range);
  327. }
  328. }
  329. // If there were no visible items previously, use binarySearch to find a visible PointItem or RangeItem (based on startTime)
  330. if (newVisibleItems.length == 0) {
  331. initialPosByStart = util.binarySearch(orderedItems.byStart, range, 'data','start');
  332. }
  333. else {
  334. initialPosByStart = orderedItems.byStart.indexOf(newVisibleItems[0]);
  335. }
  336. // use visible search to find a visible RangeItem (only based on endTime)
  337. var initialPosByEnd = util.binarySearch(orderedItems.byEnd, range, 'data','end');
  338. // if we found a initial ID to use, trace it up and down until we meet an invisible item.
  339. if (initialPosByStart != -1) {
  340. for (i = initialPosByStart; i >= 0; i--) {
  341. if (this._checkIfInvisible(orderedItems.byStart[i], newVisibleItems, range)) {break;}
  342. }
  343. for (i = initialPosByStart + 1; i < orderedItems.byStart.length; i++) {
  344. if (this._checkIfInvisible(orderedItems.byStart[i], newVisibleItems, range)) {break;}
  345. }
  346. }
  347. // if we found a initial ID to use, trace it up and down until we meet an invisible item.
  348. if (initialPosByEnd != -1) {
  349. for (i = initialPosByEnd; i >= 0; i--) {
  350. if (this._checkIfInvisible(orderedItems.byEnd[i], newVisibleItems, range)) {break;}
  351. }
  352. for (i = initialPosByEnd + 1; i < orderedItems.byEnd.length; i++) {
  353. if (this._checkIfInvisible(orderedItems.byEnd[i], newVisibleItems, range)) {break;}
  354. }
  355. }
  356. return newVisibleItems;
  357. };
  358. /**
  359. * this function checks if an item is invisible. If it is NOT we make it visible
  360. * and add it to the global visible items. If it is, return true.
  361. *
  362. * @param {Item} item
  363. * @param {Item[]} visibleItems
  364. * @param {{start:number, end:number}} range
  365. * @returns {boolean}
  366. * @private
  367. */
  368. Group.prototype._checkIfInvisible = function(item, visibleItems, range) {
  369. if (item.isVisible(range)) {
  370. if (!item.displayed) item.show();
  371. item.repositionX();
  372. if (visibleItems.indexOf(item) == -1) {
  373. visibleItems.push(item);
  374. }
  375. return false;
  376. }
  377. else {
  378. if (item.displayed) item.hide();
  379. return true;
  380. }
  381. };
  382. /**
  383. * this function is very similar to the _checkIfInvisible() but it does not
  384. * return booleans, hides the item if it should not be seen and always adds to
  385. * the visibleItems.
  386. * this one is for brute forcing and hiding.
  387. *
  388. * @param {Item} item
  389. * @param {Array} visibleItems
  390. * @param {{start:number, end:number}} range
  391. * @private
  392. */
  393. Group.prototype._checkIfVisible = function(item, visibleItems, range) {
  394. if (item.isVisible(range)) {
  395. if (!item.displayed) item.show();
  396. // reposition item horizontally
  397. item.repositionX();
  398. visibleItems.push(item);
  399. }
  400. else {
  401. if (item.displayed) item.hide();
  402. }
  403. };
  404. module.exports = Group;