Combine Knockout and Isotope
Make Isotope layout elements as they are added to or removed from a ko.observableArray.
by aknuds1
HTML
<script src="http://isotope.metafizzy.co/jquery.isotope.min.js"></script>
<script src="https://github.com/downloads/SteveSanderson/knockout/knockout-2.2.0.debug.js"></script>
<h1>Knockout + Isotope Demo</h1>
<p>Click on an item to remove it or click in the item container to add a new item</p>
<div id="container" data-bind="template: {foreach: items, afterAdd: afterAdd, beforeRemove: beforeRemove}, click: addItem">
<div class="item show" data-bind="text: text, click: $parent.removeItem, clickBubble: false"></div>
</div>
CSS
h1 {
font-size: 24px;
font-weight: bold;
margin-bottom: 4px;
}
#container {
margin-top: 20px;
border: 1px dashed;
height: 600px;
}
.item {
background-color: blue;
border: none;
width: 200px;
height: 150px;
text-align: center;
line-height: 150px;
margin: 0;
}
.item {
background-color: blue;
border: none;
width: 200px;
height: 150px;
text-align: center;
line-height: 150px;
margin-bottom: 10px;
}
/**** Isotope Filtering ****/
.isotope-item {
z-index: 2;
}
.isotope-hidden.isotope-item {
pointer-events: none;
z-index: 1;
}
/**** Isotope CSS3 transitions ****/
.isotope, .isotope .isotope-item {
-webkit-transition-duration: 0.8s;
-moz-transition-duration: 0.8s;
-ms-transition-duration: 0.8s;
-o-transition-duration: 0.8s;
transition-duration: 0.8s;
}
.isotope {
-webkit-transition-property: height, width;
-moz-transition-property: height, width;
-ms-transition-property: height, width;
-o-transition-property: height, width;
transition-property: height, width;
}
.isotope .isotope-item {
-webkit-transition-property: -webkit-transform, opacity;
-moz-transition-property: -moz-transform, opacity;
-ms-transition-property: -ms-transform, opacity;
-o-transition-property: -o-transform, opacity;
transition-property: transform, opacity;
}
/**** disabling Isotope CSS3 transitions ****/
.isotope.no-transition, .isotope.no-transition .isotope-item, .isotope .isotope-item.no-transition {
-webkit-transition-duration: 0s;
-moz-transition-duration: 0s;
-ms-transition-duration: 0s;
-o-transition-duration: 0s;
transition-duration: 0s;
}
JavaScript
function ItemModel(parent) {
var value, self = this,
found, i;
for (value = 0; value < parent.items().length; ++value) {
found = false;
for (i in parent.items()) {
var item = parent.items()[i];
if (item.value() == value) {
found = true;
break;
}
}
if (!found) {
break;
}
}
this.value = ko.observable(value);
this.text = ko.computed(function () {
return "Item " + self.value();
});
}
var ViewModel = function () {
var self = this;
self.items = ko.observableArray();
self.items.push(new ItemModel(self));
self.items.push(new ItemModel(self));
this.afterAdd = function (elem) {
// Node type 1 means the element node itself, we may get notifications for sub-nodes,
// e.g. text
if (elem.nodeType != 1) {
return;
}
//$('#container').isotope('appended', $(elem));
//$('#container').isotope({'sortBy': 'value'});
};
this.beforeRemove = function (elem, event) {
// Node type 1 means the element node itself, we may get notifications for sub-nodes,
// e.g. text
if (elem.nodeType != 1) {
return;
}
$('#container').isotope('remove', $(elem));
};
this.removeItem = function (item, event) {
self.items.remove(item);
return false;
};
this.addItem = function () {
self.items.unshift(new ItemModel(self));
};
setTimeout(function () {
self.addItem();
setTimeout(function () {
$('#container').isotope('reloadItems');
$('#container').isotope({sortBy: 'original-order'});
});
}, 2000);
};
ko.applyBindings(new ViewModel("Test"));
$('#container').isotope({
itemSelector: '.item',
layoutMode: 'cellsByRow',
cellsByRow: {
columnWidth: 220,
rowHeight: 170
},
...