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', |
||
12 | 'DataAdapter', function($rootScope, $timeout, Core, Security, LockManager, Upload, MessageService, DataAdapter) { |
||
13 | function UploadConstructor($scope, $element, $attrs) { |
||
14 | var constructor = this; |
||
0 ignored issues
–
show
Unused Code
introduced
by
![]() |
|||
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"); |
||
0 ignored issues
–
show
|
|||
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){ |
||
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);
}
![]() |
|||
47 | UploadFile(files[index]); |
||
48 | } |
||
49 | } |
||
50 | } |
||
51 | |||
52 | function UploadFile(file, options, callback) { |
||
53 | var url = $rootScope.serverHost; |
||
0 ignored issues
–
show
|
|||
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"); |
||
0 ignored issues
–
show
|
|||
156 | } |
||
157 | function EventListener(){ |
||
158 | if(Core.GetConfig().debugLog.DirectiveFlow) |
||
159 | console.log("scope.$id:"+$scope.$id+", may implement $scope.EventListener() function in webapge"); |
||
0 ignored issues
–
show
|
|||
160 | } |
||
161 | // function SetDefaultValue(){ |
||
162 | // console.log("scope.$id:"+$scope.$id+", may implement $scope.SetDefaultValue() function in webapge"); |
||
163 | // } |
||
164 | function StatusChange(){ |
||
0 ignored issues
–
show
|
|||
165 | if(Core.GetConfig().debugLog.DirectiveFlow) |
||
166 | console.log("scope.$id:"+$scope.$id+", may implement $scope.StatusChange() function in webapge"); |
||
0 ignored issues
–
show
|
|||
167 | } |
||
168 | |||
169 | // $scope.Initialize(); |
||
170 | } |
||
171 | function templateFunction(tElement, tAttrs) { |
||
172 | var globalCriteria = $rootScope.globalCriteria; |
||
173 | |||
174 | var template = '' + |
||
175 | '<div class="custom-transclude"></div>'; |
||
176 | return template; |
||
177 | } |
||
178 | |||
179 | return { |
||
180 | require: ['ngModel', '?import'], |
||
181 | restrict: 'EA', //'EA', //Default in 1.3+ |
||
182 | transclude: true, |
||
183 | |||
184 | // scope: [false | true | {...}] |
||
185 | // false = use parent scope |
||
186 | // true = A new child scope that prototypically inherits from its parent |
||
187 | // {} = create a isolate scope |
||
188 | scope: true, |
||
189 | |||
190 | controller: UploadConstructor, |
||
191 | controllerAs: 'uploadCtrl', |
||
192 | |||
193 | //If both bindToController and scope are defined and have object hashes, bindToController overrides scope. |
||
194 | bindToController: { |
||
195 | ngModel: '=', |
||
196 | }, |
||
197 | template: templateFunction, |
||
198 | compile: function compile(tElement, tAttrs, transclude) { |
||
199 | return { |
||
200 | pre: function preLink(scope, iElement, iAttrs, controller) { |
||
201 | //console.log("entry preLink() compile"); |
||
202 | }, |
||
203 | post: function postLink(scope, iElement, iAttrs, controller) { |
||
204 | //console.log("entry postLink() compile"); |
||
205 | |||
206 | // "scope" here is the directive's isolate scope |
||
207 | // iElement.find('.custom-transclude').append( |
||
208 | // ); |
||
209 | transclude(scope, function (clone, scope) { |
||
210 | iElement.find('.custom-transclude').append(clone); |
||
211 | }) |
||
212 | scope.Initialize(); |
||
213 | } |
||
214 | } |
||
215 | }, |
||
216 | }; |
||
217 | }]); |
||
218 |