Commit 7aaad32f authored by Robert Knight's avatar Robert Knight

Run <excerpt> initialization asynchronously

Run initialization of <excerpt> asynchronously so that any
directives inside its children have been resolved by the time
updateContentMaxHeight() is called.

In the context of the annotation list, the maxHeight
style property of the content element was set to 0 for the
annotation quotes because the 'ngRepeat' directive which
creates the DOM elements for the quotes had not run by the time
that updateContentMaxHeight() was called.
parent 5f80a473
'use strict'; 'use strict';
function ExcerptController() { function ExcerptController() {
if (this.collapse === undefined) {
this.collapse = true; this.collapse = true;
}
if (this.animate === undefined) {
this.animate = true;
}
this.enabled = this.enabled || function () { this.enabled = this.enabled || function () {
return true; return true;
...@@ -42,7 +48,7 @@ function toPx(val) { ...@@ -42,7 +48,7 @@ function toPx(val) {
* and collapsing the resulting truncated element. * and collapsing the resulting truncated element.
*/ */
// @ngInject // @ngInject
function excerpt($timeout) { function excerpt() {
return { return {
bindToController: true, bindToController: true,
controller: ExcerptController, controller: ExcerptController,
...@@ -52,13 +58,15 @@ function excerpt($timeout) { ...@@ -52,13 +58,15 @@ function excerpt($timeout) {
function checkForOverflowChange() { function checkForOverflowChange() {
scope.$digest(); scope.$digest();
updateContentMaxHeight();
} }
function updateContentMaxHeight() { function updateContentMaxHeight() {
if (ctrl.collapse) { if (ctrl.collapse) {
contentElem.style.maxHeight = toPx(ctrl.collapsedHeight); contentElem.style.maxHeight = toPx(ctrl.collapsedHeight);
} else { } else {
contentElem.style.maxHeight = toPx(contentElem.scrollHeight); contentElem.style.maxHeight =
ctrl.animate ? toPx(contentElem.scrollHeight) : '';
} }
} }
...@@ -69,6 +77,10 @@ function excerpt($timeout) { ...@@ -69,6 +77,10 @@ function excerpt($timeout) {
return contentElem.scrollHeight > ctrl.collapsedHeight; return contentElem.scrollHeight > ctrl.collapsedHeight;
}; };
// init function which sets up watches that perform the initial
// call to updateContentMaxHeight() to collapse the <excerpt> and
// alter the state if the <excerpt> is expanded/collapsed in future
function init() {
scope.$watch('vm.enabled()', function (isEnabled) { scope.$watch('vm.enabled()', function (isEnabled) {
if (isEnabled) { if (isEnabled) {
contentElem = elem[0].querySelector('.excerpt'); contentElem = elem[0].querySelector('.excerpt');
...@@ -81,8 +93,6 @@ function excerpt($timeout) { ...@@ -81,8 +93,6 @@ function excerpt($timeout) {
// media // media
contentElem.addEventListener('load', checkForOverflowChange, contentElem.addEventListener('load', checkForOverflowChange,
true /* capture. 'load' events do not bubble */); true /* capture. 'load' events do not bubble */);
updateContentMaxHeight();
} else { } else {
contentElem = undefined; contentElem = undefined;
} }
...@@ -100,8 +110,17 @@ function excerpt($timeout) { ...@@ -100,8 +110,17 @@ function excerpt($timeout) {
ctrl.onCollapsibleChanged({collapsible: ctrl.overflowing()}); ctrl.onCollapsibleChanged({collapsible: ctrl.overflowing()});
} }
}); });
}
// run the init() function asynchronously so that any
// directives in the transcluded content of the DOM have been
// processed by the time the first call to updateContentMaxHeight()
// happens.
scope.$evalAsync(init);
}, },
scope: { scope: {
/** Whether or not expansion should be animated. Defaults to true. */
animate: '=',
/** Whether or not truncation should be enabled */ /** Whether or not truncation should be enabled */
enabled: '&?', enabled: '&?',
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment