MediaWiki:Common.js: Difference between revisions

From The Seven Sages of Rome
No edit summary
No edit summary
Line 1: Line 1:
/** Test **/
/** Test **/
    $(document).ready(function() {
$(document).ready(function() {
        // Show the dialog when the button is clicked
    // Show the dialog when the button is clicked
        $('#copyEmbeddedStories').click(function() {
    $('#copyEmbeddedStories').click(function() {
            var dialog = new OO.ui.Dialog({
        // Create the OOUI dialog
                size: 'medium',
        var dialog = new OO.ui.Dialog({
                 content: '<div class="form-group"><label for="page-name">Select Target Page:</label>' +
            size: 'medium',
                        '<input type="text" class="form-control" id="page-name" autocomplete="off">' +
            content: [
                        '</div><button class="btn btn-primary" id="process">Process</button>'
                 new OO.ui.LabelWidget({
             });
                    label: 'Select Target Page:'
                }),
                new OO.ui.TextInputWidget({
                    id: 'page-name',
                    placeholder: 'Enter page name'
                }),
                new OO.ui.ButtonWidget({
                    label: 'Process',
                    flags: ['primary'],
                    id: 'process'
                })
             ]
        });


            dialog.open();
        // Open the dialog
           
        dialog.open();
            // Autocomplete functionality
            $('#page-name').on('input', function() {
                var query = $(this).val();
                if (query.length > 2) {
                    $.ajax({
                        url: mw.util.wikiScript('api'),
                        data: {
                            action: 'query',
                            list: 'allpages',
                            apprefix: query,
                            format: 'json'
                        },
                        success: function(response) {
                            var pages = response.query.allpages;
                            var suggestions = pages.map(function(page) {
                                return page.title;
                            });
                            // Here you can use your own UI autocomplete
                            // for the suggestions list
                        }
                    });
                }
            });


             // Process the data
        // Autocomplete functionality
            $('#process').click(function() {
        $('#page-name').on('input', function() {
                var targetPage = $('#page-name').val();
             var query = $(this).val();
                if (targetPage) {
            if (query.length > 2) {
                    copyEmbeddedStories(targetPage);
                $.ajax({
                }
                    url: mw.util.wikiScript('api'),
                dialog.close();
                    data: {
            });
                        action: 'query',
                        list: 'allpages',
                        apprefix: query,
                        format: 'json'
                    },
                    success: function(response) {
                        var pages = response.query.allpages;
                        var suggestions = pages.map(function(page) {
                            return page.title;
                        });
                        // Handle suggestions display
                    }
                });
            }
        });
 
        // Process the data
        $('#process').click(function() {
            var targetPage = $('#page-name').val();
            if (targetPage) {
                copyEmbeddedStories(targetPage);
            }
            dialog.close();
         });
         });
     });
     });
});


    function copyEmbeddedStories(targetPage) {
function copyEmbeddedStories(targetPage) {
        var content = mw.page.getCurrentPage().then(function(page) {
    var content = mw.page.getCurrentPage().then(function(page) {
            var stories = page.content.match(/\{\{EmbeddedStory[^}]*\}\}/g); // Regex to match all EmbeddedStory templates
        var stories = page.content.match(/\{\{EmbeddedStory[^}]*\}\}/g); // Regex to match all EmbeddedStory templates
            var targetContent = stories.join('\n');
        var targetContent = stories.join('\n');


            // Use the MediaWiki API to update the target page
        // Use the MediaWiki API to update the target page
            $.ajax({
        $.ajax({
                url: mw.util.wikiScript('api'),
            url: mw.util.wikiScript('api'),
                method: 'POST',
            method: 'POST',
                data: {
            data: {
                    action: 'edit',
                action: 'edit',
                    title: targetPage,
                title: targetPage,
                    text: targetContent,
                text: targetContent,
                    summary: 'Copying embedded stories',
                summary: 'Copying embedded stories',
                    token: mw.user.tokens.get('editToken'),
                token: mw.user.tokens.get('editToken'),
                    format: 'json'
                format: 'json'
                },
            },
                success: function(response) {
            success: function(response) {
                    alert('Embedded Stories copied successfully!');
                alert('Embedded Stories copied successfully!');
                }
             }
             });
         });
         });
     }
     });
}
 
/** Test End **/
/** Test End **/



Revision as of 11:53, 13 March 2025

/** Test **/
$(document).ready(function() {
    // Show the dialog when the button is clicked
    $('#copyEmbeddedStories').click(function() {
        // Create the OOUI dialog
        var dialog = new OO.ui.Dialog({
            size: 'medium',
            content: [
                new OO.ui.LabelWidget({
                    label: 'Select Target Page:'
                }),
                new OO.ui.TextInputWidget({
                    id: 'page-name',
                    placeholder: 'Enter page name'
                }),
                new OO.ui.ButtonWidget({
                    label: 'Process',
                    flags: ['primary'],
                    id: 'process'
                })
            ]
        });

        // Open the dialog
        dialog.open();

        // Autocomplete functionality
        $('#page-name').on('input', function() {
            var query = $(this).val();
            if (query.length > 2) {
                $.ajax({
                    url: mw.util.wikiScript('api'),
                    data: {
                        action: 'query',
                        list: 'allpages',
                        apprefix: query,
                        format: 'json'
                    },
                    success: function(response) {
                        var pages = response.query.allpages;
                        var suggestions = pages.map(function(page) {
                            return page.title;
                        });
                        // Handle suggestions display
                    }
                });
            }
        });

        // Process the data
        $('#process').click(function() {
            var targetPage = $('#page-name').val();
            if (targetPage) {
                copyEmbeddedStories(targetPage);
            }
            dialog.close();
        });
    });
});

function copyEmbeddedStories(targetPage) {
    var content = mw.page.getCurrentPage().then(function(page) {
        var stories = page.content.match(/\{\{EmbeddedStory[^}]*\}\}/g); // Regex to match all EmbeddedStory templates
        var targetContent = stories.join('\n');

        // Use the MediaWiki API to update the target page
        $.ajax({
            url: mw.util.wikiScript('api'),
            method: 'POST',
            data: {
                action: 'edit',
                title: targetPage,
                text: targetContent,
                summary: 'Copying embedded stories',
                token: mw.user.tokens.get('editToken'),
                format: 'json'
            },
            success: function(response) {
                alert('Embedded Stories copied successfully!');
            }
        });
    });
}

/** Test End **/

function scrollToAnchor(anchorId) {
    const element = document.getElementById(anchorId);
    const navbarHeight = 50;
    if (element) {
        // Calculate the adjusted scroll position
        const elementPosition = element.getBoundingClientRect().top + window.pageYOffset;
        const offsetPosition = elementPosition - navbarHeight;

        // Scroll to the adjusted position
        window.scrollTo({
            top: offsetPosition,
            behavior: 'smooth'
        });
    }
}
function drilldownswitcher() {
    $('#drilldown-switch:contains("Show all filters")').length ? ($(".drilldown-filter-values:has(a)").css("display", "block"),
            $(".drilldown-values-toggle").each(function () {
                $("img").each(function () {
                    $(this).attr("src", $(this).attr("src").replace("right-arrow.png", "down-arrow.png"));
                });
            }),
            (document.getElementById("drilldown-switch").innerHTML = 'Hide all filters <i class="fa fa-minus"></i>'))
        : $('#drilldown-switch:contains("Hide all filters")').length &&
        ($(".drilldown-filter-values:has(a)").css("display", "none"),
            $(".drilldown-values-toggle").each(function () {
                $("img").each(function () {
                    $(this).attr("src", $(this).attr("src").replace("down-arrow.png", "right-arrow.png"));
                });
            }),
            (document.getElementById("drilldown-switch").innerHTML = 'Show all filters <i class="fa fa-plus"></i>'));
}
$(document).ready(function () {
    $(".drilldown-results").css({ "-webkit-filter": "blur(0)", "-moz-filter": "blur(0)", "-o-filter": "blur(0)", "-ms-filter": "blur(0)", filter: "blur(0)" }), $(".drilldown-filter-values:has(a)").css("display", "none");
    $('<br /><h3 class="drilldown-pre-header">1. Selected Filters</h3>').insertBefore("#drilldown-applied-filters"),
    $('<h3 class="drilldown-pre-filters">2. Available Filters</h3>').insertBefore("#drilldown-applicable-filters"),
    $('<h3 class="drilldown-post-filters">3. Filtered Results</h3>').insertAfter("#drilldown-applicable-filters"),
    $('<html><div class="drilldown-btn-wrapper"><a class="btn primary-btn" href="javascript:;" onclick="drilldownswitcher()" id="drilldown-switch">Show all filters <i class="fa fa-plus"></i></a></div></html>').insertBefore("#drilldown-filters");
    $(".drilldown-values-toggle").each(function () {
    	$("img").each(function () {
        	$(this).attr("src", $(this).attr("src").replace("down-arrow.png", "right-arrow.png"));
    	});
    });

    if( mw.storage.get( "wip-dismissed" ) === null || JSON.parse(mw.storage.get( "wip-dismissed" )) === false){
    	$( ".wip-alert" ).fadeIn( "slow", function() {});
    }
    
    $( "#close-wip" ).on( "click", function() {
	  mw.storage.set( "wip-dismissed", true );
	  $( ".wip-alert" ).fadeOut( "slow", function() {});
    } );
})
$(document).ready(function () {
    $('#copyButton').on('click', function () {
        var targetPage = $("#targetPageInput").val().trim();
        var embeddedStories = $("#embeddedStoryTemplates").text();

        if (!targetPage) {
            alert("Please select a target page.");
            return;
        }

        if (!embeddedStories) {
            alert("No EmbeddedStory templates found on the source page.");
            return;
        }

        // Confirm action
        if (!confirm("Are you sure you want to copy the EmbeddedStory templates to " + targetPage + "?")) {
            return;
        }

        // Use MediaWiki API to append templates to the target page
        $.post(mw.util.wikiScript('api'), {
            action: 'edit',
            title: targetPage,
            appendtext: "\n" + embeddedStories,
            token: mw.user.tokens.get('csrfToken'),
            format: 'json'
        })
        .done(function (data) {
            if (data && data.edit && data.edit.result === 'Success') {
                alert("EmbeddedStory templates copied successfully to " + targetPage + "!");
            } else {
                alert("An error occurred: " + (data.error && data.error.info));
            }
        })
        .fail(function () {
            alert("An error occurred while trying to copy the EmbeddedStory templates.");
        });
    });
});