eeditiones / tei-publisher-faq

Content of the TEI Publisher FAQ static website
https://faq.teipublisher.com/
4 stars 8 forks source link

Functionality enhancements of with JavaScript #15

Open GilShalit opened 2 years ago

GilShalit commented 2 years ago

Title

Functionality enhancements with JavaScript

Category

Maybe under Webcomponents?

Problem

Sometimes a webcomponent lacks a behavior or there is a need to change standard functionality. the documentation for webcomponents lists events they emit and it is possible to hook into those events to change or enhance the application.

Solution

General Steps
document.addEventListener("event-name", (event)=> {
    // specific code
});

The Benjamin of Tudela page has three versions of this traveler's log in English, Hebrew and Arabic. In the TEI-XML file, each narrative is built from tei:seg elements, grouped in tei:ab elements. The three versions are presented in three pb-view components, each showing one tei:ab element at a time. The segments of the different versions are modeled as pb-highlight webcomponents, and these are synchronized any time the mouse hovers over any pb-highlight, using the following method.

Concurrent pb-highlight components in all three versions have identical key values. This is used to synchronize them with the following listener to the pb-highlight-on event fired by the pb-highlight component when activated (through a hover action by default). See comments in the code.

document.addEventListener("pb-highlight-on", (event)=> {
    if (event.detail && event.detail.id && event.detail.source){  

        //the key is actually returned by id...
        let key=event.detail.id; 

        //Select the pb-views with the three versions
        let pbViews=document.querySelectorAll('pb-view[append-footnotes="append-footnotes"]'); 

        //loop through those pb-views
        pbViews.forEach(view => { 

            //in each version, find the corresponding pb-highlight
            let element=view.shadowRoot.querySelector('pb-highlight[key=' + key + ']'); 

            //only progress if corresponding element is found and only bring into view if not the element firing this event
            if (element && event.detail.source.id != element.id) 

                // bring corresponding elements to center of pb-view
                element.scrollIntoView({block: 'center' }); 
        });
    }
});