Conditions | 25 |
Total Lines | 158 |
Code Lines | 71 |
Lines | 158 |
Ratio | 100 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like directive.upload.js ➔ UploadConstructor often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | // JavaScript Document |
||
13 | function UploadConstructor($scope, $element, $attrs) { |
||
14 | var constructor = this; |
||
|
|||
15 | var $ctrl = $scope.uploadCtrl; |
||
16 | var tagName = $element[0].tagName.toLowerCase(); |
||
17 | $scope.DisplayMessageList = MessageService.messageList; |
||
18 | |||
19 | function TryToCallInitDirective(){ |
||
20 | if(typeof $scope.InitDirective == "function"){ |
||
21 | $scope.InitDirective($scope, $element, $attrs, $ctrl); |
||
22 | }else{ |
||
23 | $scope.DefaultInitDirective(); |
||
24 | } |
||
25 | } |
||
26 | $scope.DefaultInitDirective = function(){ |
||
27 | if(Core.GetConfig().debugLog.DirectiveFlow) |
||
28 | console.log("scope.$id:"+$scope.$id+", may implement $scope.InitDirective() function in webapge"); |
||
29 | } |
||
30 | function InitializeUpload() { |
||
31 | $scope.uploadInfo = []; |
||
32 | $scope.uploadResult = []; |
||
33 | |||
34 | $scope.uploadInstance = null; |
||
35 | } |
||
36 | |||
37 | function UploadFileList(files){ |
||
38 | var isFiles = Array.isArray(files); |
||
39 | |||
40 | $scope.uploadInfo = []; |
||
41 | |||
42 | if(!isFiles){ |
||
43 | UploadFile(files); |
||
44 | } |
||
45 | else{ |
||
46 | for(var index in files){ |
||
47 | UploadFile(files[index]); |
||
48 | } |
||
49 | } |
||
50 | } |
||
51 | |||
52 | function UploadFile(file, options, callback) { |
||
53 | var url = $rootScope.serverHost; |
||
54 | var uploadInfoRecord = {}; |
||
55 | var recordCount = $scope.uploadInfo.length; |
||
56 | // create new row in uploadInfo, since upload in async |
||
57 | $scope.uploadInfo[recordCount] = {}; |
||
58 | |||
59 | if (!file || file.$error) { |
||
60 | return; |
||
61 | } |
||
62 | |||
63 | uploadInfoRecord.fileInfo = file; |
||
64 | uploadInfoRecord.uploadResult = {}; |
||
65 | |||
66 | uploadInfoRecord.name = file.name; |
||
67 | uploadInfoRecord.size = file.size; |
||
68 | uploadInfoRecord.uploadProgress = 0; |
||
69 | |||
70 | // File Object |
||
71 | // console.dir(file) |
||
72 | /* |
||
73 | lastModified: 1474968722283 |
||
74 | lastModifiedDate: Tue Sep 27 2016 17:32:02 GMT+0800 (China Standard Time) |
||
75 | name: "hu01ca.xlsx" |
||
76 | size: 8629 |
||
77 | type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" |
||
78 | upload: d |
||
79 | webkitRelativePath: "" |
||
80 | */ |
||
81 | |||
82 | // Upload Result from PHP |
||
83 | /* |
||
84 | { |
||
85 | "name": "hu01ca.xlsx", |
||
86 | "type": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", |
||
87 | "tmp_name": "D:\\xampp\\tmp\\phpDFD9.tmp", |
||
88 | "error": 0, |
||
89 | "size": 8629, |
||
90 | "movedTo": "D:\\xampp\\htdocs\\Develop\\model/../temp/upload/hu01ca.xlsx", |
||
91 | "fileIntegrity-md5": "3e7992dbabfbc9ea84c621762831975b", |
||
92 | "fileIntegrity-sha1": "691528b6437c8e686d342eeacd0f27620a6ba295", |
||
93 | "errorMsg": "" |
||
94 | } |
||
95 | */ |
||
96 | |||
97 | /* |
||
98 | var uploadAction = $scope.uploadInstance = Upload.upload({ |
||
99 | //url: 'https://angular-file-upload-cors-srv.appspot.com/upload', |
||
100 | url: url+'/archive/2.8.x/controller/documentUploader.for12.2.21.php', |
||
101 | data: {file: file}, |
||
102 | }); |
||
103 | */ |
||
104 | var submitData = { |
||
105 | "Table": "", |
||
106 | "file": file |
||
107 | }; |
||
108 | var uploadAction = DataAdapter.UploadData(submitData); |
||
109 | |||
110 | |||
111 | // http://api.jquery.com/deferred.then/#deferred-then-doneCallbacks-failCallbacks |
||
112 | // deferred.then( doneCallbacks, failCallbacks [, progressCallbacks ] ) |
||
113 | uploadAction.then(function (response) { |
||
114 | uploadInfoRecord.uploadResult = response.data; |
||
115 | //if(response.data.error) |
||
116 | //$scope.errorMsg = response.data.error + " - "+response.data.errorMsg |
||
117 | |||
118 | $scope.uploadInfo[recordCount] = uploadInfoRecord; |
||
119 | // $ctrl.ngModel = $scope.uploadInfo; |
||
120 | |||
121 | $scope.uploadResult[$scope.uploadResult.length] = response.data; |
||
122 | $ctrl.ngModel = $scope.uploadResult; |
||
123 | }, function (response) { |
||
124 | //if(response.data.error) |
||
125 | //$scope.errorMsg = response.data.error + " - "+response.data.errorMsg |
||
126 | }, function (evt) { |
||
127 | // Math.min is to fix IE which reports 200% sometimes |
||
128 | var uploadedPercentage = Math.min(100, parseInt(100.0 * evt.loaded / evt.total)); |
||
129 | uploadInfoRecord.uploadProgress = uploadedPercentage; |
||
130 | }); |
||
131 | |||
132 | return uploadAction; |
||
133 | } |
||
134 | |||
135 | $scope.UploadData = function(files){ |
||
136 | // console.dir(files) |
||
137 | UploadFileList(files); |
||
138 | } |
||
139 | |||
140 | $scope.Initialize = function(){ |
||
141 | $scope.InitScope(); |
||
142 | if(typeof $scope.EventListener == "function"){ |
||
143 | $scope.EventListener($scope, $element, $attrs, $ctrl); |
||
144 | }else{ |
||
145 | EventListener(); |
||
146 | } |
||
147 | TryToCallInitDirective(); |
||
148 | } |
||
149 | $scope.InitScope = function(){ |
||
150 | InitializeUpload(); |
||
151 | } |
||
152 | |||
153 | function InitDirective(){ |
||
154 | if(Core.GetConfig().debugLog.DirectiveFlow) |
||
155 | console.log("scope.$id:"+$scope.$id+", may implement $scope.InitDirective() function in webapge"); |
||
156 | } |
||
157 | function EventListener(){ |
||
158 | if(Core.GetConfig().debugLog.DirectiveFlow) |
||
159 | console.log("scope.$id:"+$scope.$id+", may implement $scope.EventListener() function in webapge"); |
||
160 | } |
||
161 | // function SetDefaultValue(){ |
||
162 | // console.log("scope.$id:"+$scope.$id+", may implement $scope.SetDefaultValue() function in webapge"); |
||
163 | // } |
||
164 | function StatusChange(){ |
||
165 | if(Core.GetConfig().debugLog.DirectiveFlow) |
||
166 | console.log("scope.$id:"+$scope.$id+", may implement $scope.StatusChange() function in webapge"); |
||
167 | } |
||
168 | |||
169 | // $scope.Initialize(); |
||
170 | } |
||
171 | function templateFunction(tElement, tAttrs) { |
||
218 |