| 1 |
define([ |
| 2 |
"dojo/_base/kernel", |
| 3 |
"dojo/_base/declare", |
| 4 |
"dojo/_base/lang", |
| 5 |
"dojo/_base/array", |
| 6 |
"dojo/_base/connect", |
| 7 |
"dojo/_base/window", |
| 8 |
"dojo/dom-style", |
| 9 |
"dojo/dom-class", |
| 10 |
"dojo/dom-geometry", |
| 11 |
"dojo/dom-attr", |
| 12 |
"dojo/dom-construct", |
| 13 |
"dojo/dom-form", |
| 14 |
"dijit", |
| 15 |
"dijit/form/Button", |
| 16 |
"./uploader/_Base", |
| 17 |
"./uploader/_HTML5", |
| 18 |
"./uploader/_IFrame", |
| 19 |
"./uploader/_Flash", |
| 20 |
"dojo/i18n!./nls/Uploader", |
| 21 |
"dojo/text!./resources/Uploader.html" |
| 22 |
],function(kernel, declare, lang, array, connect, win, domStyle, domClass, domGeometry, domAttr, domConstruct, |
| 23 |
domForm, dijit, Button, Base, HTML5, IFrame, Flash, res, template){ |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
return declare("dojox.form.Uploader", [Base, Button, HTML5, IFrame, Flash], { |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
uploadOnSelect:false, |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
tabIndex:0, |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
multiple:false, |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
label:res.label, |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
url:"", |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
name:"uploadedfile", |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
flashFieldName:"", |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
force:"", |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
uploadType:"", |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
showInput: "", |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
focusedClass:"dijitButtonHover", |
| 119 |
|
| 120 |
_nameIndex:0, |
| 121 |
|
| 122 |
templateString: template, |
| 123 |
|
| 124 |
baseClass: 'dijitUploader '+Button.prototype.baseClass, |
| 125 |
|
| 126 |
postMixInProperties: function(){ |
| 127 |
this._inputs = []; |
| 128 |
this._cons = []; |
| 129 |
this.force = this.force.toLowerCase(); |
| 130 |
if(this.supports("multiple")){ |
| 131 |
this.uploadType = this.force === 'form' ? 'form' : 'html5'; |
| 132 |
}else{ |
| 133 |
this.uploadType = this.force === 'flash' ? 'flash' : 'iframe'; |
| 134 |
} |
| 135 |
|
| 136 |
this.inherited(arguments); |
| 137 |
}, |
| 138 |
buildRendering: function(){ |
| 139 |
this.inherited(arguments); |
| 140 |
domStyle.set(this.domNode, { |
| 141 |
overflow:"hidden", |
| 142 |
position:"relative" |
| 143 |
}); |
| 144 |
this._buildDisplay(); |
| 145 |
|
| 146 |
|
| 147 |
domAttr.set(this.titleNode, 'tabIndex', -1); |
| 148 |
}, |
| 149 |
_buildDisplay: function(){ |
| 150 |
if(this.showInput){ |
| 151 |
this.displayInput = domConstruct.create('input', { |
| 152 |
'class':'dijitUploadDisplayInput', |
| 153 |
'tabIndex':-1, 'autocomplete':'off', |
| 154 |
'role':'presentation'}, |
| 155 |
this.containerNode, this.showInput); |
| 156 |
|
| 157 |
this._attachPoints.push('displayInput'); |
| 158 |
this.connect(this,'onChange', function(files){ |
| 159 |
var i=0,l=files.length, f, r=[]; |
| 160 |
while((f=files[i++])){ |
| 161 |
if(f && f.name){ |
| 162 |
r.push(f.name); |
| 163 |
} |
| 164 |
} |
| 165 |
this.displayInput.value = r.join(', '); |
| 166 |
}); |
| 167 |
this.connect(this,'reset', function(){ |
| 168 |
this.displayInput.value = ''; |
| 169 |
}); |
| 170 |
} |
| 171 |
}, |
| 172 |
|
| 173 |
startup: function(){ |
| 174 |
if(this._buildInitialized){ |
| 175 |
return; |
| 176 |
} |
| 177 |
this._buildInitialized = true; |
| 178 |
this._getButtonStyle(this.domNode); |
| 179 |
this._setButtonStyle(); |
| 180 |
this.inherited(arguments); |
| 181 |
}, |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
onChange: function( fileArray){ |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
}, |
| 193 |
|
| 194 |
onBegin: function( dataArray){ |
| 195 |
|
| 196 |
|
| 197 |
}, |
| 198 |
|
| 199 |
onProgress: function( customEvent){ |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
}, |
| 214 |
|
| 215 |
onComplete: function( customEvent){ |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
this.reset(); |
| 221 |
}, |
| 222 |
|
| 223 |
onCancel: function(){ |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
}, |
| 229 |
|
| 230 |
onAbort: function(){ |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
}, |
| 235 |
|
| 236 |
onError: function( evtObject){ |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
}, |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
upload: function( formData){ |
| 248 |
|
| 249 |
|
| 250 |
formData = formData || {}; |
| 251 |
formData.uploadType = this.uploadType; |
| 252 |
this.inherited(arguments); |
| 253 |
}, |
| 254 |
|
| 255 |
submit: function( form){ |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
form = !!form ? form.tagName ? form : this.getForm() : this.getForm(); |
| 260 |
var data = domForm.toObject(form); |
| 261 |
data.uploadType = this.uploadType; |
| 262 |
this.upload(data); |
| 263 |
}, |
| 264 |
|
| 265 |
reset: function(){ |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
delete this._files; |
| 275 |
this._disconnectButton(); |
| 276 |
array.forEach(this._inputs, domConstruct.destroy); |
| 277 |
this._inputs = []; |
| 278 |
this._nameIndex = 0; |
| 279 |
this._createInput(); |
| 280 |
}, |
| 281 |
|
| 282 |
getFileList: function(){ |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
var fileArray = []; |
| 287 |
if(this.supports("multiple")){ |
| 288 |
array.forEach(this._files, function(f, i){ |
| 289 |
fileArray.push({ |
| 290 |
index:i, |
| 291 |
name:f.name, |
| 292 |
size:f.size, |
| 293 |
type:f.type |
| 294 |
}); |
| 295 |
}, this); |
| 296 |
}else{ |
| 297 |
array.forEach(this._inputs, function(n, i){ |
| 298 |
if(n.value){ |
| 299 |
fileArray.push({ |
| 300 |
index:i, |
| 301 |
name:n.value.substring(n.value.lastIndexOf("\\")+1), |
| 302 |
size:0, |
| 303 |
type:n.value.substring(n.value.lastIndexOf(".")+1) |
| 304 |
}); |
| 305 |
} |
| 306 |
}, this); |
| 307 |
|
| 308 |
} |
| 309 |
return fileArray; |
| 310 |
}, |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
_getValueAttr: function(){ |
| 317 |
|
| 318 |
|
| 319 |
return this.getFileList(); |
| 320 |
}, |
| 321 |
|
| 322 |
_setValueAttr: function(disabled){ |
| 323 |
console.error("Uploader value is read only"); |
| 324 |
}, |
| 325 |
|
| 326 |
_setDisabledAttr: function(disabled){ |
| 327 |
|
| 328 |
|
| 329 |
if(this.disabled == disabled || !this.inputNode){ return; } |
| 330 |
this.inherited(arguments); |
| 331 |
domStyle.set(this.inputNode, "display", disabled ? "none" : ""); |
| 332 |
}, |
| 333 |
|
| 334 |
_getButtonStyle: function(node){ |
| 335 |
this.btnSize = {w:domStyle.get(node,'width'), h:domStyle.get(node,'height')}; |
| 336 |
}, |
| 337 |
|
| 338 |
_setButtonStyle: function(){ |
| 339 |
this.inputNodeFontSize = Math.max(2, Math.max(Math.ceil(this.btnSize.w / 60), Math.ceil(this.btnSize.h / 15))); |
| 340 |
this._createInput(); |
| 341 |
}, |
| 342 |
|
| 343 |
_getFileFieldName: function(){ |
| 344 |
var name; |
| 345 |
if(this.supports("multiple") && this.multiple){ |
| 346 |
name = this.name+"s[]"; |
| 347 |
}else{ |
| 348 |
|
| 349 |
name = this.name + (this.multiple ? this._nameIndex : ""); |
| 350 |
} |
| 351 |
return name; |
| 352 |
}, |
| 353 |
|
| 354 |
_createInput: function(){ |
| 355 |
if(this._inputs.length){ |
| 356 |
domStyle.set(this.inputNode, { |
| 357 |
top:"500px" |
| 358 |
}); |
| 359 |
this._disconnectButton(); |
| 360 |
this._nameIndex++; |
| 361 |
} |
| 362 |
var name = this._getFileFieldName(); |
| 363 |
|
| 364 |
|
| 365 |
this.focusNode = this.inputNode = domConstruct.create("input", {type:"file", name:name, "aria-labelledby":this.id+"_label"}, this.domNode, "first"); |
| 366 |
if(this.supports("multiple") && this.multiple){ |
| 367 |
domAttr.set(this.inputNode, "multiple", true); |
| 368 |
} |
| 369 |
this._inputs.push(this.inputNode); |
| 370 |
|
| 371 |
domStyle.set(this.inputNode, { |
| 372 |
position:"absolute", |
| 373 |
fontSize:this.inputNodeFontSize+"em", |
| 374 |
top:"-3px", |
| 375 |
right:"-3px", |
| 376 |
opacity:0 |
| 377 |
}); |
| 378 |
this._connectButton(); |
| 379 |
}, |
| 380 |
|
| 381 |
_connectButton: function(){ |
| 382 |
this._cons.push(connect.connect(this.inputNode, "change", this, function(evt){ |
| 383 |
this._files = this.inputNode.files; |
| 384 |
this.onChange(this.getFileList(evt)); |
| 385 |
if(!this.supports("multiple") && this.multiple) this._createInput(); |
| 386 |
})); |
| 387 |
|
| 388 |
if(this.tabIndex > -1){ |
| 389 |
this.inputNode.tabIndex = this.tabIndex; |
| 390 |
|
| 391 |
this._cons.push(connect.connect(this.inputNode, "focus", this, function(){ |
| 392 |
domClass.add(this.domNode, this.focusedClass); |
| 393 |
})); |
| 394 |
this._cons.push(connect.connect(this.inputNode, "blur", this, function(){ |
| 395 |
domClass.remove(this.domNode, this.focusedClass); |
| 396 |
})); |
| 397 |
} |
| 398 |
}, |
| 399 |
|
| 400 |
_disconnectButton: function(){ |
| 401 |
array.forEach(this._cons, connect.disconnect); |
| 402 |
this._cons.splice(0,this._cons.length); |
| 403 |
} |
| 404 |
}); |
| 405 |
|
| 406 |
}); |