မီႇတီႇယႃႇဝီႇၶီႇ:Request-page-protection-form.js

မၢႆတွင်း: ဝၢႆးသေသိမ်းပၼ်ယဝ်ႉ၊ တွၼ်ႈတႃႇ ၸဝ်ႈၵဝ်ႇ တေႁၼ်လႆႈ လွင်ႈလႅၵ်ႈလၢႆႈၼၼ်ႉ ၸဝ်ႈၵဝ်ႇတေၸၢင်ႈလႆႈလတ်းၶၢမ်ႈ ၶႅတ်ႉၶျ် တူဝ်ပိုတ်ႇဝႅပ်ႉၸဝ်ႈၵဝ်ႇယဝ်ႉ။

  • ၽွင်းမိူဝ်ႈတိုၵ်ႉၼဵၵ်း Reload တီႈ Firefox / Safari: ၼၼ်ႉ ၼဵၵ်းဝႆႉပႃး Shift ၊ဢမ်ႇၼၼ် ၼဵၵ်းပၼ် Ctrl-F5 ဢမ်ႇၼၼ် Ctrl-R (တီႈၼႂ်း Mac ၼႆ ၼဵၵ်းပၼ်⌘-R)
  • တီႈၼႂ်း Google Chrome: ၼဵၵ်းပၼ် Ctrl-Shift-R (တီႈၼႂ်း Mac ၼႆႉ ၼဵၵ်းပၼ်⌘-Shift-R )
  • ၽွင်းမိူဝ်ႈ တိုၵ်ႉၼဵၵ်း Refreshတီႈ Internet Explorer/ Edge: ၼဵၵ်းဝႆႉပၼ် Ctrl ဢမ်ႇၼၼ် ၼဵၵ်းပၼ် Ctrl-F5
  • တီႈၼႂ်း Opera: ၵႂႃႇၸူးတီႈ Menu → Settings (ပေႃးပဵၼ်တီႈၼႂ်း Mac ၸိုင် Opera → Preferences ) သေ သိုပ်ႇၵႂႃႇ Privacy & security → Clear browsing data → Cached images and files ၼၼ်ႉလႄႈ။
/**
 * 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(fetchJsonData("Wikipedia:Requests for page protection/Forms-configuration.json"), $.ready, mw.loader.using(['mediawiki.api', 'mediawiki.widgets', 'mediawiki.util'])).then(function(config) {
    if ((mw.config.get("wgPageName") !== "Wikipedia:Requests_for_page_protection/Increase/Form") &&
            (mw.config.get("wgPageName") !== "Wikipedia:Requests_for_page_protection/Decrease/Form") &&
            (mw.config.get("wgPageName") !== "Wikipedia:Requests_for_page_protection/Edit/Form")) {
        return;
    }
    var api = new mw.Api();
    var previewApi = new mw.Api();

    // either 'Increase', 'Decrease', or 'Edit'
    var subpage = (mw.config.get("wgPageName").indexOf('Increase') >= 0) ? 'Increase' : ((mw.config.get(
        "wgPageName").indexOf('Decrease') >= 0) ? 'Decrease' : 'Edit');
	var requestsPageTitle = 'Wikipedia:Requests_for_page_protection/' + subpage;
    config = config[subpage];

    var titleInput = new mw.widgets.TitleInputWidget({
        'showMissing': false,
        'required': true,
        'value': mw.util.getParamValue('prefillPage') || '',
    });
    var reasonInput = new OO.ui.MultilineTextInputWidget({
        'placeholder': config.reasonInputPlaceholder,
        'required': 'true'
    });
	var reasonPreview = new OO.ui.LabelWidget();
    var submitButton = new OO.ui.ButtonWidget({
        'label': 'Submit request',
        'flags': ['progressive', 'primary'],
        'accesskey': 's'
    });
    var fieldset = new OO.ui.FieldsetLayout();
    fieldset.addItems([
        new OO.ui.FieldLayout(titleInput, {
            'label': 'Page title:',
            align: 'top'
        }),
        new OO.ui.FieldLayout(reasonInput, {
            'label': config.reasonInputLabel,
            align: 'top'
        }),
        new OO.ui.FieldLayout(reasonPreview, {
            'label': 'Preview:',
            align: 'top'
        }),
        new OO.ui.FieldLayout(submitButton)
    ]);
    $("#mw-content-text").empty().append(fieldset.$element);

    function makeRequestText() {
	    var reason = reasonInput.getValue() + (
			(reasonInput.getValue().indexOf('~~' + '~~') >= 0)
			? ''
			: (' ~~' + '~~'));
        return config.template.replace(/\$title/g, titleInput.getValue()).replace(/\$reason/g, reason)
    }
    function updateForm() {
        var hasTitle = titleInput.getValue().trim().length > 0;
        var hasReason = reasonInput.getValue().trim().length > 0;
        
        var formEnabled = hasTitle && hasReason;
        submitButton.setDisabled(!formEnabled);
        var reasonOrRequest = (subpage === 'Edit') ? 'request' : 'reason';
        if (!hasTitle && !hasReason) submitButton.setLabel('Select page and enter ' + reasonOrRequest);
        else if (!hasTitle) submitButton.setLabel('Select page');
        else if (!hasReason) submitButton.setLabel('Enter ' + reasonOrRequest);
        else submitButton.setLabel('Submit request');
        
        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);
			});
        }
    }
    titleInput.on('change', updateForm);
    reasonInput.on('change', updateForm);

    submitButton.on('click', function() {
        updateForm();
        if (submitButton.isDisabled()) {
            return;
        }

        submitButton.setDisabled(true);
        submitButton.setLabel('Submitting...');
        api.edit(requestsPageTitle, function() {
            return {
                appendtext: '\n\n' + makeRequestText(),
                summary: config.summary.replace(/\$title/g, titleInput.getValue())
            };
        }).then(function() {
            window.location.href = mw.util.getUrl(requestsPageTitle);
        });
    });
});