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.

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