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

Commit 6065e18

Browse files
committed
feat(directive): add reload-on-resize option
Add a `$watch` to detect when `masonry` is resized and call `ctrl.reload()`.
1 parent b90f686 commit 6065e18

3 files changed

Lines changed: 44 additions & 0 deletions

File tree

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,24 @@ hidden it may not render properly when shown again.
118118

119119
When `showList` changes from falsey to truthy `ctrl.reload` will be called.
120120

121+
122+
### `reload-on-resize`
123+
124+
The `reload-on-resize` attribute triggers a reload when the masonry element changes
125+
its width, useful when only the parent element is resized (and not the window) and
126+
you want the elements to be rearranged. Without this if the parent is resized then
127+
some blank space may be left on the sides.
128+
129+
*Example:*
130+
131+
```html
132+
<masonry reload-on-resize>
133+
<div class="masonry-brick">...</div>
134+
<div class="masonry-brick">...</div>
135+
</masonry>
136+
```
137+
138+
121139
### `masonry-options`
122140

123141
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
@@ -145,6 +145,16 @@
145145
}
146146
});
147147
}
148+
var reloadOnResize = scope.$eval(attrs.reloadOnResize);
149+
if (reloadOnResize !== false && attrs.reloadOnResize !== undefined) {
150+
scope.$watch(function () {
151+
return element.prop('offsetWidth');
152+
}, function (newWidth, oldWidth) {
153+
if (newWidth != oldWidth) {
154+
ctrl.reload();
155+
}
156+
});
157+
}
148158

149159
scope.$emit('masonry.created', element);
150160
scope.$on('$destroy', ctrl.destroy);

test/spec/directive.coffee

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,22 @@ describe 'angular-masonry', ->
7777
expect(@scope.$watch).not.toHaveBeenCalled()
7878
)
7979

80+
it 'should setup a $watch when the reload-on-resize is present', inject(($compile) =>
81+
sinon.spy(@scope, '$watch')
82+
element = angular.element '<masonry reload-on-resize></masonry>'
83+
element = $compile(element)(@scope)
84+
85+
expect(@scope.$watch).toHaveBeenCalled()
86+
)
87+
88+
it 'should not setup a $watch when the reload-on-resize is missing', inject(($compile) =>
89+
sinon.spy(@scope, '$watch')
90+
element = angular.element '<masonry></masonry>'
91+
element = $compile(element)(@scope)
92+
93+
expect(@scope.$watch).not.toHaveBeenCalled()
94+
)
95+
8096
describe 'MasonryCtrl', =>
8197
beforeEach inject(($controller, $compile) =>
8298
@element = angular.element '<div></div>'

0 commit comments

Comments
 (0)