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.

454 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. var me = this;
  151. if (visibleItems.length) {
  152. var min = visibleItems[0].top;
  153. var max = visibleItems[0].top + visibleItems[0].height;
  154. util.forEach(visibleItems, function (item) {
  155. min = Math.min(min, item.top);
  156. max = Math.max(max, (item.top + item.height));
  157. if (item.data.subgroup !== undefined) {
  158. me.subgroups[item.data.subgroup].height = Math.max(me.subgroups[item.data.subgroup].height,item.height);
  159. if (visibleSubgroups.indexOf(item.data.subgroup) == -1){
  160. visibleSubgroups.push(item.data.subgroup);
  161. me.visibleSubgroups += 1;
  162. }
  163. }
  164. });
  165. if (min > margin.axis) {
  166. // there is an empty gap between the lowest item and the axis
  167. var offset = min - margin.axis;
  168. max -= offset;
  169. util.forEach(visibleItems, function (item) {
  170. item.top -= offset;
  171. });
  172. }
  173. height = max + margin.item.vertical / 2;
  174. }
  175. else {
  176. height = margin.axis + margin.item.vertical;
  177. }
  178. height = Math.max(height, this.props.label.height);
  179. // calculate actual size and position
  180. var foreground = this.dom.foreground;
  181. this.top = foreground.offsetTop;
  182. this.left = foreground.offsetLeft;
  183. this.width = foreground.offsetWidth;
  184. resized = util.updateProperty(this, 'height', height) || resized;
  185. // recalculate size of label
  186. resized = util.updateProperty(this.props.label, 'width', this.dom.inner.clientWidth) || resized;
  187. resized = util.updateProperty(this.props.label, 'height', this.dom.inner.clientHeight) || resized;
  188. // apply new height
  189. this.dom.background.style.height = height + 'px';
  190. this.dom.foreground.style.height = height + 'px';
  191. this.dom.label.style.height = height + 'px';
  192. // update vertical position of items after they are re-stacked and the height of the group is calculated
  193. for (var i = 0, ii = this.visibleItems.length; i < ii; i++) {
  194. var item = this.visibleItems[i];
  195. item.repositionY(margin);
  196. }
  197. return resized;
  198. };
  199. /**
  200. * Show this group: attach to the DOM
  201. */
  202. Group.prototype.show = function() {
  203. if (!this.dom.label.parentNode) {
  204. this.itemSet.dom.labelSet.appendChild(this.dom.label);
  205. }
  206. if (!this.dom.foreground.parentNode) {
  207. this.itemSet.dom.foreground.appendChild(this.dom.foreground);
  208. }
  209. if (!this.dom.background.parentNode) {
  210. this.itemSet.dom.background.appendChild(this.dom.background);
  211. }
  212. if (!this.dom.axis.parentNode) {
  213. this.itemSet.dom.axis.appendChild(this.dom.axis);
  214. }
  215. };
  216. /**
  217. * Hide this group: remove from the DOM
  218. */
  219. Group.prototype.hide = function() {
  220. var label = this.dom.label;
  221. if (label.parentNode) {
  222. label.parentNode.removeChild(label);
  223. }
  224. var foreground = this.dom.foreground;
  225. if (foreground.parentNode) {
  226. foreground.parentNode.removeChild(foreground);
  227. }
  228. var background = this.dom.background;
  229. if (background.parentNode) {
  230. background.parentNode.removeChild(background);
  231. }
  232. var axis = this.dom.axis;
  233. if (axis.parentNode) {
  234. axis.parentNode.removeChild(axis);
  235. }
  236. };
  237. /**
  238. * Add an item to the group
  239. * @param {Item} item
  240. */
  241. Group.prototype.add = function(item) {
  242. this.items[item.id] = item;
  243. item.setParent(this);
  244. // add to
  245. if (item.data.subgroup !== undefined) {
  246. if (this.subgroups[item.data.subgroup] === undefined) {
  247. this.subgroups[item.data.subgroup] = {height:0};
  248. }
  249. }
  250. if (this.visibleItems.indexOf(item) == -1) {
  251. var range = this.itemSet.body.range; // TODO: not nice accessing the range like this
  252. this._checkIfVisible(item, this.visibleItems, range);
  253. }
  254. };
  255. /**
  256. * Remove an item from the group
  257. * @param {Item} item
  258. */
  259. Group.prototype.remove = function(item) {
  260. delete this.items[item.id];
  261. item.setParent(this.itemSet);
  262. // remove from visible items
  263. var index = this.visibleItems.indexOf(item);
  264. if (index != -1) this.visibleItems.splice(index, 1);
  265. // TODO: also remove from ordered items?
  266. };
  267. /**
  268. * Remove an item from the corresponding DataSet
  269. * @param {Item} item
  270. */
  271. Group.prototype.removeFromDataSet = function(item) {
  272. this.itemSet.removeItem(item.id);
  273. };
  274. /**
  275. * Reorder the items
  276. */
  277. Group.prototype.order = function() {
  278. var array = util.toArray(this.items);
  279. this.orderedItems.byStart = array;
  280. this.orderedItems.byEnd = this._constructByEndArray(array);
  281. stack.orderByStart(this.orderedItems.byStart);
  282. stack.orderByEnd(this.orderedItems.byEnd);
  283. };
  284. /**
  285. * Create an array containing all items being a range (having an end date)
  286. * @param {Item[]} array
  287. * @returns {RangeItem[]}
  288. * @private
  289. */
  290. Group.prototype._constructByEndArray = function(array) {
  291. var endArray = [];
  292. for (var i = 0; i < array.length; i++) {
  293. if (array[i] instanceof RangeItem) {
  294. endArray.push(array[i]);
  295. }
  296. }
  297. return endArray;
  298. };
  299. /**
  300. * Update the visible items
  301. * @param {{byStart: Item[], byEnd: Item[]}} orderedItems All items ordered by start date and by end date
  302. * @param {Item[]} visibleItems The previously visible items.
  303. * @param {{start: number, end: number}} range Visible range
  304. * @return {Item[]} visibleItems The new visible items.
  305. * @private
  306. */
  307. Group.prototype._updateVisibleItems = function(orderedItems, visibleItems, range) {
  308. var initialPosByStart,
  309. newVisibleItems = [],
  310. i;
  311. // first check if the items that were in view previously are still in view.
  312. // this handles the case for the RangeItem that is both before and after the current one.
  313. if (visibleItems.length > 0) {
  314. for (i = 0; i < visibleItems.length; i++) {
  315. this._checkIfVisible(visibleItems[i], newVisibleItems, range);
  316. }
  317. }
  318. // If there were no visible items previously, use binarySearch to find a visible PointItem or RangeItem (based on startTime)
  319. if (newVisibleItems.length == 0) {
  320. initialPosByStart = util.binarySearch(orderedItems.byStart, range, 'data','start');
  321. }
  322. else {
  323. initialPosByStart = orderedItems.byStart.indexOf(newVisibleItems[0]);
  324. }
  325. // use visible search to find a visible RangeItem (only based on endTime)
  326. var initialPosByEnd = util.binarySearch(orderedItems.byEnd, range, 'data','end');
  327. // if we found a initial ID to use, trace it up and down until we meet an invisible item.
  328. if (initialPosByStart != -1) {
  329. for (i = initialPosByStart; i >= 0; i--) {
  330. if (this._checkIfInvisible(orderedItems.byStart[i], newVisibleItems, range)) {break;}
  331. }
  332. for (i = initialPosByStart + 1; i < orderedItems.byStart.length; i++) {
  333. if (this._checkIfInvisible(orderedItems.byStart[i], newVisibleItems, range)) {break;}
  334. }
  335. }
  336. // if we found a initial ID to use, trace it up and down until we meet an invisible item.
  337. if (initialPosByEnd != -1) {
  338. for (i = initialPosByEnd; i >= 0; i--) {
  339. if (this._checkIfInvisible(orderedItems.byEnd[i], newVisibleItems, range)) {break;}
  340. }
  341. for (i = initialPosByEnd + 1; i < orderedItems.byEnd.length; i++) {
  342. if (this._checkIfInvisible(orderedItems.byEnd[i], newVisibleItems, range)) {break;}
  343. }
  344. }
  345. return newVisibleItems;
  346. };
  347. /**
  348. * this function checks if an item is invisible. If it is NOT we make it visible
  349. * and add it to the global visible items. If it is, return true.
  350. *
  351. * @param {Item} item
  352. * @param {Item[]} visibleItems
  353. * @param {{start:number, end:number}} range
  354. * @returns {boolean}
  355. * @private
  356. */
  357. Group.prototype._checkIfInvisible = function(item, visibleItems, range) {
  358. if (item.isVisible(range)) {
  359. if (!item.displayed) item.show();
  360. item.repositionX();
  361. if (visibleItems.indexOf(item) == -1) {
  362. visibleItems.push(item);
  363. }
  364. return false;
  365. }
  366. else {
  367. if (item.displayed) item.hide();
  368. return true;
  369. }
  370. };
  371. /**
  372. * this function is very similar to the _checkIfInvisible() but it does not
  373. * return booleans, hides the item if it should not be seen and always adds to
  374. * the visibleItems.
  375. * this one is for brute forcing and hiding.
  376. *
  377. * @param {Item} item
  378. * @param {Array} visibleItems
  379. * @param {{start:number, end:number}} range
  380. * @private
  381. */
  382. Group.prototype._checkIfVisible = function(item, visibleItems, range) {
  383. if (item.isVisible(range)) {
  384. if (!item.displayed) item.show();
  385. // reposition item horizontally
  386. item.repositionX();
  387. visibleItems.push(item);
  388. }
  389. else {
  390. if (item.displayed) item.hide();
  391. }
  392. };
  393. module.exports = Group;