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.

518 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) {
  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. // create and show tooltip
  218. var me = this;
  219. var onItemUpdateTimeTooltip = document.createElement('div');
  220. onItemUpdateTimeTooltip.className = 'vis-onUpdateTime-tooltip';
  221. anchor.appendChild(onItemUpdateTimeTooltip);
  222. this.dom.onItemUpdateTimeTooltip = onItemUpdateTimeTooltip;
  223. } else if (!this.selected && this.dom.onItemUpdateTimeTooltip) {
  224. // remove button
  225. if (this.dom.onItemUpdateTimeTooltip.parentNode) {
  226. this.dom.onItemUpdateTimeTooltip.parentNode.removeChild(this.dom.onItemUpdateTimeTooltip);
  227. }
  228. this.dom.onItemUpdateTimeTooltip = null;
  229. }
  230. // position onChange tooltip
  231. if (this.dom.onItemUpdateTimeTooltip) {
  232. // only show when editing
  233. this.dom.onItemUpdateTimeTooltip.style.visibility = this.parent.itemSet.touchParams.itemIsDragging ? 'visible' : 'hidden';
  234. // position relative to item's content
  235. if (this.options.rtl) {
  236. this.dom.onItemUpdateTimeTooltip.style.right = this.dom.content.style.right;
  237. } else {
  238. this.dom.onItemUpdateTimeTooltip.style.left = this.dom.content.style.left;
  239. }
  240. // position above or below the item depending on the item's position in the window
  241. var tooltipOffset = 50; // TODO: should be tooltip height (depends on template)
  242. var scrollTop = this.parent.itemSet.body.domProps.scrollTop;
  243. // TODO: this.top for orientation:true is actually the items distance from the bottom...
  244. // (should be this.bottom)
  245. var itemDistanceFromTop
  246. if (this.options.orientation.item == 'top') {
  247. itemDistanceFromTop = this.top;
  248. } else {
  249. itemDistanceFromTop = (this.parent.height - this.top - this.height)
  250. }
  251. var isCloseToTop = itemDistanceFromTop + this.parent.top - tooltipOffset < -scrollTop;
  252. if (isCloseToTop) {
  253. this.dom.onItemUpdateTimeTooltip.style.bottom = "";
  254. this.dom.onItemUpdateTimeTooltip.style.top = this.height + 2 + "px";
  255. } else {
  256. this.dom.onItemUpdateTimeTooltip.style.top = "";
  257. this.dom.onItemUpdateTimeTooltip.style.bottom = this.height + 2 + "px";
  258. }
  259. // handle tooltip content
  260. var content;
  261. var templateFunction;
  262. if (this.options.tooltipOnItemUpdateTime && this.options.tooltipOnItemUpdateTime.template) {
  263. templateFunction = this.options.tooltipOnItemUpdateTime.template.bind(this);
  264. content = templateFunction(this.data);
  265. } else {
  266. content = 'start: ' + moment(this.data.start).format('MM/DD/YYYY hh:mm');
  267. if (this.data.end) {
  268. content += '<br> end: ' + moment(this.data.end).format('MM/DD/YYYY hh:mm');
  269. }
  270. }
  271. this.dom.onItemUpdateTimeTooltip.innerHTML = content;
  272. }
  273. };
  274. /**
  275. * Set HTML contents for the item
  276. * @param {Element} element HTML element to fill with the contents
  277. * @private
  278. */
  279. Item.prototype._updateContents = function (element) {
  280. var content;
  281. var templateFunction;
  282. var itemVisibleFrameContent;
  283. var visibleFrameTemplateFunction;
  284. var itemData = this.parent.itemSet.itemsData.get(this.id); // get a clone of the data from the dataset
  285. var frameElement = this.dom.box || this.dom.point;
  286. var itemVisibleFrameContentElement = frameElement.getElementsByClassName('vis-item-visible-frame')[0]
  287. if (this.options.visibleFrameTemplate) {
  288. visibleFrameTemplateFunction = this.options.visibleFrameTemplate.bind(this);
  289. itemVisibleFrameContent = visibleFrameTemplateFunction(itemData, frameElement);
  290. } else {
  291. itemVisibleFrameContent = '';
  292. }
  293. if (itemVisibleFrameContentElement) {
  294. if ((itemVisibleFrameContent instanceof Object) && !(itemVisibleFrameContent instanceof Element)) {
  295. visibleFrameTemplateFunction(itemData, itemVisibleFrameContentElement)
  296. } else {
  297. var changed = this._contentToString(this.itemVisibleFrameContent) !== this._contentToString(itemVisibleFrameContent);
  298. if (changed) {
  299. // only replace the content when changed
  300. if (itemVisibleFrameContent instanceof Element) {
  301. itemVisibleFrameContentElement.innerHTML = '';
  302. itemVisibleFrameContentElement.appendChild(itemVisibleFrameContent);
  303. }
  304. else if (itemVisibleFrameContent != undefined) {
  305. itemVisibleFrameContentElement.innerHTML = itemVisibleFrameContent;
  306. }
  307. else {
  308. if (!(this.data.type == 'background' && this.data.content === undefined)) {
  309. throw new Error('Property "content" missing in item ' + this.id);
  310. }
  311. }
  312. this.itemVisibleFrameContent = itemVisibleFrameContent;
  313. }
  314. }
  315. }
  316. if (this.options.template) {
  317. templateFunction = this.options.template.bind(this);
  318. content = templateFunction(itemData, element, this.data);
  319. } else {
  320. content = this.data.content;
  321. }
  322. if ((content instanceof Object) && !(content instanceof Element)) {
  323. templateFunction(itemData, element)
  324. } else {
  325. var changed = this._contentToString(this.content) !== this._contentToString(content);
  326. if (changed) {
  327. // only replace the content when changed
  328. if (content instanceof Element) {
  329. element.innerHTML = '';
  330. element.appendChild(content);
  331. }
  332. else if (content != undefined) {
  333. element.innerHTML = content;
  334. }
  335. else {
  336. if (!(this.data.type == 'background' && this.data.content === undefined)) {
  337. throw new Error('Property "content" missing in item ' + this.id);
  338. }
  339. }
  340. this.content = content;
  341. }
  342. }
  343. };
  344. /**
  345. * Process dataAttributes timeline option and set as data- attributes on dom.content
  346. * @param {Element} element HTML element to which the attributes will be attached
  347. * @private
  348. */
  349. Item.prototype._updateDataAttributes = function(element) {
  350. if (this.options.dataAttributes && this.options.dataAttributes.length > 0) {
  351. var attributes = [];
  352. if (Array.isArray(this.options.dataAttributes)) {
  353. attributes = this.options.dataAttributes;
  354. }
  355. else if (this.options.dataAttributes == 'all') {
  356. attributes = Object.keys(this.data);
  357. }
  358. else {
  359. return;
  360. }
  361. for (var i = 0; i < attributes.length; i++) {
  362. var name = attributes[i];
  363. var value = this.data[name];
  364. if (value != null) {
  365. element.setAttribute('data-' + name, value);
  366. }
  367. else {
  368. element.removeAttribute('data-' + name);
  369. }
  370. }
  371. }
  372. };
  373. /**
  374. * Update custom styles of the element
  375. * @param element
  376. * @private
  377. */
  378. Item.prototype._updateStyle = function(element) {
  379. // remove old styles
  380. if (this.style) {
  381. util.removeCssText(element, this.style);
  382. this.style = null;
  383. }
  384. // append new styles
  385. if (this.data.style) {
  386. util.addCssText(element, this.data.style);
  387. this.style = this.data.style;
  388. }
  389. };
  390. /**
  391. * Stringify the items contents
  392. * @param {string | Element | undefined} content
  393. * @returns {string | undefined}
  394. * @private
  395. */
  396. Item.prototype._contentToString = function (content) {
  397. if (typeof content === 'string') return content;
  398. if (content && 'outerHTML' in content) return content.outerHTML;
  399. return content;
  400. };
  401. /**
  402. * Update the editability of this item.
  403. */
  404. Item.prototype._updateEditStatus = function() {
  405. if (this.options) {
  406. if(typeof this.options.editable === 'boolean') {
  407. this.editable = {
  408. updateTime: this.options.editable,
  409. updateGroup: this.options.editable,
  410. remove: this.options.editable
  411. };
  412. } else if(typeof this.options.editable === 'object') {
  413. this.editable = {};
  414. util.selectiveExtend(['updateTime', 'updateGroup', 'remove'], this.editable, this.options.editable);
  415. };
  416. }
  417. // Item data overrides, except if options.editable.overrideItems is set.
  418. if (!this.options || !(this.options.editable) || (this.options.editable.overrideItems !== true)) {
  419. if (this.data) {
  420. if (typeof this.data.editable === 'boolean') {
  421. this.editable = {
  422. updateTime: this.data.editable,
  423. updateGroup: this.data.editable,
  424. remove: this.data.editable
  425. }
  426. } else if (typeof this.data.editable === 'object') {
  427. // TODO: in vis.js 5.0, we should change this to not reset options from the timeline configuration.
  428. // Basically just remove the next line...
  429. this.editable = {};
  430. util.selectiveExtend(['updateTime', 'updateGroup', 'remove'], this.editable, this.data.editable);
  431. }
  432. }
  433. }
  434. };
  435. /**
  436. * Return the width of the item left from its start date
  437. * @return {number}
  438. */
  439. Item.prototype.getWidthLeft = function () {
  440. return 0;
  441. };
  442. /**
  443. * Return the width of the item right from the max of its start and end date
  444. * @return {number}
  445. */
  446. Item.prototype.getWidthRight = function () {
  447. return 0;
  448. };
  449. /**
  450. * Return the title of the item
  451. * @return {string | undefined}
  452. */
  453. Item.prototype.getTitle = function () {
  454. return this.data.title;
  455. };
  456. module.exports = Item;