MediaWiki:Common.js: Difference between revisions

From Doc-Wiki
Jump to navigation Jump to search
Updated gallery to link to wiki pages with dynamic thumbnail loading
Added support for Simple Nursing Videos category gallery
 
(8 intermediate revisions by the same user not shown)
Line 3: Line 3:




/* YouTube Playlists Gallery - Final Version */
 
 
 
 
 
 
 
 
 
/* Level Up RN, Nursing.com & Simple Nursing - Video Galleries and Embeds */
(function() {
(function() {
     'use strict';
     'use strict';
    // Only run on Level Up RN Videos category pages
    var pageName = mw.config.get('wgPageName');
    if (pageName !== 'Category:Level_Up_RN_Videos' && pageName !== 'Category:Level_Up_RN') return;


     mw.loader.using(['jquery'], function() {
     mw.loader.using(['jquery'], function() {
         $(document).ready(function() {
         $(document).ready(function() {
            // Find the playlists table and transform it
            var $table = $('.wikitable.sortable').first();
            if (!$table.length) return;


             var $gallery = $('<div class="playlists-gallery"></div>');
             // Create working iframe embeds for individual video pages
            $('.youtube-video-embed[data-video-id]').each(function() {
                var $container = $(this);
                var videoId = $container.attr('data-video-id');


            $table.find('tr').each(function(index) {
                if (videoId) {
                if (index === 0) return; // Skip header
                    var iframe = $('<iframe>')
                        .attr({
                            'src': 'https://www.youtube.com/embed/' + videoId,
                            'frameborder': '0',
                            'allow': 'accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share',
                            'allowfullscreen': '',
                            'referrerpolicy': 'strict-origin-when-cross-origin'
                        })
                        .css({
                            'position': 'absolute',
                            'top': '0',
                            'left': '0',
                            'width': '100%',
                            'height': '100%'
                        });


                var $row = $(this);
                    $container.empty().append(iframe);
                 var $cells = $row.find('td');
                 }
            });


                 if ($cells.length < 3) return;
            // Transform playlist videos container into gallery
            $('.playlist-videos-container').each(function() {
                 var $container = $(this);
                var $gallery = $('<div class="video-cards-gallery"></div>');


                 var $titleCell = $cells.eq(0);
                 $container.find('.video-item').each(function() {
                var $descCell = $cells.eq(1);
                    var $item = $(this);
                var $linkCell = $cells.eq(2);
                    var videoId = $item.attr('data-video-id');
                    var pageUrl = $item.attr('data-page-url');
                    var videoTitle = $item.attr('data-title');


                // Get playlist ID from data attribute
                    if (!videoId || !pageUrl || !videoTitle) return;
                var playlistId = $titleCell.attr('data-playlist-id');


                var $titleLink = $titleCell.find('a').first();
                    var $card = $('<div class="video-card"></div>');
                var wikiPageUrl = $titleLink.attr('href');
                var title = $titleLink.text();
                var description = $descCell.text().trim();


                var $watchLink = $linkCell.find('a').first();
                    var $thumb = $('<div class="video-card-thumb"></div>');
                    var $thumbLink = $('<a></a>').attr('href', pageUrl);


                if (!playlistId || !wikiPageUrl) return;
                    var $thumbImg = $('<img>')
                        .attr('src', 'https://i.ytimg.com/vi/' + videoId + '/hqdefault.jpg')
                        .attr('alt', videoTitle)
                        .css({
                            'width': '100%',
                            'height': '100%',
                            'object-fit': 'cover'
                        });


                // Create box
                    var $playButton = $('<div class="video-card-play"></div>');
                var $box = $('<div class="playlist-box"></div>');
                var $thumb = $('<div class="playlist-box-thumb"></div>');
                var $thumbLink = $('<a></a>').attr('href', wikiPageUrl);


                // Create image with fallback chain
                    $thumbLink.append($thumbImg).append($playButton);
                var $img = $('<img>')
                    $thumb.append($thumbLink);
                    .attr('alt', title)
                    .css({'width': '100%', 'height': '100%', 'object-fit': 'cover'})
                    .on('error', function() {
                        // If first attempt fails, try a different thumbnail
                        var currentSrc = $(this).attr('src');
                        if (currentSrc.indexOf('maxresdefault') !== -1) {
                            $(this).attr('src', 'https://i.ytimg.com/vi/' + playlistId + '/hqdefault.jpg');
                        } else if (currentSrc.indexOf('hqdefault') !== -1) {
                            $(this).attr('src', 'https://i.ytimg.com/vi/' + playlistId + '/mqdefault.jpg');
                        } else {
                            // Last resort: show a placeholder
                            $(this).replaceWith('<div style="width:100%;height:100%;background:#ccc;display:flex;align-items:center;justify-content:center;color:#666;font-size:48px;">▶</div>');
                        }
                    });


                // Try to fetch thumbnail using YouTube's img service
                    var $info = $('<div class="video-card-info"></div>');
                // We'll try the playlist thumbnail first
                    var $titleLink = $('<a></a>')
                fetchPlaylistThumbnail(playlistId, $img);
                        .attr('href', pageUrl)
                        .text(videoTitle);
                    $info.append($titleLink);


                $thumbLink.append($img);
                    $card.append($thumb).append($info);
                $thumbLink.append('<div class="playlist-box-overlay">▶ Playlist</div>');
                    $gallery.append($card);
                $thumb.append($thumbLink);
                });


                 var $info = $('<div class="playlist-box-info"></div>');
                 if ($gallery.children().length > 0) {
                $info.append('<div class="playlist-box-title">' + title + '</div>');
                     $container.replaceWith($gallery);
                if (description) {
                     $info.append('<div style="margin: 8px 0; font-size: 13px; color: #666;">' + description + '</div>');
                 }
                 }
                $info.append('<a href="' + wikiPageUrl + '" class="playlist-box-link">Watch Playlist →</a>');
            });


                 $box.append($thumb).append($info);
            // Transform category page playlist gallery (works for all nursing categories)
                $gallery.append($box);
            var pageName = mw.config.get('wgPageName');
            });
            if (pageName === 'Category:Level_Up_RN_Videos' ||
                 pageName === 'Category:Level_Up_RN' ||
                pageName === 'Category:Nursing.com_Videos' ||
                pageName === 'Category:Simple_Nursing_Videos') {
 
                var $table = $('.wikitable.sortable').first();
                if (!$table.length) return;
 
                var $gallery = $('<div class="playlists-gallery"></div>');
 
                $table.find('tr').each(function(index) {
                    if (index === 0) return;
 
                    var $row = $(this);
                    var $cells = $row.find('td');
                    if ($cells.length < 3) return;
 
                    var $titleCell = $cells.eq(0);
                    var $descCell = $cells.eq(1);
 
                    var playlistId = $titleCell.attr('data-playlist-id');
                    var $titleLink = $titleCell.find('a').first();
                    var wikiPageUrl = $titleLink.attr('href');
                    var title = $titleLink.text();
                    var description = $descCell.text().trim();


            if ($gallery.children().length > 0) {
                    if (!playlistId || !wikiPageUrl) return;
                $table.replaceWith($gallery);
            }


            function fetchPlaylistThumbnail(playlistId, $img) {
                    var $box = $('<div class="playlist-box"></div>');
                // Try to get thumbnail from YouTube's oembed service
                var oembedUrl = 'https://www.youtube.com/oembed?url=https://www.youtube.com/playlist?list=' + playlistId + '&format=json';


                $.ajax({
                    // Make the entire box clickable
                    url: oembedUrl,
                    $box.css('cursor', 'pointer').on('click', function(e) {
                    dataType: 'jsonp',
                         if (!$(e.target).is('a')) {
                    success: function(data) {
                             window.location.href = wikiPageUrl;
                         if (data && data.thumbnail_url) {
                            $img.attr('src', data.thumbnail_url);
                        } else {
                             // Fallback to standard format
                            $img.attr('src', 'https://i.ytimg.com/vi/' + playlistId + '/maxresdefault.jpg');
                         }
                         }
                     },
                     });
                     error: function() {
 
                        // Fallback: use playlist ID as video ID (may or may not work)
                     var $thumb = $('<div class="playlist-box-thumb"></div>');
                         $img.attr('src', 'https://i.ytimg.com/vi/' + playlistId + '/maxresdefault.jpg');
 
                    // Use YouTube iframe for thumbnail with pointer-events: none
                    var $thumbFrame = $('<iframe>')
                         .attr({
                            'src': 'https://www.youtube.com/embed/videoseries?list=' + playlistId + '&controls=0&showinfo=0&rel=0&modestbranding=1',
                            'frameborder': '0',
                            'loading': 'lazy'
                        })
                        .css({
                            'position': 'absolute',
                            'top': '0',
                            'left': '0',
                            'width': '100%',
                            'height': '100%',
                            'pointer-events': 'none'
                        });
 
                    $thumb.append($thumbFrame);
                    $thumb.append('<div class="playlist-box-overlay">▶ Playlist</div>');
 
                    var $info = $('<div class="playlist-box-info"></div>');
                    $info.append('<div class="playlist-box-title">' + title + '</div>');
                    if (description) {
                        $info.append('<div style="margin: 8px 0; font-size: 13px; color: #666;">' + description + '</div>');
                     }
                     }
                    $info.append('<a href="' + wikiPageUrl + '" class="playlist-box-link" onclick="event.stopPropagation();">Watch Playlist →</a>');
                    $box.append($thumb).append($info);
                    $gallery.append($box);
                 });
                 });
                if ($gallery.children().length > 0) {
                    $table.replaceWith($gallery);
                }
             }
             }
         });
         });
     });
     });
})();
})();

Latest revision as of 21:54, 17 January 2026

/* Any JavaScript here will be loaded for all users on every page load. */












/* Level Up RN, Nursing.com & Simple Nursing - Video Galleries and Embeds */
(function() {
    'use strict';

    mw.loader.using(['jquery'], function() {
        $(document).ready(function() {

            // Create working iframe embeds for individual video pages
            $('.youtube-video-embed[data-video-id]').each(function() {
                var $container = $(this);
                var videoId = $container.attr('data-video-id');

                if (videoId) {
                    var iframe = $('<iframe>')
                        .attr({
                            'src': 'https://www.youtube.com/embed/' + videoId,
                            'frameborder': '0',
                            'allow': 'accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share',
                            'allowfullscreen': '',
                            'referrerpolicy': 'strict-origin-when-cross-origin'
                        })
                        .css({
                            'position': 'absolute',
                            'top': '0',
                            'left': '0',
                            'width': '100%',
                            'height': '100%'
                        });

                    $container.empty().append(iframe);
                }
            });

            // Transform playlist videos container into gallery
            $('.playlist-videos-container').each(function() {
                var $container = $(this);
                var $gallery = $('<div class="video-cards-gallery"></div>');

                $container.find('.video-item').each(function() {
                    var $item = $(this);
                    var videoId = $item.attr('data-video-id');
                    var pageUrl = $item.attr('data-page-url');
                    var videoTitle = $item.attr('data-title');

                    if (!videoId || !pageUrl || !videoTitle) return;

                    var $card = $('<div class="video-card"></div>');

                    var $thumb = $('<div class="video-card-thumb"></div>');
                    var $thumbLink = $('<a></a>').attr('href', pageUrl);

                    var $thumbImg = $('<img>')
                        .attr('src', 'https://i.ytimg.com/vi/' + videoId + '/hqdefault.jpg')
                        .attr('alt', videoTitle)
                        .css({
                            'width': '100%',
                            'height': '100%',
                            'object-fit': 'cover'
                        });

                    var $playButton = $('<div class="video-card-play">▶</div>');

                    $thumbLink.append($thumbImg).append($playButton);
                    $thumb.append($thumbLink);

                    var $info = $('<div class="video-card-info"></div>');
                    var $titleLink = $('<a></a>')
                        .attr('href', pageUrl)
                        .text(videoTitle);
                    $info.append($titleLink);

                    $card.append($thumb).append($info);
                    $gallery.append($card);
                });

                if ($gallery.children().length > 0) {
                    $container.replaceWith($gallery);
                }
            });

            // Transform category page playlist gallery (works for all nursing categories)
            var pageName = mw.config.get('wgPageName');
            if (pageName === 'Category:Level_Up_RN_Videos' ||
                pageName === 'Category:Level_Up_RN' ||
                pageName === 'Category:Nursing.com_Videos' ||
                pageName === 'Category:Simple_Nursing_Videos') {

                var $table = $('.wikitable.sortable').first();
                if (!$table.length) return;

                var $gallery = $('<div class="playlists-gallery"></div>');

                $table.find('tr').each(function(index) {
                    if (index === 0) return;

                    var $row = $(this);
                    var $cells = $row.find('td');
                    if ($cells.length < 3) return;

                    var $titleCell = $cells.eq(0);
                    var $descCell = $cells.eq(1);

                    var playlistId = $titleCell.attr('data-playlist-id');
                    var $titleLink = $titleCell.find('a').first();
                    var wikiPageUrl = $titleLink.attr('href');
                    var title = $titleLink.text();
                    var description = $descCell.text().trim();

                    if (!playlistId || !wikiPageUrl) return;

                    var $box = $('<div class="playlist-box"></div>');

                    // Make the entire box clickable
                    $box.css('cursor', 'pointer').on('click', function(e) {
                        if (!$(e.target).is('a')) {
                            window.location.href = wikiPageUrl;
                        }
                    });

                    var $thumb = $('<div class="playlist-box-thumb"></div>');

                    // Use YouTube iframe for thumbnail with pointer-events: none
                    var $thumbFrame = $('<iframe>')
                        .attr({
                            'src': 'https://www.youtube.com/embed/videoseries?list=' + playlistId + '&controls=0&showinfo=0&rel=0&modestbranding=1',
                            'frameborder': '0',
                            'loading': 'lazy'
                        })
                        .css({
                            'position': 'absolute',
                            'top': '0',
                            'left': '0',
                            'width': '100%',
                            'height': '100%',
                            'pointer-events': 'none'
                        });

                    $thumb.append($thumbFrame);
                    $thumb.append('<div class="playlist-box-overlay">▶ Playlist</div>');

                    var $info = $('<div class="playlist-box-info"></div>');
                    $info.append('<div class="playlist-box-title">' + title + '</div>');
                    if (description) {
                        $info.append('<div style="margin: 8px 0; font-size: 13px; color: #666;">' + description + '</div>');
                    }
                    $info.append('<a href="' + wikiPageUrl + '" class="playlist-box-link" onclick="event.stopPropagation();">Watch Playlist →</a>');

                    $box.append($thumb).append($info);
                    $gallery.append($box);
                });

                if ($gallery.children().length > 0) {
                    $table.replaceWith($gallery);
                }
            }
        });
    });
})();