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.

313 lines
7.5 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. 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. * Set HTML contents for the item
  162. * @param {Element} element HTML element to fill with the contents
  163. * @private
  164. */
  165. Item.prototype._updateContents = function (element) {
  166. var content;
  167. if (this.options.template) {
  168. var itemData = this.parent.itemSet.itemsData.get(this.id); // get a clone of the data from the dataset
  169. content = this.options.template(itemData, element);
  170. }
  171. else {
  172. content = this.data.content;
  173. }
  174. var changed = this._contentToString(this.content) !== this._contentToString(content);
  175. if (changed) {
  176. // only replace the content when changed
  177. if (content instanceof Element) {
  178. element.innerHTML = '';
  179. element.appendChild(content);
  180. }
  181. else if (content != undefined) {
  182. element.innerHTML = content;
  183. }
  184. else {
  185. if (!(this.data.type == 'background' && this.data.content === undefined)) {
  186. throw new Error('Property "content" missing in item ' + this.id);
  187. }
  188. }
  189. this.content = content;
  190. }
  191. };
  192. /**
  193. * Set HTML contents for the item
  194. * @param {Element} element HTML element to fill with the contents
  195. * @private
  196. */
  197. Item.prototype._updateTitle = function (element) {
  198. if (this.data.title != null) {
  199. element.title = this.data.title || '';
  200. }
  201. else {
  202. element.removeAttribute('vis-title');
  203. }
  204. };
  205. /**
  206. * Process dataAttributes timeline option and set as data- attributes on dom.content
  207. * @param {Element} element HTML element to which the attributes will be attached
  208. * @private
  209. */
  210. Item.prototype._updateDataAttributes = function(element) {
  211. if (this.options.dataAttributes && this.options.dataAttributes.length > 0) {
  212. var attributes = [];
  213. if (Array.isArray(this.options.dataAttributes)) {
  214. attributes = this.options.dataAttributes;
  215. }
  216. else if (this.options.dataAttributes == 'all') {
  217. attributes = Object.keys(this.data);
  218. }
  219. else {
  220. return;
  221. }
  222. for (var i = 0; i < attributes.length; i++) {
  223. var name = attributes[i];
  224. var value = this.data[name];
  225. if (value != null) {
  226. element.setAttribute('data-' + name, value);
  227. }
  228. else {
  229. element.removeAttribute('data-' + name);
  230. }
  231. }
  232. }
  233. };
  234. /**
  235. * Update custom styles of the element
  236. * @param element
  237. * @private
  238. */
  239. Item.prototype._updateStyle = function(element) {
  240. // remove old styles
  241. if (this.style) {
  242. util.removeCssText(element, this.style);
  243. this.style = null;
  244. }
  245. // append new styles
  246. if (this.data.style) {
  247. util.addCssText(element, this.data.style);
  248. this.style = this.data.style;
  249. }
  250. };
  251. /**
  252. * Stringify the items contents
  253. * @param {string | Element | undefined} content
  254. * @returns {string | undefined}
  255. * @private
  256. */
  257. Item.prototype._contentToString = function (content) {
  258. if (typeof content === 'string') return content;
  259. if (content && 'outerHTML' in content) return content.outerHTML;
  260. return content;
  261. };
  262. /**
  263. * Return the width of the item left from its start date
  264. * @return {number}
  265. */
  266. Item.prototype.getWidthLeft = function () {
  267. return 0;
  268. };
  269. /**
  270. * Return the width of the item right from the max of its start and end date
  271. * @return {number}
  272. */
  273. Item.prototype.getWidthRight = function () {
  274. return 0;
  275. };
  276. module.exports = Item;