1
+ /*
2
+ * jQuery File Upload User Interface Plugin 1.0
3
+ *
4
+ * Copyright 2010, Sebastian Tschan, AQUANTUM
5
+ * Licensed under the MIT license:
6
+ * http://creativecommons.org/licenses/MIT/
7
+ *
8
+ * https://blueimp.net
9
+ * http://www.aquantum.de
10
+ */
11
+
12
+ /*jslint browser: true */
13
+ /*global jQuery */
14
+
15
+ ( function ( $ ) {
16
+
17
+ var UploadHandler = function ( dropZone , options ) {
18
+ if ( ! options . uploadTable ) {
19
+ $ . error ( 'jQuery.fileUploadUI requires option uploadTable: $(uploadTable)' ) ;
20
+ }
21
+ if ( ! options . downloadTable ) {
22
+ $ . error ( 'jQuery.fileUploadUI requires option downloadTable: $(downloadTable)' ) ;
23
+ }
24
+ if ( typeof options . buildUploadRow !== 'function' ) {
25
+ $ . error ( 'jQuery.fileUploadUI requires option buildUploadRow: function (files, index) {return $(row)}' ) ;
26
+ }
27
+ if ( typeof options . buildDownloadRow !== 'function' ) {
28
+ $ . error ( 'jQuery.fileUploadUI requires option buildDownloadRow: function (json) {return $(row)}' ) ;
29
+ }
30
+
31
+ var uploadHandler = this ,
32
+ dragLeaveTimeout ,
33
+ isDropZoneEnlarged ,
34
+ normalizeFiles = function ( files ) {
35
+ var file , i ;
36
+ for ( i = 0 ; i < files . length ; i += 1 ) {
37
+ file = files [ i ] ;
38
+ if ( typeof file === 'string' ) {
39
+ files [ i ] = { name : file , type : null , size : null } ;
40
+ }
41
+ }
42
+ return files ;
43
+ } ;
44
+
45
+ this . progressSelector = '.file_upload_progress div' ;
46
+ this . cancelSelector = '.file_upload_cancel div' ;
47
+ this . cssClassSmall = 'file_upload_small' ;
48
+ this . cssClassLarge = 'file_upload_large' ;
49
+ this . cssClassHighlight = 'file_upload_highlight' ;
50
+ this . dropEffect = 'highlight' ;
51
+
52
+ this . init = function ( files , index , xhr , callBack ) {
53
+ files = normalizeFiles ( files ) ;
54
+ var uploadRow = uploadHandler . buildUploadRow ( files , index ) ,
55
+ progressbar = uploadRow . find ( uploadHandler . progressSelector ) . progressbar ( {
56
+ value : ( xhr ? 0 : 100 )
57
+ } ) ;
58
+ uploadRow . find ( uploadHandler . cancelSelector ) . click ( function ( ) {
59
+ if ( xhr ) {
60
+ xhr . abort ( ) ;
61
+ } else {
62
+ // javascript:false as iframe src prevents warning popups on HTTPS in IE6
63
+ // concat is used here to prevent the "Script URL" JSLint error:
64
+ dropZone . find ( 'iframe' ) . attr ( 'src' , 'javascript' . concat ( ':false;' ) ) ;
65
+ uploadRow . fadeOut ( function ( ) {
66
+ $ ( this ) . remove ( ) ;
67
+ } ) ;
68
+ }
69
+ } ) ;
70
+ uploadRow . appendTo ( uploadHandler . uploadTable ) . fadeIn ( ) ;
71
+ if ( typeof uploadHandler . initCallBack === 'function' ) {
72
+ uploadHandler . initCallBack ( files , index , xhr , function ( settings ) {
73
+ callBack ( $ . extend ( settings , { uploadRow : uploadRow , progressbar : progressbar } ) ) ;
74
+ } ) ;
75
+ } else {
76
+ callBack ( { uploadRow : uploadRow , progressbar : progressbar } ) ;
77
+ }
78
+ } ;
79
+
80
+ this . abort = function ( event , files , index , xhr , settings ) {
81
+ settings . uploadRow . fadeOut ( function ( ) {
82
+ $ ( this ) . remove ( ) ;
83
+ } ) ;
84
+ } ;
85
+
86
+ this . progress = function ( event , files , index , xhr , settings ) {
87
+ settings . progressbar . progressbar (
88
+ 'value' ,
89
+ parseInt ( event . loaded / event . total * 100 , 10 )
90
+ ) ;
91
+ } ;
92
+
93
+ this . load = function ( event , files , index , xhr , settings ) {
94
+ settings . uploadRow . fadeOut ( function ( ) {
95
+ $ ( this ) . remove ( ) ;
96
+ } ) ;
97
+ var json ;
98
+ if ( xhr ) {
99
+ json = $ . parseJSON ( xhr . responseText ) ;
100
+ } else {
101
+ json = $ . parseJSON ( $ ( event . target ) . contents ( ) . text ( ) ) ;
102
+ }
103
+ if ( json ) {
104
+ uploadHandler . buildDownloadRow ( json )
105
+ . appendTo ( uploadHandler . downloadTable ) . fadeIn ( ) ;
106
+ }
107
+ } ;
108
+
109
+ this . documentDragEnter = function ( event ) {
110
+ setTimeout ( function ( ) {
111
+ if ( dragLeaveTimeout ) {
112
+ clearTimeout ( dragLeaveTimeout ) ;
113
+ }
114
+ } , 50 ) ;
115
+ if ( ! isDropZoneEnlarged ) {
116
+ dropZone . switchClass (
117
+ uploadHandler . cssClassSmall ,
118
+ uploadHandler . cssClassLarge
119
+ ) ;
120
+ isDropZoneEnlarged = true ;
121
+ }
122
+ } ;
123
+
124
+ this . documentDragLeave = function ( event ) {
125
+ if ( dragLeaveTimeout ) {
126
+ clearTimeout ( dragLeaveTimeout ) ;
127
+ }
128
+ dragLeaveTimeout = setTimeout ( function ( ) {
129
+ dropZone . switchClass (
130
+ uploadHandler . cssClassLarge ,
131
+ uploadHandler . cssClassSmall
132
+ ) ;
133
+ isDropZoneEnlarged = false ;
134
+ } , 100 ) ;
135
+ } ;
136
+
137
+ this . dragEnter = this . dragLeave = function ( event ) {
138
+ dropZone . toggleClass ( uploadHandler . cssClassHighlight ) ;
139
+ } ;
140
+
141
+ this . drop = function ( event ) {
142
+ dropZone . effect ( uploadHandler . dropEffect , function ( ) {
143
+ dropZone . removeClass ( uploadHandler . cssClassHighlight ) ;
144
+ dropZone . switchClass (
145
+ uploadHandler . cssClassLarge ,
146
+ uploadHandler . cssClassSmall
147
+ ) ;
148
+ } ) ;
149
+ } ;
150
+
151
+ $ . extend ( this , options ) ;
152
+ } ,
153
+
154
+ methods = {
155
+ init : function ( options ) {
156
+ return this . each ( function ( ) {
157
+ $ ( this ) . fileUpload ( new UploadHandler ( $ ( this ) , options ) ) ;
158
+ } ) ;
159
+ } ,
160
+
161
+ destroy : function ( namespace ) {
162
+ return this . each ( function ( ) {
163
+ $ ( this ) . fileUpload ( 'destroy' , namespace ) ;
164
+ } ) ;
165
+ }
166
+ } ;
167
+
168
+ $ . fn . fileUploadUI = function ( method ) {
169
+ if ( methods [ method ] ) {
170
+ return methods [ method ] . apply ( this , Array . prototype . slice . call ( arguments , 1 ) ) ;
171
+ } else if ( typeof method === 'object' || ! method ) {
172
+ return methods . init . apply ( this , arguments ) ;
173
+ } else {
174
+ $ . error ( 'Method ' + method + ' does not exist on jQuery.fileUploadUI' ) ;
175
+ }
176
+ } ;
177
+
178
+ } ( jQuery ) ) ;
0 commit comments