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.

106 lines
3.5 KiB

  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <title>Timeline | Edit items</title>
  5. <style type="text/css">
  6. body, html {
  7. font-family: sans-serif;
  8. font-size: 12pt;
  9. }
  10. </style>
  11. <script src="../../dist/vis.js"></script>
  12. <link href="../../dist/vis.css" rel="stylesheet" type="text/css" />
  13. </head>
  14. <body>
  15. <p style="max-width: 800px;">
  16. This example shows how to use callback functions <code>onAdd</code>, <code>onMove</code>, <code>onMoving</code>, <code>onUpdate</code>, and <code>onRemove</code>. The <code>onMoving</code> function updates an item while dragging, and can be used to prevent the item from being drawn at disallowed or infeasible timeslots. In this example, the items cannot be moved outside of the month April 2013. The other callback functions are called after an add, move, update, or remove action has taken place, and can be used to cancel these actions.
  17. </p>
  18. <div id="visualization"></div>
  19. <p></p>
  20. <div id="log"></div>
  21. <script type="text/javascript">
  22. // note that months are zero-based in the JavaScript Date object, so month 3 is April
  23. var items = new vis.DataSet([
  24. {id: 1, content: 'item 1', start: new Date(2013, 3, 20)},
  25. {id: 2, content: 'item 2', start: new Date(2013, 3, 14)},
  26. {id: 3, content: 'item 3', start: new Date(2013, 3, 18)},
  27. {id: 4, content: 'item 4', start: new Date(2013, 3, 16), end: new Date(2013, 3, 19)},
  28. {id: 5, content: 'item 5', start: new Date(2013, 3, 25)},
  29. {id: 6, content: 'item 6', start: new Date(2013, 3, 27)}
  30. ]);
  31. var min = new Date(2013, 3, 1); // 1 april
  32. var max = new Date(2013, 3, 30, 23, 59, 59); // 30 april
  33. var container = document.getElementById('visualization');
  34. var options = {
  35. editable: true,
  36. onAdd: function (item, callback) {
  37. item.content = prompt('Enter text content for new item:', item.content);
  38. if (item.content != null) {
  39. callback(item); // send back adjusted new item
  40. }
  41. else {
  42. callback(null); // cancel item creation
  43. }
  44. },
  45. onMove: function (item, callback) {
  46. if (confirm('Do you really want to move the item to\n' +
  47. 'start: ' + item.start + '\n' +
  48. 'end: ' + item.end + '?')) {
  49. callback(item); // send back item as confirmation (can be changed)
  50. }
  51. else {
  52. callback(null); // cancel editing item
  53. }
  54. },
  55. onMoving: function (item, callback) {
  56. if (item.start < min) item.start = min;
  57. if (item.start > max) item.start = max;
  58. if (item.end > max) item.end = max;
  59. callback(item); // send back the (possibly) changed item
  60. },
  61. onUpdate: function (item, callback) {
  62. item.content = prompt('Edit items text:', item.content);
  63. if (item.content != null) {
  64. callback(item); // send back adjusted item
  65. }
  66. else {
  67. callback(null); // cancel updating the item
  68. }
  69. },
  70. onRemove: function (item, callback) {
  71. if (confirm('Remove item ' + item.content + '?')) {
  72. callback(item); // confirm deletion
  73. }
  74. else {
  75. callback(null); // cancel deletion
  76. }
  77. }
  78. };
  79. var timeline = new vis.Timeline(container, items, options);
  80. items.on('*', function (event, properties) {
  81. logEvent(event, properties);
  82. });
  83. function logEvent(event, properties) {
  84. var log = document.getElementById('log');
  85. var msg = document.createElement('div');
  86. msg.innerHTML = 'event=' + JSON.stringify(event) + ', ' +
  87. 'properties=' + JSON.stringify(properties);
  88. log.firstChild ? log.insertBefore(msg, log.firstChild) : log.appendChild(msg);
  89. }
  90. </script>
  91. </body>
  92. </html>