source: Dev/trunk/src/client/dojox/form/uploader/_Flash.js

Last change on this file was 483, checked in by hendrikvanantwerpen, 11 years ago

Added Dojo 1.9.3 release.

File size: 8.9 KB
Line 
1define([
2        "dojo/dom-form",
3        "dojo/dom-style",
4        "dojo/dom-construct",
5        "dojo/dom-attr",
6        "dojo/_base/declare",
7        "dojo/_base/config",
8        "dojo/_base/connect",
9        "dojo/_base/lang",
10        "dojo/_base/array",
11        "dojox/embed/Flash"
12],function(domForm, domStyle, domConstruct, domAttr, declare, config, connect, lang, arrayUtil, embedFlash){
13
14
15        return declare("dojox.form.uploader._Flash", [], {
16                // summary:
17                //              A mixin for dojox/form/Uploader that utilizes a Flash SWF for handling to upload in IE.
18                //              All other browsers will use the HTML5 plugin, unless force="flash" is used, then Flash
19                //              will be used in all browsers. force="flash"     is provided because Flash has some features
20                //              that HTML5 does not yet have. But it is still not recommended because of the many problems
21                //              that Firefox and Webkit have with the Flash plugin.
22                //
23                // description:
24                //              All properties and methods listed here are specific to the Flash version only.
25                //
26                //
27                // swfPath:String
28                //              Path to SWF. Can be overwritten or provided in djConfig.
29                swfPath:config.uploaderPath || require.toUrl("dojox/form/resources/uploader.swf"),
30
31                // preventCache: Boolean
32                //              If true, then flash request is sent with a value that changes with each request (timestamp)
33                preventCache: true,
34       
35                // skipServerCheck: Boolean
36                //              If true, will not verify that the server was sent the correct format.
37                //              This can be safely set to true. The purpose of the server side check
38                //              is mainly to show the dev if they've implemented the different returns
39                //              correctly.
40                skipServerCheck:true,
41       
42                // serverTimeout:Number (milliseconds)
43                //              The amount of time given to the uploaded file
44                //              to wait for a server response. After this amount
45                //              of time, the onComplete is fired but with a 'server timeout'
46                //              error in the returned item.
47                serverTimeout: 2000,
48       
49                // isDebug: Boolean
50                //              If true, outputs traces from the SWF to console. What exactly gets passed
51                //              is very relative, and depends upon what traces have been left in the DEFT SWF.
52                isDebug:false,
53       
54                // devMode: Boolean.
55                //              Re-implemented. devMode increases the logging, adding style tracing from the SWF.
56                devMode:false,
57       
58                // deferredUploading: Number (1 - X)
59                //              (Flash only) throttles the upload to a certain amount of files at a time.
60                //              By default, Flash uploads file one at a time to the server, but in parallel.
61                //              Firefox will try to queue all files at once, leading to problems. Set this
62                //              to the amount to upload in parallel at a time.
63                //              Generally, 1 should work fine, but you can experiment with queuing more than
64                //              one at a time.
65                //              This is of course ignored if selectMultipleFiles equals false.
66                deferredUploading:0,
67       
68                postMixInProperties: function(){
69                        if(this.uploadType === 'flash'){
70                                this._files = [];
71                                this._fileMap = {};
72                                this._createInput = this._createFlashUploader;
73                                this.getFileList = this.getFlashFileList;
74                                this.reset = this.flashReset;
75                                this.upload = this.uploadFlash;
76                                this.fieldname = "flashUploadFiles"; ///////////////////// this.name
77                        }
78                        this.inherited(arguments);
79                },
80       
81                /*************************
82                 *         Public Events         *
83                 *************************/
84       
85                onReady: function(/*dojox/form/FileUploader*/ uploader){
86                        // summary:
87                        //              Stub - Fired when embedFlash has created the
88                        //              Flash object, but it has not necessarilly finished
89                        //              downloading, and is ready to be communicated with.
90                },
91       
92                onLoad: function(/*dojox/form/FileUploader*/ uploader){
93                        // summary:
94                        //              Stub - SWF has been downloaded 100%.
95                },
96       
97                onFileChange: function(fileArray){
98                        // summary:
99                        //              Stub - Flash-specific event. Fires on each selection of files
100                        //              and only provides the files selected on that event - not all files
101                        //              selected, as with HTML5
102                },
103       
104                onFileProgress: function(fileArray){
105                        // summary:
106                        //              Stub - Flash-specific event. Fires on progress of upload
107                        //              and only provides a file-specific event
108                },
109       
110       
111                /*************************
112                 *         Public Methods        *
113                 *************************/
114       
115                getFlashFileList: function(){
116                        // summary:
117                        //              Returns list of currently selected files
118                        return this._files; // Array
119                },
120       
121                flashReset: function(){
122                        this.flashMovie.reset();
123                        this._files = [];
124                        this._fileMap = {};
125                },
126       
127                /*************************
128                 *         Private Methods       *
129                 *************************/
130       
131                uploadFlash: function(/*Object ? */ formData){
132                        // summary:
133                        //              Uploads selected files. Alias "upload()" should be used instead.
134                        // tags:
135                        //              private
136                        this.onBegin(this.getFileList());
137                        formData = formData || {};
138                        formData.returnType = "F";
139                        formData.uploadType = this.uploadType;
140                        console.log('flas upload', formData);
141                        this.flashMovie.doUpload(formData);
142                },
143       
144                _change: function(fileArray){
145                        this._files = this._files.concat(fileArray);
146                        arrayUtil.forEach(fileArray, function(f){
147                                f.bytesLoaded = 0;
148                                f.bytesTotal = f.size;
149                                this._fileMap[f.name+"_"+f.size] = f;
150                        }, this);
151                        this.onChange(this._files);
152                        this.onFileChange(fileArray);
153                },
154                _complete: function(fileArray){
155                        var o = this._getCustomEvent();
156                        o.type = "load";
157                        this.onComplete(fileArray);
158                },
159                _progress: function(f){
160                        this._fileMap[f.name+"_"+f.bytesTotal].bytesLoaded = f.bytesLoaded;
161                        var o = this._getCustomEvent();
162                        this.onFileProgress(f);
163                        this.onProgress(o);
164                },
165                _error: function(err){
166                        this.onError(err);
167                },
168                _onFlashBlur: function(fileArray){
169                        //console.log("UploaderFlash._onFlashBlur");
170                },
171       
172                _getCustomEvent: function(){
173                        var o = {
174                                bytesLoaded:0,
175                                bytesTotal:0,
176                                type:"progress",
177                                timeStamp:new Date().getTime()
178                        };
179       
180       
181                        for(var nm in this._fileMap){
182                                o.bytesTotal += this._fileMap[nm].bytesTotal;
183                                o.bytesLoaded += this._fileMap[nm].bytesLoaded;
184                        }
185                        o.decimal = o.bytesLoaded / o.bytesTotal;
186                        o.percent = Math.ceil((o.bytesLoaded / o.bytesTotal)*100)+"%";
187                        return o; // Object
188                },
189       
190                _connectFlash: function(){
191                        // summary:
192                        //              Subscribing to published topics coming from the
193                        //              Flash uploader.
194       
195                        // Sacrificing some readability for compactness. this.id
196                        // will be on the beginning of the topic, so more than
197                        // one uploader can be on a page and can have unique calls.
198       
199                        this._subs = [];
200                        this._cons = [];
201       
202                        var doSub = lang.hitch(this, function(s, funcStr){
203                                this._subs.push(connect.subscribe(this.id + s, this, funcStr));
204                        });
205       
206                        doSub("/filesSelected", "_change");
207                        doSub("/filesUploaded", "_complete");
208                        doSub("/filesProgress", "_progress");
209                        doSub("/filesError", "_error");
210                        doSub("/filesCanceled", "onCancel");
211                        doSub("/stageBlur", "_onFlashBlur");
212       
213                        this.connect(this.domNode, "focus", function(){
214                                // TODO: some kind of indicator that the Flash button is in focus
215                                this.flashMovie.focus();
216                                this.flashMovie.doFocus();
217                        });
218                        if(this.tabIndex>=0){
219                                domAttr.set(this.domNode, "tabIndex", this.tabIndex);
220                        }
221                },
222                _createFlashUploader: function(){
223                        // summary:
224                        //              Internal. Creates Flash Uploader
225       
226                        var w = this.btnSize.w;
227                        var h = this.btnSize.h;
228                        if(!w){
229                                // FIXME: Commit this
230                                setTimeout(dojo.hitch(this, function(){
231                                        this._getButtonStyle(this.domNode);
232                                        this._createFlashUploader();
233                                }), 200);
234                                return;
235                        }
236                        var url = this.getUrl();
237                        if(url){
238                                if(url.toLowerCase().indexOf("http")<0 && url.indexOf("/")!=0){
239                                        // Appears to be a relative path. Attempt to
240                                        // convert it to absolute, so it will better
241                                        // target the SWF.
242                                        var loc = window.location.href.split("/");
243                                        loc.pop();
244                                        loc = loc.join("/")+"/";
245                                        url = loc+url;
246                                }
247                        }else{
248                                console.warn("Warning: no uploadUrl provided.");
249                        }
250       
251                        this.inputNode = domConstruct.create("div", {className:"dojoxFlashNode"}, this.domNode, "first");
252                        domStyle.set(this.inputNode, {
253                                position:"absolute",
254                                top:"-2px",
255                                width:w+"px",
256                                height:h+"px",
257                                opacity:0
258                        });
259       
260       
261       
262                        var args = {
263                                expressInstall:true,
264                                path: (this.swfPath.uri || this.swfPath) + ((this.preventCache)?("?cb_" + (new Date().getTime())):""),
265                                width: w,
266                                height: h,
267                                allowScriptAccess:"always",
268                                allowNetworking:"all",
269                                vars: {
270                                        uploadDataFieldName: this.flashFieldName || this.name+"Flash",
271                                        uploadUrl: url,
272                                        uploadOnSelect: this.uploadOnSelect,
273                                        deferredUploading:this.deferredUploading || 0,
274                                        selectMultipleFiles: this.multiple,
275                                        id: this.id,
276                                        isDebug: this.isDebug,
277                                        noReturnCheck: this.skipServerCheck,
278                                        serverTimeout:this.serverTimeout
279                                },
280                                params: {
281                                        scale:"noscale",
282                                        //wmode:"transparent",
283                                        wmode:"opaque",
284                                        allowScriptAccess:"always",
285                                        allowNetworking:"all"
286                                }
287       
288                        };
289       
290                        this.flashObject = new embedFlash(args, this.inputNode);
291                        this.flashObject.onError = lang.hitch(function(msg){
292                                console.error("Flash Error: " + msg);
293                        });
294                        this.flashObject.onReady = lang.hitch(this, function(){
295                                this.onReady(this);
296                        });
297                        this.flashObject.onLoad = lang.hitch(this, function(mov){
298                                this.flashMovie = mov;
299                                this.flashReady = true;
300       
301                                this.onLoad(this);
302                        });
303                        this._connectFlash();
304                }
305        });
306});
Note: See TracBrowser for help on using the repository browser.