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.

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