1/** 2 * alg-wc-checkout-files-upload-ajax.js 3 * 4 * @version 2.3.0 5 * @since 1.3.0 6 * @author Algoritmika Ltd. 7 * @author WP Wham 8 * @todo [dev] maybe validate file type with `upload.getType()` (same as `upload.getSize()`) 9 * @todo [feature] text messages (e.g. `span` or `div`) instead of `alert()` 10 */ 11 12(function( $ ) { 13 "use strict"; 14 15 var $container; 16 var counter = 0; 17 18 var queue = []; 19 var activeQueue = []; 20 var maxWorkers = 4; 21 var suppressedErrors = { 22 fail_max_files: false 23 } 24 25 var processQueue = function() { 26 27 var key; 28 29 for ( var i = 0; i < queue.length; i++ ) { 30 if ( activeQueue.length < maxWorkers ) { 31 activeQueue.push( queue.shift() ); 32 key = activeQueue.length - 1; 33 activeQueue[key].upload.doUpload( activeQueue[key].fileUploader, key ); 34 } 35 } 36 37 if ( queue.length ) { 38 setTimeout( processQueue, 100 ); 39 } else { 40 suppressedErrors.fail_max_files = false; 41 } 42 43 } 44 45 var blockCheckout = function() { 46 $container.block({ 47 message: null, 48 overlayCSS: { 49 background: '#fff', 50 opacity: 0.6 51 } 52 }); 53 }; 54 55 var unblockCheckout = function() { 56 $container.unblock(); 57 }; 58 59 var Upload = function ( file ) { 60 this.file = file; 61 }; 62 Upload.prototype.getType = function() { 63 return this.file.type; 64 }; 65 Upload.prototype.getSize = function() { 66 return this.file.size; 67 }; 68 Upload.prototype.getName = function() { 69 return this.file.name; 70 }; 71 Upload.prototype.doUpload = function ( fileUploader, queuePosition ) { 72 73 blockCheckout(); 74 75 var formData = new FormData(); 76 formData.append( 'file', this.file, this.getName() ); 77 formData.append( 'action', 'alg_ajax_file_upload' ); 78 formData.append( 'file_uploader', fileUploader ); 79 formData.append( 'order_id', $( '#alg_checkout_files_upload_order_id_' + fileUploader ).val() ); 80 formData.append( 'key', $( '#alg_checkout_files_upload_order_key_' + fileUploader ).val() ); 81 formData.append( 'nonce', $( '#wpwham-checkout-files-upload-nonce-' + fileUploader ).val() ); 82 83 if ( alg_wc_checkout_files_upload.progress_bar_enabled ) { 84 var progressBarId = 'wpw_cfu_progress_' + counter++; 85 // grab template div, copy, and populate with stuff needed for this file upload 86 var $progressBarTemplate = $( '#alg-wc-checkout-files-upload-progress-wrapper-' + fileUploader ); 87 var $progressBar = $progressBarTemplate.clone(); 88 $progressBar.first().attr( 'id', progressBarId ); 89 $( '#alg_checkout_files_upload_form_' + fileUploader + 90 ' .alg_checkout_files_upload_result_' + fileUploader + ', ' + 91 '#alg_checkout_files_upload_form_' + fileUploader + 92 ' .alg-wc-checkout-files-upload-progress-wrapper' 93 ).last().after( $progressBar.show() ); 94 } 95 96 $.ajax({ 97 type: "POST", 98 url: ajax_object.ajax_url, 99 xhr: function () { 100 var xhr = $.ajaxSettings.xhr(); 101 if ( alg_wc_checkout_files_upload.progress_bar_enabled && xhr.upload ) { 102 xhr.upload.fileUploader = fileUploader; 103 xhr.upload.addEventListener( 'progress', function( event ) { 104 if ( event.lengthComputable ) { 105 var percentComplete = ( event.loaded / event.total ) * 100; 106 percentComplete = percentComplete.toFixed( 0 ); 107 $( '#' + progressBarId + ' .alg-wc-checkout-files-upload-progress-bar' ) 108 .css( 'width', percentComplete + '%' ); 109 $( '#' + progressBarId + ' .alg-wc-checkout-files-upload-progress-status' ) 110 .text( percentComplete + '%' ); 111 } 112 }, false ); 113 } 114 return xhr; 115 }, 116 success: function (data) { 117 var $template; 118 var $result; 119 var data_decoded = $.parseJSON(data); 120 if ( data_decoded['result'] != 0 ) { 121 // grab template div, copy, and populate with the results of this file upload 122 $template = $( '#alg_checkout_files_upload_result_' + fileUploader ); 123 $result = $template.clone(); 124 $result.first().removeAttr( 'id' ); 125 $result.find( '.alg_checkout_files_upload_result_file_name' ) 126 .html( data_decoded['data'] ); 127 if ( typeof data_decoded['data_img'] !== 'undefined' && data_decoded['data_img'] > '') { 128 $result.find( '.alg_checkout_files_upload_result_image' ) 129 .html( data_decoded['data_img'] ); 130 } 131 if ( alg_wc_checkout_files_upload.progress_bar_enabled ) { 132 $result.data( 'progress-bar-id', progressBarId ); 133 $( '#' + progressBarId ).before( $result.show() ); 134 window.setTimeout( function(){ $( '#' + progressBarId ).fadeOut(); }, 1000 ); 135 } else { 136 $( '.alg_checkout_files_upload_result_' + fileUploader ).last() 137 .after( $result.show() ); 138 } 139 // if no more file uploads possible, hide button 140 if ( ! $( '#alg_checkout_files_upload_' + fileUploader ).attr( 'multiple' ) ) { 141 $( '#alg_checkout_files_upload_button_' + fileUploader ).hide(); 142 } 143 } else { 144 if ( alg_wc_checkout_files_upload.progress_bar_enabled ) { 145 $( '#' + progressBarId ).remove(); 146 } 147 } 148 if ( data_decoded['message'] != '' ) { 149 if ( 150 typeof data_decoded['error_code'] !== "undefined" 151 && data_decoded['error_code'] === 'fail_max_files' 152 ) { 153 if ( ! suppressedErrors.fail_max_files ) { 154 alert( data_decoded['message'] ); 155 } 156 suppressedErrors.fail_max_files = true; 157 queue = []; 158 } else { 159 alert( data_decoded['message'] ); 160 } 161 } 162 activeQueue.splice( queuePosition, 1); 163 unblockCheckout(); 164 }, 165 error: function (error) { 166 activeQueue.splice( queuePosition, 1); 167 unblockCheckout(); 168 }, 169 async: true, 170 data: formData, 171 cache: false, 172 contentType: false, 173 processData: false, 174 timeout: 60000 175 }); 176 }; 177 178 $(document).ready(function() { 179
180 $container = jQuery( 'form.woocommerce-checkout' ).closest( 'div' ); 181 182 $( document ).on( 'click', '.alg_checkout_files_upload_button', function() { 183 var fileUploader = $( this ).data( 'file-uploader' ); 184 $( '#alg_checkout_files_upload_' + fileUploader ).click(); 185 }); 186 187 $( document ).on( 'change', '.alg_checkout_files_upload_file_input', function() { 188 var files = $( this )[0].files; 189 var fileUploader = $( this ).data( 'file-uploader' ); 190 if ( ! files.length ) { 191 return; 192 } 193 for ( var i = 0; i < files.length; i++ ) { 194 var file = files[i]; 195 var upload = new Upload( file ); 196 var max_file_size = parseInt( alg_wc_checkout_files_upload.max_file_size ); 197 if ( max_file_size > 0 && upload.getSize() > max_file_size ) { 198 alert( alg_wc_checkout_files_upload.max_file_size_exceeded_message ); 199 } else { 200 queue.push({ 'fileUploader': fileUploader, 'upload': upload }); 201 } 202 } 203 processQueue(); 204 $( this ).val(''); 205 }); 206 207 $( document ).on( 'click', '.alg_checkout_files_upload_result_delete', function( event ) { 208 event.preventDefault(); 209 blockCheckout(); 210 var $fileContainer = $( this ).closest( 'div' ); 211 var fileUploader = $fileContainer.data( 'file-uploader' ); 212 var fileKey = $fileContainer.find( '.alg_checkout_files_upload_result_file_name a' ) 213 .first().data( 'file-key' ); 214 var formData = new FormData(); 215 formData.append( 'action', 'alg_ajax_file_delete' ); 216 formData.append( 'file_uploader', fileUploader ); 217 formData.append( 'file_key', fileKey ); 218 formData.append( 'order_id', $( '#alg_checkout_files_upload_order_id_' + fileUploader ).val() ); 219 formData.append( 'nonce', $( '#wpwham-checkout-files-upload-nonce-' + fileUploader ).val() ); 220 $.ajax({ 221 type: "POST", 222 url: ajax_object.ajax_url, 223 success: function ( data ) { 224 var data_decoded = $.parseJSON( data ); 225 if ( data_decoded['result'] != 0 ) { 226 if ( alg_wc_checkout_files_upload.progress_bar_enabled ) { 227 $( '#' + $fileContainer.data( 'progress-bar-id' ) ).remove(); 228 } 229 $fileContainer.remove(); 230 $( '#alg_checkout_files_upload_button_' + fileUploader ).show(); 231 } 232 if ( data_decoded['message'] != '' ) { 233 alert( data_decoded['message'] ); 234 } 235 unblockCheckout(); 236 }, 237 error: function (error) { 238 unblockCheckout(); 239 }, 240 async: true, 241 data: formData, 242 cache: false, 243 contentType: false, 244 processData: false, 245 timeout: 60000 246 }); 247 }); 248 }); 249 250})( 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.