From f7d130f413184d1b309c24dae7edf60922da9785 Mon Sep 17 00:00:00 2001 From: Patrick McNeil Date: Tue, 15 Dec 2020 15:37:00 -0500 Subject: [PATCH 1/4] Extends the cf7 autofill to include select menus and checkboxes Select menus were plug and play. Checkboxes were also similarly simple to add/retrieve. I did *not* investigate the "update matching form fields on other windows/tabs" feature as I don't fully understand how it triggers/works. --- assets/js/nebula.js | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/assets/js/nebula.js b/assets/js/nebula.js index 94c258c10..7b9d44b58 100644 --- a/assets/js/nebula.js +++ b/assets/js/nebula.js @@ -2728,23 +2728,34 @@ nebula.cf7LocalStorage = function(){ return false; } - jQuery('.wpcf7-textarea, .wpcf7-text').each(function(){ + jQuery('.wpcf7-textarea, .wpcf7-text, .wpcf7-select, .wpcf7-checkbox input').each(function(){ var thisLocalStorageVal = localStorage.getItem('cf7_' + jQuery(this).attr('name')); - //Fill textareas with localstorage data on load + //Complete inputs with localstorage data on load if ( !jQuery(this).hasClass('do-not-store') && !jQuery(this).hasClass('.wpcf7-captchar') && thisLocalStorageVal && thisLocalStorageVal !== 'undefined' && thisLocalStorageVal !== '' ){ - if ( jQuery(this).val() === '' ){ //Don't overwrite a field that already has text in it! + if( jQuery(this).attr('type') == 'checkbox' ){ + // local storange vals are stored as strings but the .prop method requires a bool + if( thisLocalStorageVal == 'true' ){ + jQuery(this).prop('checked', true); + }else{ + jQuery(this).prop('checked', false); + } + }else if( jQuery(this).val() === '' ){ //Don't overwrite a field that already has text in it! jQuery(this).val(thisLocalStorageVal).trigger('keyup'); - } - jQuery(this).blur(); + jQuery(this).blur(); + } } else { localStorage.removeItem('cf7_' + jQuery(this).attr('name')); //Remove localstorage if it is undefined or inelligible } //Update localstorage data jQuery(this).on('keyup blur', function(){ - if ( !jQuery(this).hasClass('do-not-store') && !jQuery(this).hasClass('.wpcf7-captchar') ){ - localStorage.setItem('cf7_' + jQuery(this).attr('name'), jQuery(this).val()); + if ( !jQuery(this).hasClass('do-not-store') && !jQuery(this).hasClass('.wpcf7-captchar') ){ + if( jQuery(this).attr('type') == 'checkbox' ){ + localStorage.setItem('cf7_' + jQuery(this).attr('name'), jQuery(this).prop('checked')); + }else{ + localStorage.setItem('cf7_' + jQuery(this).attr('name'), jQuery(this).val()); + } } }); }); From 849267bf7a5e49b231e35b62777c19398532d27a Mon Sep 17 00:00:00 2001 From: Patrick McNeil Date: Tue, 15 Dec 2020 19:43:17 -0500 Subject: [PATCH 2/4] Revert "Extends the cf7 autofill to include select menus and checkboxes" This reverts commit f7d130f413184d1b309c24dae7edf60922da9785. --- assets/js/nebula.js | 25 +++++++------------------ 1 file changed, 7 insertions(+), 18 deletions(-) diff --git a/assets/js/nebula.js b/assets/js/nebula.js index 7b9d44b58..94c258c10 100644 --- a/assets/js/nebula.js +++ b/assets/js/nebula.js @@ -2728,34 +2728,23 @@ nebula.cf7LocalStorage = function(){ return false; } - jQuery('.wpcf7-textarea, .wpcf7-text, .wpcf7-select, .wpcf7-checkbox input').each(function(){ + jQuery('.wpcf7-textarea, .wpcf7-text').each(function(){ var thisLocalStorageVal = localStorage.getItem('cf7_' + jQuery(this).attr('name')); - //Complete inputs with localstorage data on load + //Fill textareas with localstorage data on load if ( !jQuery(this).hasClass('do-not-store') && !jQuery(this).hasClass('.wpcf7-captchar') && thisLocalStorageVal && thisLocalStorageVal !== 'undefined' && thisLocalStorageVal !== '' ){ - if( jQuery(this).attr('type') == 'checkbox' ){ - // local storange vals are stored as strings but the .prop method requires a bool - if( thisLocalStorageVal == 'true' ){ - jQuery(this).prop('checked', true); - }else{ - jQuery(this).prop('checked', false); - } - }else if( jQuery(this).val() === '' ){ //Don't overwrite a field that already has text in it! + if ( jQuery(this).val() === '' ){ //Don't overwrite a field that already has text in it! jQuery(this).val(thisLocalStorageVal).trigger('keyup'); - jQuery(this).blur(); - } + } + jQuery(this).blur(); } else { localStorage.removeItem('cf7_' + jQuery(this).attr('name')); //Remove localstorage if it is undefined or inelligible } //Update localstorage data jQuery(this).on('keyup blur', function(){ - if ( !jQuery(this).hasClass('do-not-store') && !jQuery(this).hasClass('.wpcf7-captchar') ){ - if( jQuery(this).attr('type') == 'checkbox' ){ - localStorage.setItem('cf7_' + jQuery(this).attr('name'), jQuery(this).prop('checked')); - }else{ - localStorage.setItem('cf7_' + jQuery(this).attr('name'), jQuery(this).val()); - } + if ( !jQuery(this).hasClass('do-not-store') && !jQuery(this).hasClass('.wpcf7-captchar') ){ + localStorage.setItem('cf7_' + jQuery(this).attr('name'), jQuery(this).val()); } }); }); From b6f54312dc200a1c9e1fa520eb03053c52bc7d3d Mon Sep 17 00:00:00 2001 From: Patrick McNeil Date: Tue, 15 Dec 2020 21:57:42 -0500 Subject: [PATCH 3/4] Updates to the CF7 local storage autofilling --- assets/js/nebula.js | 42 ++++++++++++++++++++++++++++++------------ 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/assets/js/nebula.js b/assets/js/nebula.js index 94c258c10..ffb43833c 100644 --- a/assets/js/nebula.js +++ b/assets/js/nebula.js @@ -2728,32 +2728,50 @@ nebula.cf7LocalStorage = function(){ return false; } - jQuery('.wpcf7-textarea, .wpcf7-text').each(function(){ + jQuery('.wpcf7-textarea, .wpcf7-text, .wpcf7-select, .wpcf7-checkbox input, .wpcf7-radio input').each(function(){ var thisLocalStorageVal = localStorage.getItem('cf7_' + jQuery(this).attr('name')); - - //Fill textareas with localstorage data on load + //Complete inputs with localstorage data on load if ( !jQuery(this).hasClass('do-not-store') && !jQuery(this).hasClass('.wpcf7-captchar') && thisLocalStorageVal && thisLocalStorageVal !== 'undefined' && thisLocalStorageVal !== '' ){ - if ( jQuery(this).val() === '' ){ //Don't overwrite a field that already has text in it! - jQuery(this).val(thisLocalStorageVal).trigger('keyup'); + if( thisLocalStorageVal !== null ){ + if ( jQuery(this).attr('type') === 'checkbox' || jQuery(this).attr('type') === 'radio' ) { + jQuery(this).prop('checked', (jQuery(this).val() === thisLocalStorageVal)); + } else { + // This must be a text or textarea input + jQuery(this).val( thisLocalStorageVal ); + } + jQuery(this).addClass('nebula-autofilled'); } - jQuery(this).blur(); } else { localStorage.removeItem('cf7_' + jQuery(this).attr('name')); //Remove localstorage if it is undefined or inelligible } //Update localstorage data - jQuery(this).on('keyup blur', function(){ - if ( !jQuery(this).hasClass('do-not-store') && !jQuery(this).hasClass('.wpcf7-captchar') ){ - localStorage.setItem('cf7_' + jQuery(this).attr('name'), jQuery(this).val()); + jQuery(this).on('keyup blur click', function(){ + if ( !jQuery(this).hasClass('do-not-store') && !jQuery(this).hasClass('.wpcf7-captchar') ) { + var thisVal = jQuery(this).val(); + if ( jQuery(this).attr('type') === 'checkbox' ) { + thisVal = null; + if ( jQuery(this).prop('checked') ) { + thisVal = jQuery(this).val(); + } + } + localStorage.setItem('cf7_' + jQuery(this).attr('name'), thisVal); } }); }); //Update matching form fields on other windows/tabs nebula.dom.window.on('storage', function(e){ //This causes an infinite loop in IE11 - jQuery('.wpcf7-textarea, .wpcf7-text').each(function(){ + jQuery('.wpcf7-textarea, .wpcf7-text, .wpcf7-select, .wpcf7-checkbox input, .wpcf7-radio input').each(function(){ if ( !jQuery(this).hasClass('do-not-store') && !jQuery(this).hasClass('.wpcf7-captchar') ){ - jQuery(this).val(localStorage.getItem('cf7_' + jQuery(this).attr('name'))).trigger('keyup'); + var thisLocalStorageVal = localStorage.getItem('cf7_' + jQuery(this).attr('name')); + if( thisLocalStorageVal !== null ){ + if( jQuery(this).attr('type') === 'checkbox' || jQuery(this).attr('type') === 'radio' ){ + jQuery(this).prop('checked', (jQuery(this).val() === thisLocalStorageVal)); + } else { + jQuery(this).val( thisLocalStorageVal ); + } + } } }); }); @@ -2761,7 +2779,7 @@ nebula.cf7LocalStorage = function(){ //Clear localstorage when AJAX submit fails (but submit still succeeds) if ( window.location.hash.indexOf('wpcf7') > 0 ){ if ( jQuery(escape(window.location.hash) + ' .wpcf7-mail-sent-ok').length ){ - jQuery(escape(window.location.hash) + ' .wpcf7-textarea, ' + escape(window.location.hash) + ' .wpcf7-text').each(function(){ + jQuery(escape(window.location.hash) + ' .wpcf7-textarea, ' + escape(window.location.hash) + ' .wpcf7-text, ' + escape(window.location.hash) + ' .wpcf7-checkbox input, ' + escape(window.location.hash) + ' .wpcf7-radio input, ' + escape(window.location.hash) + ' .wpcf7-select, ').each(function(){ localStorage.removeItem('cf7_' + jQuery(this).attr('name')); jQuery(this).val('').trigger('keyup'); }); From 694eab329d002072ed8fe6034943c65fc265f19c Mon Sep 17 00:00:00 2001 From: Patrick McNeil Date: Tue, 15 Dec 2020 22:17:26 -0500 Subject: [PATCH 4/4] Formatting edits --- assets/js/nebula.js | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/assets/js/nebula.js b/assets/js/nebula.js index ffb43833c..d450d4c94 100644 --- a/assets/js/nebula.js +++ b/assets/js/nebula.js @@ -2732,11 +2732,10 @@ nebula.cf7LocalStorage = function(){ var thisLocalStorageVal = localStorage.getItem('cf7_' + jQuery(this).attr('name')); //Complete inputs with localstorage data on load if ( !jQuery(this).hasClass('do-not-store') && !jQuery(this).hasClass('.wpcf7-captchar') && thisLocalStorageVal && thisLocalStorageVal !== 'undefined' && thisLocalStorageVal !== '' ){ - if( thisLocalStorageVal !== null ){ - if ( jQuery(this).attr('type') === 'checkbox' || jQuery(this).attr('type') === 'radio' ) { + if ( thisLocalStorageVal !== null ){ + if ( jQuery(this).attr('type') === 'checkbox' || jQuery(this).attr('type') === 'radio' ){ jQuery(this).prop('checked', (jQuery(this).val() === thisLocalStorageVal)); } else { - // This must be a text or textarea input jQuery(this).val( thisLocalStorageVal ); } jQuery(this).addClass('nebula-autofilled'); @@ -2747,11 +2746,11 @@ nebula.cf7LocalStorage = function(){ //Update localstorage data jQuery(this).on('keyup blur click', function(){ - if ( !jQuery(this).hasClass('do-not-store') && !jQuery(this).hasClass('.wpcf7-captchar') ) { + if ( !jQuery(this).hasClass('do-not-store') && !jQuery(this).hasClass('.wpcf7-captchar') ){ var thisVal = jQuery(this).val(); - if ( jQuery(this).attr('type') === 'checkbox' ) { + if ( jQuery(this).attr('type') === 'checkbox' ){ thisVal = null; - if ( jQuery(this).prop('checked') ) { + if ( jQuery(this).prop('checked') ){ thisVal = jQuery(this).val(); } } @@ -2765,8 +2764,8 @@ nebula.cf7LocalStorage = function(){ jQuery('.wpcf7-textarea, .wpcf7-text, .wpcf7-select, .wpcf7-checkbox input, .wpcf7-radio input').each(function(){ if ( !jQuery(this).hasClass('do-not-store') && !jQuery(this).hasClass('.wpcf7-captchar') ){ var thisLocalStorageVal = localStorage.getItem('cf7_' + jQuery(this).attr('name')); - if( thisLocalStorageVal !== null ){ - if( jQuery(this).attr('type') === 'checkbox' || jQuery(this).attr('type') === 'radio' ){ + if ( thisLocalStorageVal !== null ){ + if ( jQuery(this).attr('type') === 'checkbox' || jQuery(this).attr('type') === 'radio' ){ jQuery(this).prop('checked', (jQuery(this).val() === thisLocalStorageVal)); } else { jQuery(this).val( thisLocalStorageVal );