1/* Contains custom javascript initialized by Plain Text CH cards. */ 2 3/* Workaround product limitation/bug, partial reload of donate page wipes out fields. */ 4function saveDonateFormFields() { 5 saveDonateFormFieldsOnLoad(); 6 saveDonateFormFieldsOnUpdate(); 7 restoreDonateFormFields(); 8} 9 10/* Save pre-filled inputs to local storage. */ 11function saveDonateFormFieldsOnLoad() { 12 $('input[type="text"]').each(function(){ 13 setLocalStorageValue(this); 14 }); 15 16 $('textarea').each(function(){ 17 setLocalStorageValue(this); 18 }); 19 20 $('input[type="checkbox"]').each(function(){ 21 setLocalStorageBoolean(this); 22 }); 23 24 $('select').each(function(){ 25 setLocalStorageValue(this); 26 }); 27} 28 29function setLocalStorageValue(targetNode) { 30 var id = $(targetNode).attr('id'); 31 /* On postback, the SF changes the GUID, so we must key by label text. */ 32 var keyName = $('label[for="' + id + '"]').text().replace(/\s/g, ''); 33 if (keyName.length === 0) { 34 /* Catch Address/Payment fields alt format */ 35 var relatedLabel = $(targetNode).parent().prev(); 36 37 if (relatedLabel.attr('for') !== undefined && relatedLabel.attr('for').indexOf('credit-card') >= 0) { 38 /* Don't store CC info */ 39 return; 40 } 41 42 keyName = relatedLabel.text().replace(/\s/g, ''); 43 44 if (keyName.length === 0) { 45 /* Catch $ Amount that has no label */ 46 relatedLabel = $(targetNode).prev(); 47 if (relatedLabel.text() === '$') { 48 keyName = '$'; 49 $(targetNode).val($(targetNode).val()); 50 $(targetNode).attr('value', $(targetNode).val()); 51 } 52 } 53 } 54 55 if (keyName.length === 0) { 56 return; 57 } 58 59 var value = $(targetNode).val(); 60 /* Strip whitespace */ 61 localStorage.setItem(keyName.replace(/\s/g, ''), value); 62} 63 64function setLocalStorageBoolean(targetNode) { 65 var keyName = $(targetNode).next().text(); 66 var value = $(targetNode).prop('checked'); 67 localStorage.setItem(keyName.replace(/\s/g, ''), value); 68} 69 70function saveDonateFormFieldsOnUpdate() { 71 /* On update trigger, save inputs to local storage. */ 72 $('input[type="text"]').keyup(function(){ 73 setLocalStorageValue(this); 74 }); 75 76 $('textarea').keyup(function(){ 77 setLocalStorageValue(this); 78 }); 79 80 $('input[type="checkbox"]').click(function(){ 81 setLocalStorageBoolean(this); 82 }); 83 84 $('select').change(function(){ 85 setLocalStorageValue(this); 86 }); 87} 88 89/* Restore inputs with values from local storage. */ 90function restoreDonateFormFields() { 91 92 /* Create a mutation observer so we can be notified when the page is partially reloaded. */ 93 var targetNodes = $(document); 94 var MutationObserver = window.MutationObserver || window.WebKitMutationObserver; 95 var myObserver = new MutationObserver (mutationHandler); 96 var obsConfig = { childList: true, characterData: true, attributes: true, subtree: true }; 97 98 targetNodes.each ( function () { 99 myObserver.observe (this, obsConfig); 100 } ); 101 102 function mutationHandler (mutationRecords) { 103 mutationRecords.forEach ( function (mutation) { 104 105 if (mutation.target.id != null) { 106 107 if (mutation.target.id.indexOf('ViewState') >= 0 || mutation.target.id.indexOf('formField')) { 108 var isUpdated = false; 109 // Restore stored value. 110 $('input[type="text"]').each(function(){ 111 restoreLocalStorageValue(this); 112 isUpdated = true; 113 }); 114 115 $('textarea').each(function(){ 116 if( !$(this).val() ) { 117 restoreLocalStorageValue(this); 118 isUpdated = true; 119 } 120 }); 121 122 $('select').each(function(){ 123 if( !$(this).val() ) { 124 restoreLocalStorageValue(this); 125 isUpdated = true; 126 } 127 }); 128 129 $('input[type="checkbox"]').each(function(){ 130 restoreLocalStorageBoolean(this); 131 isUpdated = true; 132 133 }); 134 135 if (isUpdated) { 136 /* Rebind the new/updated dom elements. */ 137 saveDonateFormFieldsOnUpdate(); 138 } 139 } 140 } 141 } ); 142 } 143} 144 145function restoreLocalStorageValue(targetNode) { 146 var id = $(targetNode).attr('id'); 147 148 var keyName = ''; 149 if (id !== undefined) { 150 keyName = $('label[for="' + id + '"]').text(); 151 } 152 153 if (keyName.length === 0) { 154 /* Catch Address/Payment fields alt format */ 155 var relatedLabel = $(targetNode).parent().prev(); 156 157 if (relatedLabel.attr('for') !== undefined && relatedLabel.attr('for').indexOf('credit-card') >= 0) { 158 /* CC Info wasn't stored */ 159 return; 160 } 161 162 keyName = relatedLabel.text().replace(/\s/g, ''); 163 164 if (keyName.length === 0) { 165 /* Catch $ Amount that has no label */ 166 relatedLabel = $(targetNode).prev(); 167 168 if (relatedLabel.text() === '$') { 169 keyName = '$'; 170 } 171 } 172 } 173 174 keyName = keyName.replace(/\s/g, ''); 175 if (keyName.length === 0) { 176 return; 177 } 178 179 var value = localStorage.getItem(keyName); 180 181 $(targetNode).val(value); 182} 183 184function restoreLocalStorageBoolean(targetNode) { 185 var keyName = $(targetNode).next().text(); 186 var value = localStorage.getItem(keyName.replace(/\s/g, '')); 187 188 $(targetNode).prop('checked', value === 'true'); 189}
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.