Open card when following link

This commit is contained in:
Andras Schmelczer 2022-09-25 18:01:45 +02:00
parent a8a21a58fb
commit 0b754bf7ae
No known key found for this signature in database
GPG key ID: 0EA1BC97D0AB076E
3 changed files with 23 additions and 11 deletions

View file

@ -1,2 +1,2 @@
export const titleToFragment = (title: string): string => export const titleToFragment = (title: string): string =>
encodeURIComponent(title.toLocaleLowerCase().replace(/\W+/g, '-')); '#' + encodeURIComponent(title.toLocaleLowerCase().replace(/\W+/g, '-'));

View file

@ -8,7 +8,7 @@ export const generate = (
{ date, title, description, more, links }: TimelineElementParameters, { date, title, description, more, links }: TimelineElementParameters,
showMore: string showMore: string
): html => ` ): html => `
<article id="${titleToFragment(title)}" class="timeline-element"> <article id="${titleToFragment(title).replace('#', '')}" class="timeline-element">
<div class="line-container"> <div class="line-container">
<div class="line"></div> <div class="line"></div>
<p class="date">${date}</p> <p class="date">${date}</p>
@ -17,7 +17,7 @@ export const generate = (
<div class="figure"></div> <div class="figure"></div>
<div class="lower"> <div class="lower">
<h2> <h2>
<a href="#${titleToFragment(title)}">${title}</a> <a href="${titleToFragment(title)}">${title}</a>
</h2> </h2>
<p class="description">${description}</p> <p class="description">${description}</p>

View file

@ -1,4 +1,5 @@
import { createElement } from '../../helper/create-element'; import { createElement } from '../../helper/create-element';
import { titleToFragment } from '../../helper/title-to-fragment';
import { PageElement } from '../page-element'; import { PageElement } from '../page-element';
import { TimelineElementParameters } from './timeline-element-parameters'; import { TimelineElementParameters } from './timeline-element-parameters';
import { generate } from './timeline-element.html'; import { generate } from './timeline-element.html';
@ -8,50 +9,61 @@ export class TimelineElement extends PageElement {
private more: HTMLElement; private more: HTMLElement;
public constructor( public constructor(
timelineElement: TimelineElementParameters, private timelineElement: TimelineElementParameters,
private readonly showMore: string, private readonly showMore: string,
private readonly showLess: string private readonly showLess: string
) { ) {
super(createElement(generate(timelineElement, showMore))); super(createElement(generate(timelineElement, showMore)));
this.isOpen = false;
this.more = this.query('.more'); this.more = this.query('.more');
addEventListener('resize', this.handleResize.bind(this)); addEventListener('resize', this.handleResize.bind(this));
this.query('.info-button').addEventListener('click', this.toggleOpen.bind(this)); this.query('.info-button').addEventListener('click', this.toggleOpen.bind(this));
this.attachElementByReplacing( this.attachElementByReplacing(
'.figure', '.figure',
timelineElement.figure instanceof PageElement timelineElement.figure instanceof PageElement
? timelineElement.figure ? timelineElement.figure
: new PageElement(createElement(timelineElement.figure)) : new PageElement(createElement(timelineElement.figure))
); );
this.isOpen = false; }
protected initialize(): void {
super.initialize();
if (titleToFragment(this.timelineElement.title) === window.location.hash) {
this.openMore();
}
} }
private toggleOpen() { private toggleOpen() {
if (this.isOpen) { if (this.isOpen) {
this.query('.info-button p').innerText = this.showMore;
this.closeMore(); this.closeMore();
} else { } else {
this.query('.info-button p').innerText = this.showLess;
this.openMore(); this.openMore();
} }
this.isOpen = !this.isOpen;
} }
private openMore() { private openMore() {
this.isOpen = true;
this.query('.info-button > p').innerText = this.showLess;
const deltaHeight = this.more.scrollHeight; const deltaHeight = this.more.scrollHeight;
this.more.style.height = `${deltaHeight.toString()}px`; this.more.style.height = `${deltaHeight.toString()}px`;
} }
private closeMore() { private closeMore() {
this.isOpen = false;
this.query('.info-button > p').innerText = this.showMore;
this.more.style.height = '0'; this.more.style.height = '0';
} }
private handleResize() { private handleResize() {
if (this.isOpen) { if (this.isOpen) {
this.more.style.height = 'auto'; this.more.style.height = 'auto';
this.openMore.bind(this); this.openMore();
} }
} }
} }