Jump to content

Widget:UPDateOnline: Difference between revisions

From Iskomunidad
No edit summary
No edit summary
 
(6 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;">


        <!-- 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 newest 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
    * --------------------------------
     */
     */


     var api =
     var api =
         'https://upd.edu.ph/wp-json/wp/v2/posts' +
         'https://upd.edu.ph/wp-json/wp/v2/posts' +
         '?categories=4&per_page=1&_embed=true';
         '?categories=4&per_page=1';


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


     fetch(api)
     fetch(api)


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


         if (!response.ok) {
         })
            throw new Error(
 
                'Unable to retrieve UPDate Online'
 
            );
        .then(function (posts) {
        }
 
            if (!posts || posts.length === 0) {
                throw new Error(
                    'No UPDate Online article found'
                );
            }
 
 
            var post =
                posts[0];
 
 
            /*
            * --------------------------------
            * TITLE
            * --------------------------------
            */
 
            title.innerHTML =
                post.title.rendered;
 
 
            /*
            * --------------------------------
            * EXCERPT
            * --------------------------------
            */
 
            excerpt.innerHTML =
                post.excerpt.rendered;
 
 
            /*
            * --------------------------------
            * ARTICLE LINK
            * --------------------------------
            */
 
            link.href =
                post.link;
 
 
            /*
            * --------------------------------
            * DATE
            * --------------------------------
            */


        return response.json();
            var articleDate =
                new Date(post.date);


    })


    .then(function (posts) {
            dateElement.textContent =
                articleDate.toLocaleDateString(
                    'en-US',
                    {
                        year: 'numeric',
                        month: 'long',
                        day: 'numeric'
                    }
                );


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


        var post = posts[0];
            /*
            * --------------------------------
            * ARTICLE CONTENT
            * --------------------------------
            */


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


        /*
        * TITLE
        */


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




        /*
            /*
        * EXCERPT
            * --------------------------------
        */
            * FIND FIRST PARAGRAPH
            * --------------------------------
            */


        document.getElementById(
            var paragraphs =
            'update-online-excerpt'
                article.querySelectorAll('p');
        ).innerHTML =
            post.excerpt.rendered;




        /*
            var firstParagraph =
        * ARTICLE LINK
                null;
        */


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


            for (
                var i = 0;
                i < paragraphs.length;
                i++
            ) {


        /*
                if (
        * DATE
                    paragraphs[i]
        */
                        .textContent
                        .trim() !== ''
                ) {


        var date = new Date(post.date);
                    firstParagraph =
                        paragraphs[i];


        document.getElementById(
                     break;
            'update-online-date'
        ).textContent =
            date.toLocaleDateString(
                'en-US',
                {
                     year: 'numeric',
                    month: 'long',
                    day: 'numeric'
                 }
                 }
             );
 
             }
 
 
            /*
            * --------------------------------
            * FIND FIRST IMAGE AFTER
            * FIRST PARAGRAPH
            * --------------------------------
            */
 
            var selectedImage =
                null;
 
 
            if (firstParagraph) {
 
                var images =
                    article.querySelectorAll('img');




        /*
                for (
        * FEATURED IMAGE
                    var j = 0;
        */
                    j < images.length;
                    j++
                ) {


        if (
                    var position =
            post._embedded &&
                        firstParagraph.compareDocumentPosition(
            post._embedded['wp:featuredmedia'] &&
                            images[j]
            post._embedded['wp:featuredmedia'].length
                        );
        ) {


            var media =
                post._embedded['wp:featuredmedia'][0];


            if (media.source_url) {
                    if (
                        position &
                        Node.DOCUMENT_POSITION_FOLLOWING
                    ) {


                var image =
                        selectedImage =
                    document.getElementById(
                            images[j];
                        'update-online-image'
                    );


                image.src =
                        break;
                     media.source_url;
                     }


                 image.alt =
                 }
                    post.title.rendered;


             }
             }


        } else {


             /*
             /*
             * If the article has no
             * --------------------------------
             * featured image, hide
             * DISPLAY IMAGE
             * the image container.
             * --------------------------------
             */
             */


             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
            * --------------------------------
        */
            * SHOW CONTENT
            * --------------------------------
            */


        document.getElementById(
             loading.style.display =
             'update-online-loading'
                'none';
        ).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 212: Line 380:


</includeonly>
</includeonly>


<noinclude>
<noinclude>
Line 217: Line 386:
== UPDate Online Widget ==
== UPDate Online Widget ==


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


Usage:
=== Usage ===


<pre>
<pre>

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}}