-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GH-321: Resize Callout image to parent height
CAVEAT: The addition to 0001 migration was manual and is untested.
- Loading branch information
1 parent
acefadf
commit 9767d45
Showing
5 changed files
with
116 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
taccsite_cms/static/site_cms/js/modules/elementTransformer.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/** | ||
* Resize content to fit parent height | ||
* | ||
* - Manipulates attributes within existing markup. | ||
* - Size is NOT dynamically updated after initial load. | ||
* @module elementTransformer | ||
*/ | ||
|
||
/** Resize first child element to match parent height (track state in markup) */ | ||
export class SizeContentToFit { | ||
|
||
|
||
|
||
// FAQ: Offers clarity of intent and consistency with state attr. | ||
// NOTE: Could faciliate programmatic transforms (like as a plugin option) | ||
/** A suggested selector to match the container */ | ||
static containerSelector = '[data-transform="size-content-to-fit"]'; | ||
|
||
|
||
|
||
/** | ||
* Initialize and resize | ||
* @param {HTMLElement} container - The direct parent of the content to resize | ||
*/ | ||
constructor (container) { | ||
/** The `HTMLElement` containing the content to resize */ | ||
this.container = container; | ||
/** The `HTMLElement` to resize */ | ||
this.content = container.querySelector(':scope > *'); | ||
|
||
// GH-320: Test whether `this.content` was in the DOM at runtime | ||
// FAQ: Use `cloneNode` to NOT watch element reference that is updated later | ||
// console.log({ | ||
// container: this.container.cloneNode(true), | ||
// content: this.content.cloneNode(true) | ||
// }); | ||
|
||
this.resizeContent(); | ||
} | ||
|
||
|
||
|
||
/** Mark transformation as in the given state */ | ||
setState(state) { | ||
this.container.dataset.transformState = state; | ||
} | ||
|
||
/** Mark transformation as NOT in the given state */ | ||
removeState(state) { | ||
// NOTE: Multiple states are not supported, so there is no use for `state` | ||
this.container.dataset.transformState = null; | ||
} | ||
|
||
/** Whether transformation is in given state */ | ||
isState(state) { | ||
return (this.container.dataset.transformState == state); | ||
} | ||
|
||
/** Whether to resize the content */ | ||
shouldResizeContent() { | ||
if (this.container.getAttribute('hidden') !== null) { | ||
this.content.style.offsetHeight = '0'; | ||
this.container.removeAttribute('hidden'); | ||
} | ||
} | ||
|
||
|
||
/** Resize the content */ | ||
resizeContent() { | ||
/* To prevent natural height of content from increasing container height */ | ||
/* FAQ: Module will set wrong height if content is taller than is desired */ | ||
if (this.container.getAttribute('hidden') !== null) { | ||
this.content.style.height = '0'; | ||
this.container.removeAttribute('hidden'); | ||
} | ||
|
||
/* To inform observers that this module is active */ | ||
this.setState('resizing-content'); | ||
|
||
/* To make container (and its content) the same height as a root element */ | ||
/* FAQ: Given tall content, figure height equals content height */ | ||
/* FAQ: Given hidden content, figure height equals desired content height */ | ||
this.container.style.height = '100%'; | ||
this.content.style.height = this.container.offsetHeight + 'px'; | ||
this.container.style.height = null; | ||
|
||
/* To clean up mess (only if it appears to be the mess of this module) */ | ||
if (this.isState('resizing-content')) { | ||
this.removeState('resizing-content'); | ||
if ( ! this.container.getAttribute('style')) { | ||
this.container.removeAttribute('style'); | ||
} | ||
} | ||
|
||
/* To inform observers that this module is done */ | ||
this.setState('complete'); | ||
} | ||
} |