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.

220 lines
5.3 KiB

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. /**
  3. * @constructor Item
  4. * @param {Object} data Object containing (optional) parameters type,
  5. * start, end, content, group, className.
  6. * @param {{toScreen: function, toTime: function}} conversion
  7. * Conversion functions from time to screen and vice versa
  8. * @param {Object} options Configuration options
  9. * // TODO: describe available options
  10. */
  11. function Item (data, conversion, options) {
  12. this.id = null;
  13. this.parent = null;
  14. this.data = data;
  15. this.dom = null;
  16. this.conversion = conversion || {};
  17. this.options = options || {};
  18. this.selected = false;
  19. this.displayed = false;
  20. this.dirty = true;
  21. this.top = null;
  22. this.left = null;
  23. this.width = null;
  24. this.height = null;
  25. }
  26. /**
  27. * Select current item
  28. */
  29. Item.prototype.select = function() {
  30. this.selected = true;
  31. this.dirty = true;
  32. if (this.displayed) this.redraw();
  33. };
  34. /**
  35. * Unselect current item
  36. */
  37. Item.prototype.unselect = function() {
  38. this.selected = false;
  39. this.dirty = true;
  40. if (this.displayed) this.redraw();
  41. };
  42. /**
  43. * Set data for the item. Existing data will be updated. The id should not
  44. * be changed. When the item is displayed, it will be redrawn immediately.
  45. * @param {Object} data
  46. */
  47. Item.prototype.setData = function(data) {
  48. this.data = data;
  49. this.dirty = true;
  50. if (this.displayed) this.redraw();
  51. };
  52. /**
  53. * Set a parent for the item
  54. * @param {ItemSet | Group} parent
  55. */
  56. Item.prototype.setParent = function(parent) {
  57. if (this.displayed) {
  58. this.hide();
  59. this.parent = parent;
  60. if (this.parent) {
  61. this.show();
  62. }
  63. }
  64. else {
  65. this.parent = parent;
  66. }
  67. };
  68. /**
  69. * Check whether this item is visible inside given range
  70. * @returns {{start: Number, end: Number}} range with a timestamp for start and end
  71. * @returns {boolean} True if visible
  72. */
  73. Item.prototype.isVisible = function(range) {
  74. // Should be implemented by Item implementations
  75. return false;
  76. };
  77. /**
  78. * Show the Item in the DOM (when not already visible)
  79. * @return {Boolean} changed
  80. */
  81. Item.prototype.show = function() {
  82. return false;
  83. };
  84. /**
  85. * Hide the Item from the DOM (when visible)
  86. * @return {Boolean} changed
  87. */
  88. Item.prototype.hide = function() {
  89. return false;
  90. };
  91. /**
  92. * Repaint the item
  93. */
  94. Item.prototype.redraw = function() {
  95. // should be implemented by the item
  96. };
  97. /**
  98. * Reposition the Item horizontally
  99. */
  100. Item.prototype.repositionX = function() {
  101. // should be implemented by the item
  102. };
  103. /**
  104. * Reposition the Item vertically
  105. */
  106. Item.prototype.repositionY = function() {
  107. // should be implemented by the item
  108. };
  109. /**
  110. * Repaint a delete button on the top right of the item when the item is selected
  111. * @param {HTMLElement} anchor
  112. * @protected
  113. */
  114. Item.prototype._repaintDeleteButton = function (anchor) {
  115. if (this.selected && this.options.editable.remove && !this.dom.deleteButton) {
  116. // create and show button
  117. var me = this;
  118. var deleteButton = document.createElement('div');
  119. deleteButton.className = 'delete';
  120. deleteButton.title = 'Delete this item';
  121. Hammer(deleteButton, {
  122. preventDefault: true
  123. }).on('tap', function (event) {
  124. me.parent.removeFromDataSet(me);
  125. event.stopPropagation();
  126. });
  127. anchor.appendChild(deleteButton);
  128. this.dom.deleteButton = deleteButton;
  129. }
  130. else if (!this.selected && this.dom.deleteButton) {
  131. // remove button
  132. if (this.dom.deleteButton.parentNode) {
  133. this.dom.deleteButton.parentNode.removeChild(this.dom.deleteButton);
  134. }
  135. this.dom.deleteButton = null;
  136. }
  137. };
  138. /**
  139. * Set HTML contents for the item
  140. * @param {Element} element HTML element to fill with the contents
  141. * @private
  142. */
  143. Item.prototype._updateContents = function (element) {
  144. var content;
  145. if (this.options.template) {
  146. var itemData = this.parent.itemSet.itemsData.get(this.id); // get a clone of the data from the dataset
  147. content = this.options.template(itemData);
  148. }
  149. else {
  150. content = this.data.content;
  151. }
  152. if (content instanceof Element) {
  153. element.innerHTML = '';
  154. element.appendChild(content);
  155. }
  156. else if (content != undefined) {
  157. element.innerHTML = content;
  158. }
  159. else {
  160. if (!(this.data.type == 'background' && this.data.content === undefined)) {
  161. throw new Error('Property "content" missing in item ' + this.id);
  162. }
  163. }
  164. };
  165. /**
  166. * Set HTML contents for the item
  167. * @param {Element} element HTML element to fill with the contents
  168. * @private
  169. */
  170. Item.prototype._updateTitle = function (element) {
  171. if (this.data.title != null) {
  172. element.title = this.data.title || '';
  173. }
  174. else {
  175. element.removeAttribute('title');
  176. }
  177. };
  178. /**
  179. * Process dataAttributes timeline option and set as data- attributes on dom.content
  180. * @param {Element} element HTML element to which the attributes will be attached
  181. * @private
  182. */
  183. Item.prototype._updateDataAttributes = function(element) {
  184. if (this.options.dataAttributes && this.options.dataAttributes.length > 0) {
  185. for (var i = 0; i < this.options.dataAttributes.length; i++) {
  186. var name = this.options.dataAttributes[i];
  187. var value = this.data[name];
  188. if (value != null) {
  189. element.setAttribute('data-' + name, value);
  190. }
  191. else {
  192. element.removeAttribute('data-' + name);
  193. }
  194. }
  195. }
  196. };
  197. module.exports = Item;