Created
August 3, 2020 18:04
-
-
Save amberhinds/55ceda022a7a941af3bb2588c2b33c4b to your computer and use it in GitHub Desktop.
Base JS file from the Workforce Solutions Portal; includes GP Nested Forms Accessibility Fixes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* RWC Base entry point. | |
* | |
* @package RWC Base JS | |
* @author Road Warrior Creative | |
* @license GPL-2.0-or-later | |
*/ | |
var rwcBase = ( function( $ ) { | |
'use strict'; | |
// Accessibility fixes form GP Nest Forms | |
if($('.gform_wrapper').length > 0){ | |
let nestedFormsSubmitText = false; | |
let nestedFormsFirstInput = false; | |
function checkDOMChange(){ | |
if($('.gform_wrapper').length > 0){ | |
if(($('.gpnf-btn-submit').length > 0)){ | |
$('.gpnf-btn-submit').html('Submit'); | |
} | |
if(($('.tingle-modal-box__content form').length > 0) && (nestedFormsFirstInput == false)){ | |
var firstInput = $('.tingle-modal-box__content form').find('*').filter(':input:visible:first'); | |
firstInput.select(); | |
$('.tingle-modal-box__content form').prepend("<p class='screen-reader-text' role='alert'>Popup Open</p>"); | |
nestedFormsFirstInput = true; | |
} | |
// call the function again after 1000 milliseconds | |
setTimeout( checkDOMChange, 100 ); | |
} | |
} | |
$('.gpnf-add-entry').click(function(){ | |
nestedFormsFirstInput = false; | |
checkDOMChange(); | |
}); | |
} | |
// Accessibility fixes for Gravity Forms | |
$('.add_list_item img').each(function(){ | |
$(this).attr('alt','Add a new row'); | |
}); | |
$('.delete_list_item img').each(function(){ | |
$(this).attr('alt','Remove this row'); | |
}); | |
$('.ginput_container_password button').each(function(){ | |
$(this).append('<span class="screen-reader-text">Enable Password Visibility</span>'); | |
}); | |
// Accessibility custom logo title | |
$('.custom-logo-link').attr('title', 'Return To Home Page'); | |
// Step complete | |
$( ".rwc-wspp-complete-step-form" ).submit(function( event ) { | |
event.preventDefault(); | |
process_step_entry(); | |
}); | |
/* | |
Step Entry Ajax | |
*/ | |
function process_step_entry(){ | |
let complete_step = true; | |
// disable the submit button | |
$("#rwc-wspp-mark-complete").attr("disabled", true); | |
// Validate Quiz | |
if (document.getElementById('process-step-quiz')) { | |
var check = true; | |
$("input:radio").each(function(){ | |
var name = $(this).attr("name"); | |
if($("input:radio[name="+name+"]:checked").length == 0){ | |
check = false; | |
} | |
}); | |
if(check){ | |
// One radio in each group is checked. | |
let error = false; | |
// loop through all checked questions | |
$( "[name^=question-]:checked" ).each(function( i ) { | |
// if not checked or doesn't equal true error out | |
if($(this).prop('value') != 1){ | |
error = true; | |
} | |
}); | |
if(error == true){ | |
completeError('You have answered some questions incorrectly. Please try again.'); | |
} | |
}else{ | |
completeError('Please select one option in each question.'); | |
} | |
} | |
// Error function | |
function completeError(message){ | |
complete_step = false; | |
alert(message); | |
$("#rwc-wspp-mark-complete").removeAttr("disabled", true); | |
} | |
// Validate Confirm Agreement | |
let off_site_link_confirmation = document.getElementById('off-site-link-confirmation'); | |
if (off_site_link_confirmation) { | |
if(!off_site_link_confirmation.checked){ | |
completeError('Please check the confirm agreement checkbox.'); | |
} | |
} | |
if(complete_step == true){ | |
let process = $('input[name="rwc-wspp-process"]').val(); | |
let step = $('input[name="rwc-wspp-step"]').val(); | |
let step_type = $('input[name="rwc-wspp-step-type"]').val(); | |
let resource = $('input[name="rwc-wspp-resource"]').val(); | |
jQuery.post(ajaxurl, {action: "wspp_step_entry", step: step, step_type: step_type, process: process, resource: resource }, function(response){ | |
let data = response; | |
data = JSON.parse(data); | |
if(!data){ | |
window.location='/'; | |
}else{ | |
window.location=data; | |
} | |
}); | |
} | |
}; | |
/* | |
Alert's | |
*/ | |
$('.close').click(function(){ | |
jQuery.post(ajaxurl, {action: "dismiss_alert", alert_id: $(this).data('alert-id') }, function(response){ | |
let data = response; | |
data = JSON.parse(data); | |
}); | |
$(this).parent().fadeOut("normal", function() { | |
$(this).remove(); | |
}); | |
}); | |
/* | |
FAQ's | |
*/ | |
$('.video-transcript-title').click(function(){ | |
$('.video-transcript').toggleClass('active'); | |
$('.video-transcript-content').slideToggle(); | |
}); | |
/* | |
Todo's | |
*/ | |
$('input[name="todo-complete"]').click(function(){ | |
let complete; | |
if($(this).prop("checked") == true){ | |
complete = 'complete'; | |
} | |
else if($(this).prop("checked") == false){ | |
complete = null; | |
} | |
let todo = $(this).val(); | |
$.post(ajaxurl, {action: "rwc_wspp_todo_complete", todo: todo, complete: complete }, function(response){ | |
let data = response; | |
data = JSON.parse(data); | |
}); | |
}); | |
/* | |
Single File Upload File Name | |
*/ | |
if ($("#input_3_1")[0]){ | |
document.getElementById("input_3_1").onchange = function () { | |
var filename = this.value.replace(/C:\\fakepath\\/i, ''); | |
if(filename){ | |
$( ".fileupload-filename" ).remove(); | |
$("#field_3_1").append('<div class="fileupload-filename"><i class="wspp-checkmark"></i>'+filename+'</div>'); | |
} | |
}; | |
} | |
/* | |
Process Nav Mobile Menu Toggle | |
*/ | |
$('.nav-process-title').click(function(){ | |
var win = $(window); | |
if (win.width() <= 768) { | |
$('.nav-process ol').slideToggle(); | |
$('.nav-process-title').toggleClass('active'); | |
} | |
$(window).on('resize', function(){ | |
var win = $(this); | |
if (win.width() >= 768) { | |
$('.nav-process ol').show(); | |
$('.nav-process-title').removeClass('active'); | |
}else{ | |
$('.nav-process ol').hide(); | |
} | |
}); | |
}); | |
/** | |
* Adjust site inner margin top to compensate for sticky header height. | |
* | |
* @since 2.6.0 | |
*/ | |
var moveContentBelowFixedHeader = function() { | |
var siteInnerMarginTop = 0; | |
if( $('.site-header').css('position') === 'fixed' ) { | |
siteInnerMarginTop = $('.site-header').outerHeight(); | |
} | |
$('.site-inner').css('margin-top', siteInnerMarginTop); | |
$('.nav-primary').css('margin-top', siteInnerMarginTop); | |
}, | |
/** | |
* Initialize RWC Base. | |
* | |
* Internal functions to execute on full page load. | |
* | |
* @since 2.6.0 | |
*/ | |
load = function() { | |
moveContentBelowFixedHeader(); | |
$( window ).resize(function() { | |
moveContentBelowFixedHeader(); | |
}); | |
// Run after the Customizer updates. | |
// 1.5s delay is to allow logo area reflow. | |
if (typeof wp != "undefined" && typeof wp.customize != "undefined") { | |
wp.customize.bind( 'change', function ( setting ) { | |
setTimeout(function() { | |
moveContentBelowFixedHeader(); | |
}, 1500); | |
}); | |
} | |
// GP Nested Forms Close Button Screen Reader Text | |
if($('.tingle-modal__close').length > 0){ | |
$('.tingle-modal__close').append('<span class="screen-reader-text">Close</span>'); | |
} | |
}; | |
// Search toggle | |
$('.nav-primary .search-toggle').click(function(e){ | |
e.preventDefault(); | |
$(this).parent().toggleClass('active').find('input[type="search"]').focus(); | |
}); | |
$('.search-submit').click(function(e){ | |
if( $(this).parent().find('.search-field').val() == '' ) { | |
e.preventDefault(); | |
$(this).parent().parent().removeClass('active'); | |
} | |
}); | |
/* | |
Gravity forms date picker force week to start on Sunday | |
*/ | |
if (typeof gform !== 'undefined') { | |
gform.addFilter( 'gform_datepicker_options_pre_init', function( optionsObj, formId, fieldId ) { | |
// Sunday is 0, Monday is 1, etc. | |
optionsObj.firstDay = 0; | |
return optionsObj; | |
}); | |
} | |
// Expose the load and ready functions. | |
return { | |
load: load | |
}; | |
})( jQuery ); | |
jQuery( window ).on( 'load', rwcBase.load ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment