<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>Sortable List of Components</h2>
</header>
<footer>
<div id="demo">
<sorter></sorter>
</div>
<div class="div-comp">
<b class="name"></b>
</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>
</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;
}
/* MagJS Example - Example: sorting list of components */
var HTML1 = mag.html2dom("<div></div>")
var SorterItem = mag(HTML1, props => {
return {
_html: props.item.outerHTML,
_style: {
color: props.turned ? "red" : "blue"
}
}
})
var HTML2 = mag.html2dom("<div><button>Sort List</button><ol><li></li></ol></div>")
var Sorter = mag(HTML2, {
controller: function(props) {
this.comps = props.components;
this.button = {
_onClick: () => this.comps.rotate(5)
}
},
view: function() {
// list of items
this.state.li = this.state.comps.map((comp, index) => {
return SorterItem({
key: index,
item: comp(),
turned: comp.getProps().isOn
})
})
}
})
var DivComp = {
view: (state, props) => {
state.name = props.name
}
}
var comp1 = mag("div-comp", DivComp, {
name: "Mike",
isOn: false,
key: 1
})
var comp2 = mag("div-comp", DivComp, {
name: "John",
isOn: true,
key: 2
})
var comp3 = mag("div-comp", DivComp, {
name: "Joe",
isOn: true,
key: 3
})
var comps = [comp1, comp2, comp3]
Array.prototype.rotate = (function() {
var unshift = Array.prototype.unshift,
splice = Array.prototype.splice;
return function(count) {
var len = this.length >>> 0,
count = count >> 0;
unshift.apply(this, splice.call(this, count % len, len));
return this;
};
})();
mag.module("demo", {
view: function() {
this.state.sorter = Sorter({
components: comps
})
}
})
Output
300px
You can jump to the latest bin by adding /latest
to your URL
Keyboard Shortcuts
Shortcut | Action |
---|---|
ctrl + [num] | Toggle nth panel |
ctrl + 0 | Close focused panel |
ctrl + enter | Re-render output. If console visible: run JS in console |
Ctrl + l | Clear the console |
ctrl + / | Toggle comment on selected lines |
ctrl + ] | Indents selected lines |
ctrl + [ | Unindents selected lines |
tab | Code complete & Emmet expand |
ctrl + shift + L | Beautify code in active panel |
ctrl + s | Save & lock current Bin from further changes |
ctrl + shift + s | Open the share options |
ctrl + y | Archive Bin |
Complete list of JS Bin shortcuts |
JS Bin URLs
URL | Action |
---|---|
/ | Show the full rendered output. This content will update in real time as it's updated from the /edit url. |
/edit | Edit the current bin |
/watch | Follow a Code Casting session |
/embed | Create an embeddable version of the bin |
/latest | Load the very latest bin (/latest goes in place of the revision) |
/[username]/last | View the last edited bin for this user |
/[username]/last/edit | Edit the last edited bin for this user |
/[username]/last/watch | Follow the Code Casting session for the latest bin for this user |
/quiet | Remove analytics and edit button from rendered output |
.js | Load only the JavaScript for a bin |
.css | Load only the CSS for a bin |
Except for username prefixed urls, the url may start with http://jsbin.com/abc and the url fragments can be added to the url to view it differently. |