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.

230 lines
5.5 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. throw new Error('Property "content" missing in item ' + this.data.id);
  161. }
  162. };
  163. /**
  164. * Set HTML contents for the item
  165. * @param {Element} element HTML element to fill with the contents
  166. * @private
  167. */
  168. Item.prototype._updateTitle = function (element) {
  169. if (this.data.title != null) {
  170. element.title = this.data.title || '';
  171. }
  172. else {
  173. element.removeAttribute('title');
  174. }
  175. };
  176. /**
  177. * Process dataAttributes timeline option and set as data- attributes on dom.content
  178. * @param {Element} element HTML element to which the attributes will be attached
  179. * @private
  180. */
  181. Item.prototype._updateDataAttributes = function(element) {
  182. if (this.options.dataAttributes && this.options.dataAttributes.length > 0) {
  183. var attributes = [];
  184. if (Array.isArray(this.options.dataAttributes)) {
  185. attributes = this.options.dataAttributes;
  186. }
  187. else if (this.options.dataAttributes == 'all') {
  188. attributes = Object.keys(this.data);
  189. }
  190. else {
  191. return;
  192. }
  193. for (var i = 0; i < attributes.length; i++) {
  194. var name = attributes[i];
  195. var value = this.data[name];
  196. if (value != null) {
  197. element.setAttribute('data-' + name, value);
  198. }
  199. else {
  200. element.removeAttribute('data-' + name);
  201. }
  202. }
  203. }
  204. };
  205. module.exports = Item;