-
-
Save CMCDragonkai/6191419 to your computer and use it in GitHub Desktop.
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); | |
} | |
}; | |
} | |
); | |
}); |
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!
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
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!