Jump to content

Widget:UPDateOnline: Difference between revisions

From Iskomunidad
No edit summary
No edit summary
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
<includeonly>
<includeonly>


<div class="update-news" id="update-online-news">
<div class="update-news">


     <div id="update-online-loading">
     <div class="update-online-loading">
         Loading latest news...
         Loading latest news...
     </div>
     </div>


     <div id="update-online-content" style="display:none;">
     <div class="update-online-content" style="display:none;">


        <!-- News image -->
         <div class="update-news-image">
         <div class="update-news-image">
             <img id="update-online-image"
             <img class="update-online-image"
                 src=""
                 src=""
                 alt="">
                 alt=""
                style="display:none;">
         </div>
         </div>


        <!-- Article information -->
         <div class="update-news-content">
         <div class="update-news-content">


             <div class="update-online-date"
             <div class="update-online-date"></div>
                id="update-online-date">
            </div>


             <h2 class="update-online-title"
             <h2 class="update-online-title"></h2>
                id="update-online-title">
            </h2>


             <div class="update-online-excerpt"
             <div class="update-online-excerpt"></div>
                id="update-online-excerpt">
            </div>


             <a class="update-online-link"
             <a class="update-online-link"
              id="update-online-link"
               href=""
               href=""
               target="_blank"
               target="_blank"
Line 50: Line 42:


     /*
     /*
     * Get the latest UPDate Online article.
     * --------------------------------
    * Find THIS widget instance
    * --------------------------------
    */
 
    var widgets =
        document.querySelectorAll('.update-news');
 
    var widget =
        widgets[widgets.length - 1];
 
    if (!widget) {
        return;
    }
 
 
    /*
    * --------------------------------
    * Find elements inside this widget
    * --------------------------------
    */
 
    var loading =
        widget.querySelector(
            '.update-online-loading'
        );
 
    var content =
        widget.querySelector(
            '.update-online-content'
        );
 
    var image =
        widget.querySelector(
            '.update-online-image'
        );
 
    var title =
        widget.querySelector(
            '.update-online-title'
        );
 
    var excerpt =
        widget.querySelector(
            '.update-online-excerpt'
        );
 
    var link =
        widget.querySelector(
            '.update-online-link'
        );
 
    var dateElement =
        widget.querySelector(
            '.update-online-date'
        );
 
 
    /*
    * --------------------------------
    * UPDate Online API
    * --------------------------------
     */
     */


Line 57: Line 110:
         '?categories=4&per_page=1';
         '?categories=4&per_page=1';


    /*
    * --------------------------------
    * Retrieve latest article
    * --------------------------------
    */


     fetch(api)
     fetch(api)


    .then(function (response) {
        .then(function (response) {


        if (!response.ok) {
            if (!response.ok) {
            throw new Error(
                throw new Error(
                'Unable to retrieve UPDate Online'
                    'Unable to retrieve UPDate Online'
            );
                );
        }
            }


        return response.json();
            return response.json();


    })
        })


    .then(function (posts) {


         if (!posts || !posts.length) {
         .then(function (posts) {
            throw new Error(
                'No UPDate Online article found'
            );
        }


        var post = posts[0];
            if (!posts || posts.length === 0) {
                throw new Error(
                    'No UPDate Online article found'
                );
            }




        /*
            var post =
        * --------------------------------
                posts[0];
        * TITLE
        * --------------------------------
        */


        document.getElementById(
            'update-online-title'
        ).innerHTML =
            post.title.rendered;


            /*
            * --------------------------------
            * TITLE
            * --------------------------------
            */


        /*
            title.innerHTML =
        * --------------------------------
                post.title.rendered;
        * EXCERPT
        * --------------------------------
        */


        document.getElementById(
            'update-online-excerpt'
        ).innerHTML =
            post.excerpt.rendered;


            /*
            * --------------------------------
            * EXCERPT
            * --------------------------------
            */


        /*
            excerpt.innerHTML =
        * --------------------------------
                post.excerpt.rendered;
        * ARTICLE LINK
        * --------------------------------
        */


        document.getElementById(
            'update-online-link'
        ).href =
            post.link;


            /*
            * --------------------------------
            * ARTICLE LINK
            * --------------------------------
            */


        /*
            link.href =
        * --------------------------------
                post.link;
        * DATE
        * --------------------------------
        */


        var date = new Date(post.date);


        document.getElementById(
            /*
            'update-online-date'
            * --------------------------------
        ).textContent =
            * DATE
            date.toLocaleDateString(
            * --------------------------------
                'en-US',
            */
                {
                    year: 'numeric',
                    month: 'long',
                    day: 'numeric'
                }
            );


            var articleDate =
                new Date(post.date);


        /*
        * --------------------------------
        * FIND FIRST IMAGE AFTER
        * THE FIRST PARAGRAPH
        * --------------------------------
        */


        var temp =
            dateElement.textContent =
            document.createElement('div');
                articleDate.toLocaleDateString(
                    'en-US',
                    {
                        year: 'numeric',
                        month: 'long',
                        day: 'numeric'
                    }
                );


        temp.innerHTML =
            post.content.rendered;


            /*
            * --------------------------------
            * ARTICLE CONTENT
            * --------------------------------
            */


        /*
            var article =
        * Find the first paragraph
                document.createElement('div');
        */
 
        var firstParagraph =
            temp.querySelector('p');
 


        var firstImage = null;


            article.innerHTML =
                post.content.rendered;


        if (firstParagraph) {


             /*
             /*
             * Look through everything that
             * --------------------------------
             * comes AFTER the first paragraph.
             * FIND FIRST PARAGRAPH
            * --------------------------------
             */
             */


             var current =
             var paragraphs =
                 firstParagraph.nextElementSibling;
                 article.querySelectorAll('p');
 


            var firstParagraph =
                null;


            while (current) {


                 /*
            for (
                * Check whether this element
                 var i = 0;
                * itself contains an image.
                i < paragraphs.length;
                */
                i++
            ) {


                 if (
                 if (
                     current.tagName &&
                     paragraphs[i]
                    current.tagName.toLowerCase() === 'img'
                        .textContent
                        .trim() !== ''
                 ) {
                 ) {


                     firstImage =
                     firstParagraph =
                         current;
                         paragraphs[i];


                     break;
                     break;
                }
            }


                }


            /*
            * --------------------------------
            * FIND FIRST IMAGE AFTER
            * FIRST PARAGRAPH
            * --------------------------------
            */


                 /*
            var selectedImage =
                * Check for an image inside
                 null;
                * this element.
                */


                var image =
                    current.querySelector('img');


            if (firstParagraph) {


                 if (image) {
                 var images =
                    article.querySelectorAll('img');


                    firstImage =
                        image;


                     break;
                for (
                     var j = 0;
                    j < images.length;
                    j++
                ) {


                }
                    var position =
                        firstParagraph.compareDocumentPosition(
                            images[j]
                        );




                current =
                    if (
                    current.nextElementSibling;
                        position &
            }
                        Node.DOCUMENT_POSITION_FOLLOWING
        }
                    ) {


                        selectedImage =
                            images[j];


        /*
                        break;
        * --------------------------------
                    }
        * DISPLAY THE IMAGE
        * --------------------------------
        */


        if (firstImage) {
                }


             var image =
             }
                document.getElementById(
                    'update-online-image'
                );




             /*
             /*
             * Use the actual image URL.
             * --------------------------------
            * DISPLAY IMAGE
            * --------------------------------
             */
             */


             image.src =
             if (selectedImage) {
                firstImage.src;


                var imageUrl =
                    selectedImage.getAttribute(
                        'src'
                    );


            /*
            * Use existing alt text if
            * available.
            */


            image.alt =
                if (!imageUrl) {
                firstImage.alt ||
 
                post.title.rendered;
                    imageUrl =
                        selectedImage.getAttribute(
                            'data-src'
                        );
 
                }
 
 
                if (!imageUrl) {
 
                    imageUrl =
                        selectedImage.getAttribute(
                            'data-lazy-src'
                        );
 
                }
 
 
                if (imageUrl) {
 
                    image.src =
                        imageUrl;
 
                    image.alt =
                        selectedImage.getAttribute(
                            'alt'
                        ) ||
                        post.title.rendered;
 
                    image.style.display =
                        'block';
 
                }


        }
            }


        else {


             /*
             /*
             * No image found after the
             * --------------------------------
             * opening paragraph.
             * SHOW CONTENT
            * --------------------------------
             */
             */


             document.querySelector(
             loading.style.display =
                '.update-news-image'
            ).style.display =
                 'none';
                 'none';


         }
            content.style.display =
                'block';
 
         })




         /*
         /*
         * --------------------------------
         * --------------------------------
         * SHOW ARTICLE
         * ERROR HANDLING
         * --------------------------------
         * --------------------------------
         */
         */


         document.getElementById(
         .catch(function (error) {
            'update-online-loading'
        ).style.display =
            'none';


            console.error(
                'UPDate Online Widget:',
                error
            );


        document.getElementById(
             loading.textContent =
            'update-online-content'
                'Unable to load the latest UPDate Online article.';
        ).style.display =
            'flex';
 
    })
 
 
    /*
    * --------------------------------
    * ERROR HANDLING
    * --------------------------------
    */
 
    .catch(function (error) {
 
        document.getElementById(
             'update-online-loading'
        ).innerHTML =
            'Unable to load the latest UPDate Online article.';
 
        console.error(error);


    });
        });


})();
})();
Line 316: Line 386:
== UPDate Online Widget ==
== UPDate Online Widget ==


Displays the latest UPDate Online article and
Displays the latest UPDate Online article
the first image appearing after the opening paragraph.
and the first image appearing after the
opening paragraph.


=== Usage ===
=== Usage ===

Latest revision as of 00:17, 7 October 2026



UPDate Online Widget

Displays the latest UPDate Online article and the first image appearing after the opening paragraph.

Usage

{{#widget:UPDateOnline}}