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.

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