jQuery Isotope with AngularJS
Creating an angular directive to use isotope. This one creates the html elements in place of a call to ng-repeat.
by Shardul Pendse
HTML
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="http://cdn.jsdelivr.net/isotope/1.5.21/jquery.isotope.min.js"></script>
<script src="http://cdn.jsdelivr.net/angularjs/1.0.2/angular.min.js"></script>
<div id="items" ng-controller="ItemsCtrl">
<div id="container" my-Dir="" item-Arr="items"></div>
</div>
CSS
body {
font-family:'Helvetica Neue', Arial, sans-serif;
}
#container {
border: 2px solid;
padding: 5px;
width: 350px;
height: 160px;
}
.item {
width: 70px;
height: 70px;
background: #26D;
float: left;
margin: 5px;
color: white;
font-size: 60px;
}
.item.large {
width: 70px;
height: 200px;
background: #2D6;
z-index: 10;
}
.largeCss {
width: 70px;
height: 200px;
background: #2D6;
z-index: 10;
}
.normal {
width: 70px;
height: 70px;
background: #26D;
float: left;
margin: 5px;
color: white;
font-size: 60px;
}
.item:hover {
cursor: pointer;
}
/**** Isotope CSS3 transitions ****/
.isotope, .isotope .isotope-item {
-webkit-transition-duration: 0.8s;
-moz-transition-duration: 0.8s;
transition-duration: 0.8s;
}
.isotope {
-webkit-transition-property: height, width;
-moz-transition-property: height, width;
transition-property: height, width;
}
.isotope .isotope-item {
-webkit-transition-property: -webkit-transform, opacity;
-moz-transition-property: -moz-transform, opacity;
transition-property: transform, opacity;
}
JavaScript
var app = angular.module('isotopeApp', []);
app.controller('ItemsCtrl', function ItemsCtrl($scope, $timeout) {
$scope.items = [{
value: 1,
expand: false
}, {
value: 2,
expand: false
}, {
value: 3,
expand: false
}, {
value: 4,
expand: false
}, {
value: 5,
expand: false
}, {
value: 6,
expand: false
}, {
value: 7,
expand: false
}];
$scope.update = function () {
console.log('in update');
};
$scope.update();
});
app.directive('myDir', function () {
return {
scope: {
itemArr: "="
},
template: '<div class="item" ng-click="clickFunc($index);" ng-repeat="elements in itemArr" ng-class="{largeCss:elements.expand==true,normal:elements.expand==false}" >{{elements.value+elements.expand}}</div>',
link: function (scope, element, attrs) {
scope.previousClick = "";
scope.clickFunc = function (iIndex) {
if (scope.itemArr[iIndex].expand == true) {
console.log('in 1st if---->> ')
console.log(scope.itemArr[iIndex].expand)
scope.itemArr[iIndex].expand = false;
} else {
if (scope.previousClick != "") {
console.log("previous click");
console.log(scope.previousClick);
scope.itemArr[scope.previousClick].expand = false
scope.itemArr[iIndex].expand = true;
} else {
scope.previousClick = iIndex;
scope.itemArr[iIndex].expand = true;
console.log("in 1st click");
console.log(scope.itemArr[iIndex].expand)
console.log(scope.previousClick)
}
}
console.log("got...