MediaWiki:SKTForm.js

Vikipedi, özgür ansiklopedi

Not: Sayfayı kaydettikten sonra değişiklikleri görebilmek için tarayıcınızın önbelleğinizi temizlemeniz gerekir. Google Chrome, Firefox, Microsoft Edge ve Safari: ⇧ Shift tuşuna basılı tutun ve Yeniden Yükle araç çubuğu düğmesine tıklayın. Ayrıntılar ve diğer tarayıcılara yönelik yönergeler için Vikipedi:Önbelleğinizi atlayın sayfasını inceleyin.

/**
 * <nowiki>
 * Invoked via [[mw:Snippets/Load JS and CSS by URL]] for
 * [[Wikipedia:Requests_for_page_protection]]
 * 
 * Authors: [[User:Enterprisey]], [[User:SD0001]]
 */

/**
 * Fetch JSON page with 1-day caching
 */
function fetchJsonData(page) {
    return $.getJSON("/w/api.php?action=query&format=json&formatversion=2" +
        "&titles=" + encodeURIComponent(page) + "&prop=revisions&rvprop=content" +
        "&uselang=content&maxage=86400&smaxage=86400" // parameters to force caching
    ).then(function(apiResponse) {
        return JSON.parse(apiResponse.query.pages[0].revisions[0].content);
    });
}

$.when(), $.ready, mw.loader.using(['mediawiki.api', 'mediawiki.widgets', 'mediawiki.util', 'oojs-ui-core', 'oojs-ui-windows']).then(function() {
    var reasonInput, titleInput, reasonPreview;
    if ((mw.config.get("wgPageName") !== "Vikipedi:Sayfa_koruma_talepleri")) {
        return;
    }
    console.log(mw.config.get("wgPageName"));
    var api = new mw.Api();
    var previewApi = new mw.Api();
    var requestsPageTitle = 'Vikipedi:Sayfa koruma talepleri';


    function PageProtectionDialog(config) {
        PageProtectionDialog.super.call(this, config);
    }
    OO.inheritClass(PageProtectionDialog, OO.ui.ProcessDialog);
    PageProtectionDialog.static.name = 'PageProtectionDialog';
    PageProtectionDialog.static.title = 'Sayfa Koruma Talebi';
    PageProtectionDialog.static.actions = [{
        action: 'save',
        label: 'Talep Oluştur',
        flags: ['primary', 'progressive']
    }, {
        label: 'İptal',
        flags: 'safe'
    }];
    PageProtectionDialog.prototype.initialize = function() {
        PageProtectionDialog.super.prototype.initialize.apply(this, arguments);
        var headerTitle = new OO.ui.MessageWidget({
            type: 'notice',
            inline: true,
            label: new OO.ui.HtmlSnippet('<strong>Sayfa koruma talebi</strong><br><small>Bu araç, tam koruma ve yarı koruma için kullanılmaktadır. Lütfen Vikipedi:Koruma politikası sayfasını okuyun.</small>')
        });
        titleInput = new mw.widgets.TitleInputWidget({
            'showMissing': false,
            'required': true,
            'value': mw.util.getParamValue('prefillPage') || '',
        });
        reasonInput = new OO.ui.MultilineTextInputWidget({
            'placeholder': 'Aşırı vandalizm',
            'required': 'true'
        });
        reasonPreview = new OO.ui.LabelWidget();
        var fieldset = new OO.ui.FieldsetLayout();
        fieldset.addItems([
            new OO.ui.FieldLayout(titleInput, {
                'label': 'Sayfa Adı:',
                align: 'top'
            }),
            DurationOfProtection = new OO.ui.DropdownWidget({
                menu: {
                    items: [
                        new OO.ui.MenuOptionWidget({
                            data: 'Geçici',
                            label: 'Geçici'
                        }),
                        new OO.ui.MenuOptionWidget({
                            data: 'Süresiz',
                            label: 'Süresiz'
                        }),
                    ]
                },
                label: "Süre"
            }),
            TypeOfProtection = new OO.ui.DropdownWidget({
                menu: {
                    items: [
                        new OO.ui.MenuOptionWidget({
                            data: "tam koruma",
                            label: 'Tam koruma'
                        }),
                        new OO.ui.MenuOptionWidget({
                            data: "yarı koruma",
                            label: 'Yarı koruma'
                        }),
                    ]
                },
                label: "Koruma türü seçiniz"
            }),
            new OO.ui.FieldLayout(reasonInput, {
                'label': 'Koruma Gerekçesi:',
                align: 'top'
            }),
            new OO.ui.FieldLayout(reasonPreview, {
                'label': 'Önizleme:',
                align: 'top'
            }),
        ]);
        this.content = new OO.ui.PanelLayout({
            padded: true,
            expanded: false
        });
        this.content.$element.append(headerTitle.$element, '<br><hr><br>', fieldset.$element);
        this.$body.append(this.content.$element);
        titleInput.on('change', updateForm);
        reasonInput.on('change', updateForm);
    };
    PageProtectionDialog.prototype.getActionProcess = function(action) {
        var dialog = this;
        if (action) {
            return new OO.ui.Process(function() {
                updateForm();
                api.edit(requestsPageTitle, function() {
                    return {
                        section: 1,
                        appendtext: '\n\n' + makeRequestText(),
                        summary: 'Koruma talebi eklendi'
                    };
                }).then(function() {
                    window.location.href = mw.util.getUrl(requestsPageTitle);
                });

                dialog.close({
                    action: action
                });
            });
        }
        return PageProtectionDialog.super.prototype.getActionProcess.call(this, action);
    };
    var windowManager = new OO.ui.WindowManager();
    $(document.body).append(windowManager.$element);
    var dialog = new PageProtectionDialog();
    windowManager.addWindows([dialog]);
    windowManager.openWindow(dialog);


    TypeOfProtection.getMenu().on('choose', function(menuOption) {
        RPPData = menuOption.getData();
    });
    DurationOfProtection.getMenu().on('choose', function(duration) {
        RPPDuration = duration.getData();
    });

    function makeRequestText() {
        var reason = reasonInput.getValue() + (
            (reasonInput.getValue().indexOf('~~' + '~~') >= 0) ?
            '' :
            (' ~~' + '~~'));
        return '==== {{lmad|' + titleInput.getValue() + '}} ====' + '\n\n' + '{{SKT|tür= ' + RPPDuration + ' ' + RPPData + '|gerekçe= ' + reason + ' |yorum=|karar=}}';
    }

    function updateForm() {
        var hasTitle = titleInput.getValue().trim().length > 0;
        var hasReason = reasonInput.getValue().trim().length > 0;

        var formEnabled = hasTitle && hasReason;
        if (formEnabled) {
            previewApi.abort();
            previewApi.parse(makeRequestText(), {
                pst: true,
                title: requestsPageTitle
            }).then(function(text) {
                text = text.replace(/<script/g, '&lt;script');
                reasonPreview.$element.html(text);
            });
        }
    }
});

// </nowiki>