Skip to content

Instantly share code, notes, and snippets.

@CMCDragonkai
Last active December 20, 2015 20:38
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CMCDragonkai/6191419 to your computer and use it in GitHub Desktop.
Save CMCDragonkai/6191419 to your computer and use it in GitHub Desktop.
JS: AngularJS Masonry Directive. http://plnkr.co/edit/ZuSrSh?p=preview
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);
}
};
}
);
});
@CMCDragonkai
Copy link
Author

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!

@gkoberger
Copy link

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.

@CMCDragonkai
Copy link
Author

@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.

@heldrida
Copy link

heldrida commented Oct 4, 2013

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

@heldrida
Copy link

heldrida commented Oct 5, 2013

@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!

@CMCDragonkai
Copy link
Author

@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.

@CMCDragonkai
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment