| .. | .. |
|---|
| 9 | 9 | } |
|---|
| 10 | 10 | |
|---|
| 11 | 11 | var app = angular.module('securis'); |
|---|
| 12 | + |
|---|
| 13 | + app.directive('fileLoader', |
|---|
| 14 | + function($timeout, $parse) { |
|---|
| 15 | + return { |
|---|
| 16 | + restrict : 'A', // only activate on element attribute |
|---|
| 17 | + require : '', |
|---|
| 18 | + link : function(scope, element, attrs) { |
|---|
| 19 | + console.log('scope.license: ' + scope.$parent.license); |
|---|
| 20 | + var setter = $parse(attrs.fileLoader).assign; |
|---|
| 21 | + element.bind('change', function(evt) { |
|---|
| 22 | + console.log('scope.license: ' + scope.$parent.license); |
|---|
| 23 | + var field = $parse(attrs.fileLoader); |
|---|
| 24 | + console.log('field: ' + field); |
|---|
| 25 | + var fileList = evt.target.files; |
|---|
| 26 | + if (fileList != null && fileList[0]) { |
|---|
| 27 | + var reader = new FileReader(); |
|---|
| 28 | + reader.onerror = function(data) { |
|---|
| 29 | + setter(scope.$parent, 'ERROR'); |
|---|
| 30 | + scope.$apply(); |
|---|
| 31 | + } |
|---|
| 32 | + reader.onload = function(data) { |
|---|
| 33 | + setter(scope.$parent, reader.result); |
|---|
| 34 | + scope.$apply(); |
|---|
| 35 | + } |
|---|
| 36 | + |
|---|
| 37 | + reader.readAsText(fileList[0]); |
|---|
| 38 | + } else { |
|---|
| 39 | + console.log('NO FILE: '); |
|---|
| 40 | + field = ''; |
|---|
| 41 | + scope.$apply(); |
|---|
| 42 | + } |
|---|
| 43 | + }); |
|---|
| 44 | + |
|---|
| 45 | + } |
|---|
| 46 | + }; |
|---|
| 47 | + }); |
|---|
| 48 | + |
|---|
| 49 | + console.log(' OK ????? '); |
|---|
| 12 | 50 | |
|---|
| 13 | 51 | app.controller('PackAndLicensesCtrl', [ |
|---|
| 14 | 52 | '$scope', |
|---|
| .. | .. |
|---|
| 17 | 55 | '$store', |
|---|
| 18 | 56 | '$L', |
|---|
| 19 | 57 | function($scope, $http, toaster, $store, $L) { |
|---|
| 20 | | - $scope.licenses = [ |
|---|
| 21 | | - {id: 1, |
|---|
| 22 | | - "code": "BP-SA-001-AKSJMS234", |
|---|
| 23 | | - "user_fullname": "Johnny Belmonte", |
|---|
| 24 | | - "user_email": "jb@curisit.net", |
|---|
| 25 | | - "status": 3}, |
|---|
| 26 | | - {id: 2, |
|---|
| 27 | | - "code": "BP-SA-001-KAJSDHAJS", |
|---|
| 28 | | - "user_fullname": "Walter Simons", |
|---|
| 29 | | - "user_email": "ws@curisit.net", |
|---|
| 30 | | - "status": 1}, |
|---|
| 31 | | - {id: 3, |
|---|
| 32 | | - "code": "BP-SA-001-ASKDGHKA", |
|---|
| 33 | | - "user_fullname": "Frank Belmonte", |
|---|
| 34 | | - "user_email": "fb@curisit.net", |
|---|
| 35 | | - "status": 2}, |
|---|
| 36 | | - {id: 4, |
|---|
| 37 | | - "code": "BP-SA-001-BBBGGGG", |
|---|
| 38 | | - "user_fullname": "John Dalton", |
|---|
| 39 | | - "user_email": "jd@curisit.net", |
|---|
| 40 | | - "status": 3}, |
|---|
| 41 | | - {id: 5, |
|---|
| 42 | | - "code": "BP-SA-001-AKADNAJANA", |
|---|
| 43 | | - "user_fullname": "Walter Martins", |
|---|
| 44 | | - "user_email": "wm@curisit.net", |
|---|
| 45 | | - "status": 3}, |
|---|
| 46 | | - {id: 6, |
|---|
| 47 | | - "code": "BP-SA-001-AKANDAKS", |
|---|
| 48 | | - "user_fullname": "Joe Bolton", |
|---|
| 49 | | - "user_email": "jbol@curisit.net", |
|---|
| 50 | | - "status": 2} |
|---|
| 51 | | - ]; |
|---|
| 52 | 58 | |
|---|
| 53 | 59 | $scope.maxLengthErrorMsg = function(displayname, fieldMaxlength) { |
|---|
| 54 | 60 | return $L.get("{0} length is too long (max: {1}).", $L.get(displayname), fieldMaxlength); |
|---|
| .. | .. |
|---|
| 171 | 177 | function($scope, $http, $resource, toaster, $store, $L) { |
|---|
| 172 | 178 | $scope.$on('pack_changed', function(evt, message) { |
|---|
| 173 | 179 | $scope.licenses = licenseResource.query({packId: $scope.currentPack.id}); |
|---|
| 174 | | - console.log('on pack_changed'); |
|---|
| 175 | 180 | if ($scope.showForm) { |
|---|
| 176 | 181 | if ($scope.isNew) { |
|---|
| 177 | 182 | $scope.license.pack_id = $scope.currentPack.id |
|---|
| .. | .. |
|---|
| 183 | 188 | |
|---|
| 184 | 189 | var licenseResource = $resource('/license/:licenseId', { |
|---|
| 185 | 190 | licenseId : '@id' |
|---|
| 191 | + }, { |
|---|
| 192 | + save_w_upload: { |
|---|
| 193 | + method: "POST", |
|---|
| 194 | + transformRequest: function(data, headersGetter) { |
|---|
| 195 | + // To use an object without FormData, follow: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest?redirectlocale=en-US&redirectslug=DOM%2FXMLHttpRequest%2FUsing_XMLHttpRequest#Submitting_forms_and_uploading_files |
|---|
| 196 | + var formData = new FormData(); |
|---|
| 197 | + angular.forEach(data, function(value, key) { |
|---|
| 198 | + if (key !== 'request_data') |
|---|
| 199 | + formData.append(key, value) |
|---|
| 200 | + else |
|---|
| 201 | + formData.append(key, $('input#request_data_file').get(0).files[0]); |
|---|
| 202 | + }) |
|---|
| 203 | + |
|---|
| 204 | + return formData; |
|---|
| 205 | + }, |
|---|
| 206 | + headers: { |
|---|
| 207 | + 'Content-Type': undefined |
|---|
| 208 | + } |
|---|
| 209 | + } |
|---|
| 186 | 210 | }); |
|---|
| 187 | 211 | $scope.mandatory = { |
|---|
| 188 | 212 | code: true |
|---|
| .. | .. |
|---|
| 202 | 226 | if ($scope.currentPack) |
|---|
| 203 | 227 | $scope.licenses = licenseResource.query({packId: $scope.currentPack.id}); |
|---|
| 204 | 228 | |
|---|
| 229 | +// $(document).on('change', '#request_data_file', function(newValue, oldValue) { |
|---|
| 230 | +// console.log('File changed!!!!'); |
|---|
| 231 | +// var reader = new FileReader(); |
|---|
| 232 | +// reader.onload = function(data) { |
|---|
| 233 | +// console.log('LOAD complete: ' + data); |
|---|
| 234 | +// console.log('LOAD reader.result: ' + reader.result); |
|---|
| 235 | +// $('input#request_data').val(reader.result) |
|---|
| 236 | +// } |
|---|
| 237 | +// console.log('file: ' + $('input#request_data_file').get(0).files[0]); |
|---|
| 238 | +// console.log('file2: ' + $scope.request_data_file); |
|---|
| 239 | +// reader.readAsText($('input#request_data_file').get(0).files[0]); |
|---|
| 240 | +// }); |
|---|
| 241 | + |
|---|
| 205 | 242 | $scope.save = function() { |
|---|
| 243 | + $( "form#licenseForm" ) |
|---|
| 244 | + .attr( "enctype", "multipart/form-data" ) |
|---|
| 245 | + .attr( "encoding", "multipart/form-data" ); |
|---|
| 206 | 246 | var _success = function() { |
|---|
| 207 | 247 | if (!$scope.isNew) $scope.showForm = false; |
|---|
| 208 | 248 | $scope.licenses = licenseResource.query({packId: $scope.currentPack.id}); |
|---|
| 209 | 249 | } |
|---|
| 210 | | - licenseResource.save($scope.license, _success) |
|---|
| 250 | + licenseResource.save_w_upload($scope.license, _success) |
|---|
| 211 | 251 | } |
|---|
| 212 | 252 | |
|---|
| 213 | 253 | $scope.newLicense = function() { |
|---|