משתמש:מ. רובין/ניסוי.js – הבדלי גרסאות

מ. רובין (שיחה | תרומות)
הועבר למשתמש:מ. רובין/בינוויקי.js
תגיות: ריקון שחזור ידני
מ. רובין (שיחה | תרומות)
אין תקציר עריכה
שורה 1: שורה 1:
mw.loader.using(['mediawiki.api', 'mediawiki.util']).then(function () {
    $(function () {
        // הוספת כפתור בתפריט הצד
        const link = mw.util.addPortletLink(
            'p-tb',
            '#',
            'הוסף תבנית',
            't-add-maint-template',
            'הוסף תבנית תחזוקה לדף'
        );


        $(link).click(function (e) {
            e.preventDefault();
            if ($('#template-box').length) return; // לא לפתוח פעמיים
            const $box = $(`
                <div id="template-box" style="
                    background: #f9f9f9;
                    border: 1px solid #ccc;
                    padding: 10px;
                    margin-top: 10px;
                    border-radius: 8px;
                    max-width: 300px;
                    font-family: sans-serif;
                ">
                    <label for="template-select" style="display: block; margin-bottom: 5px;">בחר תבנית להוספה:</label>
                    <select id="template-select" style="width: 100%; padding: 5px;">
                        <option value="עריכה דחופה">עריכה דחופה</option>
                    </select>
                    <div style="margin-top: 10px;">
                        <button id="add-template-button" style="padding: 5px 10px;">הוסף</button>
                        <button id="cancel-template-button" style="padding: 5px 10px; margin-right: 5px;">ביטול</button>
                    </div>
                    <div id="template-status" style="margin-top: 10px; font-weight: bold;"></div>
                </div>
            `);
            $(link).after($box);
            $('#cancel-template-button').click(() => $box.remove());
            $('#add-template-button').click(function () {
                const template = $('#template-select').val();
                if (!template) return;
                const api = new mw.Api();
                const title = mw.config.get('wgPageName');
                $('#template-status').text('טוען תוכן...');
                api.get({
                    action: 'query',
                    prop: 'revisions',
                    titles: title,
                    rvslots: 'main',
                    rvprop: 'content',
                    formatversion: 2
                }).done(function (data) {
                    const page = data.query.pages[0];
                    if (!page || !page.revisions || !page.revisions.length) {
                        $('#template-status').text('שגיאה: לא ניתן לטעון את הדף.');
                        return;
                    }
                    let content = page.revisions[0].slots.main.content;
                    const templateRegex = new RegExp(`{{\\s*${template}\\s*}}`, 'i');
                    if (templateRegex.test(content)) {
                        $('#template-status').text('כבר קיימת התבנית בדף.');
                        return;
                    }
                    const newContent = `{{${template}}}\n` + content;
                    $('#template-status').text('שומר...');
                    api.postWithToken('csrf', {
                        action: 'edit',
                        title: title,
                        text: newContent,
                        summary: `הוספת תבנית {{${template}}}`,
                        format: 'json'
                    }).done(function () {
                        $('#template-status').text('✓ נוסף! מרענן...');
                        setTimeout(() => location.reload(), 1500);
                    }).fail(function () {
                        $('#template-status').text('⚠ שגיאה בעת השמירה.');
                    });
                }).fail(function () {
                    $('#template-status').text('⚠ שגיאה בטעינת הדף.');
                });
            });
        });
    });
});