1
+ /*
2
+ * jQuery File Upload PHP APC Plugin 1.0.0
3
+ * https://github.com/teynon/jQuery-File-Upload
4
+ */
5
+
6
+ /*jslint nomen: true, unparam: true, regexp: true */
7
+ /*global define, window, URL, webkitURL, FileReader */
8
+
9
+ ( function ( factory ) {
10
+ 'use strict' ;
11
+ if ( typeof define === 'function' && define . amd ) {
12
+ // Register as an anonymous AMD module:
13
+ define ( [
14
+ 'jquery' ,
15
+ './jquery.fileupload-apc'
16
+ ] , factory ) ;
17
+ } else {
18
+ // Browser globals:
19
+ factory (
20
+ window . jQuery
21
+ ) ;
22
+ }
23
+ } ( function ( $ ) {
24
+ 'use strict' ;
25
+
26
+ $ . widget ( 'blueimp.fileupload' , $ . blueimp . fileupload , {
27
+
28
+ options : {
29
+ // By default, APC Upload Progress (for older browsers) is disabled
30
+ // if APC is enabled, older browsers will need to limit the maximum
31
+ // number of concurrent uploads to that browsers limit.
32
+ // Max Concurrent:
33
+ // IE6/7=2, IE8=2 OR 6(depending on modem) Most others is 6
34
+ apc : false ,
35
+ // The default wait time in between apc file progress checks.
36
+ apcTimeout : 1000 ,
37
+ // The default APC variable name to be included with the post.
38
+ apcVarname : "APC_UPLOAD_PROGRESS" ,
39
+
40
+ // Overwrite the send function to prevent progress from going
41
+ // directly to 100% during APC file uploads.
42
+ send : function ( e , data ) {
43
+ var that = $ ( this ) . data ( 'blueimp-fileupload' ) ||
44
+ $ ( this ) . data ( 'fileupload' ) ;
45
+ if ( ! data . isValidated ) {
46
+ if ( ! data . maxNumberOfFilesAdjusted ) {
47
+ that . _adjustMaxNumberOfFiles ( - data . files . length ) ;
48
+ data . maxNumberOfFilesAdjusted = true ;
49
+ }
50
+ if ( ! that . _validate ( data . files ) ) {
51
+ return false ;
52
+ }
53
+ }
54
+ if ( data . context && data . dataType &&
55
+ data . dataType . substr ( 0 , 6 ) === 'iframe' && ! data . apc ) {
56
+ // Iframe Transport does not support progress events.
57
+ // In lack of an indeterminate progress bar, we set
58
+ // the progress to 100%, showing the full animated bar:
59
+ data . context
60
+ . find ( '.progress' ) . addClass (
61
+ ! $ . support . transition && 'progress-animated'
62
+ )
63
+ . attr ( 'aria-valuenow' , 100 )
64
+ . find ( '.bar' ) . css (
65
+ 'width' ,
66
+ '100%'
67
+ ) ;
68
+ }
69
+ return that . _trigger ( 'sent' , e , data ) ;
70
+ }
71
+ } ,
72
+
73
+ _apcProgress : function ( options ) {
74
+ var that = this ;
75
+ // Generate a random APC key.
76
+ if ( ! options . apccode ) {
77
+ options . apccode = options . apccode || 'apc' + ( new Date ( ) ) . getTime ( ) ;
78
+ options . formData = this . _getFormData ( options ) ;
79
+ options . formData . push ( { name : options . apcVarname , value : options . apccode } ) ;
80
+ }
81
+ this . apct = setTimeout ( function ( ) {
82
+ var self = that , opts = options ;
83
+ $ . ajax ( {
84
+ url : options . url ,
85
+ type : "POST" ,
86
+ data : { "apc" : true , "apccode" : options . apccode }
87
+ } ) . done ( function ( o ) {
88
+ // Set the apc_data progress.
89
+ var r = $ . parseJSON ( o ) ;
90
+ if ( r ) {
91
+ r = r . apc_data ;
92
+ var e = $ . Event ( 'progress' , {
93
+ lengthComputable : true ,
94
+ loaded : r . current ,
95
+ total : r . total
96
+ } ) , now = ( new Date ( ) ) . getTime ( ) , loaded = Math . floor ( r . current ) ;
97
+ // Add the difference from the previously loaded state
98
+ // to the global loaded counter:
99
+ self . _progress . total = r . total ;
100
+ self . _progress . loaded += ( loaded - opts . _progress . loaded ) ;
101
+ self . _progress . bitrate = self . _bitrateTimer . getBitrate (
102
+ now ,
103
+ self . _progress . loaded ,
104
+ self . options . apcTimeout
105
+ ) ;
106
+ opts . _progress . loaded = opts . loaded = loaded ;
107
+ opts . _progress . bitrate = opts . bitrate = opts . _bitrateTimer . getBitrate (
108
+ now ,
109
+ loaded ,
110
+ self . options . apcTimeout
111
+ ) ;
112
+ opts . _time = now ;
113
+ opts . total = r . total ;
114
+
115
+ // Trigger a custom progress event with a total data property set
116
+ // to the file size(s) of the current upload and a loaded data
117
+ // property calculated accordingly:
118
+ self . _trigger ( 'progress' , e , opts ) ;
119
+ // Trigger a global progress event for all current file uploads,
120
+ // including ajax calls queued for sequential file uploads:
121
+ self . _trigger ( 'progressall' , e , self . _progress ) ;
122
+ }
123
+ self . _apcProgress ( opts ) ;
124
+ } ) . fail ( function ( o ) { } ) . always ( function ( o ) { } ) ;
125
+ } , this . options . apcTimeout ) ;
126
+ } ,
127
+
128
+ // Override - If APC is enabled, we need to start upload progress timeouts now.
129
+ // - We should also limit the concurrent uploads. (IE 8 and less may have max
130
+ // of 2 async ajax requests.)
131
+ _initDataSettings : function ( options ) {
132
+ if ( this . _isXHRUpload ( options ) ) {
133
+ if ( ! this . _chunkedUpload ( options , true ) ) {
134
+ if ( ! options . data ) {
135
+ this . _initXHRData ( options ) ;
136
+ }
137
+ this . _initProgressListener ( options ) ;
138
+ }
139
+ if ( options . postMessage ) {
140
+ // Setting the dataType to postmessage enables the
141
+ // postMessage transport:
142
+ options . dataType = 'postmessage ' + ( options . dataType || '' ) ;
143
+ }
144
+ } else {
145
+ if ( options . apc ) {
146
+ options . apccode = false ;
147
+ // If we are using APC, only let one file download at a time.
148
+ options . limitConcurrentUploads = 0 ;
149
+ // If we're using APC, set the APC progress listener.
150
+ this . _apcProgress ( options ) ;
151
+ }
152
+ this . _initIframeSettings ( options ) ;
153
+ }
154
+ } ,
155
+
156
+ _onAlways : function ( jqXHRorResult , textStatus , jqXHRorError , options ) {
157
+ if ( this . apct ) {
158
+ clearTimeout ( this . apct ) ;
159
+ }
160
+ // jqXHRorResult, textStatus and jqXHRorError are added to the
161
+ // options object via done and fail callbacks
162
+ this . _trigger ( 'always' , null , options ) ;
163
+ }
164
+
165
+
166
+ } ) ;
167
+ } ) ) ;
0 commit comments