Debian Codegraph @

dojox/form/FileInputAuto.js

raw symbols SCIP
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 _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 // summary:
144 // The callback to toggle the progressbar, and fire the user-defined callback
145
146 // innerHTML throws errors in IE! so use DOM manipulation instead
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 //remove the form used to send the request
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 // summary:
174 // accommodate our extra focusListeners
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 // summary:
191 // stub function fired when an upload has finished.
192 // data:
193 // the raw data found in the first [TEXTAREA] tag of the post url
194 // ioArgs:
195 // the Deferred data being passed from the handle: callback
196 // widgetRef:
197 // this widget pointer, so you can set this.overlay to a completed/error message easily
198 }
199 });
200
201 declare("dojox.form.FileInputBlind", FileInputAuto, {
202 // summary:
203 // An extended version of dojox.form.FileInputAuto
204 // that does not display an input node, but rather only a button
205 // and otherwise behaves just like FileInputAuto
206
207 startup: function(){
208 // summary:
209 // hide our fileInput input field
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 // summary:
218 // in this case, set the button under where the visible button is
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 // summary:
228 // onclick, we need to reposition our newly created input type="file"
229 this.inherited(arguments);
230 this._fixPosition();
231 }
232 });
233
234 return FileInputAuto;
235 });