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() {
this.collapse = true; if (this.collapse === undefined) {
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,39 +77,50 @@ function excerpt($timeout) { ...@@ -69,39 +77,50 @@ function excerpt($timeout) {
return contentElem.scrollHeight > ctrl.collapsedHeight; return contentElem.scrollHeight > ctrl.collapsedHeight;
}; };
scope.$watch('vm.enabled()', function (isEnabled) { // init function which sets up watches that perform the initial
if (isEnabled) { // call to updateContentMaxHeight() to collapse the <excerpt> and
contentElem = elem[0].querySelector('.excerpt'); // alter the state if the <excerpt> is expanded/collapsed in future
function init() {
// trigger a recalculation of ctrl.overflowing() and properties scope.$watch('vm.enabled()', function (isEnabled) {
// which depend upon it when embedded media loads. if (isEnabled) {
// contentElem = elem[0].querySelector('.excerpt');
// In future we might wish to trigger checking for other events
// outside of Angular's knowledge as well, eg. loading of embedded // trigger a recalculation of ctrl.overflowing() and properties
// media // which depend upon it when embedded media loads.
contentElem.addEventListener('load', checkForOverflowChange, //
true /* capture. 'load' events do not bubble */); // In future we might wish to trigger checking for other events
// outside of Angular's knowledge as well, eg. loading of embedded
// media
contentElem.addEventListener('load', checkForOverflowChange,
true /* capture. 'load' events do not bubble */);
} else {
contentElem = undefined;
}
});
scope.$watch('vm.collapse', function (isCollapsed) {
if (!contentElem) {
return;
}
updateContentMaxHeight(); updateContentMaxHeight();
} else { });
contentElem = undefined;
}
});
scope.$watch('vm.collapse', function (isCollapsed) { scope.$watch('vm.overflowing()', function () {
if (!contentElem) { if (ctrl.onCollapsibleChanged) {
return; ctrl.onCollapsibleChanged({collapsible: ctrl.overflowing()});
} }
updateContentMaxHeight(); });
}); }
scope.$watch('vm.overflowing()', function () { // run the init() function asynchronously so that any
if (ctrl.onCollapsibleChanged) { // directives in the transcluded content of the DOM have been
ctrl.onCollapsibleChanged({collapsible: ctrl.overflowing()}); // 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