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.

433 lines
12 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.dirty = true;
  23. this.top = null;
  24. this.right = null;
  25. this.left = null;
  26. this.width = null;
  27. this.height = null;
  28. this.editable = null;
  29. if (this.data &&
  30. this.data.hasOwnProperty('editable') &&
  31. typeof this.data.editable === 'boolean') {
  32. this.editable = data.editable;
  33. }
  34. }
  35. Item.prototype.stack = true;
  36. /**
  37. * Select current item
  38. */
  39. Item.prototype.select = function() {
  40. this.selected = true;
  41. this.dirty = true;
  42. if (this.displayed) this.redraw();
  43. };
  44. /**
  45. * Unselect current item
  46. */
  47. Item.prototype.unselect = function() {
  48. this.selected = false;
  49. this.dirty = true;
  50. if (this.displayed) this.redraw();
  51. };
  52. /**
  53. * Set data for the item. Existing data will be updated. The id should not
  54. * be changed. When the item is displayed, it will be redrawn immediately.
  55. * @param {Object} data
  56. */
  57. Item.prototype.setData = function(data) {
  58. var groupChanged = data.group != undefined && this.data.group != data.group;
  59. if (groupChanged) {
  60. this.parent.itemSet._moveToGroup(this, data.group);
  61. }
  62. if (data.hasOwnProperty('editable') && typeof data.editable === 'boolean') {
  63. this.editable = data.editable;
  64. }
  65. this.data = data;
  66. this.dirty = true;
  67. if (this.displayed) this.redraw();
  68. };
  69. /**
  70. * Set a parent for the item
  71. * @param {ItemSet | 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 delete button on the top right of the item when the item is selected
  127. * @param {HTMLElement} anchor
  128. * @protected
  129. */
  130. Item.prototype._repaintDeleteButton = function (anchor) {
  131. var editable = (this.options.editable.remove &&
  132. this.options.editable.overrideItems)
  133. || (this.data.editable === true &&
  134. !this.options.editable.overrideItems);
  135. if (this.selected && editable && !this.dom.deleteButton) {
  136. // create and show button
  137. var me = this;
  138. var deleteButton = document.createElement('div');
  139. if (this.options.rtl) {
  140. deleteButton.className = 'vis-delete-rtl';
  141. } else {
  142. deleteButton.className = 'vis-delete';
  143. }
  144. deleteButton.title = 'Delete this item';
  145. // TODO: be able to destroy the delete button
  146. new Hammer(deleteButton).on('tap', function (event) {
  147. event.stopPropagation();
  148. me.parent.removeFromDataSet(me);
  149. });
  150. anchor.appendChild(deleteButton);
  151. this.dom.deleteButton = deleteButton;
  152. }
  153. else if (!this.selected && this.dom.deleteButton) {
  154. // remove button
  155. if (this.dom.deleteButton.parentNode) {
  156. this.dom.deleteButton.parentNode.removeChild(this.dom.deleteButton);
  157. }
  158. this.dom.deleteButton = null;
  159. }
  160. };
  161. /**
  162. * Repaint a onChange tooltip on the top right of the item when the item is selected
  163. * @param {HTMLElement} anchor
  164. * @protected
  165. */
  166. Item.prototype._repaintOnItemUpdateTimeTooltip = function (anchor) {
  167. if (!this.options.tooltipOnItemUpdateTime) return;
  168. var editable = (this.options.editable.updateTime ||
  169. this.data.editable === true) &&
  170. this.data.editable !== false;
  171. if (this.selected && editable && !this.dom.onItemUpdateTimeTooltip) {
  172. // create and show tooltip
  173. var me = this;
  174. var onItemUpdateTimeTooltip = document.createElement('div');
  175. onItemUpdateTimeTooltip.className = 'vis-onUpdateTime-tooltip';
  176. anchor.appendChild(onItemUpdateTimeTooltip);
  177. this.dom.onItemUpdateTimeTooltip = onItemUpdateTimeTooltip;
  178. } else if (!this.selected && this.dom.onItemUpdateTimeTooltip) {
  179. // remove button
  180. if (this.dom.onItemUpdateTimeTooltip.parentNode) {
  181. this.dom.onItemUpdateTimeTooltip.parentNode.removeChild(this.dom.onItemUpdateTimeTooltip);
  182. }
  183. this.dom.onItemUpdateTimeTooltip = null;
  184. }
  185. // position onChange tooltip
  186. if (this.dom.onItemUpdateTimeTooltip) {
  187. // only show when editing
  188. this.dom.onItemUpdateTimeTooltip.style.visibility = this.parent.itemSet.touchParams.itemIsDragging ? 'visible' : 'hidden';
  189. // position relative to item's content
  190. if (this.options.rtl) {
  191. this.dom.onItemUpdateTimeTooltip.style.right = this.dom.content.style.right;
  192. } else {
  193. this.dom.onItemUpdateTimeTooltip.style.left = this.dom.content.style.left;
  194. }
  195. // position above or below the item depending on the item's position in the window
  196. var tooltipOffset = 50; // TODO: should be tooltip height (depends on template)
  197. var scrollTop = this.parent.itemSet.body.domProps.scrollTop;
  198. // TODO: this.top for orientation:true is actually the items distance from the bottom...
  199. // (should be this.bottom)
  200. var itemDistanceFromTop
  201. if (this.options.orientation.item == 'top') {
  202. itemDistanceFromTop = this.top;
  203. } else {
  204. itemDistanceFromTop = (this.parent.height - this.top - this.height)
  205. }
  206. var isCloseToTop = itemDistanceFromTop + this.parent.top - tooltipOffset < -scrollTop;
  207. if (isCloseToTop) {
  208. this.dom.onItemUpdateTimeTooltip.style.bottom = "";
  209. this.dom.onItemUpdateTimeTooltip.style.top = this.height + 2 + "px";
  210. } else {
  211. this.dom.onItemUpdateTimeTooltip.style.top = "";
  212. this.dom.onItemUpdateTimeTooltip.style.bottom = this.height + 2 + "px";
  213. }
  214. // handle tooltip content
  215. var content;
  216. var templateFunction;
  217. if (this.options.tooltipOnItemUpdateTime && this.options.tooltipOnItemUpdateTime.template) {
  218. templateFunction = this.options.tooltipOnItemUpdateTime.template.bind(this);
  219. content = templateFunction(this.data);
  220. } else {
  221. content = 'start: ' + moment(this.data.start).format('MM/DD/YYYY hh:mm');
  222. if (this.data.end) {
  223. content += '<br> end: ' + moment(this.data.end).format('MM/DD/YYYY hh:mm');
  224. }
  225. }
  226. this.dom.onItemUpdateTimeTooltip.innerHTML = content;
  227. }
  228. };
  229. /**
  230. * Set HTML contents for the item
  231. * @param {Element} element HTML element to fill with the contents
  232. * @private
  233. */
  234. Item.prototype._updateContents = function (element) {
  235. var content;
  236. var templateFunction;
  237. if (this.options.template) {
  238. var itemData = this.parent.itemSet.itemsData.get(this.id); // get a clone of the data from the dataset
  239. templateFunction = this.options.template.bind(this);
  240. content = templateFunction(itemData, element);
  241. } else {
  242. content = this.data.content;
  243. }
  244. if ((content instanceof Object) && !(content instanceof Element)) {
  245. templateFunction(itemData, element)
  246. } else {
  247. var changed = this._contentToString(this.content) !== this._contentToString(content);
  248. if (changed) {
  249. // only replace the content when changed
  250. if (content instanceof Element) {
  251. element.innerHTML = '';
  252. element.appendChild(content);
  253. }
  254. else if (content != undefined) {
  255. element.innerHTML = content;
  256. }
  257. else {
  258. if (!(this.data.type == 'background' && this.data.content === undefined)) {
  259. throw new Error('Property "content" missing in item ' + this.id);
  260. }
  261. }
  262. this.content = content;
  263. }
  264. }
  265. };
  266. /**
  267. * Set HTML contents for the item
  268. * @param {Element} element HTML element to fill with the contents
  269. * @private
  270. */
  271. Item.prototype._updateTitle = function (element) {
  272. if (this.data.title != null) {
  273. element.title = this.data.title || '';
  274. }
  275. else {
  276. element.removeAttribute('vis-title');
  277. }
  278. };
  279. /**
  280. * Process dataAttributes timeline option and set as data- attributes on dom.content
  281. * @param {Element} element HTML element to which the attributes will be attached
  282. * @private
  283. */
  284. Item.prototype._updateDataAttributes = function(element) {
  285. if (this.options.dataAttributes && this.options.dataAttributes.length > 0) {
  286. var attributes = [];
  287. if (Array.isArray(this.options.dataAttributes)) {
  288. attributes = this.options.dataAttributes;
  289. }
  290. else if (this.options.dataAttributes == 'all') {
  291. attributes = Object.keys(this.data);
  292. }
  293. else {
  294. return;
  295. }
  296. for (var i = 0; i < attributes.length; i++) {
  297. var name = attributes[i];
  298. var value = this.data[name];
  299. if (value != null) {
  300. element.setAttribute('data-' + name, value);
  301. }
  302. else {
  303. element.removeAttribute('data-' + name);
  304. }
  305. }
  306. }
  307. };
  308. /**
  309. * Update custom styles of the element
  310. * @param element
  311. * @private
  312. */
  313. Item.prototype._updateStyle = function(element) {
  314. // remove old styles
  315. if (this.style) {
  316. util.removeCssText(element, this.style);
  317. this.style = null;
  318. }
  319. // append new styles
  320. if (this.data.style) {
  321. util.addCssText(element, this.data.style);
  322. this.style = this.data.style;
  323. }
  324. };
  325. /**
  326. * Stringify the items contents
  327. * @param {string | Element | undefined} content
  328. * @returns {string | undefined}
  329. * @private
  330. */
  331. Item.prototype._contentToString = function (content) {
  332. if (typeof content === 'string') return content;
  333. if (content && 'outerHTML' in content) return content.outerHTML;
  334. return content;
  335. };
  336. /**
  337. * Return the width of the item left from its start date
  338. * @return {number}
  339. */
  340. Item.prototype.getWidthLeft = function () {
  341. return 0;
  342. };
  343. /**
  344. * Return the width of the item right from the max of its start and end date
  345. * @return {number}
  346. */
  347. Item.prototype.getWidthRight = function () {
  348. return 0;
  349. };
  350. /**
  351. * Repaint a drag area on the center of the item when the item is selected
  352. * @protected
  353. */
  354. Item.prototype._repaintDragCenter = function () {
  355. if (this.selected && this.options.editable.updateTime && !this.dom.dragCenter) {
  356. // create and show drag area
  357. var dragCenter = document.createElement('div');
  358. dragCenter.className = 'vis-drag-center';
  359. dragCenter.dragCenterItem = this;
  360. if (this.dom.box) {
  361. this.dom.box.appendChild(dragCenter);
  362. }
  363. else if (this.dom.point) {
  364. this.dom.point.appendChild(dragCenter);
  365. }
  366. this.dom.dragCenter = dragCenter;
  367. }
  368. else if (!this.selected && this.dom.dragCenter) {
  369. // delete drag area
  370. if (this.dom.dragCenter.parentNode) {
  371. this.dom.dragCenter.parentNode.removeChild(this.dom.dragCenter);
  372. }
  373. this.dom.dragCenter = null;
  374. }
  375. };
  376. module.exports = Item;