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.

516 lines
15 KiB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
8 years ago
  1. var Hammer = require('../../../module/hammer');
  2. var util = require('../../../util');
  3. var moment = require('../../../module/moment');
  4. /**
  5. * @constructor Item
  6. * @param {Object} data Object containing (optional) parameters type,
  7. * start, end, content, group, className.
  8. * @param {{toScreen: function, toTime: function}} conversion
  9. * Conversion functions from time to screen and vice versa
  10. * @param {Object} options Configuration options
  11. * // TODO: describe available options
  12. */
  13. function Item (data, conversion, options) {
  14. this.id = null;
  15. this.parent = null;
  16. this.data = data;
  17. this.dom = null;
  18. this.conversion = conversion || {};
  19. this.options = options || {};
  20. this.selected = false;
  21. this.displayed = false;
  22. this.groupShowing = true;
  23. this.dirty = true;
  24. this.top = null;
  25. this.right = null;
  26. this.left = null;
  27. this.width = null;
  28. this.height = null;
  29. this.editable = null;
  30. this._updateEditStatus();
  31. }
  32. Item.prototype.stack = true;
  33. /**
  34. * Select current item
  35. */
  36. Item.prototype.select = function() {
  37. this.selected = true;
  38. this.dirty = true;
  39. if (this.displayed) this.redraw();
  40. };
  41. /**
  42. * Unselect current item
  43. */
  44. Item.prototype.unselect = function() {
  45. this.selected = false;
  46. this.dirty = true;
  47. if (this.displayed) this.redraw();
  48. };
  49. /**
  50. * Set data for the item. Existing data will be updated. The id should not
  51. * be changed. When the item is displayed, it will be redrawn immediately.
  52. * @param {Object} data
  53. */
  54. Item.prototype.setData = function(data) {
  55. var groupChanged = data.group != undefined && this.data.group != data.group;
  56. if (groupChanged && this.parent != null) {
  57. this.parent.itemSet._moveToGroup(this, data.group);
  58. }
  59. this.parent.stackDirty = true;
  60. var subGroupChanged = data.subgroup != undefined && this.data.subgroup != data.subgroup;
  61. if (subGroupChanged && this.parent != null) {
  62. this.parent.changeSubgroup(this, this.data.subgroup, data.subgroup);
  63. }
  64. this.data = data;
  65. this._updateEditStatus();
  66. this.dirty = true;
  67. if (this.displayed) this.redraw();
  68. };
  69. /**
  70. * Set a parent for the item
  71. * @param {Group} parent
  72. */
  73. Item.prototype.setParent = function(parent) {
  74. if (this.displayed) {
  75. this.hide();
  76. this.parent = parent;
  77. if (this.parent) {
  78. this.show();
  79. }
  80. }
  81. else {
  82. this.parent = parent;
  83. }
  84. };
  85. /**
  86. * Check whether this item is visible inside given range
  87. * @returns {{start: Number, end: Number}} range with a timestamp for start and end
  88. * @returns {boolean} True if visible
  89. */
  90. Item.prototype.isVisible = function(range) { // eslint-disable-line no-unused-vars
  91. return false;
  92. };
  93. /**
  94. * Show the Item in the DOM (when not already visible)
  95. * @return {Boolean} changed
  96. */
  97. Item.prototype.show = function() {
  98. return false;
  99. };
  100. /**
  101. * Hide the Item from the DOM (when visible)
  102. * @return {Boolean} changed
  103. */
  104. Item.prototype.hide = function() {
  105. return false;
  106. };
  107. /**
  108. * Repaint the item
  109. */
  110. Item.prototype.redraw = function() {
  111. // should be implemented by the item
  112. };
  113. /**
  114. * Reposition the Item horizontally
  115. */
  116. Item.prototype.repositionX = function() {
  117. // should be implemented by the item
  118. };
  119. /**
  120. * Reposition the Item vertically
  121. */
  122. Item.prototype.repositionY = function() {
  123. // should be implemented by the item
  124. };
  125. /**
  126. * Repaint a drag area on the center of the item when the item is selected
  127. * @protected
  128. */
  129. Item.prototype._repaintDragCenter = function () {
  130. if (this.selected && this.options.editable.updateTime && !this.dom.dragCenter) {
  131. var me = this;
  132. // create and show drag area
  133. var dragCenter = document.createElement('div');
  134. dragCenter.className = 'vis-drag-center';
  135. dragCenter.dragCenterItem = this;
  136. var hammer = new Hammer(dragCenter);
  137. hammer.on('tap', function (event) {
  138. me.parent.itemSet.body.emitter.emit('click', {
  139. event: event,
  140. item: me.id
  141. });
  142. });
  143. hammer.on('doubletap', function (event) {
  144. event.stopPropagation();
  145. me.parent.itemSet._onUpdateItem(me);
  146. me.parent.itemSet.body.emitter.emit('doubleClick', {
  147. event: event,
  148. item: me.id
  149. });
  150. });
  151. if (this.dom.box) {
  152. if (this.dom.dragLeft) {
  153. this.dom.box.insertBefore(dragCenter, this.dom.dragLeft);
  154. }
  155. else {
  156. this.dom.box.appendChild(dragCenter);
  157. }
  158. }
  159. else if (this.dom.point) {
  160. this.dom.point.appendChild(dragCenter);
  161. }
  162. this.dom.dragCenter = dragCenter;
  163. }
  164. else if (!this.selected && this.dom.dragCenter) {
  165. // delete drag area
  166. if (this.dom.dragCenter.parentNode) {
  167. this.dom.dragCenter.parentNode.removeChild(this.dom.dragCenter);
  168. }
  169. this.dom.dragCenter = null;
  170. }
  171. };
  172. /**
  173. * Repaint a delete button on the top right of the item when the item is selected
  174. * @param {HTMLElement} anchor
  175. * @protected
  176. */
  177. Item.prototype._repaintDeleteButton = function (anchor) {
  178. var editable = ((this.options.editable.overrideItems || this.editable == null) && this.options.editable.remove) ||
  179. (!this.options.editable.overrideItems && this.editable != null && this.editable.remove);
  180. if (this.selected && editable && !this.dom.deleteButton) {
  181. // create and show button
  182. var me = this;
  183. var deleteButton = document.createElement('div');
  184. if (this.options.rtl) {
  185. deleteButton.className = 'vis-delete-rtl';
  186. } else {
  187. deleteButton.className = 'vis-delete';
  188. }
  189. deleteButton.title = 'Delete this item';
  190. // TODO: be able to destroy the delete button
  191. new Hammer(deleteButton).on('tap', function (event) {
  192. event.stopPropagation();
  193. me.parent.removeFromDataSet(me);
  194. });
  195. anchor.appendChild(deleteButton);
  196. this.dom.deleteButton = deleteButton;
  197. }
  198. else if (!this.selected && this.dom.deleteButton) {
  199. // remove button
  200. if (this.dom.deleteButton.parentNode) {
  201. this.dom.deleteButton.parentNode.removeChild(this.dom.deleteButton);
  202. }
  203. this.dom.deleteButton = null;
  204. }
  205. };
  206. /**
  207. * Repaint a onChange tooltip on the top right of the item when the item is selected
  208. * @param {HTMLElement} anchor
  209. * @protected
  210. */
  211. Item.prototype._repaintOnItemUpdateTimeTooltip = function (anchor) {
  212. if (!this.options.tooltipOnItemUpdateTime) return;
  213. var editable = (this.options.editable.updateTime ||
  214. this.data.editable === true) &&
  215. this.data.editable !== false;
  216. if (this.selected && editable && !this.dom.onItemUpdateTimeTooltip) {
  217. var onItemUpdateTimeTooltip = document.createElement('div');
  218. onItemUpdateTimeTooltip.className = 'vis-onUpdateTime-tooltip';
  219. anchor.appendChild(onItemUpdateTimeTooltip);
  220. this.dom.onItemUpdateTimeTooltip = onItemUpdateTimeTooltip;
  221. } else if (!this.selected && this.dom.onItemUpdateTimeTooltip) {
  222. // remove button
  223. if (this.dom.onItemUpdateTimeTooltip.parentNode) {
  224. this.dom.onItemUpdateTimeTooltip.parentNode.removeChild(this.dom.onItemUpdateTimeTooltip);
  225. }
  226. this.dom.onItemUpdateTimeTooltip = null;
  227. }
  228. // position onChange tooltip
  229. if (this.dom.onItemUpdateTimeTooltip) {
  230. // only show when editing
  231. this.dom.onItemUpdateTimeTooltip.style.visibility = this.parent.itemSet.touchParams.itemIsDragging ? 'visible' : 'hidden';
  232. // position relative to item's content
  233. if (this.options.rtl) {
  234. this.dom.onItemUpdateTimeTooltip.style.right = this.dom.content.style.right;
  235. } else {
  236. this.dom.onItemUpdateTimeTooltip.style.left = this.dom.content.style.left;
  237. }
  238. // position above or below the item depending on the item's position in the window
  239. var tooltipOffset = 50; // TODO: should be tooltip height (depends on template)
  240. var scrollTop = this.parent.itemSet.body.domProps.scrollTop;
  241. // TODO: this.top for orientation:true is actually the items distance from the bottom...
  242. // (should be this.bottom)
  243. var itemDistanceFromTop
  244. if (this.options.orientation.item == 'top') {
  245. itemDistanceFromTop = this.top;
  246. } else {
  247. itemDistanceFromTop = (this.parent.height - this.top - this.height)
  248. }
  249. var isCloseToTop = itemDistanceFromTop + this.parent.top - tooltipOffset < -scrollTop;
  250. if (isCloseToTop) {
  251. this.dom.onItemUpdateTimeTooltip.style.bottom = "";
  252. this.dom.onItemUpdateTimeTooltip.style.top = this.height + 2 + "px";
  253. } else {
  254. this.dom.onItemUpdateTimeTooltip.style.top = "";
  255. this.dom.onItemUpdateTimeTooltip.style.bottom = this.height + 2 + "px";
  256. }
  257. // handle tooltip content
  258. var content;
  259. var templateFunction;
  260. if (this.options.tooltipOnItemUpdateTime && this.options.tooltipOnItemUpdateTime.template) {
  261. templateFunction = this.options.tooltipOnItemUpdateTime.template.bind(this);
  262. content = templateFunction(this.data);
  263. } else {
  264. content = 'start: ' + moment(this.data.start).format('MM/DD/YYYY hh:mm');
  265. if (this.data.end) {
  266. content += '<br> end: ' + moment(this.data.end).format('MM/DD/YYYY hh:mm');
  267. }
  268. }
  269. this.dom.onItemUpdateTimeTooltip.innerHTML = content;
  270. }
  271. };
  272. /**
  273. * Set HTML contents for the item
  274. * @param {Element} element HTML element to fill with the contents
  275. * @private
  276. */
  277. Item.prototype._updateContents = function (element) {
  278. var content;
  279. var changed;
  280. var templateFunction;
  281. var itemVisibleFrameContent;
  282. var visibleFrameTemplateFunction;
  283. var itemData = this.parent.itemSet.itemsData.get(this.id); // get a clone of the data from the dataset
  284. var frameElement = this.dom.box || this.dom.point;
  285. var itemVisibleFrameContentElement = frameElement.getElementsByClassName('vis-item-visible-frame')[0]
  286. if (this.options.visibleFrameTemplate) {
  287. visibleFrameTemplateFunction = this.options.visibleFrameTemplate.bind(this);
  288. itemVisibleFrameContent = visibleFrameTemplateFunction(itemData, frameElement);
  289. } else {
  290. itemVisibleFrameContent = '';
  291. }
  292. if (itemVisibleFrameContentElement) {
  293. if ((itemVisibleFrameContent instanceof Object) && !(itemVisibleFrameContent instanceof Element)) {
  294. visibleFrameTemplateFunction(itemData, itemVisibleFrameContentElement)
  295. } else {
  296. changed = this._contentToString(this.itemVisibleFrameContent) !== this._contentToString(itemVisibleFrameContent);
  297. if (changed) {
  298. // only replace the content when changed
  299. if (itemVisibleFrameContent instanceof Element) {
  300. itemVisibleFrameContentElement.innerHTML = '';
  301. itemVisibleFrameContentElement.appendChild(itemVisibleFrameContent);
  302. }
  303. else if (itemVisibleFrameContent != undefined) {
  304. itemVisibleFrameContentElement.innerHTML = itemVisibleFrameContent;
  305. }
  306. else {
  307. if (!(this.data.type == 'background' && this.data.content === undefined)) {
  308. throw new Error('Property "content" missing in item ' + this.id);
  309. }
  310. }
  311. this.itemVisibleFrameContent = itemVisibleFrameContent;
  312. }
  313. }
  314. }
  315. if (this.options.template) {
  316. templateFunction = this.options.template.bind(this);
  317. content = templateFunction(itemData, element, this.data);
  318. } else {
  319. content = this.data.content;
  320. }
  321. if ((content instanceof Object) && !(content instanceof Element)) {
  322. templateFunction(itemData, element)
  323. } else {
  324. changed = this._contentToString(this.content) !== this._contentToString(content);
  325. if (changed) {
  326. // only replace the content when changed
  327. if (content instanceof Element) {
  328. element.innerHTML = '';
  329. element.appendChild(content);
  330. }
  331. else if (content != undefined) {
  332. element.innerHTML = content;
  333. }
  334. else {
  335. if (!(this.data.type == 'background' && this.data.content === undefined)) {
  336. throw new Error('Property "content" missing in item ' + this.id);
  337. }
  338. }
  339. this.content = content;
  340. }
  341. }
  342. };
  343. /**
  344. * Process dataAttributes timeline option and set as data- attributes on dom.content
  345. * @param {Element} element HTML element to which the attributes will be attached
  346. * @private
  347. */
  348. Item.prototype._updateDataAttributes = function(element) {
  349. if (this.options.dataAttributes && this.options.dataAttributes.length > 0) {
  350. var attributes = [];
  351. if (Array.isArray(this.options.dataAttributes)) {
  352. attributes = this.options.dataAttributes;
  353. }
  354. else if (this.options.dataAttributes == 'all') {
  355. attributes = Object.keys(this.data);
  356. }
  357. else {
  358. return;
  359. }
  360. for (var i = 0; i < attributes.length; i++) {
  361. var name = attributes[i];
  362. var value = this.data[name];
  363. if (value != null) {
  364. element.setAttribute('data-' + name, value);
  365. }
  366. else {
  367. element.removeAttribute('data-' + name);
  368. }
  369. }
  370. }
  371. };
  372. /**
  373. * Update custom styles of the element
  374. * @param element
  375. * @private
  376. */
  377. Item.prototype._updateStyle = function(element) {
  378. // remove old styles
  379. if (this.style) {
  380. util.removeCssText(element, this.style);
  381. this.style = null;
  382. }
  383. // append new styles
  384. if (this.data.style) {
  385. util.addCssText(element, this.data.style);
  386. this.style = this.data.style;
  387. }
  388. };
  389. /**
  390. * Stringify the items contents
  391. * @param {string | Element | undefined} content
  392. * @returns {string | undefined}
  393. * @private
  394. */
  395. Item.prototype._contentToString = function (content) {
  396. if (typeof content === 'string') return content;
  397. if (content && 'outerHTML' in content) return content.outerHTML;
  398. return content;
  399. };
  400. /**
  401. * Update the editability of this item.
  402. */
  403. Item.prototype._updateEditStatus = function() {
  404. if (this.options) {
  405. if(typeof this.options.editable === 'boolean') {
  406. this.editable = {
  407. updateTime: this.options.editable,
  408. updateGroup: this.options.editable,
  409. remove: this.options.editable
  410. };
  411. } else if(typeof this.options.editable === 'object') {
  412. this.editable = {};
  413. util.selectiveExtend(['updateTime', 'updateGroup', 'remove'], this.editable, this.options.editable);
  414. }
  415. }
  416. // Item data overrides, except if options.editable.overrideItems is set.
  417. if (!this.options || !(this.options.editable) || (this.options.editable.overrideItems !== true)) {
  418. if (this.data) {
  419. if (typeof this.data.editable === 'boolean') {
  420. this.editable = {
  421. updateTime: this.data.editable,
  422. updateGroup: this.data.editable,
  423. remove: this.data.editable
  424. }
  425. } else if (typeof this.data.editable === 'object') {
  426. // TODO: in vis.js 5.0, we should change this to not reset options from the timeline configuration.
  427. // Basically just remove the next line...
  428. this.editable = {};
  429. util.selectiveExtend(['updateTime', 'updateGroup', 'remove'], this.editable, this.data.editable);
  430. }
  431. }
  432. }
  433. };
  434. /**
  435. * Return the width of the item left from its start date
  436. * @return {number}
  437. */
  438. Item.prototype.getWidthLeft = function () {
  439. return 0;
  440. };
  441. /**
  442. * Return the width of the item right from the max of its start and end date
  443. * @return {number}
  444. */
  445. Item.prototype.getWidthRight = function () {
  446. return 0;
  447. };
  448. /**
  449. * Return the title of the item
  450. * @return {string | undefined}
  451. */
  452. Item.prototype.getTitle = function () {
  453. return this.data.title;
  454. };
  455. module.exports = Item;