MediaWiki:Gadget-FormCheck.js: Difference between revisions

From Tsadra Foundation Advanced Contemplative Scholarships
((by SublimeText.Mediawiker))
((by SublimeText.Mediawiker))
 
(45 intermediate revisions by the same user not shown)
Line 1: Line 1:
$('#TfacsSubmission').ready( function() {
$( function() {
 
 
    // NAVIGATION FUNCTIONS


     $('.section-nav-toggles').click( function(event) {
     $('.section-nav-toggles').click( function(event) {
Line 21: Line 24:
     });
     });


     function checkFields(classes) {
 
        if( classes.filter(function() { return this.value === ''; }).length === 0 ) {
 
            var status = $('input[name="status"]').val();
    // FORM CHECK FUNCTIONS
 
     var allInputs = $('#TfacsSubmission input, #TfacsSubmission select, #TfacsSubmission textarea');
    if( $('select[name="retreattype[]"]').val() === 'Three-year retreat') {
        var mustFields = $('.MustField, .ThreeMustField');
    } else if( $('select[name="retreattype[]"]').val() === 'Solitary hermitage') {
        var mustFields = $('.MustField, .SolMustField');
    }
    var retreatType = $('select[name="retreattype[]"]').val();
    var status = $('input[name="status"]').val();
 
 
    if( ( status === 'FirstSave' ) || ( status === 'Saved' ) || ( status === 'Complete' ) ) {
        $(checkForm());
        $('#TfacsSubmission input, #TfacsSubmission select, #TfacsSubmission textarea').keyup(checkForm);
    } else if ( ( status === 'Submitted' ) || ( status === 'Checked' ) ) {
        allInputs.not('#DummyField, #TfacsSubmitApplication').prop('disabled', true);
        $('.form-status').addClass('alert-dark').html('Current status: <b>SUBMITTED</b>. <br> Your application has been submitted. The contemplative scholarship administrator will contact you shortly.' );
    }
 
    $('#TfacsSubmission input, #TfacsSubmission select, #TfacsSubmission textarea').keyup(checkTables);
 
 
 
 
 
 
 
    // Check the form content
    function checkForm() {
        allInputs.prop('disabled', false);
        if (mustFields) {
            checkFields(mustFields);
        } else {
            console.log("Retreat type not set.");
            $('.form-status').addClass('alert-primary').html('Current status: <b>SAVED</b>. Retreat type not set.' );
        }
    }
 
    // Check for required fields
    function checkFields(mustFields) {
        if( mustFields.filter(function() { return this.value === ''; }).length === 0 ) {
             if( status === 'Complete' ) {
             if( status === 'Complete' ) {
                 $('.form-status').addClass('alert-danger').html("Current status: <b>COMPLETE</b>. It looks like you've completed your application. Please revise carefully all the fields in the form and verify that your information is correct. You can then click 'Submit your final application' at the bottom.");
                 $('.form-status').addClass('alert-danger').html("Current status: <b>COMPLETE</b>. It looks like you've completed your application. Please review carefully all the fields in the form and verify that your information is correct. You can then click 'Submit your final application' at the bottom.");
             } else if( status === 'Saved' ){
             } else if( status === 'Saved' ){
                 $('.form-status').addClass('alert-success').html('Current status: <b>SAVED</b>. All filled! Status will change to complete upon next save.');
                 $('.form-status').removeClass('alert-primary').addClass('alert-success').html('Current status: <b>SAVED</b>. All filled! Status will change to complete upon next save.');
                 console.log('Setting STATUS to "Complete".');
                 console.log('All filled!');
                 $('input[name="status"]').val("Complete");
                 $('input[name="status"]').val("Complete");
             }
             }
        } else if( status === 'FirstSave' ) {
            $('.form-status').addClass('alert-info').html("Current status: <b>FirstSave</b>. Please continue filling out the form, saving as often as you need to save your progress.");
            $('input[name="status"]').val("Saved");
            console.log('First save. Take your time! Setting status to "Saved".');
         } else {
         } else {
             console.log("Fields still missing.");
             console.log('Missing fields:');
             var fieldsMissing = ''
             var fieldsMissing = ''
             classes.each( function(index, val) {
             mustFields.each( function(index, val) {
                 if( !$(this).val() ){
                 if( !$(this).val() ){
                     var name = $(this).attr("name");
                     var name = $(this).attr("name");
Line 42: Line 90:
             });
             });
             $('input[name="status"]').val("Saved");
             $('input[name="status"]').val("Saved");
             $('.form-status').addClass('alert-primary').html('Current status: <b>SAVED</b>. Some fields are still missing.' );
             $('.form-status').removeClass('alert-success').addClass('alert-primary').html('Current status: <b>SAVED</b>. Some fields are still missing.' );
         }
         }
    }


 
         checkTables();
    function checkForm() {
        $('#TfacsSubmission input, #TfacsSubmission select, #TfacsSubmission textarea').prop('disabled', false);
        if( $('select[name="retreattype[]"]').val() === 'Three-year retreat') {
            checkFields($('.MustField, .ThreeMustField'))
        } else if( $('select[name="retreattype[]"]').val() === 'Solitary hermitage') {
            checkFields($('.MustField, .SolMustField'))
         } else {
            console.log("Retreat type not set." + $('select[name="retreattype[]"]').val());
            $('.form-status').addClass('alert-primary').html('Current status: <b>SAVED</b>. Retreat type not set.' );
        }
     }
     }


    // Check individual section and toggles the "Complete" flag.
     function checkTables() {
     function checkTables() {
         if( $('select[name="retreattype[]"]').val() === 'Three-year retreat') {
         if( retreatType === 'Three-year retreat') {
             $('table.mw-made-collapsible').each(function(index, elem) {
             $('table').each(function(index, elem) {
                 if( elem.find('.MustField, .ThreeMustField').filter(function() { return this.value === ''; }).length === 0 ) {
                // console.log( 'CheckMRK for ' + $(this).attr('id') )
                     elem.children('caption checkMrk').show();
                 if( $(this).find('.MustField, .ThreeMustField').filter(function() { return this.value === ''; }).length === 0 ) {
                     $(this).children('caption').children('.checkMrk').show();
                } else {
                    $(this).children('caption').children('.checkMrk').hide();
                 }
                 }
             });
             });
         } else if( $('select[name="retreattype[]"]').val() === 'Solitary hermitage') {
         } else if( retreatType === 'Solitary hermitage') {
             $('table.mw-made-collapsible').each(function(index, elem) {
             $('table').each(function(index, elem) {
                 if( elem.find('.MustField, .SolMustField').filter(function() { return this.value === ''; }).length === 0 ) {
                // console.log( 'CheckMRK for ' + $(this).attr('id') )
                     elem.children('caption checkMrk').show();
                 if( $(this).find('.MustField, .SolMustField').filter(function() { return this.value === ''; }).length === 0 ) {
                     $(this).children('caption').children('.checkMrk').show();
                } else {
                    $(this).children('caption').children('.checkMrk').hide();
                 }
                 }
             });
             });
         }
         }
     }
     }


    if( ($('input[name="status"]').val() === 'Saved') || ($('input[name="status"]').val() === 'Complete') ) {
        jQuery(document).ready( checkForm(), checkTables() );
        $('#TfacsSubmission input, #TfacsSubmission select, #TfacsSubmission textarea').on( "keyup", checkTables() );
    } else {
        $('#TfacsSubmission input, #TfacsSubmission select, #TfacsSubmission textarea').not('#DummyField, #TfacsSubmitApplication').prop('disabled', true);
        $('.form-status').addClass('alert-dark').html('Current status: <b>SUBMITTED</b>. <br> Your application has been submitted. The contemplative scholarship administrator will contact you shortly.' );
    }
})
})

Latest revision as of 13:09, 4 September 2024

$( function() {


    // NAVIGATION FUNCTIONS

    $('.section-nav-toggles').click( function(event) {
        setTimeout( function() {
            var activeTable = $('.mw-collapsible:not(.mw-collapsed)')
            var atOffset = $(activeTable).offset().top
            var navBarHeight = $('#mw-navigation').outerHeight()
            $([document.documentElement, document.body]).animate({
                scrollTop: atOffset - ( navBarHeight * 2 ) - 7
            }, 0);
        }, "1");
        $(this).next().next('.section-nav-toggles').toggle();
        $(this).prev().prev('.section-nav-toggles').toggle();
        var nextText = 'Next section <i class="fa-duotone fa-diagram-next pl-1"></i>'
        var prevText = 'Previous section <i class="fa-duotone fa-diagram-previous pl-1"></i>'
        if ( $(this).html() == nextText ) {
           $(this).html(prevText);
        } else {
           $(this).html(nextText);
        }
    });



    // FORM CHECK FUNCTIONS

    var allInputs = $('#TfacsSubmission input, #TfacsSubmission select, #TfacsSubmission textarea');
    if( $('select[name="retreattype[]"]').val() === 'Three-year retreat') {
        var mustFields = $('.MustField, .ThreeMustField');
    } else if( $('select[name="retreattype[]"]').val() === 'Solitary hermitage') {
        var mustFields = $('.MustField, .SolMustField');
    }
    var retreatType = $('select[name="retreattype[]"]').val();
    var status = $('input[name="status"]').val();


    if( ( status === 'FirstSave' ) || ( status === 'Saved' ) || ( status === 'Complete' ) ) {
        $(checkForm());
        $('#TfacsSubmission input, #TfacsSubmission select, #TfacsSubmission textarea').keyup(checkForm);
    } else if ( ( status === 'Submitted' ) || ( status === 'Checked' ) ) {
        allInputs.not('#DummyField, #TfacsSubmitApplication').prop('disabled', true);
        $('.form-status').addClass('alert-dark').html('Current status: <b>SUBMITTED</b>. <br> Your application has been submitted. The contemplative scholarship administrator will contact you shortly.' );
    }

    $('#TfacsSubmission input, #TfacsSubmission select, #TfacsSubmission textarea').keyup(checkTables);







    // Check the form content
    function checkForm() {
        allInputs.prop('disabled', false);
        if (mustFields) {
            checkFields(mustFields);
        } else {
            console.log("Retreat type not set.");
            $('.form-status').addClass('alert-primary').html('Current status: <b>SAVED</b>. Retreat type not set.' );
        }
    }

    // Check for required fields
    function checkFields(mustFields) {
        if( mustFields.filter(function() { return this.value === ''; }).length === 0 ) {
            if( status === 'Complete' ) {
                $('.form-status').addClass('alert-danger').html("Current status: <b>COMPLETE</b>. It looks like you've completed your application. Please review carefully all the fields in the form and verify that your information is correct. You can then click 'Submit your final application' at the bottom.");
            } else if( status === 'Saved' ){
                $('.form-status').removeClass('alert-primary').addClass('alert-success').html('Current status: <b>SAVED</b>. All filled! Status will change to complete upon next save.');
                console.log('All filled!');
                $('input[name="status"]').val("Complete");
            }
        } else if( status === 'FirstSave' ) {
            $('.form-status').addClass('alert-info').html("Current status: <b>FirstSave</b>. Please continue filling out the form, saving as often as you need to save your progress.");
            $('input[name="status"]').val("Saved");
            console.log('First save. Take your time! Setting status to "Saved".');
        } else {
            console.log('Missing fields:');
            var fieldsMissing = ''
            mustFields.each( function(index, val) {
                if( !$(this).val() ){
                    var name = $(this).attr("name");
                    console.log( index + ': ' + name );
                    fieldsMissing = fieldsMissing + '<div>' + index + ': ' + name + '</div>'
                }
            });
            $('input[name="status"]').val("Saved");
            $('.form-status').removeClass('alert-success').addClass('alert-primary').html('Current status: <b>SAVED</b>. Some fields are still missing.' );
        }

        checkTables();
    }

    // Check individual section and toggles the "Complete" flag.
    function checkTables() {
        if( retreatType === 'Three-year retreat') {
            $('table').each(function(index, elem) {
                // console.log( 'CheckMRK for ' + $(this).attr('id') )
                if( $(this).find('.MustField, .ThreeMustField').filter(function() { return this.value === ''; }).length === 0 ) {
                    $(this).children('caption').children('.checkMrk').show();
                } else {
                    $(this).children('caption').children('.checkMrk').hide();
                }
            });
        } else if( retreatType === 'Solitary hermitage') {
            $('table').each(function(index, elem) {
                // console.log( 'CheckMRK for ' + $(this).attr('id') )
                if( $(this).find('.MustField, .SolMustField').filter(function() { return this.value === ''; }).length === 0 ) {
                    $(this).children('caption').children('.checkMrk').show();
                } else {
                    $(this).children('caption').children('.checkMrk').hide();
                }
            });
        }
    }  

})