[483] | 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 | // summary: |
---|
| 17 | // An extension on FileInput providing background upload progress |
---|
| 18 | // |
---|
| 19 | // description: |
---|
| 20 | // An extended version of FileInput - when the user focuses away from the input |
---|
| 21 | // the selected file is posted via ioIframe to the url. example implementation |
---|
| 22 | // comes with PHP solution for handling upload, and returning required data. |
---|
| 23 | // |
---|
| 24 | // notes: the return data from the io.iframe is used to populate the input element with |
---|
| 25 | // data regarding the results. it will be a JSON object, like: |
---|
| 26 | // |
---|
| 27 | // | results = { size: "1024", filename: "file.txt" } |
---|
| 28 | // |
---|
| 29 | // all the parameters allowed to FileInput apply |
---|
| 30 | |
---|
| 31 | // url: String |
---|
| 32 | // the URL where our background FileUpload will be sent |
---|
| 33 | url: "", |
---|
| 34 | |
---|
| 35 | // blurDelay: Integer |
---|
| 36 | // time in ms before an un-focused widget will wait before uploading the file to the url="" specified |
---|
| 37 | // default: 2 seconds |
---|
| 38 | blurDelay: 2000, |
---|
| 39 | |
---|
| 40 | // duration: Integer |
---|
| 41 | // The time in ms to use as the generic timing mechanism for the animations |
---|
| 42 | // set to 1 or 0 for "immediate response" |
---|
| 43 | duration: 500, |
---|
| 44 | |
---|
| 45 | // uploadMessage: String |
---|
| 46 | // FIXME: i18n somehow? |
---|
| 47 | uploadMessage: "Uploading ...", |
---|
| 48 | |
---|
| 49 | // triggerEvent: String |
---|
| 50 | // Event which triggers the upload. Defaults to onblur, sending the file selected |
---|
| 51 | // 'blurDelay' milliseconds after losing focus. Set to "onchange" with a low blurDelay |
---|
| 52 | // to send files immediately after uploading. |
---|
| 53 | triggerEvent: "onblur", |
---|
| 54 | |
---|
| 55 | _sent: false, |
---|
| 56 | |
---|
| 57 | // small template changes, new attachpoint: overlay |
---|
| 58 | templateString: template, |
---|
| 59 | |
---|
| 60 | onBeforeSend: function(){ |
---|
| 61 | // summary: |
---|
| 62 | // Called immediately before a FileInput sends it's file via io.iframe.send. |
---|
| 63 | // The return of this function is passed as the `content` member in the io.iframe IOArgs |
---|
| 64 | // object. |
---|
| 65 | return {}; |
---|
| 66 | }, |
---|
| 67 | |
---|
| 68 | startup: function(){ |
---|
| 69 | // summary: |
---|
| 70 | // add our extra blur listeners |
---|
| 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 | // summary: |
---|
| 78 | // clear the upload timer |
---|
| 79 | if(this._blurTimer){ clearTimeout(this._blurTimer); } |
---|
| 80 | }, |
---|
| 81 | |
---|
| 82 | _onBlur: function(){ |
---|
| 83 | // summary: |
---|
| 84 | // start the upload timer |
---|
| 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(/*String*/ title){ |
---|
| 92 | // summary: |
---|
| 93 | // set the text of the progressbar |
---|
| 94 | |
---|
| 95 | // innerHTML throws errors in IE! so use DOM manipulation instead |
---|
| 96 | //this.overlay.innerHTML = title; |
---|
| 97 | this.overlay.removeChild(this.overlay.firstChild); |
---|
| 98 | this.overlay.appendChild(document.createTextNode(title)); |
---|
| 99 | }, |
---|
| 100 | |
---|
| 101 | _sendFile: function(/*Event*/ e){ |
---|
| 102 | // summary: |
---|
| 103 | // triggers the chain of events needed to upload a file in the background. |
---|
| 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 | // just to reiterate, IE is a steaming pile of code. |
---|
| 121 | _newForm = document.createElement('<form enctype="multipart/form-data" method="post">'); |
---|
| 122 | _newForm.encoding = "multipart/form-data"; |
---|
| 123 | |
---|
| 124 | }else{ |
---|
| 125 | // this is how all other sane browsers do it |
---|
| 126 | _newForm = document.createElement('form'); |
---|
| 127 | _newForm.setAttribute("enctype","multipart/form-data"); |
---|
| 128 | } |
---|
| 129 | _newForm.appendChild(this.fileInput); |
---|
| 130 | win.body().appendChild(_newForm); |
---|
| 131 | |
---|
| 132 | ioIframe.send({ |
---|
| 133 | url: this.url, |
---|
| 134 | form: _newForm, |
---|
| 135 | handleAs: "json", |
---|
| 136 | handle: lang.hitch(this,"_handleSend"), |
---|
| 137 | content: this.onBeforeSend() |
---|
| 138 | }); |
---|
| 139 | }, |
---|
| 140 | |
---|
| 141 | _handleSend: function(data,ioArgs){ |
---|
| 142 | // summary: |
---|
| 143 | // The callback to toggle the progressbar, and fire the user-defined callback |
---|
| 144 | |
---|
| 145 | // innerHTML throws errors in IE! so use DOM manipulation instead |
---|
| 146 | this.overlay.removeChild(this.overlay.firstChild); |
---|
| 147 | |
---|
| 148 | this._sent = true; |
---|
| 149 | this._sending = false; |
---|
| 150 | domStyle.set(this.overlay,{ |
---|
| 151 | opacity:0, |
---|
| 152 | border:"none", |
---|
| 153 | background:"none" |
---|
| 154 | }); |
---|
| 155 | |
---|
| 156 | this.overlay.style.backgroundImage = "none"; |
---|
| 157 | this.fileInput.style.display = "none"; |
---|
| 158 | this.fakeNodeHolder.style.display = "none"; |
---|
| 159 | fx.fadeIn({ node:this.overlay, duration:this.duration }).play(250); |
---|
| 160 | |
---|
| 161 | this.disconnect(this._blurListener); |
---|
| 162 | this.disconnect(this._focusListener); |
---|
| 163 | |
---|
| 164 | //remove the form used to send the request |
---|
| 165 | win.body().removeChild(ioArgs.args.form); |
---|
| 166 | this.fileInput = null; |
---|
| 167 | |
---|
| 168 | this.onComplete(data,ioArgs,this); |
---|
| 169 | }, |
---|
| 170 | |
---|
| 171 | reset: function(e){ |
---|
| 172 | // summary: |
---|
| 173 | // accommodate our extra focusListeners |
---|
| 174 | if(this._blurTimer){ clearTimeout(this._blurTimer); } |
---|
| 175 | |
---|
| 176 | this.disconnect(this._blurListener); |
---|
| 177 | this.disconnect(this._focusListener); |
---|
| 178 | |
---|
| 179 | this.overlay.style.display = "none"; |
---|
| 180 | this.fakeNodeHolder.style.display = ""; |
---|
| 181 | this.inherited(arguments); |
---|
| 182 | this._sent = false; |
---|
| 183 | this._sending = false; |
---|
| 184 | this._blurListener = this.connect(this.fileInput, this.triggerEvent,"_onBlur"); |
---|
| 185 | this._focusListener = this.connect(this.fileInput,"onfocus","_onFocus"); |
---|
| 186 | }, |
---|
| 187 | |
---|
| 188 | onComplete: function(data,ioArgs,widgetRef){ |
---|
| 189 | // summary: |
---|
| 190 | // stub function fired when an upload has finished. |
---|
| 191 | // data: |
---|
| 192 | // the raw data found in the first [TEXTAREA] tag of the post url |
---|
| 193 | // ioArgs: |
---|
| 194 | // the Deferred data being passed from the handle: callback |
---|
| 195 | // widgetRef: |
---|
| 196 | // this widget pointer, so you can set this.overlay to a completed/error message easily |
---|
| 197 | } |
---|
| 198 | }); |
---|
| 199 | |
---|
| 200 | declare("dojox.form.FileInputBlind", FileInputAuto, { |
---|
| 201 | // summary: |
---|
| 202 | // An extended version of dojox.form.FileInputAuto |
---|
| 203 | // that does not display an input node, but rather only a button |
---|
| 204 | // and otherwise behaves just like FileInputAuto |
---|
| 205 | |
---|
| 206 | startup: function(){ |
---|
| 207 | // summary: |
---|
| 208 | // hide our fileInput input field |
---|
| 209 | this.inherited(arguments); |
---|
| 210 | this._off = domStyle.get(this.inputNode,"width"); |
---|
| 211 | this.inputNode.style.display = "none"; |
---|
| 212 | this._fixPosition(); |
---|
| 213 | }, |
---|
| 214 | |
---|
| 215 | _fixPosition: function(){ |
---|
| 216 | // summary: |
---|
| 217 | // in this case, set the button under where the visible button is |
---|
| 218 | if(has('ie')){ |
---|
| 219 | domStyle.set(this.fileInput,"width","1px"); |
---|
| 220 | }else{ |
---|
| 221 | domStyle.set(this.fileInput,"left","-"+(this._off)+"px"); |
---|
| 222 | } |
---|
| 223 | }, |
---|
| 224 | |
---|
| 225 | reset: function(e){ |
---|
| 226 | // summary: |
---|
| 227 | // onclick, we need to reposition our newly created input type="file" |
---|
| 228 | this.inherited(arguments); |
---|
| 229 | this._fixPosition(); |
---|
| 230 | } |
---|
| 231 | }); |
---|
| 232 | |
---|
| 233 | return FileInputAuto; |
---|
| 234 | }); |
---|