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.

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