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.

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