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.

278 lines
6.7 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. }
  27. Item.prototype.stack = true;
  28. /**
  29. * Select current item
  30. */
  31. Item.prototype.select = function() {
  32. this.selected = true;
  33. this.dirty = true;
  34. if (this.displayed) this.redraw();
  35. };
  36. /**
  37. * Unselect current item
  38. */
  39. Item.prototype.unselect = function() {
  40. this.selected = false;
  41. this.dirty = true;
  42. if (this.displayed) this.redraw();
  43. };
  44. /**
  45. * Set data for the item. Existing data will be updated. The id should not
  46. * be changed. When the item is displayed, it will be redrawn immediately.
  47. * @param {Object} data
  48. */
  49. Item.prototype.setData = function(data) {
  50. var groupChanged = data.group != undefined && this.data.group != data.group;
  51. if (groupChanged) {
  52. this.parent.itemSet._moveToGroup(this, data.group);
  53. }
  54. this.data = data;
  55. this.dirty = true;
  56. if (this.displayed) this.redraw();
  57. };
  58. /**
  59. * Set a parent for the item
  60. * @param {ItemSet | Group} parent
  61. */
  62. Item.prototype.setParent = function(parent) {
  63. if (this.displayed) {
  64. this.hide();
  65. this.parent = parent;
  66. if (this.parent) {
  67. this.show();
  68. }
  69. }
  70. else {
  71. this.parent = parent;
  72. }
  73. };
  74. /**
  75. * Check whether this item is visible inside given range
  76. * @returns {{start: Number, end: Number}} range with a timestamp for start and end
  77. * @returns {boolean} True if visible
  78. */
  79. Item.prototype.isVisible = function(range) {
  80. // Should be implemented by Item implementations
  81. return false;
  82. };
  83. /**
  84. * Show the Item in the DOM (when not already visible)
  85. * @return {Boolean} changed
  86. */
  87. Item.prototype.show = function() {
  88. return false;
  89. };
  90. /**
  91. * Hide the Item from the DOM (when visible)
  92. * @return {Boolean} changed
  93. */
  94. Item.prototype.hide = function() {
  95. return false;
  96. };
  97. /**
  98. * Repaint the item
  99. */
  100. Item.prototype.redraw = function() {
  101. // should be implemented by the item
  102. };
  103. /**
  104. * Reposition the Item horizontally
  105. */
  106. Item.prototype.repositionX = function() {
  107. // should be implemented by the item
  108. };
  109. /**
  110. * Reposition the Item vertically
  111. */
  112. Item.prototype.repositionY = function() {
  113. // should be implemented by the item
  114. };
  115. /**
  116. * Repaint a delete button on the top right of the item when the item is selected
  117. * @param {HTMLElement} anchor
  118. * @protected
  119. */
  120. Item.prototype._repaintDeleteButton = function (anchor) {
  121. if (this.selected && this.options.editable.remove && !this.dom.deleteButton) {
  122. // create and show button
  123. var me = this;
  124. var deleteButton = document.createElement('div');
  125. deleteButton.className = 'delete';
  126. deleteButton.title = 'Delete this item';
  127. Hammer(deleteButton, {
  128. preventDefault: true
  129. }).on('tap', function (event) {
  130. event.preventDefault();
  131. event.stopPropagation();
  132. me.parent.removeFromDataSet(me);
  133. });
  134. anchor.appendChild(deleteButton);
  135. this.dom.deleteButton = deleteButton;
  136. }
  137. else if (!this.selected && this.dom.deleteButton) {
  138. // remove button
  139. if (this.dom.deleteButton.parentNode) {
  140. this.dom.deleteButton.parentNode.removeChild(this.dom.deleteButton);
  141. }
  142. this.dom.deleteButton = null;
  143. }
  144. };
  145. /**
  146. * Set HTML contents for the item
  147. * @param {Element} element HTML element to fill with the contents
  148. * @private
  149. */
  150. Item.prototype._updateContents = function (element) {
  151. var content;
  152. if (this.options.template) {
  153. var itemData = this.parent.itemSet.itemsData.get(this.id); // get a clone of the data from the dataset
  154. content = this.options.template(itemData);
  155. }
  156. else {
  157. content = this.data.content;
  158. }
  159. var changed = this._contentToString(this.content) !== this._contentToString(content);
  160. if (changed) {
  161. // only replace the content when changed
  162. if (content instanceof Element) {
  163. element.innerHTML = '';
  164. element.appendChild(content);
  165. }
  166. else if (content != undefined) {
  167. element.innerHTML = content;
  168. }
  169. else {
  170. if (!(this.data.type == 'background' && this.data.content === undefined)) {
  171. throw new Error('Property "content" missing in item ' + this.id);
  172. }
  173. }
  174. this.content = content;
  175. }
  176. };
  177. /**
  178. * Set HTML contents for the item
  179. * @param {Element} element HTML element to fill with the contents
  180. * @private
  181. */
  182. Item.prototype._updateTitle = function (element) {
  183. if (this.data.title != null) {
  184. element.title = this.data.title || '';
  185. }
  186. else {
  187. element.removeAttribute('title');
  188. }
  189. };
  190. /**
  191. * Process dataAttributes timeline option and set as data- attributes on dom.content
  192. * @param {Element} element HTML element to which the attributes will be attached
  193. * @private
  194. */
  195. Item.prototype._updateDataAttributes = function(element) {
  196. if (this.options.dataAttributes && this.options.dataAttributes.length > 0) {
  197. var attributes = [];
  198. if (Array.isArray(this.options.dataAttributes)) {
  199. attributes = this.options.dataAttributes;
  200. }
  201. else if (this.options.dataAttributes == 'all') {
  202. attributes = Object.keys(this.data);
  203. }
  204. else {
  205. return;
  206. }
  207. for (var i = 0; i < attributes.length; i++) {
  208. var name = attributes[i];
  209. var value = this.data[name];
  210. if (value != null) {
  211. element.setAttribute('data-' + name, value);
  212. }
  213. else {
  214. element.removeAttribute('data-' + name);
  215. }
  216. }
  217. }
  218. };
  219. /**
  220. * Update custom styles of the element
  221. * @param element
  222. * @private
  223. */
  224. Item.prototype._updateStyle = function(element) {
  225. // remove old styles
  226. if (this.style) {
  227. util.removeCssText(element, this.style);
  228. this.style = null;
  229. }
  230. // append new styles
  231. if (this.data.style) {
  232. util.addCssText(element, this.data.style);
  233. this.style = this.data.style;
  234. }
  235. };
  236. /**
  237. * Stringify the items contents
  238. * @param {string | Element | undefined} content
  239. * @returns {string | undefined}
  240. * @private
  241. */
  242. Item.prototype._contentToString = function (content) {
  243. if (typeof content === 'string') return content;
  244. if (content && 'outerHTML' in content) return content.outerHTML;
  245. return content;
  246. };
  247. module.exports = Item;