Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  <!-- <link href="//cdn.jsdelivr.net/picnicss/4.1.1/picnic.min.css" rel="stylesheet"> -->
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>
  <nav>
    <h1>Hello Mag.JS!</h1>
    <a target="_top" href="https://github.com/magnumjs/mag.js">GitHub</a>
  </nav>
  <article class="card">
    <header>
      <h2>Drag and drop</h2>
    </header>
    <footer>
      <div id="sorter">
        <p>Drag the list items over the dustbin, and drop them to have the bin eat the item</p>
        <div id="bin">
          <p></p>
        </div>
        <ul>
          <li><a id="one" href="#" draggable="true">one</a>
          </li>
        </ul>
      </div>
    </footer>
  </article>
  <script src="//rawgit.com/magnumjs/mag.js/master/mag-latest.min.js"></script>
  <script src="//rawgit.com/magnumjs/mag.js/master/dist/mag.addons.0.22.min.js"></script>
  <script src="//cdn.rawgit.com/Bernardo-Castilho/dragdroptouch/master/DragDropTouch.js"></script>
  </body>
</html>
 
.cloak,
li:empty,
.hide {
  display: none;
}
.mainButton {
  font-size: 1.5em;
}
a {
  display: block;
}
a:after {
  content: " \bb";
}
nav a {
  float: right;
  margin-top: -50px;
}
body {
  background: #fff;
  text-align: left;
  width: 90%;
  max-width: 960px;
  margin: 0 auto;
  padding: 20px 0 0;
}
nav {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 3em;
  padding: 0 .6em;
  background: #fff;
  box-shadow: 0 0 0.2em rgba(17, 17, 17, 0.2);
  z-index: 10000;
  transition: all .3s;
  transform-style: preserve-3d;
}
header {
  font-weight: bold;
  position: relative;
  border-bottom: 1px solid #eee;
  padding: .6em .8em;
}
footer {
  padding: .8em;
}
article {
  top: 100px;
}
.card {
  max-width: 100%;
  display: block;
  position: relative;
  box-shadow: 0;
  border-radius: .2em;
  border: 1px solid #ccc;
  overflow: hidden;
  text-align: left;
  background: #fff;
  margin-bottom: .6em;
  padding: 6px;
  transition: all .3s ease;
}
/* draggable list */
.zoom {
  transition: transform 500ms;
}
li {
  list-style: none;
}
li a {
  text-decoration: none;
  color: #000;
  margin: 10px;
  width: 150px;
  border: 3px dashed #999;
  background: #eee;
  padding: 10px;
  display: block;
}
*[draggable=true] {
  -moz-user-select: none;
  -khtml-user-drag: element;
  cursor: move;
}
*:-khtml-drag {
  background-color: rgba(238, 238, 238, 0.5);
}
li a:hover:after {
  content: ' (drag me)';
}
ul {
  margin-left: 200px;
  min-height: 300px;
}
li.over {
  border-color: #333;
  background: #ccc;
}
#bin {
  background: url(//raw.githubusercontent.com/timruffles/ios-html5-drag-drop-shim/master/demo/bin.jpg) top right no-repeat;
  height: 250px;
  width: 166px;
  float: left;
  border: 5px solid #000;
  position: relative;
  margin-top: 0;
  overflow: hidden;
}
#bin.over {
  background: url(//raw.githubusercontent.com/timruffles/ios-html5-drag-drop-shim/master/demo/bin.jpg) top left no-repeat;
}
#bin p {
  font-weight: bold;
  text-align: center;
  position: absolute;
  bottom: 20px;
  width: 166px;
  font-size: 32px;
  color: #fff;
  text-shadow: #000 2px 2px 2px;
}
 
/* Mag.JS Example - Drag and Drop
From: https://html5demos.com/drag/  */
var Sorter = {};
Sorter.controller = function(props) {
  this.selectedId = '';
  this.components = props.components;
  var eat = ['yum!', 'gulp', 'burp!', 'nom'];
  this.bin = {
    _ondrop: function(e) {
      e.stopPropagation(); // stops the browser from redirecting...why???
      e.target.classList.toggle('over');
      var id = e.dataTransfer.getData('Text');
      this.state.selectedId = id;
      this.state.bin.p = {
        _text: eat[parseInt(Math.random() * eat.length)],
        _style: {
          opacity: 1.0
        }
      }
      var tick = () => {
        this.state.bin.p._style.opacity = +this.state.bin.p._style.opacity - 0.01;
        if (+this.state.bin.p._style.opacity > 0) {
          setTimeout(tick, 16)
        }
      };
      tick()
    },
    _ondragleave: (e) => {
      e.stopPropagation()
      e.target.classList.remove('over');
    },
    _ondragover: function(e) {
      e.preventDefault(); // allows us to drop
      e.target.classList.add('over');
      e.dataTransfer.dropEffect = 'copy';
      return false;
    }
  }
};
Sorter.view = function(state, props) {
  if (state.selectedId) {
    //remove from components
    state.components = state.components.filter(item => item != state.selectedId)
  }
  state.li = state.components.map(item => {
    return {
      _text: item,
      _id: item,
      a: {
        _id: item
      },
      _ondragstart: function(e) {
        e.dataTransfer.effectAllowed = 'copy'; // only dropEffect='copy' will be dropable
        e.dataTransfer.setData('Text', e.target.id); // required otherwise doesn't work
      }
    }
  })
};
var props = {
  components: [1, 2, 3, 4]
}
mag.module("sorter", Sorter, props);
Output 300px

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
anonymouspro
0viewers