MediaWiki:Common.js: Difference between revisions

From Honour Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
Line 1: Line 1:
// Force college page edits to open Page Forms instead of raw wikitext
// Honour Wiki: force CollegePage editing through Page Forms


(function () {
(function () {
     function pageTitle() {
     function currentTitle() {
         return mw.config.get('wgPageName');
         return mw.config.get('wgPageName');
     }
     }


     function formEditUrl(title) {
     function isLoggedIn() {
         return mw.util.getUrl('Special:FormEdit/CollegePage/' + title);
         return !!mw.config.get('wgUserName');
     }
     }


     function redirectIfRawCollegeEdit() {
     function formEditUrl(title, focusField) {
         var action = mw.config.get('wgAction');
         var url = mw.util.getUrl('Special:FormEdit/CollegePage/' + title);
         var title = pageTitle();
         if (focusField) {
            url += '?pf_focus=' + encodeURIComponent(focusField);
        }
        return url;
    }


         if (action === 'edit') {
    function loginUrl() {
            var textarea = document.querySelector('#wpTextbox1');
         return mw.util.getUrl('Special:UserLogin', {
            if (textarea && textarea.value.indexOf('{{CollegePage') !== -1) {
            returnto: currentTitle()
                window.location.href = formEditUrl(title);
        });
            }
    }
        }
 
    function showLoginBox() {
        var box = document.querySelector('.honour-college-edit-box');
        if (!box || isLoggedIn()) return;
 
        box.innerHTML =
            '<div style="font-size:18px;font-weight:700;margin-bottom:8px;">Login required to edit</div>' +
            '<div style="margin-bottom:12px;">Only logged-in students can update college information.</div>' +
            '<a href="' + loginUrl() + '" style="display:inline-block;padding:10px 16px;border:1px solid #3366cc;border-radius:6px;background:#3366cc;color:#fff;text-decoration:none;font-weight:600;">Login to edit</a>';
     }
     }


     function rewriteEditTab() {
     function rewriteMainEditTab() {
         var title = pageTitle();
         var editTab = document.querySelector('#ca-edit a');
         var formButton = document.querySelector('a[href*="Special:FormEdit/CollegePage"]');
         var formButton = document.querySelector('a[href*="Special:FormEdit/CollegePage"]');
        var editTab = document.querySelector('#ca-edit a');


         if (formButton && editTab) {
        if (!editTab) return;
             editTab.href = formEditUrl(title);
 
        if (!isLoggedIn()) {
            editTab.href = loginUrl();
            editTab.textContent = 'Login to edit';
            return;
        }
 
         if (formButton) {
             editTab.href = formEditUrl(currentTitle());
             editTab.textContent = 'Edit';
             editTab.textContent = 'Edit';
         }
         }
    }
    function sectionToField(sectionText) {
        sectionText = (sectionText || '').toLowerCase();
        if (sectionText.indexOf('overview') !== -1) return 'overview';
        if (sectionText.indexOf('history') !== -1) return 'history';
        if (sectionText.indexOf('programs') !== -1) return 'programs';
        if (sectionText.indexOf('facilities') !== -1) return 'facilities';
        if (sectionText.indexOf('admissions') !== -1) return 'admissions';
        if (sectionText.indexOf('affiliations') !== -1) return 'affiliations';
        if (sectionText.indexOf('alumni') !== -1) return 'alumni';
        if (sectionText.indexOf('references') !== -1) return 'references';
        return '';
    }
    function rewriteSectionEditLinks() {
        var sectionLinks = document.querySelectorAll('.mw-editsection a');
        sectionLinks.forEach(function (link) {
            var heading = link.closest('.mw-heading, h2, h3, h4');
            var headingText = heading ? heading.textContent : '';
            var field = sectionToField(headingText);
            if (!field) return;
            if (!isLoggedIn()) {
                link.href = loginUrl();
                link.textContent = 'login to edit';
            } else {
                link.href = formEditUrl(currentTitle(), field);
                link.textContent = 'edit';
            }
        });
    }
    function redirectRawCollegeEdit() {
        if (mw.config.get('wgAction') !== 'edit') return;
        var textarea = document.querySelector('#wpTextbox1');
        if (!textarea) return;
        if (textarea.value.indexOf('{{CollegePage') !== -1) {
            window.location.href = formEditUrl(currentTitle());
        }
    }
    function focusFormField() {
        var params = new URLSearchParams(window.location.search);
        var field = params.get('pf_focus');
        if (!field) return;
        setTimeout(function () {
            var selectors = [
                '[name*="[' + field + ']"]',
                '[name="' + field + '"]',
                'textarea[name*="' + field + '"]',
                'input[name*="' + field + '"]',
                '#input_' + field
            ];
            for (var i = 0; i < selectors.length; i++) {
                var el = document.querySelector(selectors[i]);
                if (el) {
                    el.scrollIntoView({ behavior: 'smooth', block: 'center' });
                    el.focus();
                    return;
                }
            }
        }, 500);
     }
     }


     $(function () {
     $(function () {
         redirectIfRawCollegeEdit();
         showLoginBox();
         rewriteEditTab();
         rewriteMainEditTab();
        rewriteSectionEditLinks();
        redirectRawCollegeEdit();
        focusFormField();
     });
     });
})();
})();

Latest revision as of 18:10, 16 May 2026

// Honour Wiki: force CollegePage editing through Page Forms

(function () {
    function currentTitle() {
        return mw.config.get('wgPageName');
    }

    function isLoggedIn() {
        return !!mw.config.get('wgUserName');
    }

    function formEditUrl(title, focusField) {
        var url = mw.util.getUrl('Special:FormEdit/CollegePage/' + title);
        if (focusField) {
            url += '?pf_focus=' + encodeURIComponent(focusField);
        }
        return url;
    }

    function loginUrl() {
        return mw.util.getUrl('Special:UserLogin', {
            returnto: currentTitle()
        });
    }

    function showLoginBox() {
        var box = document.querySelector('.honour-college-edit-box');
        if (!box || isLoggedIn()) return;

        box.innerHTML =
            '<div style="font-size:18px;font-weight:700;margin-bottom:8px;">Login required to edit</div>' +
            '<div style="margin-bottom:12px;">Only logged-in students can update college information.</div>' +
            '<a href="' + loginUrl() + '" style="display:inline-block;padding:10px 16px;border:1px solid #3366cc;border-radius:6px;background:#3366cc;color:#fff;text-decoration:none;font-weight:600;">Login to edit</a>';
    }

    function rewriteMainEditTab() {
        var editTab = document.querySelector('#ca-edit a');
        var formButton = document.querySelector('a[href*="Special:FormEdit/CollegePage"]');

        if (!editTab) return;

        if (!isLoggedIn()) {
            editTab.href = loginUrl();
            editTab.textContent = 'Login to edit';
            return;
        }

        if (formButton) {
            editTab.href = formEditUrl(currentTitle());
            editTab.textContent = 'Edit';
        }
    }

    function sectionToField(sectionText) {
        sectionText = (sectionText || '').toLowerCase();

        if (sectionText.indexOf('overview') !== -1) return 'overview';
        if (sectionText.indexOf('history') !== -1) return 'history';
        if (sectionText.indexOf('programs') !== -1) return 'programs';
        if (sectionText.indexOf('facilities') !== -1) return 'facilities';
        if (sectionText.indexOf('admissions') !== -1) return 'admissions';
        if (sectionText.indexOf('affiliations') !== -1) return 'affiliations';
        if (sectionText.indexOf('alumni') !== -1) return 'alumni';
        if (sectionText.indexOf('references') !== -1) return 'references';

        return '';
    }

    function rewriteSectionEditLinks() {
        var sectionLinks = document.querySelectorAll('.mw-editsection a');

        sectionLinks.forEach(function (link) {
            var heading = link.closest('.mw-heading, h2, h3, h4');
            var headingText = heading ? heading.textContent : '';
            var field = sectionToField(headingText);

            if (!field) return;

            if (!isLoggedIn()) {
                link.href = loginUrl();
                link.textContent = 'login to edit';
            } else {
                link.href = formEditUrl(currentTitle(), field);
                link.textContent = 'edit';
            }
        });
    }

    function redirectRawCollegeEdit() {
        if (mw.config.get('wgAction') !== 'edit') return;

        var textarea = document.querySelector('#wpTextbox1');
        if (!textarea) return;

        if (textarea.value.indexOf('{{CollegePage') !== -1) {
            window.location.href = formEditUrl(currentTitle());
        }
    }

    function focusFormField() {
        var params = new URLSearchParams(window.location.search);
        var field = params.get('pf_focus');

        if (!field) return;

        setTimeout(function () {
            var selectors = [
                '[name*="[' + field + ']"]',
                '[name="' + field + '"]',
                'textarea[name*="' + field + '"]',
                'input[name*="' + field + '"]',
                '#input_' + field
            ];

            for (var i = 0; i < selectors.length; i++) {
                var el = document.querySelector(selectors[i]);
                if (el) {
                    el.scrollIntoView({ behavior: 'smooth', block: 'center' });
                    el.focus();
                    return;
                }
            }
        }, 500);
    }

    $(function () {
        showLoginBox();
        rewriteMainEditTab();
        rewriteSectionEditLinks();
        redirectRawCollegeEdit();
        focusFormField();
    });
})();