1( function ( $ ) { 2 "use strict"; 3 4 const FormBuilder = { 5 6 refund_form_qty_fields: $( '#fr_refund_table' ), 7 8 init: function () { 9 $( '#fb-field-type' ).val( '' ); 10 $( '#fb-field-name' ).val( '' ); 11 $( '#fb-field-label' ).val( '' ); 12 }, 13 14 formatMoney: function ( number, decPlaces, decSep, thouSep ) { 15 decPlaces = isNaN( decPlaces = Math.abs( decPlaces ) ) ? 2 : decPlaces, 16 decSep = typeof decSep === "undefined" ? "." : decSep; 17 thouSep = typeof thouSep === "undefined" ? "," : thouSep; 18 var sign = number < 0 ? "-" : ""; 19 var i = String( parseInt( number = Math.abs( Number( number ) || 0 ).toFixed( decPlaces ) ) ); 20 var j = ( j = i.length ) > 3 ? j % 3 : 0; 21 22 return sign + 23 ( j ? i.substr( 0, j ) + thouSep : "" ) + 24 i.substr( j ).replace( /(\decSep{3})(?=\decSep)/g, "$1" + thouSep ) + 25 ( decPlaces ? decSep + Math.abs( number - i ).toFixed( decPlaces ).slice( 2 ) : "" ); 26 }, 27 28 number_format: function ( value ) { 29 return this.formatMoney( value.toString(), 2, fr_front_i18n.decimal_point, fr_front_i18n.thousand_point ); 30 }, 31 32 calculateRefundTotals: function () { 33 let _this = this; 34 _this.refund_form_qty_fields.on( 'change', '.item-qty input', function () { 35 let qty = $( this ).val(); 36 let qty_max = parseInt( $( this ).attr( 'max' ) ); 37 let value = parseFloat( $( this ).attr( 'data-item-price' ) ) * qty; 38 if ( $( this ).attr( 'type' ) === 'checkbox' ) { 39 if ( $( this ).prop( 'checked' ) ) { 40 value = parseFloat( $( this ).attr( 'data-item-price' ) ); 41 } else { 42 value = 0; 43 } 44 } 45 46 $( this ).closest( 'tr' ).find( '.item-total-refund-qty' ).html( _this.number_format( value ) ); 47 48 let total_amount = 0; 49 let total_qty = 0; 50 let total_amount_wrapper = $( '.refund-total-calc' ); 51 let total_qty_wrapper = $( '.refund-total-qty' ); 52 let total_qty_input = $( '#refund-total-qty-input' ); 53 $( this ).closest( 'table' ).find( '.item-qty input' ).each( function () { 54 let qty = $( this ).val(); 55 let qty_max = parseInt( $( this ).attr( 'max' ) ); 56 let total_value = parseFloat( $( this ).attr( 'data-item-price' ) ) * qty; 57 if ( $( this ).attr( 'type' ) === 'checkbox' ) { 58 if ( $( this ).prop( 'checked' ) ) { 59 total_value = parseFloat( $( this ).attr( 'data-item-price' ) ); 60 qty = 1; 61 } else { 62 total_value = 0; 63 qty = 0; 64 } 65 } 66 67 68 total_amount += parseFloat( total_value ); 69 total_qty += parseInt( qty ); 70 } ) 71 72 total_amount_wrapper.html( _this.number_format( total_amount ) ); 73 total_qty_wrapper.html( total_qty ); 74 total_qty_input.val( total_qty ); 75 } ); 76 77 $( '.woocommerce-table-refund-details .item-qty input' ).trigger( 'change' ); 78 }, 79 80 validateUploadInput: function () { 81 $( '.refund-front-form' ).find( 'input[type="file"]' ).each( function ( i, v ) { 82 let field = $( v ); 83 const limit = field.attr( 'data-files-limit' ); 84 85 $( this ).on( 'change', () => { 86 const fileLimit = parseInt( limit ); 87 const fileCount = this.files ? this.files.length : 0; 88 let notice_wrapper = field.closest( '.field-row' ).find( '.fr-required-field-notice' ); 89 90 if ( fileCount > fileLimit ) { 91 notice_wrapper.html( `<span class="label-error-required">${fr_front_i18n.files_limit} (limit: ${fileLimit})</span>` ); 92 this.value = ''; 93 } else { 94 notice_wrapper.html( '' ); 95 } 96 } ) 97 } ); 98 }, 99 100 validateRefundForm: function () { 101 $( '.refund-front-form' ).submit( function () { 102 let is_valid = true; 103 let qty_total = 0; 104 $( this ).find( '.qty-input' ).each( function ( i, v ) { 105 let qty_value = parseInt( $( this ).val() ); 106 if ( $( this ).attr( 'type' ) === 'checkbox' ) { 107 if ( $( this ).prop( 'checked' ) ) { 108 qty_value = 1; 109 } else { 110 qty_value = 0; 111 } 112 } 113 114 qty_total += parseInt( qty_value ); 115 } ); 116 117 if ( qty_total === 0 ) { 118 is_valid = false; 119 } 120 121 if ( ! is_valid ) { 122 $( '#fr-front-refund-table-errors' ).html( '<p class="required fr-form-error">' + fr_front_i18n.qty_empty + '</p>' ); 123 } else { 124 $( '#fr-front-refund-table-errors' ).html( '' ); 125 } 126 127 var name_map = {}; 128 $( '.field-required' ).find( 'input,textarea,select' ).each( function ( i, v ) { 129 let field = $( v ); 130 let type = field.attr( 'type' ); 131 let name = field.attr( 'name' ); 132 let notice_wrapper = field.closest( '.field-row' ).find( '.fr-required-field-notice' ); 133 if ( type === 'radio' || type === 'checkbox' ) { 134 if ( $( 'input[name^="' + name + '"]:checked' ).length < 1 ) { 135 is_valid = false;
136 name_map[ name ] = field; 137 notice_wrapper.html( '<span class="label-error-required">' + fr_front_i18n.required_field + '</span>' ); 138 } else { 139 notice_wrapper.html( '' ) 140 } 141 } else { 142 if ( field.val() === '' ) { 143 is_valid = false; 144 name_map[ name ] = field; 145 notice_wrapper.html( '<span class="label-error-required">' + fr_front_i18n.required_field + '</span>' ); 146 } else { 147 notice_wrapper.html( '' ) 148 } 149 } 150 } ); 151 152 return is_valid; 153 } ); 154 }, 155 156 checkAll: function () { 157 let _this = this; 158 159 var button = $( '.check-all-button button' ); 160 var check_text = button.attr( 'data-text-check' ); 161 var uncheck_text = button.attr( 'data-text-uncheck' ); 162 163 if ( _this.isAllInputChecked() ) { 164 button.html( uncheck_text ); 165 button.addClass( 'uncheck' ); 166 } else { 167 button.html( check_text ); 168 button.removeClass( 'uncheck' ); 169 } 170 171 button.on( 'click', function () { 172 if ( ! $( this ).hasClass( 'uncheck' ) ) { 173 $( this ).html( uncheck_text ); 174 _this.checkAllInputs( 'max', true ); 175 $( this ).addClass( 'uncheck' ); 176 } else { 177 $( this ).html( check_text ); 178 _this.checkAllInputs( 'min', false ); 179 $( this ).removeClass( 'uncheck' ); 180 } 181 182 return false; 183 } ); 184 }, 185 186 isAllInputChecked: function () { 187 let is_checked = true; 188 $( '.qty-input', this.refund_form_qty_fields ).each( function ( i, v ) { 189 let field = $( v ); 190 let qty = field.attr( 'max' ); 191 let type = field.attr( 'type' ); 192 if ( type === 'number' ) { 193 if ( qty !== field.val() ) { 194 is_checked = false; 195 } 196 field.change(); 197 } else if ( type === 'checkbox' ) { 198 if ( ! field.prop( 'checked' ) ) { 199 is_checked = false; 200 } 201 } 202 } ); 203 204 return is_checked; 205 }, 206 207 checkAllInputs: function ( attr, prop ) { 208 $( '.qty-input', this.refund_form_qty_fields ).each( function ( i, v ) { 209 let field = $( v ); 210 let qty = field.attr( attr ); 211 let type = field.attr( 'type' ); 212 if ( type === 'number' ) { 213 field.val( qty ); 214 field.change(); 215 } else if ( type === 'checkbox' ) { 216 field.prop( 'checked', prop ); 217 field.change(); 218 } 219 } ); 220 }, 221 222 refundCancelButtons: function () { 223 let cancel_button = $( '#fr-cancel-request-section a.cr-button' ); 224 let dismiss_button = $( '#fr-cancel-request-section a.ds-button' ); 225 let confirm_button = $( '#fr-cancel-request-section a.cf-button' ); 226 if ( cancel_button.length ) { 227 cancel_button.click( function () { 228 confirm_button.show(); 229 dismiss_button.show(); 230 cancel_button.hide(); 231 232 return false; 233 } ); 234 } 235 if ( dismiss_button.length ) { 236 dismiss_button.click( function () { 237 confirm_button.hide(); 238 dismiss_button.hide(); 239 cancel_button.show(); 240 241 return false; 242 } ); 243 } 244 }, 245 246 initSelect2: function () { 247 $( document ).ready( function () { 248 let multiselect = $( '.multiselect' ); 249 if ( multiselect.length ) { 250 multiselect.select2(); 251 } 252 } ); 253 } 254 } 255 256 FormBuilder.init(); 257 FormBuilder.calculateRefundTotals(); 258 FormBuilder.validateRefundForm(); 259 FormBuilder.checkAll(); 260 FormBuilder.refundCancelButtons(); 261 FormBuilder.initSelect2(); 262 FormBuilder.validateUploadInput(); 263 264} )( jQuery );
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.