_ _    _ _____  ___   __                       
 __      _(_) | _(_)___ / ( _ ) / /_   ___ ___  _ __ ___  
 \ \ /\ / / | |/ / | |_ \ / _ \| '_ \ / __/ _ \| '_ ` _ \ 
  \ V  V /| |   <| |___) | (_) | (_) | (_| (_) | | | | | |
   \_/\_/ |_|_|\_\_|____/ \___/ \___(_)___\___/|_| |_| |_|

MediaWiki:AFCHD-wizard.js

In this article, we will explore a variety of aspects related to MediaWiki:AFCHD-wizard.js, from its origins to its current impact on society. We will analyze its evolution over time, as well as its relevance in the current context. In addition, we will examine the different perspectives and opinions that exist around MediaWiki:AFCHD-wizard.js, with the aim of offering a comprehensive vision that allows us to understand its true meaning. Through this in-depth analysis, we seek to give the reader a broader and more complete understanding of MediaWiki:AFCHD-wizard.js, addressing all the relevant aspects that encompass this topic.
/**
 * Invoked via ]
 * on ]
 * 
 * Author: ]
 * Licence: MIT
 */

// <nowiki>
$.when($.ready, mw.loader.using()).then(function() {
    if (mw.config.get("wgPageName") !== "Wikipedia:WikiProject_Articles_for_creation/Help_desk/New_question") return;
    
    var api = new mw.Api();
    var previewApi = new mw.Api();
    var titleInput = new mw.widgets.TitleInputWidget({
    	'value': mw.util.getParamValue('page') || '',
        'required': true
    });
    var reasonInput = new OO.ui.MultilineTextInputWidget({
        'placeholder': 'Tell us why you are requesting assistance',
        'required': true,
        'rows': 5
    });
    var previewLayout = new OO.ui.HorizontalLayout();
    var submitButton = new OO.ui.ButtonWidget({
        'label': 'Submit request',
        'flags': ,
        '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': 'Reason for requesting assistance:',
            align: 'top'
        }),
        previewLayout,
        new OO.ui.FieldLayout(submitButton)
    ]);
    $("#afchd-wizard-container").empty().append(fieldset.$element);
    $("#catlinks").remove();
    
    function updateForm() {
        var hasTitle = titleInput.getValue().trim().length > 0;
        var hasReason = reasonInput.getValue().trim().length > 0;
        
        var formEnabled = hasTitle && hasReason;
        submitButton.setDisabled(!formEnabled);
        if (!hasTitle && !hasReason) submitButton.setLabel('Select page and enter reason');
        else if (!hasTitle) submitButton.setLabel('Select page');
        else if (!hasReason) submitButton.setLabel('Enter reason');
        else submitButton.setLabel('Submit');
        
        if (formEnabled) {
			previewApi.abort();
			previewApi.parse(makeRequestText(), { 
				pst: true, 
				title: 'Wikipedia:WikiProject_Articles_for_creation/Help_desk' 
			}).then(function(text) {
				text = text.replace(/<script/g, '&lt;script');
				previewLayout.$element.html(text);
			});
        }
    }
    updateForm();
    titleInput.on('change', updateForm);
    reasonInput.on('change', updateForm);

    function makeRequestText() {
        var title = titleInput.getValue();
        var text = '== {{subst:#time:H:i, j F Y}} review of submission by {{subst:REVISIONUSER}} ==' + 
        	'\n{{Lafc|username={{subst:REVISIONUSER}}|ts={{subst:#time:H:i, j F Y}}|draft=' + title + '}}' + 
        	'\n' + reasonInput.getValue() + (reasonInput.getValue().indexOf('~~~~') >= 0 ? '' : ' ~~~~');
        return text;
    }
    
    var beforeUnloadHandler = function(e) {
    	var hasTitle = titleInput.getValue().trim().length > 0;
        var hasReason = reasonInput.getValue().trim().length > 0;
        if (hasTitle && hasReason) {
        	e.preventDefault();
        	return event.returnValue = "Are you sure you want to leave without submitting? (Click \"Submit\" to post)";
        }
    };
    $(window).on('beforeunload', beforeUnloadHandler);

    submitButton.on('click', function() {
        submitButton.setDisabled(true);
        submitButton.setLabel('Submitting...');
        $('.afchd-wizard-error').remove();
        api.edit('Wikipedia:WikiProject_Articles_for_creation/Help_desk', function() {
            return {
                appendtext: '\n\n' + makeRequestText(),
                summary: 'Requesting assistance regarding ]'
            };
        }).then(function() {
        	$(window).off('beforeunload', beforeUnloadHandler);
            window.location.href = mw.util.getUrl('Wikipedia:WikiProject_Articles_for_creation/Help_desk#footer');
        }).catch(function(e) {
        	submitButton.setDisabled(false);
        	submitButton.setLabel('Submit');
        	submitButton.$element.after(
        		$('<div>').css('color', 'red').text('An error occurred: ' + e).addClass('afchd-wizard-error')
        	);
        });
    });
});
// </nowiki>