Skip to content
This repository was archived by the owner on Dec 30, 2018. It is now read-only.

Commit 1b6e2b0

Browse files
kkolstadpassy
authored andcommitted
feat(directive): add reload-on-show option
Add a `$watch` to detect when `masonry` is displayed after being hidden and call `ctrl.reload()`.
1 parent 12c927f commit 1b6e2b0

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,24 @@ doesn't actually make use of images.
9898
</masonry>
9999
```
100100

101+
### `reload-on-show`
102+
103+
The `reload-on-show` attribute triggers a reload when the masonry element (or an
104+
ancestor element) is shown after being hidden, useful when using `ng-show` or
105+
`ng-hide`. Without this if the viewport is resized while the masonry element is
106+
hidden it may not render properly when shown again.
107+
108+
*Example:*
109+
110+
```html
111+
<masonry reload-on-show ng-show="showList">
112+
<div class="masonry-brick">...</div>
113+
<div class="masonry-brick">...</div>
114+
</masonry>
115+
```
116+
117+
When `showList` changes from falsey to truthy `ctrl.reload` will be called.
118+
101119
### `masonry-options`
102120

103121
You can provide [additional options](http://masonry.desandro.com/options.html)

src/angular-masonry.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,16 @@
135135
ctrl.loadImages = loadImages !== false;
136136
var preserveOrder = scope.$eval(attrs.preserveOrder);
137137
ctrl.preserveOrder = (preserveOrder !== false && attrs.preserveOrder !== undefined);
138+
var reloadOnShow = scope.$eval(attrs.reloadOnShow);
139+
if (reloadOnShow !== false && attrs.reloadOnShow !== undefined) {
140+
scope.$watch(function () {
141+
return element.prop('offsetParent');
142+
}, function (isVisible, wasVisible) {
143+
if (isVisible && !wasVisible) {
144+
ctrl.reload();
145+
}
146+
});
147+
}
138148

139149
scope.$emit('masonry.created', element);
140150
scope.$on('$destroy', ctrl.destroy);

test/spec/directive.coffee

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,22 @@ describe 'angular-masonry', ->
6161
expect(call.args[0].isOriginLeft).toBeTruthy()
6262
)
6363

64+
it 'should setup a $watch when the reload-on-show is present', inject(($compile) =>
65+
sinon.spy(@scope, '$watch')
66+
element = angular.element '<masonry reload-on-show></masonry>'
67+
element = $compile(element)(@scope)
68+
69+
expect(@scope.$watch).toHaveBeenCalled()
70+
)
71+
72+
it 'should not setup a $watch when the reload-on-show is missing', inject(($compile) =>
73+
sinon.spy(@scope, '$watch')
74+
element = angular.element '<masonry></masonry>'
75+
element = $compile(element)(@scope)
76+
77+
expect(@scope.$watch).not.toHaveBeenCalled()
78+
)
79+
6480
describe 'MasonryCtrl', =>
6581
beforeEach inject(($controller, $compile) =>
6682
@element = angular.element '<div></div>'

0 commit comments

Comments
 (0)