| 1 |
define([ |
| 2 |
"dojo/_base/declare", |
| 3 |
"dojo/_base/lang", |
| 4 |
"dojo/_base/fx", |
| 5 |
"dojo/_base/window", |
| 6 |
"dojo/dom-style", |
| 7 |
"dojo/_base/sniff", |
| 8 |
"dojo/text!./resources/FileInputAuto.html", |
| 9 |
"dojox/form/FileInput", |
| 10 |
"dojo/io/iframe" |
| 11 |
], |
| 12 |
function(declare, lang, fx, win, domStyle, has, template, FileInput, ioIframe){ |
| 13 |
|
| 14 |
var FileInputAuto = declare("dojox.form.FileInputAuto", FileInput, |
| 15 |
{ |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
url: "", |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
blurDelay: 2000, |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
duration: 500, |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
uploadMessage: "Uploading ...", |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
triggerEvent: "onblur", |
| 54 |
|
| 55 |
_sent: false, |
| 56 |
|
| 57 |
|
| 58 |
templateString: template, |
| 59 |
|
| 60 |
onBeforeSend: function(){ |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
return {}; |
| 66 |
}, |
| 67 |
|
| 68 |
startup: function(){ |
| 69 |
|
| 70 |
|
| 71 |
this._blurListener = this.connect(this.fileInput, this.triggerEvent, "_onBlur"); |
| 72 |
this._focusListener = this.connect(this.fileInput, "onfocus", "_onFocus"); |
| 73 |
this.inherited(arguments); |
| 74 |
}, |
| 75 |
|
| 76 |
_onFocus: function(){ |
| 77 |
|
| 78 |
|
| 79 |
if(this._blurTimer){ clearTimeout(this._blurTimer); } |
| 80 |
}, |
| 81 |
|
| 82 |
_onBlur: function(){ |
| 83 |
|
| 84 |
|
| 85 |
if(this._blurTimer){ clearTimeout(this._blurTimer); } |
| 86 |
if(!this._sent){ |
| 87 |
this._blurTimer = setTimeout(lang.hitch(this,"_sendFile"),this.blurDelay); |
| 88 |
} |
| 89 |
}, |
| 90 |
|
| 91 |
setMessage: function( title){ |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
this.overlay.removeChild(this.overlay.firstChild); |
| 98 |
this.overlay.appendChild(document.createTextNode(title)); |
| 99 |
}, |
| 100 |
|
| 101 |
_sendFile: function( e){ |
| 102 |
|
| 103 |
|
| 104 |
if(this._sent || this._sending || !this.fileInput.value){ return; } |
| 105 |
|
| 106 |
this._sending = true; |
| 107 |
|
| 108 |
domStyle.set(this.fakeNodeHolder,"display","none"); |
| 109 |
domStyle.set(this.overlay,{ |
| 110 |
opacity:0, |
| 111 |
display:"block" |
| 112 |
}); |
| 113 |
|
| 114 |
this.setMessage(this.uploadMessage); |
| 115 |
|
| 116 |
fx.fadeIn({ node: this.overlay, duration:this.duration }).play(); |
| 117 |
|
| 118 |
var _newForm; |
| 119 |
if(has('ie') < 9 || (has('ie') && has('quirks'))){ |
| 120 |
|
| 121 |
_newForm = document.createElement('<form enctype="multipart/form-data" method="post">'); |
| 122 |
_newForm.encoding = "multipart/form-data"; |
| 123 |
|
| 124 |
}else{ |
| 125 |
|
| 126 |
_newForm = document.createElement('form'); |
| 127 |
_newForm.setAttribute("enctype","multipart/form-data"); |
| 128 |
_newForm.setAttribute("method","post"); |
| 129 |
} |
| 130 |
_newForm.appendChild(this.fileInput); |
| 131 |
win.body().appendChild(_newForm); |
| 132 |
|
| 133 |
ioIframe.send({ |
| 134 |
url: this.url, |
| 135 |
form: _newForm, |
| 136 |
handleAs: "json", |
| 137 |
handle: lang.hitch(this,"_handleSend"), |
| 138 |
content: this.onBeforeSend() |
| 139 |
}); |
| 140 |
}, |
| 141 |
|
| 142 |
_handleSend: function(data,ioArgs){ |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
this.overlay.removeChild(this.overlay.firstChild); |
| 148 |
|
| 149 |
this._sent = true; |
| 150 |
this._sending = false; |
| 151 |
domStyle.set(this.overlay,{ |
| 152 |
opacity:0, |
| 153 |
border:"none", |
| 154 |
background:"none" |
| 155 |
}); |
| 156 |
|
| 157 |
this.overlay.style.backgroundImage = "none"; |
| 158 |
this.fileInput.style.display = "none"; |
| 159 |
this.fakeNodeHolder.style.display = "none"; |
| 160 |
fx.fadeIn({ node:this.overlay, duration:this.duration }).play(250); |
| 161 |
|
| 162 |
this.disconnect(this._blurListener); |
| 163 |
this.disconnect(this._focusListener); |
| 164 |
|
| 165 |
|
| 166 |
win.body().removeChild(ioArgs.args.form); |
| 167 |
this.fileInput = null; |
| 168 |
|
| 169 |
this.onComplete(data,ioArgs,this); |
| 170 |
}, |
| 171 |
|
| 172 |
reset: function(e){ |
| 173 |
|
| 174 |
|
| 175 |
if(this._blurTimer){ clearTimeout(this._blurTimer); } |
| 176 |
|
| 177 |
this.disconnect(this._blurListener); |
| 178 |
this.disconnect(this._focusListener); |
| 179 |
|
| 180 |
this.overlay.style.display = "none"; |
| 181 |
this.fakeNodeHolder.style.display = ""; |
| 182 |
this.inherited(arguments); |
| 183 |
this._sent = false; |
| 184 |
this._sending = false; |
| 185 |
this._blurListener = this.connect(this.fileInput, this.triggerEvent,"_onBlur"); |
| 186 |
this._focusListener = this.connect(this.fileInput,"onfocus","_onFocus"); |
| 187 |
}, |
| 188 |
|
| 189 |
onComplete: function(data,ioArgs,widgetRef){ |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
} |
| 199 |
}); |
| 200 |
|
| 201 |
declare("dojox.form.FileInputBlind", FileInputAuto, { |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
startup: function(){ |
| 208 |
|
| 209 |
|
| 210 |
this.inherited(arguments); |
| 211 |
this._off = domStyle.get(this.inputNode,"width"); |
| 212 |
this.inputNode.style.display = "none"; |
| 213 |
this._fixPosition(); |
| 214 |
}, |
| 215 |
|
| 216 |
_fixPosition: function(){ |
| 217 |
|
| 218 |
|
| 219 |
if(has('ie')){ |
| 220 |
domStyle.set(this.fileInput,"width","1px"); |
| 221 |
}else{ |
| 222 |
domStyle.set(this.fileInput,"left","-"+(this._off)+"px"); |
| 223 |
} |
| 224 |
}, |
| 225 |
|
| 226 |
reset: function(e){ |
| 227 |
|
| 228 |
|
| 229 |
this.inherited(arguments); |
| 230 |
this._fixPosition(); |
| 231 |
} |
| 232 |
}); |
| 233 |
|
| 234 |
return FileInputAuto; |
| 235 |
}); |