Jump to content

Widget:UPDateOnline: Difference between revisions

From Iskomunidad
No edit summary
No edit summary
 
(2 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=""
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 51: 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 58: Line 110:
         '?categories=4&per_page=1';
         '?categories=4&per_page=1';


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


     fetch(api)
     fetch(api)
Line 76: Line 134:
         .then(function (posts) {
         .then(function (posts) {


             if (!posts || !posts.length) {
             if (!posts || posts.length === 0) {
                 throw new Error(
                 throw new Error(
                     'No UPDate Online article found'
                     'No UPDate Online article found'
Line 83: Line 141:




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




Line 92: Line 151:
             */
             */


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


Line 104: Line 161:
             */
             */


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


Line 116: Line 171:
             */
             */


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


Line 128: Line 181:
             */
             */


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


            document.getElementById(
 
                'update-online-date'
             dateElement.textContent =
             ).textContent =
                 articleDate.toLocaleDateString(
                 date.toLocaleDateString(
                     'en-US',
                     'en-US',
                     {
                     {
Line 146: Line 198:
             /*
             /*
             * --------------------------------
             * --------------------------------
             * FIND THE FIRST IMAGE AFTER
             * ARTICLE CONTENT
            * THE FIRST NON-EMPTY PARAGRAPH
             * --------------------------------
             * --------------------------------
             */
             */
Line 153: Line 204:
             var article =
             var article =
                 document.createElement('div');
                 document.createElement('div');


             article.innerHTML =
             article.innerHTML =
Line 159: Line 211:


             /*
             /*
             * Find all paragraphs
             * --------------------------------
            * FIND FIRST PARAGRAPH
            * --------------------------------
             */
             */


Line 166: Line 220:




             var firstParagraph = null;
             var firstParagraph =
                null;


            /*
            * Find the first paragraph
            * containing actual text
            */


             for (
             for (
Line 183: Line 233:
                     paragraphs[i]
                     paragraphs[i]
                         .textContent
                         .textContent
                         .trim()
                         .trim() !== ''
                        .length > 0
                 ) {
                 ) {


Line 192: Line 241:
                     break;
                     break;
                 }
                 }
             }
             }




             /*
             /*
             * 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 the first image that occurs
            * AFTER the first paragraph
            */
            if (firstParagraph) {


                 for (
                 for (
Line 218: Line 267:
                     j++
                     j++
                 ) {
                 ) {
                    var image =
                        images[j];


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


                    /*
                    * 4 means the image is
                    * following the paragraph
                    */


                     if (
                     if (
Line 240: Line 280:


                         selectedImage =
                         selectedImage =
                             image;
                             images[j];


                         break;
                         break;
                     }
                     }
                 }
                 }
             }
             }


Line 250: Line 292:
             /*
             /*
             * --------------------------------
             * --------------------------------
             * DISPLAY THE IMAGE
             * DISPLAY IMAGE
             * --------------------------------
             * --------------------------------
             */
             */


             if (selectedImage) {
             if (selectedImage) {
                /*
                * WordPress normally uses src,
                * but check several possibilities
                * for future articles.
                */


                 var imageUrl =
                 var imageUrl =
                     selectedImage.getAttribute('src') ||
                     selectedImage.getAttribute(
                    selectedImage.getAttribute('data-src') ||
                        'src'
                     selectedImage.getAttribute('data-lazy-src') ||
                     );
                    selectedImage.getAttribute('data-original');


                /*
                * If there is no src, try srcset
                */


                 if (!imageUrl) {
                 if (!imageUrl) {


                     var srcset =
                     imageUrl =
                         selectedImage.getAttribute('srcset') ||
                         selectedImage.getAttribute(
                        selectedImage.getAttribute('data-srcset');
                            'data-src'
                        );


                }


                    if (srcset) {


                        /*
                if (!imageUrl) {
                        * Take the largest image
                        * from the srcset.
                        */
 
                        var sources =
                            srcset.split(',');
 
 
                        var lastSource =
                            sources[sources.length - 1]
                                .trim();


                    imageUrl =
                        selectedImage.getAttribute(
                            'data-lazy-src'
                        );


                        imageUrl =
                            lastSource.split(' ')[0];
                    }
                 }
                 }


                /*
                * Make sure the image URL is
                * absolute.
                */


                 if (imageUrl) {
                 if (imageUrl) {


                     if (
                     image.src =
                        imageUrl.indexOf('http://') !== 0 &&
                        imageUrl.indexOf('https://') !== 0
                    ) {
 
                        imageUrl =
                            new URL(
                                imageUrl,
                                'https://upd.edu.ph'
                            ).href;
                    }
 
 
                    var outputImage =
                        document.getElementById(
                            'update-online-image'
                        );
 
 
                    outputImage.src =
                         imageUrl;
                         imageUrl;


 
                     image.alt =
                     outputImage.alt =
                         selectedImage.getAttribute(
                         selectedImage.getAttribute('alt') ||
                            'alt'
                        ) ||
                         post.title.rendered;
                         post.title.rendered;


                    image.style.display =
                        'block';


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


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


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


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


         })
         })
Line 372: 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 391: 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}}