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.

348 lines
8.5 KiB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
8 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. var templateFunction;
  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. templateFunction = this.options.template.bind(this);
  170. content = templateFunction(itemData, element);
  171. } else {
  172. content = this.data.content;
  173. }
  174. if ((content instanceof Object) && !(content instanceof Element)) {
  175. templateFunction(itemData, element)
  176. } else {
  177. var changed = this._contentToString(this.content) !== this._contentToString(content);
  178. if (changed) {
  179. // only replace the content when changed
  180. if (content instanceof Element) {
  181. element.innerHTML = '';
  182. element.appendChild(content);
  183. }
  184. else if (content != undefined) {
  185. element.innerHTML = content;
  186. }
  187. else {
  188. if (!(this.data.type == 'background' && this.data.content === undefined)) {
  189. throw new Error('Property "content" missing in item ' + this.id);
  190. }
  191. }
  192. this.content = content;
  193. }
  194. }
  195. };
  196. /**
  197. * Set HTML contents for the item
  198. * @param {Element} element HTML element to fill with the contents
  199. * @private
  200. */
  201. Item.prototype._updateTitle = function (element) {
  202. if (this.data.title != null) {
  203. element.title = this.data.title || '';
  204. }
  205. else {
  206. element.removeAttribute('vis-title');
  207. }
  208. };
  209. /**
  210. * Process dataAttributes timeline option and set as data- attributes on dom.content
  211. * @param {Element} element HTML element to which the attributes will be attached
  212. * @private
  213. */
  214. Item.prototype._updateDataAttributes = function(element) {
  215. if (this.options.dataAttributes && this.options.dataAttributes.length > 0) {
  216. var attributes = [];
  217. if (Array.isArray(this.options.dataAttributes)) {
  218. attributes = this.options.dataAttributes;
  219. }
  220. else if (this.options.dataAttributes == 'all') {
  221. attributes = Object.keys(this.data);
  222. }
  223. else {
  224. return;
  225. }
  226. for (var i = 0; i < attributes.length; i++) {
  227. var name = attributes[i];
  228. var value = this.data[name];
  229. if (value != null) {
  230. element.setAttribute('data-' + name, value);
  231. }
  232. else {
  233. element.removeAttribute('data-' + name);
  234. }
  235. }
  236. }
  237. };
  238. /**
  239. * Update custom styles of the element
  240. * @param element
  241. * @private
  242. */
  243. Item.prototype._updateStyle = function(element) {
  244. // remove old styles
  245. if (this.style) {
  246. util.removeCssText(element, this.style);
  247. this.style = null;
  248. }
  249. // append new styles
  250. if (this.data.style) {
  251. util.addCssText(element, this.data.style);
  252. this.style = this.data.style;
  253. }
  254. };
  255. /**
  256. * Stringify the items contents
  257. * @param {string | Element | undefined} content
  258. * @returns {string | undefined}
  259. * @private
  260. */
  261. Item.prototype._contentToString = function (content) {
  262. if (typeof content === 'string') return content;
  263. if (content && 'outerHTML' in content) return content.outerHTML;
  264. return content;
  265. };
  266. /**
  267. * Return the width of the item left from its start date
  268. * @return {number}
  269. */
  270. Item.prototype.getWidthLeft = function () {
  271. return 0;
  272. };
  273. /**
  274. * Return the width of the item right from the max of its start and end date
  275. * @return {number}
  276. */
  277. Item.prototype.getWidthRight = function () {
  278. return 0;
  279. };
  280. /**
  281. * Repaint a drag area on the center of the item when the item is selected
  282. * @protected
  283. */
  284. Item.prototype._repaintDragCenter = function () {
  285. if (this.selected && this.options.editable.updateTime && !this.dom.dragCenter) {
  286. // create and show drag area
  287. var dragCenter = document.createElement('div');
  288. dragCenter.className = 'vis-drag-center';
  289. dragCenter.dragCenterItem = this;
  290. if (this.dom.box) {
  291. this.dom.box.appendChild(dragCenter);
  292. }
  293. else if (this.dom.point) {
  294. this.dom.point.appendChild(dragCenter);
  295. }
  296. this.dom.dragCenter = dragCenter;
  297. }
  298. else if (!this.selected && this.dom.dragCenter) {
  299. // delete drag area
  300. if (this.dom.dragCenter.parentNode) {
  301. this.dom.dragCenter.parentNode.removeChild(this.dom.dragCenter);
  302. }
  303. this.dom.dragCenter = null;
  304. }
  305. };
  306. module.exports = Item;