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.

140 lines
4.5 KiB

  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <title>Timeline | Manipulation callbacks</title>
  5. <style type="text/css">
  6. body, html {
  7. font-family: sans-serif;
  8. font-size: 11pt;
  9. }
  10. </style>
  11. <script src="http://t4t5.github.io/sweetalert/dist/sweetalert.min.js"></script>
  12. <link href="http://t4t5.github.io/sweetalert/dist/sweetalert.css" rel="stylesheet" type="text/css"/>
  13. <script src="../../../dist/vis.js"></script>
  14. <link href="../../../dist/vis.css" rel="stylesheet" type="text/css" />
  15. <script src="../../googleAnalytics.js"></script>
  16. </head>
  17. <body>
  18. <p style="max-width: 800px;">
  19. 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.
  20. </p>
  21. <div id="visualization"></div>
  22. <p></p>
  23. <div id="log"></div>
  24. <script type="text/javascript">
  25. // note that months are zero-based in the JavaScript Date object, so month 3 is April
  26. var items = new vis.DataSet([
  27. {id: 1, content: 'item 1', start: new Date(2013, 3, 20)},
  28. {id: 2, content: 'item 2', start: new Date(2013, 3, 14)},
  29. {id: 3, content: 'item 3', start: new Date(2013, 3, 18)},
  30. {id: 4, content: 'item 4', start: new Date(2013, 3, 16), end: new Date(2013, 3, 19)},
  31. {id: 5, content: 'item 5', start: new Date(2013, 3, 25)},
  32. {id: 6, content: 'item 6', start: new Date(2013, 3, 27)}
  33. ]);
  34. var min = new Date(2013, 3, 1); // 1 april
  35. var max = new Date(2013, 3, 30, 23, 59, 59); // 30 april
  36. var container = document.getElementById('visualization');
  37. var options = {
  38. editable: true,
  39. onAdd: function (item, callback) {
  40. prettyPrompt('Add item', 'Enter text content for new item:', item.content, function (value) {
  41. if (value) {
  42. item.content = value;
  43. callback(item); // send back adjusted new item
  44. }
  45. else {
  46. callback(null); // cancel item creation
  47. }
  48. });
  49. },
  50. onMove: function (item, callback) {
  51. var title = 'Do you really want to move the item to\n' +
  52. 'start: ' + item.start + '\n' +
  53. 'end: ' + item.end + '?';
  54. prettyConfirm('Move item', title, function (ok) {
  55. if (ok) {
  56. callback(item); // send back item as confirmation (can be changed)
  57. }
  58. else {
  59. callback(null); // cancel editing item
  60. }
  61. });
  62. },
  63. onMoving: function (item, callback) {
  64. if (item.start < min) item.start = min;
  65. if (item.start > max) item.start = max;
  66. if (item.end > max) item.end = max;
  67. callback(item); // send back the (possibly) changed item
  68. },
  69. onUpdate: function (item, callback) {
  70. prettyPrompt('Update item', 'Edit items text:', item.content, function (value) {
  71. if (value) {
  72. item.content = value;
  73. callback(item); // send back adjusted item
  74. }
  75. else {
  76. callback(null); // cancel updating the item
  77. }
  78. });
  79. },
  80. onRemove: function (item, callback) {
  81. prettyConfirm('Remove item', 'Do you really want to remove item ' + item.content + '?', function (ok) {
  82. if (ok) {
  83. callback(item); // confirm deletion
  84. }
  85. else {
  86. callback(null); // cancel deletion
  87. }
  88. });
  89. }
  90. };
  91. var timeline = new vis.Timeline(container, items, options);
  92. items.on('*', function (event, properties) {
  93. logEvent(event, properties);
  94. });
  95. function logEvent(event, properties) {
  96. var log = document.getElementById('log');
  97. var msg = document.createElement('div');
  98. msg.innerHTML = 'event=' + JSON.stringify(event) + ', ' +
  99. 'properties=' + JSON.stringify(properties);
  100. log.firstChild ? log.insertBefore(msg, log.firstChild) : log.appendChild(msg);
  101. }
  102. function prettyConfirm(title, text, callback) {
  103. swal({
  104. title: title,
  105. text: text,
  106. type: 'warning',
  107. showCancelButton: true,
  108. confirmButtonColor: "#DD6B55"
  109. }, callback);
  110. }
  111. function prettyPrompt(title, text, inputValue, callback) {
  112. swal({
  113. title: title,
  114. text: text,
  115. type: 'input',
  116. showCancelButton: true,
  117. inputValue: inputValue
  118. }, callback);
  119. }
  120. </script>
  121. </body>
  122. </html>