public
Last active

JS: AngularJS Masonry Directive. http://plnkr.co/edit/ZuSrSh?p=preview

  • Download Gist
MasonryWall.Directive.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
define(['angular', 'masonry', 'imagesLoaded', 'lodash'], function(angular, Masonry, imagesLoaded, _){
 
'use strict';
 
/**
* Masonry Directive for a wall of item.
* This directive is intended to be used along with ng-repeat directive.
* Put masonryWallDir on the container element and pass in a class selector for each item to be laid out.
* Pass in optional options via masonryWallOptions.
* Put the masonryItemDir next to ng-repeat directive on the item to be repeated.
* You're done!
*
* @param {String} masonryWallDir Class selector of each item
* @param {Object} masonryWallOptions Optional options that are directly passed into Masonry
*/
angular.module('Directives')
.directive('masonryWallDir', function(){
return {
controller: [
'$scope',
'$element',
'$attrs',
function($scope, $element, $attrs){
 
var wallContainer, masonryOptions;
 
wallContainer = $element[0];
 
masonryOptions = _.assign(
{},
$scope.$eval($attrs.masonryWallOptions),
{ itemSelector: $attrs.masonryWallDir }
);
 
this.masonry = new Masonry(
wallContainer,
masonryOptions
);
 
this.masonry.bindResize();
 
var self = this;
this.debouncedReload = _.debounce(function(){
self.masonry.reloadItems();
self.masonry.layout();
}, 100);
 
}
]
};
})
.directive('masonryItemDir',
function(){
return {
require: '^masonryWallDir',
link: function(scope, element, attributes, masonryWallDirCtrl){
 
imagesLoaded(element, function(){
if(scope.$first){
masonryWallDirCtrl.masonry.prepended(element);
}else{
masonryWallDirCtrl.masonry.appended(element);
}
});
 
scope.$on('$destroy', masonryWallDirCtrl.debouncedReload);
 
}
};
}
);
 
});

Compatible with AMD. Requires jQuery, imagesLoaded and lodash. The simplest implementation yet, only 73 lines uncompressed! Works with dynamic amount of items, AJAX loaded items (even with initial items), window resizing, and custom options. No $timeout hacks or $watches!

Would it be possible to put together a http://plnkr.co/ or something as a demo? Specifically, I'm not sure what UtilitiesServ is, and I'm not sold on using AMD.

@gkoberger here: http://plnkr.co/edit/ZuSrSh?p=preview

Oh yea the UtilitiesServ was a typo. I reworked it. AMD is not needed, if you don't use AMD, all you do is remove the AMD container, and it's the same thing.

Great job, thanks a lot for sharing! You should create a repo for this. Looks like the best implementation so far.

@CMCDragonkal, I'm wondering if there's a way to get the instance of Masonry ? I'm already working with Directives that communicate between them, but there's one case in special that's not nested, so I can't require another directive and then call the controller method - I know I can have an intermediate one, but it's too much configuration, for such a small case.

So, I guess this is not a good practice but this is what I'm doing, please correct me if I'm wrong:

$( $element ).data("masonry", self.masonry );

Any suggestion is appreciated, thank you!

@helderoliveira

Masonry would be attached the DOM element. It may be possible to extract from the DOM. However that's not very elegant. It would best if you attached the Masonry object to a third party service. This will require modification of the container directive. I haven't tried that, but see if both ways works for you.

Douglas Jardine had an issue with modifying Masonry after it's been loaded. So he contributed his version:

$attrs.$observe('masonryWallOptions', function(actual_value) {
    masonryOptions = _.assign(
        {},
        $scope.$eval(actual_value), //$attrs.masonryWallOptions), 
        { itemSelector: $attrs.masonryWallDir }
    );

        self.masonry = new Masonry(
        wallContainer,
        masonryOptions
    );

    self.masonry.bindResize();

    self.debouncedReload = _.debounce(function(){
        self.masonry.reloadItems();
        self.masonry.layout();
    }, 100);
});

The above code replaces: https://gist.github.com/CMCDragonkai/6191419#file-masonrywall-directive-js-L29-L46

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.