Gallery View with dynamic icons (knockout JS)
use file names to apply css classes using knockout JS
by Rishi Gautam
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-debug.js"></script>
<div class='gallery' data-bind="foreach: files">
<!-- ko if:isImage() -->
<div class='child' data-bind="text: $data.file"></div>
<!-- /ko -->
<!-- ko ifnot:isImage() -->
<div class='child'>
<div class='fileIcon'>
<div class='circle' data-bind="text: $data.file, css: $data.file">
</div>
</div>
</div>
<!-- /ko -->
</div>
CSS
.doc {
border-color: blue;
color: blue;
}
.zip {
border-color: #f1c30f;
color: #f1c30f;
}
.xls {
border-color: green;
color: green;
}
.pdf {
border-color: red;
color: red;
}
.fileIcon {
background-color: #fff;
width: 100%;
height: 100%;
position: relative;
}
.circle {
border-style: solid;
border-width: 5px;
border-radius: 50px;
position: absolute;
width: 50%;
height: 50%;
top: 20%;
left: 20%;
margin: 0 auto;
display: inline-block;
text-align: center;
line-height: 49px;
}
.gallery {
background-color: #ccc;
width: 470px;
height: 250px;
padding: 5px 5px 5px 5px;
overflow-y: scroll;
overflow-x: hidden;
}
.child {
width: 100px;
height: 100px;
margin: 5px 5px 5px 5px;
float: left;
border-style: solid;
border-width: 1px;
border-radius: 5px;
}
.child:hover {
cursor: pointer;
background-color: #ddd;
}
.selected {
background-color: #ddd;
}
JavaScript
var model = {
files: [
{file: 'pdf', isImage: function() {return false;}},
{file: 'png', isImage: function() {return true;}},
{file: 'pdf', isImage: function() {return false;}},
{file: 'doc', isImage: function() {return false;}},
{file: 'xls', isImage: function() {return false;}},
{file: 'xls', isImage: function() {return false;}},
{file: 'doc', isImage: function() {return false;}},
{file: 'zip', isImage: function() {return false;}},
{file: 'pdf', isImage: function() {return false;}},
{file: 'jpg', isImage: function() {return true;}}]
};
ko.applyBindings(model);