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.

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