Jump to content

Widget:UPDateOnline: Difference between revisions

From Iskomunidad
No edit summary
No edit summary
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=""
Line 17: Line 16:
         </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 49: Line 40:
<script>
<script>
(function () {
(function () {
    /*
    * --------------------------------
    * 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'
        );


     /*
     /*
Line 60: Line 110:
         '?categories=4&per_page=1';
         '?categories=4&per_page=1';


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


     fetch(api)
     fetch(api)
Line 85: Line 141:




             var post = posts[0];
             var post =
                posts[0];




Line 94: Line 151:
             */
             */


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


Line 106: Line 161:
             */
             */


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


Line 118: Line 171:
             */
             */


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


Line 130: Line 181:
             */
             */


             var date =
             var articleDate =
                 new Date(post.date);
                 new Date(post.date);




             document.getElementById(
             dateElement.textContent =
                'update-online-date'
                 articleDate.toLocaleDateString(
            ).textContent =
                 date.toLocaleDateString(
                     'en-US',
                     'en-US',
                     {
                     {
Line 149: Line 198:
             /*
             /*
             * --------------------------------
             * --------------------------------
             * CREATE TEMPORARY ARTICLE DOM
             * ARTICLE CONTENT
             * --------------------------------
             * --------------------------------
             */
             */
Line 163: Line 212:
             /*
             /*
             * --------------------------------
             * --------------------------------
             * FIND FIRST NON-EMPTY PARAGRAPH
             * FIND FIRST PARAGRAPH
             * --------------------------------
             * --------------------------------
             */
             */
Line 171: Line 220:




             var firstParagraph = null;
             var firstParagraph =
                null;




Line 191: Line 241:
                     break;
                     break;
                 }
                 }
             }
             }


Line 196: Line 247:
             /*
             /*
             * --------------------------------
             * --------------------------------
             * FIND ALL IMAGES
             * FIND FIRST IMAGE AFTER
            * FIRST PARAGRAPH
             * --------------------------------
             * --------------------------------
             */
             */


             var images =
             var selectedImage =
                 article.querySelectorAll('img');
                 null;




             var selectedImage = null;
             if (firstParagraph) {


                var images =
                    article.querySelectorAll('img');


            /*
            * --------------------------------
            * FIND FIRST IMAGE AFTER
            * FIRST PARAGRAPH
            * --------------------------------
            */
            if (firstParagraph) {


                 for (
                 for (
Line 221: Line 267:
                     j++
                     j++
                 ) {
                 ) {
                    var image =
                        images[j];
                    /*
                    * Check whether this image
                    * appears after the paragraph.
                    */


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


Line 243: Line 280:


                         selectedImage =
                         selectedImage =
                             image;
                             images[j];


                         break;
                         break;
Line 260: Line 297:


             if (selectedImage) {
             if (selectedImage) {
                /*
                * WordPress image URL
                */


                 var imageUrl =
                 var imageUrl =
Line 270: Line 303:
                     );
                     );


                /*
                * Fallbacks for lazy-loaded images
                */


                 if (!imageUrl) {
                 if (!imageUrl) {
Line 294: Line 323:
                 }
                 }


                if (!imageUrl) {
                    imageUrl =
                        selectedImage.getAttribute(
                            'data-original'
                        );
                }
                /*
                * --------------------------------
                * SET IMAGE
                * --------------------------------
                */


                 if (imageUrl) {
                 if (imageUrl) {


                     var outputImage =
                     image.src =
                        document.getElementById(
                            'update-online-image'
                        );
 
 
                    outputImage.src =
                         imageUrl;
                         imageUrl;


 
                     image.alt =
                     outputImage.alt =
                         selectedImage.getAttribute(
                         selectedImage.getAttribute(
                             'alt'
                             'alt'
Line 329: Line 335:
                         post.title.rendered;
                         post.title.rendered;


 
                     image.style.display =
                     outputImage.style.display =
                         'block';
                         'block';


Line 340: Line 345:
             /*
             /*
             * --------------------------------
             * --------------------------------
             * SHOW ARTICLE
             * SHOW CONTENT
             * --------------------------------
             * --------------------------------
             */
             */


             document.getElementById(
             loading.style.display =
                'update-online-loading'
            ).style.display =
                 'none';
                 'none';


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


         })
         })
Line 367: Line 366:
         .catch(function (error) {
         .catch(function (error) {


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


         });
         });
Line 386: 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 ===

Revision as of 23:57, 6 October 2026



UPDate Online Widget

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

Usage

{{#widget:UPDateOnline}}