Skip to content

Commit

Permalink
Removed dependency on jQuery. Now it works with just jqLite but you c…
Browse files Browse the repository at this point in the history
…an no longer specify which element you want to observe height changes for. It is now always the document height that is being observed.
  • Loading branch information
TueCN committed Apr 13, 2016
1 parent e53e61c commit 0ff6846
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 18 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,17 @@ config(function () {
```
<div id="parent">
<div id="sibling">I need some space too</div>
<div auto-height observe-height-of="parent">
<div auto-height="recalculate-on-css-class-changes">
I stretch to the available height,
calculated from the height available from parent and my siblings.
Also, if my parent changes height, I recalculate my height!
Also, if the document changes height because of css class changes, I recalculate my height!
</div>
</div>
```

This is useful when dynamically changing the height of elements (e.g. when collapsing/expanding sections using http://angular-ui.github.io/bootstrap/#/collapse)

_Note: This feature requires support of MutationObserver - see http://caniuse.com/#feat=mutationobserver_



## Community
Expand Down
2 changes: 1 addition & 1 deletion dist/auto-height.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 14 additions & 14 deletions src/auto-height.coffee
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
###*
# @version 0.0.5
# @version 0.1.0
# @copyright Emanuel Imhof [All Rights Reserved]
# @license MIT License (see LICENSE.txt)
###
angular.module('m43nu.auto-height', []).
directive 'autoHeight', [ '$window', '$timeout', ($window, $timeout) ->
link: ($scope, $element, $attrs) ->
observe = (observedEleId) ->
if (!observedEleId)
return
if($attrs.autoHeight == 'recalculate-on-css-class-changes')
$timeout ->
observee = if observedEleId == 'document' then $window.document else $('#' + observedEleId).get(0)
$observee = $(observee);
oldHeight = 0;
observer = new MutationObserver ->
currentHeight = $observee.height();
if (currentHeight != oldHeight)
angular.element($window).triggerHandler('resize');
oldHeight = currentHeight
if observee
options = {attributes: true,attributeFilter: ['class'], subtree: true}
observer.observe observee, options;
$timeout ->
doc = $window.document;
docEle = $window.document.documentElement;
currentHeight = Math.max(
doc.body["scrollHeight"], docEle["scrollHeight"],
doc.body["offsetHeight"], docEle["offsetHeight"],
docEle["clientHeight"] );
if (currentHeight != oldHeight)
angular.element( $window ).triggerHandler( 'resize' );
oldHeight = currentHeight
options = {attributes: true, attributeFilter: ['class'], subtree: true}
observer.observe $window.document.body, options;

combineHeights = (collection) ->
heights = 0
Expand All @@ -30,7 +31,6 @@ angular.module('m43nu.auto-height', []).
siblings = ($elm) ->
elm for elm in $elm.parent().children() when elm != $elm[0]

observe($attrs.observeHeightOf)
angular.element($window).bind 'resize', ->
additionalHeight = $attrs.additionalHeight || 0
parentHeight = $window.innerHeight - $element.parent()[0].getBoundingClientRect().top
Expand Down

0 comments on commit 0ff6846

Please sign in to comment.