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.

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