1ACC.csvimport = { 2 TEXT_CSV_CONTENT_TYPE: 'text/csv', 3 APP_EXCEL_CONTENT_TYPE: 'application/vnd.ms-excel', 4 5 _autoload: [ 6 ["changeFileUploadAppearance", $(".js-file-upload").length != 0], 7 ["bindImportCSVActions", $(".js-import-csv").length != 0] 8 ], 9 10 changeFileUploadAppearance: function() { 11 $('.js-file-upload__input').on('change',function () { 12 $('.js-file-upload__file-name').text((this.value).toLowerCase().replace(/c:\\fakepath\\/g, '')); 13 14 if($('.js-file-upload').parents('#cboxLoadedContent').length > 0){ 15 ACC.colorbox.resize(); 16 } 17 }) 18 }, 19 20 bindImportCSVActions: function() { 21 $('#chooseFileButton').on('click', function (event) { 22 ACC.csvimport.clearGlobalAlerts(); 23 }); 24 25 $('#importButton').on('click', function (event) { 26 event.preventDefault(); 27 ACC.csvimport.clearGlobalAlerts(); 28 29 if (!($('.js-file-upload__input').val().trim().length > 0)) { 30 ACC.csvimport.displayGlobalAlert({type: 'error', messageId: 'import-csv-no-file-chosen-error-message'}); 31 return; 32 } 33 34 var selectedFile = document.getElementById('csvFile').files[0]; 35 if (!ACC.csvimport.isSelectedFileValid(selectedFile)) { 36 return; 37 } 38 39 var form = document.getElementById('importCSVSavedCartForm'); 40 var formData = new window.FormData(form); 41 var selectedFileSanitize = DOMPurify.sanitize(selectedFile); 42 formData.append("csvFile", selectedFileSanitize); 43 44 ACC.csvimport.displayGlobalAlert({type: 'warning', messageId: 'import-csv-upload-message'}); 45 ACC.csvimport.enableDisableActionButtons(false); 46 47 $.ajax({ 48 url: form.action, 49 type: 'POST', 50 data: formData, 51 contentType: false, 52 processData: false, 53 success: function() { 54 ACC.csvimport.displayGlobalAlert({type: 'info', message: ''}); 55 $('#import-csv-alerts .alert-info').append($('#import-csv-success-message').html()); 56 57 ACC.csvimport.clearChosenFile(); 58 }, 59 error: function(jqXHR) { 60 if (jqXHR.status == 400) { 61 if (jqXHR.responseJSON) { 62 ACC.csvimport.displayGlobalAlert({type: 'error', message: jqXHR.responseJSON}); 63 return; 64 } 65 } 66 67 ACC.csvimport.displayGlobalAlert({type: 'error', messageId: 'import-csv-generic-error-message'}); 68 }, 69 complete: function() { 70 ACC.csvimport.enableDisableActionButtons(true); 71 } 72 }); 73 }); 74 }, 75 76 isSelectedFileValid: function(selectedFile) { 77 if (window.File && window.Blob) { 78 if (selectedFile) { 79 if (!(selectedFile.type == ACC.csvimport.TEXT_CSV_CONTENT_TYPE || 80 selectedFile.type == ACC.csvimport.APP_EXCEL_CONTENT_TYPE)) { 81 ACC.csvimport.displayGlobalAlert({type: 'error', messageId: 'import-csv-file-csv-required'}); 82 return false; 83 } 84 85 var fileName = selectedFile.name; 86 if (!fileName || !(/\.csv$/i).test(fileName)) { 87 ACC.csvimport.displayGlobalAlert({type: 'error', messageId: 'import-csv-file-csv-required'}); 88 return false; 89 } 90 } 91 92 var fileMaxSize = $('.js-file-upload__input').data('file-max-size'); 93 if ($.isNumeric(fileMaxSize) && selectedFile) { 94 if (selectedFile.size > parseFloat(fileMaxSize)) { 95 ACC.csvimport.displayGlobalAlert({type: 'error', messageId: 'import-csv-file-max-size-exceeded-error-message'}); 96 return false; 97 } 98 } 99 } 100 101 return true; 102 }, 103 104 displayGlobalAlert: function(options) { 105 ACC.csvimport.clearGlobalAlerts(); 106 var alertTemplateSelector; 107 108 switch (options.type) { 109 case 'error': 110 alertTemplateSelector = '#global-alert-danger-template'; 111 break; 112 case 'warning': 113 alertTemplateSelector = '#global-alert-warning-template'; 114 break; 115 default: 116 alertTemplateSelector = '#global-alert-info-template'; 117 } 118 119 if (typeof options.message != 'undefined') { 120 $('#import-csv-alerts').append($(alertTemplateSelector).tmpl({message: options.message})); 121 } 122 123 if (typeof options.messageId != 'undefined') 124 { 125 $('#import-csv-alerts').append($(alertTemplateSelector).tmpl({message: $('#' + options.messageId).text()})); 126 } 127 }, 128 129 clearGlobalAlerts: function() { 130 $('#import-csv-alerts').empty(); 131 }, 132 133 clearChosenFile: function() { 134 document.getElementById('csvFile').value = ''; 135 $('.js-file-upload__file-name').text(''); 136 }, 137 138 enableDisableActionButtons: function(enable) { 139 $('#chooseFileButton').attr('disabled', !enable); 140 $('#importButton').prop('disabled', !enable); 141 } 142}
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.