User:BZPN/Script2.js

From Test Wiki
Revision as of 16:38, 25 October 2024 by BZPN (talk | contribs) (Created page with "mw.loader.using(['oojs-ui', 'mediawiki.util']).done(function () { var archiveButton = mw.util.addPortletLink( 'p-tb', '#', 'Archiwizuj dyskusję', 'pt-archive', 'Archiwizuj wybrane wątki na stronach dyskusji' ); $(archiveButton).click(function () { // Create a dialog for archiving var archiveDialog = new OO.ui.ProcessDialog({ title: 'Archiwizacja dyskusji', size: 'large'...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
mw.loader.using(['oojs-ui', 'mediawiki.util']).done(function () {
    var archiveButton = mw.util.addPortletLink(
        'p-tb',
        '#',
        'Archiwizuj dyskusję',
        'pt-archive',
        'Archiwizuj wybrane wątki na stronach dyskusji'
    );

    $(archiveButton).click(function () {
        // Create a dialog for archiving
        var archiveDialog = new OO.ui.ProcessDialog({
            title: 'Archiwizacja dyskusji',
            size: 'large'
        });

        var windowManager = new OO.ui.WindowManager();
        $('body').append(windowManager.$element);
        windowManager.addWindows([archiveDialog]);
        windowManager.openWindow(archiveDialog);

        var progressBar = new OO.ui.ProgressBarWidget();
        var progressField = new OO.ui.FieldLayout(progressBar, {
            label: 'Wyszukiwanie wątków i archiwów...',
            align: 'top'
        });

        var threadsField = new OO.ui.FieldsetLayout({
            label: 'Wybierz wątki do archiwizacji'
        });

        var archiveField = new OO.ui.FieldsetLayout({
            label: 'Wybierz archiwum docelowe'
        });

        archiveDialog.$body.append(progressField.$element, threadsField.$element, archiveField.$element);

        function detectThreads() {
            // Use regex to find sections that match '== Wątek =='
            var content = $('#mw-content-text').text();
            var threads = content.match(/==\s.*?\s==/g);

            if (threads) {
                threads.forEach(function (thread) {
                    var checkbox = new OO.ui.CheckboxInputWidget({ value: thread });
                    var checkboxField = new OO.ui.FieldLayout(checkbox, { label: thread, align: 'inline' });
                    threadsField.addItems([checkboxField]);
                });
            }
        }

        function findArchives() {
            // Use MediaWiki API to search for possible archive pages
            var userName = mw.config.get('wgTitle').split(':')[1];
            var archiveSearchTitles = [
                'Dyskusja wikipedysty:' + userName + '/Archiwum/',
                'Archiwum/',
                'Dyskusja:',
                'Dyskusja ' + userName
            ];

            // Display options for archive selection
            archiveSearchTitles.forEach(function (title) {
                var radio = new OO.ui.RadioInputWidget({ value: title });
                var radioField = new OO.ui.FieldLayout(radio, { label: title + 'X', align: 'inline' });
                archiveField.addItems([radioField]);
            });
        }

        // Initialize by detecting threads and finding archives
        detectThreads();
        findArchives();

        function archiveSelected() {
            // Move the selected threads to the chosen archive
            var selectedThreads = threadsField.items
                .filter(function (item) { return item.fieldWidget.isSelected(); })
                .map(function (item) { return item.fieldWidget.getValue(); });

            var selectedArchive = archiveField.items
                .find(function (item) { return item.fieldWidget.isSelected(); })
                .fieldWidget.getValue();

            // Perform the cut and paste action
            console.log('Archiving:', selectedThreads, 'to', selectedArchive);

            // TODO: Use MediaWiki API to edit both the discussion and archive pages
        }

        // Add an "Archive" button to start the archiving process
        var archiveButton = new OO.ui.ButtonWidget({
            label: 'Archiwizuj',
            flags: ['primary', 'progressive']
        });
        archiveButton.on('click', archiveSelected);
        archiveDialog.$foot.append(archiveButton.$element);
    });
});