Otaku-Projects /
AngularJS-CRUD-PHP
| 1 | // JavaScript Document |
||
| 2 | View Code Duplication | "use strict"; |
|
| 3 | |||
| 4 | app.directive('upload', [ |
||
| 5 | '$rootScope', |
||
| 6 | '$timeout', |
||
| 7 | 'Core', |
||
| 8 | 'Security', |
||
| 9 | 'LockManager', |
||
| 10 | 'Upload', |
||
| 11 | 'MessageService', function($rootScope, $timeout, Core, Security, LockManager, Upload, MessageService) { |
||
| 12 | function UploadConstructor($scope, $element, $attrs) { |
||
| 13 | var constructor = this; |
||
|
0 ignored issues
–
show
Unused Code
introduced
by
Loading history...
|
|||
| 14 | var $ctrl = $scope.uploadCtrl; |
||
| 15 | var tagName = $element[0].tagName.toLowerCase(); |
||
| 16 | $scope.DisplayMessageList = MessageService.messageList; |
||
| 17 | |||
| 18 | function TryToCallInitDirective(){ |
||
| 19 | if(typeof $scope.InitDirective == "function"){ |
||
| 20 | $scope.InitDirective($scope, $element, $attrs, $ctrl); |
||
| 21 | }else{ |
||
| 22 | $scope.DefaultInitDirective(); |
||
| 23 | } |
||
| 24 | } |
||
| 25 | $scope.DefaultInitDirective = function(){ |
||
| 26 | if(Core.GetConfig().debugLog.DirectiveFlow) |
||
| 27 | console.log("scope.$id:"+$scope.$id+", may implement $scope.InitDirective() function in webapge"); |
||
|
0 ignored issues
–
show
|
|||
| 28 | } |
||
| 29 | function InitializeUpload() { |
||
| 30 | $scope.uploadInfo = []; |
||
| 31 | $scope.uploadResult = []; |
||
| 32 | |||
| 33 | $scope.uploadInstance = null; |
||
| 34 | } |
||
| 35 | |||
| 36 | function UploadFileList(files){ |
||
| 37 | var isFiles = Array.isArray(files); |
||
| 38 | |||
| 39 | $scope.uploadInfo = []; |
||
| 40 | |||
| 41 | if(!isFiles){ |
||
| 42 | UploadFile(files); |
||
| 43 | } |
||
| 44 | else{ |
||
| 45 | for(var index in files){ |
||
|
0 ignored issues
–
show
A for in loop automatically includes the property of any prototype object, consider checking the key using
hasOwnProperty.
When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically: var someObject;
for (var key in someObject) {
if ( ! someObject.hasOwnProperty(key)) {
continue; // Skip keys from the prototype.
}
doSomethingWith(key);
}
Loading history...
|
|||
| 46 | UploadFile(files[index]); |
||
| 47 | } |
||
| 48 | } |
||
| 49 | } |
||
| 50 | |||
| 51 | function UploadFile(file, options, callback) { |
||
| 52 | var url = $rootScope.serverHost; |
||
| 53 | var uploadInfoRecord = {}; |
||
| 54 | var recordCount = $scope.uploadInfo.length; |
||
| 55 | // create new row in uploadInfo, since upload in async |
||
| 56 | $scope.uploadInfo[recordCount] = {}; |
||
| 57 | |||
| 58 | if (!file || file.$error) { |
||
| 59 | return; |
||
| 60 | } |
||
| 61 | |||
| 62 | uploadInfoRecord.fileInfo = file; |
||
| 63 | uploadInfoRecord.uploadResult = {}; |
||
| 64 | |||
| 65 | uploadInfoRecord.name = file.name; |
||
| 66 | uploadInfoRecord.size = file.size; |
||
| 67 | uploadInfoRecord.uploadProgress = 0; |
||
| 68 | |||
| 69 | // File Object |
||
| 70 | // console.dir(file) |
||
| 71 | /* |
||
| 72 | lastModified: 1474968722283 |
||
| 73 | lastModifiedDate: Tue Sep 27 2016 17:32:02 GMT+0800 (China Standard Time) |
||
| 74 | name: "hu01ca.xlsx" |
||
| 75 | size: 8629 |
||
| 76 | type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" |
||
| 77 | upload: d |
||
| 78 | webkitRelativePath: "" |
||
| 79 | */ |
||
| 80 | |||
| 81 | // Upload Result from PHP |
||
| 82 | /* |
||
| 83 | { |
||
| 84 | "name": "hu01ca.xlsx", |
||
| 85 | "type": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", |
||
| 86 | "tmp_name": "D:\\xampp\\tmp\\phpDFD9.tmp", |
||
| 87 | "error": 0, |
||
| 88 | "size": 8629, |
||
| 89 | "movedTo": "D:\\xampp\\htdocs\\Develop\\model/../temp/upload/hu01ca.xlsx", |
||
| 90 | "fileIntegrity-md5": "3e7992dbabfbc9ea84c621762831975b", |
||
| 91 | "fileIntegrity-sha1": "691528b6437c8e686d342eeacd0f27620a6ba295", |
||
| 92 | "errorMsg": "" |
||
| 93 | } |
||
| 94 | */ |
||
| 95 | |||
| 96 | var uploadAction = $scope.uploadInstance = Upload.upload({ |
||
| 97 | //url: 'https://angular-file-upload-cors-srv.appspot.com/upload', |
||
| 98 | url: url+'/controller/documentUploader.for12.2.21.php', |
||
| 99 | data: {file: file}, |
||
| 100 | }); |
||
| 101 | // http://api.jquery.com/deferred.then/#deferred-then-doneCallbacks-failCallbacks |
||
| 102 | // deferred.then( doneCallbacks, failCallbacks [, progressCallbacks ] ) |
||
| 103 | uploadAction.then(function (response) { |
||
| 104 | uploadInfoRecord.uploadResult = response.data; |
||
| 105 | //if(response.data.error) |
||
| 106 | //$scope.errorMsg = response.data.error + " - "+response.data.errorMsg |
||
| 107 | |||
| 108 | $scope.uploadInfo[recordCount] = uploadInfoRecord; |
||
| 109 | // $ctrl.ngModel = $scope.uploadInfo; |
||
| 110 | |||
| 111 | $scope.uploadResult[$scope.uploadResult.length] = response.data; |
||
| 112 | $ctrl.ngModel = $scope.uploadResult; |
||
| 113 | }, function (response) { |
||
| 114 | //if(response.data.error) |
||
| 115 | //$scope.errorMsg = response.data.error + " - "+response.data.errorMsg |
||
| 116 | }, function (evt) { |
||
| 117 | // Math.min is to fix IE which reports 200% sometimes |
||
| 118 | var uploadedPercentage = Math.min(100, parseInt(100.0 * evt.loaded / evt.total)); |
||
| 119 | uploadInfoRecord.uploadProgress = uploadedPercentage; |
||
| 120 | }); |
||
| 121 | |||
| 122 | return uploadAction; |
||
| 123 | |||
| 124 | |||
| 125 | // if(typeof callback == "function") |
||
| 126 | // callback($scope.uploadInfo[recordCount]); |
||
| 127 | } |
||
| 128 | |||
| 129 | $scope.UploadData = function(files){ |
||
| 130 | // console.dir(files) |
||
| 131 | UploadFileList(files); |
||
| 132 | } |
||
| 133 | |||
| 134 | $scope.Initialize = function(){ |
||
| 135 | $scope.InitScope(); |
||
| 136 | if(typeof $scope.EventListener == "function"){ |
||
| 137 | $scope.EventListener($scope, $element, $attrs, $ctrl); |
||
| 138 | }else{ |
||
| 139 | EventListener(); |
||
| 140 | } |
||
| 141 | TryToCallInitDirective(); |
||
| 142 | } |
||
| 143 | $scope.InitScope = function(){ |
||
| 144 | InitializeUpload(); |
||
| 145 | } |
||
| 146 | |||
| 147 | function InitDirective(){ |
||
| 148 | if(Core.GetConfig().debugLog.DirectiveFlow) |
||
| 149 | console.log("scope.$id:"+$scope.$id+", may implement $scope.InitDirective() function in webapge"); |
||
|
0 ignored issues
–
show
|
|||
| 150 | } |
||
| 151 | function EventListener(){ |
||
| 152 | if(Core.GetConfig().debugLog.DirectiveFlow) |
||
| 153 | console.log("scope.$id:"+$scope.$id+", may implement $scope.EventListener() function in webapge"); |
||
|
0 ignored issues
–
show
|
|||
| 154 | } |
||
| 155 | // function SetDefaultValue(){ |
||
| 156 | // console.log("scope.$id:"+$scope.$id+", may implement $scope.SetDefaultValue() function in webapge"); |
||
| 157 | // } |
||
| 158 | function StatusChange(){ |
||
|
0 ignored issues
–
show
|
|||
| 159 | if(Core.GetConfig().debugLog.DirectiveFlow) |
||
| 160 | console.log("scope.$id:"+$scope.$id+", may implement $scope.StatusChange() function in webapge"); |
||
|
0 ignored issues
–
show
|
|||
| 161 | } |
||
| 162 | |||
| 163 | // $scope.Initialize(); |
||
| 164 | } |
||
| 165 | function templateFunction(tElement, tAttrs) { |
||
| 166 | var globalCriteria = $rootScope.globalCriteria; |
||
| 167 | |||
| 168 | var template = '' + |
||
| 169 | '<div class="custom-transclude"></div>'; |
||
| 170 | return template; |
||
| 171 | } |
||
| 172 | |||
| 173 | return { |
||
| 174 | require: ['ngModel', '?import'], |
||
| 175 | restrict: 'EA', //'EA', //Default in 1.3+ |
||
| 176 | transclude: true, |
||
| 177 | |||
| 178 | // scope: [false | true | {...}] |
||
| 179 | // false = use parent scope |
||
| 180 | // true = A new child scope that prototypically inherits from its parent |
||
| 181 | // {} = create a isolate scope |
||
| 182 | scope: true, |
||
| 183 | |||
| 184 | controller: UploadConstructor, |
||
| 185 | controllerAs: 'uploadCtrl', |
||
| 186 | |||
| 187 | //If both bindToController and scope are defined and have object hashes, bindToController overrides scope. |
||
| 188 | bindToController: { |
||
| 189 | ngModel: '=', |
||
| 190 | }, |
||
| 191 | template: templateFunction, |
||
| 192 | compile: function compile(tElement, tAttrs, transclude) { |
||
| 193 | return { |
||
| 194 | pre: function preLink(scope, iElement, iAttrs, controller) { |
||
| 195 | //console.log("entry preLink() compile"); |
||
| 196 | }, |
||
| 197 | post: function postLink(scope, iElement, iAttrs, controller) { |
||
| 198 | //console.log("entry postLink() compile"); |
||
| 199 | |||
| 200 | // "scope" here is the directive's isolate scope |
||
| 201 | // iElement.find('.custom-transclude').append( |
||
| 202 | // ); |
||
| 203 | transclude(scope, function (clone, scope) { |
||
| 204 | iElement.find('.custom-transclude').append(clone); |
||
| 205 | }) |
||
| 206 | scope.Initialize(); |
||
| 207 | } |
||
| 208 | } |
||
| 209 | }, |
||
| 210 | }; |
||
| 211 | }]); |
||
| 212 |