-
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.
Merge pull request #97 from Travelopia/feat/dynamic-nav-items
Dynamic lightbox navigation injection
- Loading branch information
Showing
2 changed files
with
57 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,56 @@ | ||
/** | ||
* Internal dependencies. | ||
*/ | ||
import { TPLightboxElement } from './tp-lightbox'; | ||
|
||
/** | ||
* TP Lightbox Nav. | ||
*/ | ||
export class TPLightboxNavElement extends HTMLElement { | ||
/** | ||
* Properties. | ||
*/ | ||
protected template: HTMLTemplateElement | null = null; | ||
protected lightbox : TPLightboxElement | null; | ||
|
||
/** | ||
* Constructor. | ||
*/ | ||
constructor() { | ||
// Initialize parent. | ||
super(); | ||
|
||
// Initialize properties. | ||
this.template = this.querySelector( 'template' ); | ||
this.lightbox = this.closest( 'tp-lightbox' ); | ||
|
||
// Add event listener. | ||
this.lightbox?.addEventListener( 'template-set', this.setTemplate.bind( this ) ); | ||
} | ||
|
||
/** | ||
* Set the template. | ||
*/ | ||
setTemplate(): void { | ||
// Bail if no template. | ||
if ( ! this.template ) { | ||
// Exit. | ||
return; | ||
} | ||
|
||
// Total slides. | ||
const totalSlides = Number( this.lightbox?.getAttribute( 'total' ) ?? 0 ); | ||
|
||
// Clear the navigation. | ||
this.innerHTML = ''; | ||
|
||
// Append the navigation items. | ||
for ( let i = 0; i < totalSlides; i++ ) { | ||
// Clone the template. | ||
const navItem = this.template.content.cloneNode( true ); | ||
|
||
// Append the clone. | ||
this.appendChild( navItem ); | ||
} | ||
} | ||
} |