Jump to content

MediaWiki:Common.js: Difference between revisions

From Iskomunidad
No edit summary
No edit summary
Line 327: Line 327:
             e.preventDefault();
             e.preventDefault();


             var buildingId = $(this).attr('data-building');
             var $link = $(this);
 
            var buildingId = $link.attr('data-building');
            var mapCategory = $link.attr('data-map');


             if (!buildingId) {
             if (!buildingId) {
                console.warn(
                    'Map building link is missing data-building.'
                );
                return;
            }
            if (!mapCategory) {
                console.warn(
                    'Map building link is missing data-map.'
                );
                return;
            }
            /*
            * Find the map container belonging to this category.
            *
            * Example:
            *
            * data-map="offices"
            *
            * matches:
            *
            * data-map-category="offices"
            */
            var $mapWrapper = $content.find(
                '.up-diliman-map[data-map-category="' +
                CSS.escape(mapCategory) +
                '"]'
            ).first();
            if (!$mapWrapper.length) {
                console.warn(
                    'No map found for category:',
                    mapCategory
                );
                return;
            }
            /*
            * Find the actual Leaflet map inside the wrapper.
            */
            var mapElement = $mapWrapper.find(
                '.maps-leaflet'
            ).first()[0];
            if (!mapElement) {
                console.warn(
                    'No Leaflet map found in category:',
                    mapCategory
                );
                 return;
                 return;
             }
             }


             if (typeof mapsLeafletList === 'undefined') {
            /*
                 console.warn('mapsLeafletList is not available.');
            * Find the corresponding Maps Leaflet object.
            */
            var mapsObject = null;
 
             if (typeof mapsLeafletList !== 'undefined') {
 
                $.each(mapsLeafletList, function (index, object) {
 
                    if (
                        object &&
                        object.map &&
                        object.map.getContainer &&
                        object.map.getContainer() === mapElement
                    ) {
 
                        mapsObject = object;
 
                        return false;
                    }
 
                });
            }
 
            if (!mapsObject || !mapsObject.map) {
 
                 console.warn(
                    'Could not find Leaflet map object for:',
                    mapCategory
                );
 
                 return;
                 return;
             }
             }


             var found = false;
             var map = mapsObject.map;
 
            /*
            * Find the GeoJSON marker.
            */
            var foundLayer = null;
            var foundLatLng = null;


             $.each(mapsLeafletList, function (index, mapsObject) {
             map.eachLayer(function (layer) {


                 if (!mapsObject || !mapsObject.map) {
                 if (foundLayer) {
                     return;
                     return;
                 }
                 }


                 var map = mapsObject.map;
                 if (
                    layer.feature &&
                    layer.feature.properties &&
                    layer.feature.properties.id === buildingId
                ) {


                map.eachLayer(function (layer) {
                    foundLayer = layer;


                     if (found) {
                    /*
                         return;
                    * Point marker
                    }
                    */
                     if (
                        typeof layer.getLatLng === 'function'
                    ) {
 
                         foundLatLng = layer.getLatLng();


                     if (
                     /*
                        layer.feature &&
                    * GeoJSON feature with bounds
                        layer.feature.properties &&
                    */
                         layer.feature.properties.id === buildingId
                    } else if (
                         typeof layer.getBounds === 'function'
                     ) {
                     ) {


                         found = true;
                         foundLatLng =
                            layer.getBounds().getCenter();
 
                    }
 
                }


                        var latLng = null;
            });


                        if (typeof layer.getLatLng === 'function') {
            if (!foundLayer || !foundLatLng) {
                            latLng = layer.getLatLng();
                        } else if (typeof layer.getBounds === 'function') {
                            latLng = layer.getBounds().getCenter();
                        }


                        if (!latLng) {
                console.warn(
                            return;
                    'No marker found for building:',
                        }
                    buildingId,
                    'in map:',
                    mapCategory
                );


                        var $mapContainer = $(map.getContainer());
                return;
            }


                        /*
            /*
                        * Find the map wrapper.
            * ----------------------------------------------------
                        *
            * Expand map if it is inside a collapsed container
                        * This works with your current:
            * ----------------------------------------------------
                        *
            */
                        * <div id="up-diliman-main-map">
                        *
                        * If the map is placed somewhere else later,
                        * the closest map container will still be used.
                        */
                        var $mapWrapper = $mapContainer.closest(
                            '#up-diliman-main-map'
                        );


                        /*
            var $collapsed = $mapWrapper.closest(
                        * If the map is inside a collapsed
                '.mw-collapsed'
                        * mw-collapsible table, expand it first.
            );
                        */
                        var $collapsed = $mapContainer.closest(
                            '.mw-collapsed'
                        );


                        if ($collapsed.length) {
            if ($collapsed.length) {


                            $collapsed
                $collapsed
                                .find('.mw-collapsible-toggle')
                    .find('.mw-collapsible-toggle')
                                .first()
                    .first()
                                .trigger('click');
                    .trigger('click');
                        }


                        /*
            }
                        * Scroll the page to the map.
                        *
                        * A small offset keeps the map from being
                        * hidden underneath a fixed site header.
                        */
                        if ($mapWrapper.length) {


                            $('html, body').animate({
            /*
                                scrollTop:
            * ----------------------------------------------------
                                    $mapWrapper.offset().top - 80
            * Scroll to the selected map
                            }, 500);
            * ----------------------------------------------------
            */


                        } else {
            var mapTop = $mapWrapper.offset().top;


                            $('html, body').animate({
            $('html, body').animate({
                                scrollTop:
                                    $mapContainer.offset().top - 80
                            }, 500);
                        }


                        /*
                scrollTop: mapTop - 80
                        * Give the map time to become visible after
                        * scrolling/expanding.
                        */
                        setTimeout(function () {


                            map.invalidateSize();
            }, 500);


                            map.setView(
            /*
                                latLng,
            * ----------------------------------------------------
                                Math.max(map.getZoom(), 17),
            * Wait for map to become visible and recalculate
                                {
            * its dimensions.
                                    animate: true
            * ----------------------------------------------------
                                }
            */
                            );


                            /*
            setTimeout(function () {
                            * Open the GeoJSON marker popup.
                            */
                            if (typeof layer.openPopup === 'function') {
                                layer.openPopup();
                            }


                        }, 600);
                map.invalidateSize();


                /*
                * Center the map on the selected building.
                */
                map.setView(
                    foundLatLng,
                    Math.max(map.getZoom(), 17),
                    {
                        animate: true
                     }
                     }
 
                 );
                 });


                 /*
                 /*
                 * Stop searching other maps after finding
                 * Open the marker popup.
                * the correct building.
                 */
                 */
                 if (found) {
                 if (
                     return false;
                    typeof foundLayer.openPopup === 'function'
                ) {
 
                     foundLayer.openPopup();
 
                 }
                 }


             });
             }, 600);
 
            if (!found) {
                console.warn(
                    'No map marker found for building ID:',
                    buildingId
                );
            }


         });
         });

Revision as of 01:09, 7 October 2026

/* For Main Page Announcement area //this code pulls from the https://ilc.upd.edu.ph/all-posts-and-announcements/ PAGE */
console.log("JS WORKING");

(function () {

    console.log("RUNNING IMMEDIATELY");

    const container =
        document.getElementById("ilc-announcements");

    console.log("Container:", container);

    if (!container) return;

    container.innerHTML = "Loading script...";

    fetch("https://ilc.upd.edu.ph/wp-json/wp/v2/posts?per_page=5")
        .then(res => res.json())
        .then(posts => {

            let html = "";

            posts.forEach(post => {
                html += `
                    <p>
                        <a href="${post.link}" target="_blank">
                            ${post.title.rendered}
                        </a>
                    </p>
                `;
            });

            container.innerHTML = html;

        })
        .catch(err => {
            console.error("FETCH ERROR:", err);
            container.innerHTML = "<p>Failed to load announcements</p>";
        });

})();
/*end of new code */

/* Any JavaScript here will be loaded for all users on every page load. */
$(window).on('load', function(){
	$('#site-tools ul').append("<li><a href='https://iskwiki.upd.edu.ph' target='_blank'>Flipbook</li>");
});


/* Helpdesk bubble */
$(function () {
    var bubble = $('<div id="ost-bubble">' +
    				'<img src="https://helpdesk.ilc.upd.edu.ph/images/ilc-images/Helpdesk-logo-bubble.png" alt="💬">' +
    				'</div>');

    var panel = $(
        '<div id="ost-panel">' +
            '<div id="ost-panel-header">' +
                '<img src="https://helpdesk.ilc.upd.edu.ph/logo.php" alt="Helpdesk" style="height:80px;"> ' +
                '<span id="ost-panel-close">&times;</span>' +
            '</div>' +
            '<div id="ost-panel-body">' +
                '<iframe src="https://helpdesk.ilc.upd.edu.ph/o.php"></iframe>' +
            '</div>' +
            '<div id="ost-panel-footer">' +
                '' +
            '</div>' +
        '</div>'
    );

    $('body').append(bubble).append(panel);

    bubble.on('click', function () {
        panel.toggleClass('ost-open');
    });

    panel.find('#ost-panel-close').on('click', function () {
        panel.removeClass('ost-open');
    });
});

/*Pop-up*/
$(function () {
    // Change this whenever you update the popup
    const popupVersion = "welcome-v1";

    // Only show once on initial visit
    if (localStorage.getItem(popupVersion)) {
        return;
    }
	
	//shows after browse time elapse
	 let elapsed = 0;
    const interval = 100; // 1 msecond

    const timer = setInterval(function () {
        if (!document.hidden) {
            elapsed += interval;
        }

        if (elapsed >= 6000) {
            clearInterval(timer);
            showPopup();
        }
    }, interval);
	
function showPopup() {
    const overlay = $(`
        <div id="welcome-overlay">
            <div id="welcome-popup">
                <button id="welcome-close">&times;</button>

                <a href="https://upsystemdiliman.qualtrics.com/jfe/form/SV_3r4gfKrUHQxo6c6" target="_blank">
                <img src="/images/Survey-link.png"
                alt="Survey Link" id="welcome-image">
				</a>
				
            </div>
        </div>
    `);

    $("body").append(overlay);

    function closePopup() {
        localStorage.setItem(popupVersion, "shown");
        overlay.fadeOut(300, function () {
            overlay.remove();
        });
    }

    $("#welcome-close").on("click", closePopup);
}
});

/* Maps */

/* UP Diliman Main Map */
(function () {
    'use strict';

    function restrictMap() {

        if (typeof window.mapsLeafletList === 'undefined') {
            return false;
        }

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

            var mapWrapper = mapsLeafletList[i];

            if (!mapWrapper || !mapWrapper.map) {
                continue;
            }

            var map = mapWrapper.map;
            var container = map.getContainer();

            if (!container.closest('#up-diliman-main-map')) {
                continue;
            }

            map.setMaxBounds([
                 [14.645, 121.060],
				 [14.665, 121.07514208501618]
            ]);

            map.options.maxBoundsViscosity = 1.0;

            return true;
        }

        return false;
    }

    var attempts = 0;

    var timer = setInterval(function () {
        attempts++;

        if (restrictMap() || attempts >= 50) {
            clearInterval(timer);
        }
    }, 100);

}());

/* End of UP Diliman Main Map */


/* UP Diliman Campus */
(function () {
    'use strict';

    function restrictMap() {

        if (typeof window.mapsLeafletList === 'undefined') {
            return false;
        }

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

            var mapWrapper = mapsLeafletList[i];

            if (!mapWrapper || !mapWrapper.map) {
                continue;
            }

            var map = mapWrapper.map;
            var container = map.getContainer();

            if (!container.closest('#up-diliman-campus')) {
                continue;
            }

            map.setMaxBounds([
                [14.640, 121.055],
                [14.665, 121.085]
            ]);

            map.options.maxBoundsViscosity = 1.0;

            return true;
        }

        return false;
    }

    var attempts = 0;

    var timer = setInterval(function () {
        attempts++;

        if (restrictMap() || attempts >= 50) {
            clearInterval(timer);
        }
    }, 100);

}());

/* End of UP Diliman Campus */

/*map recalculation for phones*/
(function () {
    'use strict';

    function resizeMaps() {
        if (typeof window.mapsLeafletList === 'undefined') {
            return;
        }

        for (var i = 0; i < mapsLeafletList.length; i++) {
            var mapWrapper = mapsLeafletList[i];

            if (mapWrapper && mapWrapper.map) {
                mapWrapper.map.invalidateSize();
            }
        }
    }

    window.addEventListener('resize', resizeMaps);

    setTimeout(resizeMaps, 300);
    setTimeout(resizeMaps, 1000);

}());


/*map collapse*/
$(function () {

    $('.mw-collapsible').on('afterExpand.mw-collapsible', function () {

        var collapsible = this;

        setTimeout(function () {

            $(collapsible).find('.maps-leaflet').each(function () {

                var mapElement = this;

                /*
                 * Find the corresponding Maps extension
                 * map object.
                 */
                mapsLeafletList.forEach(function (mapObject) {

                    if (mapObject[0] === mapElement) {

                        /*
                         * mapObject itself is the jQuery object
                         * containing the Leaflet map.
                         */
                        if (typeof mapObject.invalidateSize === 'function') {

                            mapObject.invalidateSize();

                        }

                    }

                });

            });

        }, 300);

    });

});

/*linking clickable building markers in GeoJson*/
/**
 * Clickable building names for Maps GeoJSON markers
 *
 * Example:
 * <span class="map-building-link" data-building="dilc">
 *     Interactive Learning Center
 * </span>
 */

mw.hook('wikipage.content').add(function ($content) {

    $content.find('.map-building-link')
        .off('click.mapBuilding')
        .on('click.mapBuilding', function (e) {

            e.preventDefault();

            var $link = $(this);

            var buildingId = $link.attr('data-building');
            var mapCategory = $link.attr('data-map');

            if (!buildingId) {
                console.warn(
                    'Map building link is missing data-building.'
                );
                return;
            }

            if (!mapCategory) {
                console.warn(
                    'Map building link is missing data-map.'
                );
                return;
            }

            /*
             * Find the map container belonging to this category.
             *
             * Example:
             *
             * data-map="offices"
             *
             * matches:
             *
             * data-map-category="offices"
             */
            var $mapWrapper = $content.find(
                '.up-diliman-map[data-map-category="' +
                CSS.escape(mapCategory) +
                '"]'
            ).first();

            if (!$mapWrapper.length) {

                console.warn(
                    'No map found for category:',
                    mapCategory
                );

                return;
            }

            /*
             * Find the actual Leaflet map inside the wrapper.
             */
            var mapElement = $mapWrapper.find(
                '.maps-leaflet'
            ).first()[0];

            if (!mapElement) {

                console.warn(
                    'No Leaflet map found in category:',
                    mapCategory
                );

                return;
            }

            /*
             * Find the corresponding Maps Leaflet object.
             */
            var mapsObject = null;

            if (typeof mapsLeafletList !== 'undefined') {

                $.each(mapsLeafletList, function (index, object) {

                    if (
                        object &&
                        object.map &&
                        object.map.getContainer &&
                        object.map.getContainer() === mapElement
                    ) {

                        mapsObject = object;

                        return false;
                    }

                });
            }

            if (!mapsObject || !mapsObject.map) {

                console.warn(
                    'Could not find Leaflet map object for:',
                    mapCategory
                );

                return;
            }

            var map = mapsObject.map;

            /*
             * Find the GeoJSON marker.
             */
            var foundLayer = null;
            var foundLatLng = null;

            map.eachLayer(function (layer) {

                if (foundLayer) {
                    return;
                }

                if (
                    layer.feature &&
                    layer.feature.properties &&
                    layer.feature.properties.id === buildingId
                ) {

                    foundLayer = layer;

                    /*
                     * Point marker
                     */
                    if (
                        typeof layer.getLatLng === 'function'
                    ) {

                        foundLatLng = layer.getLatLng();

                    /*
                     * GeoJSON feature with bounds
                     */
                    } else if (
                        typeof layer.getBounds === 'function'
                    ) {

                        foundLatLng =
                            layer.getBounds().getCenter();

                    }

                }

            });

            if (!foundLayer || !foundLatLng) {

                console.warn(
                    'No marker found for building:',
                    buildingId,
                    'in map:',
                    mapCategory
                );

                return;
            }

            /*
             * ----------------------------------------------------
             * Expand map if it is inside a collapsed container
             * ----------------------------------------------------
             */

            var $collapsed = $mapWrapper.closest(
                '.mw-collapsed'
            );

            if ($collapsed.length) {

                $collapsed
                    .find('.mw-collapsible-toggle')
                    .first()
                    .trigger('click');

            }

            /*
             * ----------------------------------------------------
             * Scroll to the selected map
             * ----------------------------------------------------
             */

            var mapTop = $mapWrapper.offset().top;

            $('html, body').animate({

                scrollTop: mapTop - 80

            }, 500);

            /*
             * ----------------------------------------------------
             * Wait for map to become visible and recalculate
             * its dimensions.
             * ----------------------------------------------------
             */

            setTimeout(function () {

                map.invalidateSize();

                /*
                 * Center the map on the selected building.
                 */
                map.setView(
                    foundLatLng,
                    Math.max(map.getZoom(), 17),
                    {
                        animate: true
                    }
                );

                /*
                 * Open the marker popup.
                 */
                if (
                    typeof foundLayer.openPopup === 'function'
                ) {

                    foundLayer.openPopup();

                }

            }, 600);

        });

});
/*end of map link*/