Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="CanJS 3.4 - File Navigator - Beginner - START">
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
​
</head>
<body>
  <!--
  <span>root</span>
  <ul>
    <li class='file'>πŸ“ <span>cats.png</span></li>
    <li class='file'>πŸ“ <span>dogs.jpg</span></li>
    <li class='folder'>πŸ“ <span>empty folder</span></li>
    <li class='folder hasChildren'>πŸ“ <span>folder with files</span></li>
    <li class='folder hasChildren'>
      πŸ“ <span>folder loading files </span>
      <div class="loading">Loading</div>
    </li>
    <li class='folder hasChildren'>
      πŸ“ <span>opened folder</span>
      <ul>
        <li class='file'>πŸ“ <span>file1.png</span></li>
        <li class='file'>πŸ“ <span>file2.jpg</span></li>
      </ul>
    </li>
  </ul>-->
​
<script type="text/stache" id="entities-template">
{{<entityChildren}}
  <ul>
  {{#each this.children}}
      <li class="{{this.type}} {{#if this.hasChildren}}hasChildren{{/if}}">
        {{#eq type 'file'}}
          πŸ“ <span>{{name}}</span>
        {{else}}
          πŸ“ <span on:click="toggleOpen()">{{name}}</span>
        {{/eq}}
        {{#if isOpen}}
         {{>entityChildren}}
        {{/if}}
      </li>
  {{/each}}
  </ul>
{{/entityChildren}}
​
<span>{{name}}</span>
{{>entityChildren}}
</script>
​
<script src="//unpkg.com/can/dist/global/can.all.js"></script>
​
<script>
​
var entityId = 1;
​
var makeEntities = function(parentId, depth){
  if(depth > 5) {
    return [];
  }
​
  var entitiesCount = can.fixture.rand(6);
​
  var entities = [];
​
  for(var i = 0 ;  i< entitiesCount; i++) {
​
    // The id for this entity
    var id = ""+(entityId++),
        // If the entity is a folder or file
        isFolder = Math.random() > 0.3,
        // The children for this folder.
        children = isFolder ? makeEntities(id, depth+1) : [];
​
    var entity = {
      id: id,
      name: (isFolder ? "Folder" : "File")+" "+id,
      parentId: parentId,
      type: (isFolder ? "folder" : "file"),
      hasChildren: children.length ? true : false
    };
    entities.push(entity);
    if(isFolder) {
      entity.children = children;
    }
​
  }
  return entities;
};
​
var rootEntityData = {
  id: "0",
  name: "ROOT/", 
  hasChildren: true,
  type: "folder",
  children: makeEntities("0", 0)
};
</script>
</body>
</html>
​
 
body {
  padding: 10px;
}
​
@import url('https://fonts.googleapis.com/css?family=Roboto+Mono:400,700');
​
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
​
body {
  font-family: "Roboto Mono", "Lucida Console", Monaco, monospace;
  font-size: 14px;
  color: #000;
}
​
ul {
  list-style: none;
}
​
li {
  padding-left: 20px;
  position: relative;
  line-height: 2em;
}
​
li:before{
  position: absolute;
  left: 5px;
  top: -2px;
  content: '';
  display: block;
  height: 1em;
  border-bottom: 1px solid #bbb;
  width: 10px;
}
​
li:after {
  position: absolute;
  left: 5px;
  bottom: 0;
  content: '';
  display: block;
  border-left: 1px solid #bbb;
  height: 100%;
}
​
.loading {
  color: black;
  padding-left: 20px;
  padding-top: 10px;
  padding-bottom: 10px;
}
​
.loading:after {
  content: "...";
}
​
.hasChildren > a-folder > span, .hasChildren > span {
  transition: all .1s ease-in-out;
  cursor: pointer;
  border-bottom: 1px dotted #000;
}
​
.hasChildren > a-folder > span:hover, .hasChildren > span:hover {
  opacity: .5;
}
 
var template = can.stache.from("entities-template");
​
var Entity = can.DefineMap.extend({
  id: "string",
  name: "string",
  hasChildren: "boolean",
  type: "string",
  children: [{
    type: function(rawData){
      return new Entity(rawData)
    }
  }],
  isOpen: {type: "boolean", value: false},
  toggleOpen: function(){
    this.isOpen = !this.isOpen;
  }
});
​
var rootEntity = new Entity(rootEntityData);
​
var frag = template(rootEntity);
​
document.body.appendChild(frag);
​
Output

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

Dismiss x