Jump to content

Widget:UPDateOnline: Difference between revisions

From Iskomunidad
No edit summary
No edit summary
 
(5 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
     * We do NOT rely on the featured image.
    * --------------------------------
     * Instead, we retrieve the article content
    */
     * and find the first image inside it.
 
    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 61: 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.toLocaleDateString(
                'en-US',
                {
                    year: 'numeric',
                    month: 'long',
                    day: 'numeric'
                }
            );


            /*
            * --------------------------------
            * DATE
            * --------------------------------
            */


        /*
            var articleDate =
        * -----------------------------
                new Date(post.date);
        * FIND FIRST ARTICLE IMAGE
        * -----------------------------
        */


        var temp =
            document.createElement('div');


        temp.innerHTML =
            dateElement.textContent =
            post.content.rendered;
                articleDate.toLocaleDateString(
                    'en-US',
                    {
                        year: 'numeric',
                        month: 'long',
                        day: 'numeric'
                    }
                );




        var firstImage =
            /*
            temp.querySelector('img');
            * --------------------------------
            * ARTICLE CONTENT
            * --------------------------------
            */


            var article =
                document.createElement('div');


        if (firstImage) {


             var image =
             article.innerHTML =
                 document.getElementById(
                 post.content.rendered;
                    'update-online-image'
                );




             /*
             /*
             * Get image URL
             * --------------------------------
            * FIND FIRST PARAGRAPH
            * --------------------------------
             */
             */


             image.src =
             var paragraphs =
                 firstImage.src;
                article.querySelectorAll('p');
 
 
            var firstParagraph =
                null;
 
 
            for (
                var i = 0;
                i < paragraphs.length;
                i++
            ) {
 
                 if (
                    paragraphs[i]
                        .textContent
                        .trim() !== ''
                ) {
 
                    firstParagraph =
                        paragraphs[i];
 
                    break;
                }
 
            }




             /*
             /*
             * Use the article's
             * --------------------------------
             * image alt text if available.
             * FIND FIRST IMAGE AFTER
            * FIRST PARAGRAPH
            * --------------------------------
             */
             */


             image.alt =
             var selectedImage =
                firstImage.alt ||
                null;
                 post.title.rendered;
 
 
            if (firstParagraph) {
 
                var images =
                    article.querySelectorAll('img');
 
 
                for (
                    var j = 0;
                    j < images.length;
                    j++
                 ) {
 
                    var position =
                        firstParagraph.compareDocumentPosition(
                            images[j]
                        );
 
 
                    if (
                        position &
                        Node.DOCUMENT_POSITION_FOLLOWING
                    ) {
 
                        selectedImage =
                            images[j];
 
                        break;
                    }
 
                }


            }


        } else {


             /*
             /*
             * No image found.
             * --------------------------------
             * Hide image container.
             * DISPLAY IMAGE
            * --------------------------------
             */
             */


             document.querySelector(
             if (selectedImage) {
                 '.update-news-image'
 
            ).style.display =
                var imageUrl =
                'none';
                    selectedImage.getAttribute(
                        'src'
                    );
 
 
                 if (!imageUrl) {
 
                    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';
                }
            }


        /*
        * -----------------------------
        * SHOW ARTICLE
        * -----------------------------
        */


        document.getElementById(
            /*
            'update-online-loading'
            * --------------------------------
        ).style.display =
            * SHOW CONTENT
            'none';
            * --------------------------------
            */


            loading.style.display =
                'none';


        document.getElementById(
             content.style.display =
             'update-online-content'
                'block';
        ).style.display =
            'flex';


    })
        })




    /*
        /*
    * -----------------------------
        * --------------------------------
    * ERROR HANDLING
        * ERROR HANDLING
    * -----------------------------
        * --------------------------------
    */
        */


    .catch(function (error) {
        .catch(function (error) {


        document.getElementById(
            console.error(
            'update-online-loading'
                'UPDate Online Widget:',
        ).innerHTML =
                error
             'Unable to load the latest UPDate Online article.';
             );


        console.error(error);
            loading.textContent =
                'Unable to load the latest UPDate Online article.';


    });
        });


})();
})();
Line 243: Line 380:


</includeonly>
</includeonly>


<noinclude>
<noinclude>
Line 248: 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 in the article.
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}}