Vis.js timeline drag item out of the timeline

261 Views Asked by At

Based on this example and exact code:

<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8"/>
  <title>Timeline | other | Drag & Drop</title>
  
  <script src="../../../standalone/umd/vis-timeline-graph2d.min.js"></script>
  <link href="../../../styles/vis-timeline-graph2d.min.css" rel="stylesheet" type="text/css" />

  

  <style type="text/css">
    li.item {
      list-style: none;
      width: 150px;
      color: #1A1A1A;
      background-color: #D5DDF6;
      border: 1px solid #97B0F8;
      border-radius: 2px;
      margin-bottom: 5px;
      padding: 5px 12px;
    }
    li.item:before {
      content: "≣";
      font-family: Arial, sans-serif;
      display: inline-block;
      font-size: inherit;
      cursor: move;
    }
    li.object-item {
      list-style: none;
      width: 150px;
      color: #1A1A1A;
      background-color: #D5DDF6;
      border: 1px solid #97B0F8;
      border-radius: 2px;
      margin-bottom: 5px;
      padding: 5px 12px;
    }
    li.object-item:before {
      content: "≣";
      font-family: Arial, sans-serif;
      display: inline-block;
      font-size: inherit;
      cursor: move;
    }
    .items-panel {
      display: flex;
      justify-content: space-around;
    }
  </style>
</head>

<body>
<div id="visualization" ></div>

<div class='items-panel'>
  <div class='side'>
    <h3>Items:</h3>
    <ul class="items">
      <li draggable="true" class="item">
        item 1 - box
      </li>
      <li draggable="true" class="item">
        item 2 - point
      </li>
      <li draggable="true" class="item">
        item 3 - range
      </li>
      <li draggable="true" class="item">
        item 3 - range - fixed times - <br>
        (start: now, end: now + 10 min)
      </li>
    </ul>
  </div>

  <div class='side'>
    <h3>Object with "target:'item'":</h3>
    <li draggable="true" class="object-item">
      object with target:'item'
    </li>
  </div>
</div>

<script>

  // create groups
  var numberOfGroups = 3; 
  var groups = new vis.DataSet()
  for (var i = 0; i < numberOfGroups; i++) {
    groups.add({
      id: i,
      content: 'Truck&nbsp;' + i
    })
  }
  
  // create items
  var numberOfItems = 10;
  var items = new vis.DataSet();

  var itemsPerGroup = Math.round(numberOfItems/numberOfGroups);

  for (var truck = 0; truck < numberOfGroups; truck++) {
    var date = new Date();
    for (var order = 0; order < itemsPerGroup; order++) {
      date.setHours(date.getHours() +  4 * (Math.random() < 0.2));
      var start = new Date(date);

      date.setHours(date.getHours() + 2 + Math.floor(Math.random()*4));
      var end = new Date(date);

      items.add({
        id: order + itemsPerGroup * truck,
        group: truck,
        start: start,
        end: end,
        content: 'Order ' + order
      });
    }
  }
  
  // specify options
  var options = {
    stack: true,
    start: new Date(),
    end: new Date(1000*60*60*24 + (new Date()).valueOf()),
    editable: true,
    orientation: 'top',
    onDropObjectOnItem: function(objectData, item, callback) {
      if (!item) { return; }
      alert('dropped object with content: "' + objectData.content + '" to item: "' + item.content + '"');
    }
  };

  // create a Timeline
  var container = document.getElementById('visualization');
  timeline1 = new vis.Timeline(container, items, groups, options);

  function handleDragStart(event) {
    var dragSrcEl = event.target;

    event.dataTransfer.effectAllowed = 'move';
    var itemType = event.target.innerHTML.split('-')[1].trim();
    var item = {
      id: new Date(),
      type: itemType,
      content: event.target.innerHTML.split('-')[0].trim()
    };

    var isFixedTimes = (event.target.innerHTML.split('-')[2] && event.target.innerHTML.split('-')[2].trim() == 'fixed times')
    if (isFixedTimes) {
      item.start = new Date();
      item.end = new Date(1000*60*10 + (new Date()).valueOf());
    }
    event.dataTransfer.setData("text", JSON.stringify(item));
  }

  function handleObjectItemDragStart(event) {
    var dragSrcEl = event.target;
    
    event.dataTransfer.effectAllowed = 'move';
    var objectItem = {
      content: 'objectItemData',
      target: 'item'
    };
    event.dataTransfer.setData("text", JSON.stringify(objectItem));
  }

  var items = document.querySelectorAll('.items .item');

  var objectItems = document.querySelectorAll('.object-item');

  for (var i = items.length - 1; i >= 0; i--) {
    var item = items[i];
    item.addEventListener('dragstart', handleDragStart.bind(this), false);
  }

  for (var i = objectItems.length - 1; i >= 0; i--) {
    var objectItem = objectItems[i];
    objectItem.addEventListener('dragstart', handleObjectItemDragStart.bind(this), false);
  }

</script>

</body>
</html>

https://jsfiddle.net/api/post/library/pure/ I created my own solution. The use case is that I have multiple factories and I want to add orders to the different factories do do the planning.

Basically this example does do the trick. The items list below the timeline represent my orders and I can drag them to the different factories and in the "handleDragEnd" function I do my database and design functions to "delete" the order from the orderlist and do additional planning calculations.

However, sometimes I dropped an order in the timeline but I want to remove it because I want to play a bit around with other orders. I know I can delete the item from the timeline (clicking the red cross and ad some functionality to that event) but for the user it would give a better experience if I just drag the item out of the timeline and in the orderlist again.

How can I achieve this?

0

There are 0 best solutions below