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.

307 lines
7.4 KiB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
  1. var Hammer = require('../../../module/hammer');
  2. var util = require('../../../util');
  3. /**
  4. * @constructor Item
  5. * @param {Object} data Object containing (optional) parameters type,
  6. * start, end, content, group, className.
  7. * @param {{toScreen: function, toTime: function}} conversion
  8. * Conversion functions from time to screen and vice versa
  9. * @param {Object} options Configuration options
  10. * // TODO: describe available options
  11. */
  12. function Item (data, conversion, options) {
  13. this.id = null;
  14. this.parent = null;
  15. this.data = data;
  16. this.dom = null;
  17. this.conversion = conversion || {};
  18. this.options = options || {};
  19. this.selected = false;
  20. this.displayed = false;
  21. this.dirty = true;
  22. this.top = null;
  23. this.left = null;
  24. this.width = null;
  25. this.height = null;
  26. this.editable = null;
  27. if (this.data &&
  28. this.data.hasOwnProperty('editable') &&
  29. typeof this.data.editable === 'boolean') {
  30. this.editable = data.editable;
  31. }
  32. }
  33. Item.prototype.stack = true;
  34. /**
  35. * Select current item
  36. */
  37. Item.prototype.select = function() {
  38. this.selected = true;
  39. this.dirty = true;
  40. if (this.displayed) this.redraw();
  41. };
  42. /**
  43. * Unselect current item
  44. */
  45. Item.prototype.unselect = function() {
  46. this.selected = false;
  47. this.dirty = true;
  48. if (this.displayed) this.redraw();
  49. };
  50. /**
  51. * Set data for the item. Existing data will be updated. The id should not
  52. * be changed. When the item is displayed, it will be redrawn immediately.
  53. * @param {Object} data
  54. */
  55. Item.prototype.setData = function(data) {
  56. var groupChanged = data.group != undefined && this.data.group != data.group;
  57. if (groupChanged) {
  58. this.parent.itemSet._moveToGroup(this, data.group);
  59. }
  60. if (data.hasOwnProperty('editable') && typeof data.editable === 'boolean') {
  61. this.editable = data.editable;
  62. }
  63. this.data = data;
  64. this.dirty = true;
  65. if (this.displayed) this.redraw();
  66. };
  67. /**
  68. * Set a parent for the item
  69. * @param {ItemSet | Group} parent
  70. */
  71. Item.prototype.setParent = function(parent) {
  72. if (this.displayed) {
  73. this.hide();
  74. this.parent = parent;
  75. if (this.parent) {
  76. this.show();
  77. }
  78. }
  79. else {
  80. this.parent = parent;
  81. }
  82. };
  83. /**
  84. * Check whether this item is visible inside given range
  85. * @returns {{start: Number, end: Number}} range with a timestamp for start and end
  86. * @returns {boolean} True if visible
  87. */
  88. Item.prototype.isVisible = function(range) {
  89. // Should be implemented by Item implementations
  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 delete button on the top right of the item when the item is selected
  126. * @param {HTMLElement} anchor
  127. * @protected
  128. */
  129. Item.prototype._repaintDeleteButton = function (anchor) {
  130. var editable = (this.options.editable.remove ||
  131. this.data.editable === true) &&
  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. deleteButton.className = 'vis-delete';
  138. deleteButton.title = 'Delete this item';
  139. // TODO: be able to destroy the delete button
  140. new Hammer(deleteButton).on('tap', function (event) {
  141. event.stopPropagation();
  142. me.parent.removeFromDataSet(me);
  143. });
  144. anchor.appendChild(deleteButton);
  145. this.dom.deleteButton = deleteButton;
  146. }
  147. else if (!this.selected && this.dom.deleteButton) {
  148. // remove button
  149. if (this.dom.deleteButton.parentNode) {
  150. this.dom.deleteButton.parentNode.removeChild(this.dom.deleteButton);
  151. }
  152. this.dom.deleteButton = null;
  153. }
  154. };
  155. /**
  156. * Set HTML contents for the item
  157. * @param {Element} element HTML element to fill with the contents
  158. * @private
  159. */
  160. Item.prototype._updateContents = function (element) {
  161. var content;
  162. if (this.options.template) {
  163. var itemData = this.parent.itemSet.itemsData.get(this.id); // get a clone of the data from the dataset
  164. content = this.options.template(itemData);
  165. }
  166. else {
  167. content = this.data.content;
  168. }
  169. var changed = this._contentToString(this.content) !== this._contentToString(content);
  170. if (changed) {
  171. // only replace the content when changed
  172. if (content instanceof Element) {
  173. element.innerHTML = '';
  174. element.appendChild(content);
  175. }
  176. else if (content != undefined) {
  177. element.innerHTML = content;
  178. }
  179. else {
  180. if (!(this.data.type == 'background' && this.data.content === undefined)) {
  181. throw new Error('Property "content" missing in item ' + this.id);
  182. }
  183. }
  184. this.content = content;
  185. }
  186. };
  187. /**
  188. * Set HTML contents for the item
  189. * @param {Element} element HTML element to fill with the contents
  190. * @private
  191. */
  192. Item.prototype._updateTitle = function (element) {
  193. if (this.data.title != null) {
  194. element.title = this.data.title || '';
  195. }
  196. else {
  197. element.removeAttribute('vis-title');
  198. }
  199. };
  200. /**
  201. * Process dataAttributes timeline option and set as data- attributes on dom.content
  202. * @param {Element} element HTML element to which the attributes will be attached
  203. * @private
  204. */
  205. Item.prototype._updateDataAttributes = function(element) {
  206. if (this.options.dataAttributes && this.options.dataAttributes.length > 0) {
  207. var attributes = [];
  208. if (Array.isArray(this.options.dataAttributes)) {
  209. attributes = this.options.dataAttributes;
  210. }
  211. else if (this.options.dataAttributes == 'all') {
  212. attributes = Object.keys(this.data);
  213. }
  214. else {
  215. return;
  216. }
  217. for (var i = 0; i < attributes.length; i++) {
  218. var name = attributes[i];
  219. var value = this.data[name];
  220. if (value != null) {
  221. element.setAttribute('data-' + name, value);
  222. }
  223. else {
  224. element.removeAttribute('data-' + name);
  225. }
  226. }
  227. }
  228. };
  229. /**
  230. * Update custom styles of the element
  231. * @param element
  232. * @private
  233. */
  234. Item.prototype._updateStyle = function(element) {
  235. // remove old styles
  236. if (this.style) {
  237. util.removeCssText(element, this.style);
  238. this.style = null;
  239. }
  240. // append new styles
  241. if (this.data.style) {
  242. util.addCssText(element, this.data.style);
  243. this.style = this.data.style;
  244. }
  245. };
  246. /**
  247. * Stringify the items contents
  248. * @param {string | Element | undefined} content
  249. * @returns {string | undefined}
  250. * @private
  251. */
  252. Item.prototype._contentToString = function (content) {
  253. if (typeof content === 'string') return content;
  254. if (content && 'outerHTML' in content) return content.outerHTML;
  255. return content;
  256. };
  257. /**
  258. * Return the width of the item left from its start date
  259. * @return {number}
  260. */
  261. Item.prototype.getWidthLeft = function () {
  262. return 0;
  263. };
  264. /**
  265. * Return the width of the item right from the max of its start and end date
  266. * @return {number}
  267. */
  268. Item.prototype.getWidthRight = function () {
  269. return 0;
  270. };
  271. module.exports = Item;