|
| 1 | +(function () { |
| 2 | + 'use strict'; |
| 3 | + |
| 4 | + angular.module('tc.submissions').controller('SubmitDevelopFilesController', SubmitDevelopFilesController); |
| 5 | + |
| 6 | + SubmitDevelopFilesController.$inject = ['$scope','$window', '$stateParams', '$log', 'UserService', 'SubmissionsService', 'challengeToSubmitTo']; |
| 7 | + |
| 8 | + function SubmitDevelopFilesController($scope, $window, $stateParams, $log, UserService, SubmissionsService, challengeToSubmitTo) { |
| 9 | + var vm = this; |
| 10 | + $log = $log.getInstance('SubmitDevelopFilesController'); |
| 11 | + var files = {}; |
| 12 | + var fileUploadProgress = {}; |
| 13 | + vm.comments = ''; |
| 14 | + vm.uploadProgress = 0; |
| 15 | + vm.uploading = false; |
| 16 | + vm.preparing = false; |
| 17 | + vm.finishing = false; |
| 18 | + vm.showProgress = false; |
| 19 | + vm.errorInUpload = false; |
| 20 | + vm.submissionForm = { |
| 21 | + files: [], |
| 22 | + |
| 23 | + // use develop name |
| 24 | + sourceZip: null, |
| 25 | + |
| 26 | + submitterComments: '', |
| 27 | + hasAgreedToTerms: false |
| 28 | + }; |
| 29 | + |
| 30 | + var userId = parseInt(UserService.getUserIdentity().userId); |
| 31 | + |
| 32 | + vm.submissionsBody = { |
| 33 | + reference: { |
| 34 | + type: 'CHALLENGE', |
| 35 | + id: $stateParams.challengeId, |
| 36 | + phaseType: challengeToSubmitTo.phaseType, |
| 37 | + phaseId: challengeToSubmitTo.phaseId |
| 38 | + }, |
| 39 | + userId: userId, |
| 40 | + data: { |
| 41 | + method: challengeToSubmitTo.challenge.track.toUpperCase() + '_CHALLENGE_ZIP_FILE', |
| 42 | + |
| 43 | + // Can delete below since they are processed and added later? |
| 44 | + files: [], |
| 45 | + submitterComments: '', |
| 46 | + } |
| 47 | + }; |
| 48 | + |
| 49 | + vm.setFileReference = setFileReference; |
| 50 | + vm.uploadSubmission = uploadSubmission; |
| 51 | + vm.refreshPage = refreshPage; |
| 52 | + vm.cancelRetry = cancelRetry; |
| 53 | + |
| 54 | + activate(); |
| 55 | + |
| 56 | + function activate() {} |
| 57 | + |
| 58 | + function setFileReference(file, fieldId) { |
| 59 | + // Can clean up since fileValue on tcFileInput has file reference? |
| 60 | + files[fieldId] = file; |
| 61 | + |
| 62 | + var fileObject = { |
| 63 | + name: file.name, |
| 64 | + type: fieldId, |
| 65 | + status: 'PENDING' |
| 66 | + }; |
| 67 | + |
| 68 | + // TODO: Refactor or develop |
| 69 | + switch(fieldId) { |
| 70 | + case 'SUBMISSION_ZIP': |
| 71 | + fileObject.mediaType = 'application/octet-stream'; |
| 72 | + break; |
| 73 | + case 'SOURCE_ZIP': |
| 74 | + fileObject.mediaType = 'application/octet-stream'; |
| 75 | + break; |
| 76 | + default: |
| 77 | + fileObject.mediaType = file.type; |
| 78 | + } |
| 79 | + |
| 80 | + // If user changes a file input's file, update the file details |
| 81 | + var isFound = vm.submissionsBody.data.files.reduce(function(isFound, file, i, filesArray) { |
| 82 | + if (isFound) { return true; } |
| 83 | + |
| 84 | + if (file.type === fileObject.type) { |
| 85 | + filesArray[i] = fileObject; |
| 86 | + return true; |
| 87 | + } |
| 88 | + |
| 89 | + return false; |
| 90 | + }, false); |
| 91 | + |
| 92 | + // Add new files to the list |
| 93 | + if (!isFound) { |
| 94 | + vm.submissionsBody.data.files.push(fileObject); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + function uploadSubmission() { |
| 99 | + vm.errorInUpload = false; |
| 100 | + vm.uploadProgress = 0; |
| 101 | + vm.fileUploadProgress = {}; |
| 102 | + vm.showProgress = true; |
| 103 | + vm.preparing = true; |
| 104 | + vm.uploading = false; |
| 105 | + vm.finishing = false; |
| 106 | + vm.submissionsBody.data.submitterComments = vm.comments; |
| 107 | + |
| 108 | + $log.debug('Body for request: ', vm.submissionsBody); |
| 109 | + SubmissionsService.getPresignedURL(vm.submissionsBody, files, updateProgress); |
| 110 | + } |
| 111 | + |
| 112 | + // Callback for updating submission upload process. It looks for different phases e.g. PREPARE, UPLOAD, FINISH |
| 113 | + // of the submission upload and updates the progress UI accordingly. |
| 114 | + function updateProgress(phase, args) { |
| 115 | + // for PREPARE phase |
| 116 | + if (phase === 'PREPARE') { |
| 117 | + // we are concerned only for completion of the phase |
| 118 | + if (args === 100) { |
| 119 | + vm.preparing = false; |
| 120 | + vm.uploading = true; |
| 121 | + $log.debug('Prepared for upload.'); |
| 122 | + } |
| 123 | + } else if (phase === 'UPLOAD') { |
| 124 | + // if args is object, this update is about XHRRequest's upload progress |
| 125 | + if (typeof args === 'object') { |
| 126 | + var requestId = args.file; |
| 127 | + var progress = args.progress; |
| 128 | + if (!fileUploadProgress[requestId] || fileUploadProgress[requestId] < progress) { |
| 129 | + fileUploadProgress[requestId] = progress; |
| 130 | + } |
| 131 | + var total = 0, count = 0; |
| 132 | + for(var requestId in fileUploadProgress) { |
| 133 | + var prog = fileUploadProgress[requestId]; |
| 134 | + total += prog; |
| 135 | + count++; |
| 136 | + } |
| 137 | + vm.uploadProgress = total / count; |
| 138 | + |
| 139 | + // initiate digest cycle because this event (xhr event) is caused outside angular |
| 140 | + $scope.$apply(); |
| 141 | + } else { // typeof args === 'number', mainly used a s fallback to mark completion of the UPLOAD phase |
| 142 | + vm.uploadProgress = args; |
| 143 | + } |
| 144 | + |
| 145 | + // start next phase when UPLOAD is done |
| 146 | + if (vm.uploadProgress == 100) { |
| 147 | + $log.debug('Uploaded files.'); |
| 148 | + vm.uploading = false; |
| 149 | + vm.finishing = true; |
| 150 | + } |
| 151 | + } else if (phase === 'FINISH') { |
| 152 | + // we are concerned only for completion of the phase |
| 153 | + if (args === 100) { |
| 154 | + $log.debug('Finished upload.'); |
| 155 | + } |
| 156 | + } else { |
| 157 | + // assume it to be error condition |
| 158 | + $log.debug("Error Condition: " + phase); |
| 159 | + vm.errorInUpload = true; |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + function refreshPage() { |
| 164 | + $window.location.reload(true); |
| 165 | + } |
| 166 | + |
| 167 | + function cancelRetry() { |
| 168 | + vm.showProgress = false; |
| 169 | + } |
| 170 | + } |
| 171 | +})(); |
0 commit comments