לדלג לתוכן

משתמש:מ. רובין/הערות שוליים.js

מתוך חב"דפדיה, אנציקלופדיה חב"דית חופשית

הערה: לאחר הפרסום, ייתכן שיהיה צורך לנקות את זיכרון המטמון (cache) של הדפדפן כדי להבחין בשינויים.

  • פיירפוקס / ספארי: להחזיק את המקש Shift בעת לחיצה על טעינה מחדש (Reload) או ללחוץ על צירוף המקשים Ctrl-F5 או Ctrl-R (במחשב מק: ⌘-R).
  • גוגל כרום: ללחוץ על צירוף המקשים Ctrl-Shift-R (במחשב מק: ⌘-Shift-R).
  • אדג': להחזיק את המקש Ctrl בעת לחיצה על רענן (Refresh) או ללחוץ על צירוף המקשים Ctrl-F5.
/*** Hot Ref Notes ***/

function initHotRefNotes($cats) {

    if ($cats.find('.hot-refnotes-btn').length) return;

    var refBox = document.createElement('span'),
        status = document.createElement('span'),
        modify = document.createElement('a'),
        api = new mw.Api();

    refBox.className = 'hot-refnotes-btn';
    refBox.style.marginRight = '0.5em';
    refBox.innerText = 'הערות שוליים: ';

    status.style.marginLeft = '0.25em';
    status.style.display = 'none';
    status.innerText = 'שומר...';

    modify.style.cursor = 'pointer';
    modify.style.marginLeft = '0.25em';

    function hasRefNotes(text) {
        return /\{\{\s*הערות\s*שוליים\s*(\|[^}]*)?\}\}/i.test(text);
    }

    function transformPage(transform) {
        status.style.display = 'inline';
        modify.style.pointerEvents = 'none';

        return api.edit(mw.config.get('wgPageName'), transform)
            .fail(function (_, data) {
                mw.notify(api.getErrorMessage(data), { type: 'error', tag: 'hotrefnotes' });
            })
            .always(function () {
                status.style.display = 'none';
                modify.style.pointerEvents = 'auto';
            });
    }

    function addRefNotes() {
        return transformPage(function (rev) {
            if (hasRefNotes(rev.content)) return;
            return {
                text: rev.content.replace(/\s*$/, '\n\n{{הערות שוליים}}\n'),
                summary: 'נוספה {{הערות שוליים}}',
                minor: true,
                bot: true
            };
        }).done(function () {
            updateButton(true);
            mw.notify('נוספה תבנית {{הערות שוליים}}.', { type: 'success' });
        });
    }

    function removeRefNotes() {
        return transformPage(function (rev) {
            if (!hasRefNotes(rev.content)) return;
            return {
                text: rev.content.replace(/\n*\{\{\s*הערות\s*שוליים\s*(\|[^}]*)?\}\}\s*\n*/gi, '\n\n'),
                summary: 'הוסרה {{הערות שוליים}}',
                minor: true,
                bot: true
            };
        }).done(function () {
            updateButton(false);
            mw.notify('הוסרה תבנית {{הערות שוליים}}.', { type: 'success' });
        });
    }

    function updateButton(exists) {
        if (exists) {
            modify.innerText = '(−)';
            modify.title = 'הסר {{הערות שוליים}}';
            modify.onclick = removeRefNotes;
        } else {
            modify.innerText = '(+)';
            modify.title = 'הוסף {{הערות שוליים}}';
            modify.onclick = addRefNotes;
        }
    }

    api.get({
        action: 'query',
        prop: 'revisions',
        rvprop: 'content',
        titles: mw.config.get('wgPageName')
    }).then(function (res) {
        var page = res.query.pages[mw.config.get('wgArticleId')],
            text = page.revisions[0]['*'] || '';
        updateButton(hasRefNotes(text));
    });

    refBox.appendChild(status);
    refBox.appendChild(modify);
    $cats.prepend(refBox); 
}

if (mw.hook) mw.hook('wikipage.categories').add(initHotRefNotes);


$(function () {
    var $cats = $('.catlinks');
    if ($cats.length) initHotRefNotes($cats);
});