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.

105 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. callback(item); // send back the (possibly) changed item
  59. },
  60. onUpdate: function (item, callback) {
  61. item.content = prompt('Edit items text:', item.content);
  62. if (item.content != null) {
  63. callback(item); // send back adjusted item
  64. }
  65. else {
  66. callback(null); // cancel updating the item
  67. }
  68. },
  69. onRemove: function (item, callback) {
  70. if (confirm('Remove item ' + item.content + '?')) {
  71. callback(item); // confirm deletion
  72. }
  73. else {
  74. callback(null); // cancel deletion
  75. }
  76. }
  77. };
  78. var timeline = new vis.Timeline(container, items, options);
  79. items.on('*', function (event, properties) {
  80. logEvent(event, properties);
  81. });
  82. function logEvent(event, properties) {
  83. var log = document.getElementById('log');
  84. var msg = document.createElement('div');
  85. msg.innerHTML = 'event=' + JSON.stringify(event) + ', ' +
  86. 'properties=' + JSON.stringify(properties);
  87. log.firstChild ? log.insertBefore(msg, log.firstChild) : log.appendChild(msg);
  88. }
  89. </script>
  90. </body>
  91. </html>