Jump to content

User:DanCherek/sandbox.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
//<nowiki>
/**
 * Attribution: This script is a derivative of [[MediaWiki:Request-page-protection-form.js]],
 * authored by User:Enterprisey and User:SD0001.
 */

$.when($.ready, mw.loader.using(['mediawiki.api', 'mediawiki.widgets', 'mediawiki.util'])).then(function(config) {
    //if ((mw.config.get("wgPageName") !== "Wikipedia:WikiProject_Resource_Exchange/Resource_Request/Form")) {
    //    return;
    //}
    var api = new mw.Api();
    var previewApi = new mw.Api();

	var requestsPageTitle = 'User:DanCherek/sandbox';
	var template = `== $heading ==
$request`;

    var headingInput = new OO.ui.TextInputWidget({
        'required': true,
    });
    var requestInput = new OO.ui.MultilineTextInputWidget({
        'value': `<!-- Brief citation of the requested materials. -->
{{refbegin}}
* {{cite journal |ref=none |no-tracking=yes<!-- prevents error categorization --> |last= |first= |date= |title= |work= |publisher= |location= |page= |url= |access-date= |isbn= |issn= |oclc= }}
{{refend}}
<!-- Wikipedia articles being improved. -->
For [[Example article]]

Thanks, ~~~~`,
        'required': 'true',
        'autosize': 'true',
        'classes': [ 'mw-editfont-monospace' ]
    });
	var requestPreview = 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(headingInput, {
            'label': 'Section heading (briefly describe your request):',
            align: 'top'
        }),
        new OO.ui.FieldLayout(requestInput, {
            'label': 'Request:',
            align: 'top'
        }),
        new OO.ui.FieldLayout(requestPreview, {
            'label': 'Preview:',
            align: 'top'
        }),
        new OO.ui.FieldLayout(submitButton)
    ]);
    $("#mw-content-text").empty().append(fieldset.$element);

    function makeRequestText() {
        return template.replace(/\$heading/g, headingInput.getValue()).replace(/\$request/g, requestInput.getValue());
    }
    function updateForm() {
        var hasHeading = headingInput.getValue().trim().length > 0;
        var hasRequest = requestInput.getValue().trim().length > 0;
        
        var formEnabled = hasHeading && hasRequest;
        submitButton.setDisabled(!formEnabled);
        if (!hasHeading && !hasRequest) submitButton.setLabel('Add heading and enter request');
        else if (!hasHeading) submitButton.setLabel('Add heading');
        else if (!hasRequest) submitButton.setLabel('Enter request');
        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');
				requestPreview.$element.html(text);
			});
        }
    }
    headingInput.on('change', updateForm);
    requestInput.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: '/* ' + headingInput.getValue() + ' */ new section'
            };
        }).then(function() {
            window.location.href = mw.util.getUrl(requestsPageTitle);
        });
    });
});
//</nowiki>