Debian Codegraph @

dojox/form/uploader/_HTML5.js

raw symbols SCIP
1 define([
2 "dojo/_base/declare",
3 "dojo/_base/lang",
4 "dojo/_base/array",
5 "dojo",
6 "dojo/request",
7 "dojo/has"
8 ],function(declare, lang, arrayUtil, dojo, request, has){
9 return declare("dojox.form.uploader._HTML5", [], {
10 // summary:
11 // A mixin for dojox/form/Uploader that adds HTML5 multiple-file upload capabilities and
12 // progress events.
13 //
14 // description:
15 // Note that this does not add these capabilities to browsers that don't support them.
16 // For IE8 or older browsers, _IFrame or _Flash mixins will be used.
17 //
18
19 // debug message:
20 errMsg:"Error uploading files. Try checking permissions",
21
22 // Overwrites "form" and could possibly be overwritten again by iframe or flash plugin.
23 uploadType:"html5",
24
25 postMixInProperties: function(){
26 this.inherited(arguments);
27 if(this.uploadType === "html5"){ }
28 },
29
30 postCreate: function(){
31 this.connectForm();
32 this.inherited(arguments);
33 if(this.uploadOnSelect){
34 this.connect(this, "onChange", function(data){
35 this.upload(data[0]);
36 });
37 }
38 },
39
40 _drop: function(e){
41 dojo.stopEvent(e);
42 var dt = e.dataTransfer;
43 this._files = dt.files;
44 this.onChange(this.getFileList());
45 },
46 /*************************
47 * Public Methods *
48 *************************/
49
50 upload: function(/*Object ? */ formData){
51 // summary:
52 // See: dojox.form.Uploader.upload
53
54 this.onBegin(this.getFileList());
55 this.uploadWithFormData(formData);
56 },
57
58 addDropTarget: function(node, /*Boolean?*/ onlyConnectDrop){
59 // summary:
60 // Add a dom node which will act as the drop target area so user
61 // can drop files to this node.
62 // description:
63 // If onlyConnectDrop is true, dragenter/dragover/dragleave events
64 // won't be connected to dojo.stopEvent, and they need to be
65 // canceled by user code to allow DnD files to happen.
66 // This API is only available in HTML5 plugin (only HTML5 allows
67 // DnD files).
68 if(!onlyConnectDrop){
69 this.connect(node, 'dragenter', dojo.stopEvent);
70 this.connect(node, 'dragover', dojo.stopEvent);
71 this.connect(node, 'dragleave', dojo.stopEvent);
72 }
73 this.connect(node, 'drop', '_drop');
74 },
75
76 uploadWithFormData: function(/*Object*/ data){
77 // summary:
78 // Used with WebKit and Firefox 4+
79 // Upload files using the much friendlier FormData browser object.
80 // tags:
81 // private
82
83 if(!this.getUrl()){
84 console.error("No upload url found.", this); return;
85 }
86 var fd = new FormData(), fieldName=this._getFileFieldName();
87 arrayUtil.forEach(this._files, function(f, i){
88 fd.append(fieldName, f);
89 }, this);
90
91 if(data){
92 data.uploadType = this.uploadType;
93 for(var nm in data){
94 fd.append(nm, data[nm]);
95 }
96 }
97
98 var self = this;
99 var deferred = request(
100 this.getUrl(),
101 {
102 method: "POST",
103 data: fd,
104 handleAs: "json",
105 uploadProgress: true,
106 headers: {
107 Accept: "application/json"
108 }
109 },
110 true
111 );
112
113 deferred.promise.response
114 .otherwise(function (error){
115 console.error(error);
116 console.error(error.response.text);
117 self.onError(error);
118 })
119 ;
120
121 function onProgressHandler(event){
122 self._xhrProgress(event);
123
124 if(event.type !== "load"){
125 return;
126 }
127
128 self.onComplete(deferred.response.data);
129
130 // Disconnect event handlers when done
131 deferred.response.xhr.removeEventListener("load", onProgressHandler, false);
132 deferred.response.xhr.upload.removeEventListener("progress", onProgressHandler, false);
133
134 deferred = null;
135 }
136
137 if(has("native-xhr2")){
138 // Use addEventListener directly to pass the raw events to Uploader#_xhrProgress
139 deferred.response.xhr.addEventListener("load", onProgressHandler, false);
140 deferred.response.xhr.upload.addEventListener("progress", onProgressHandler, false);
141 }else{
142 // If the browser doesn't have upload events, notify when the upload is complete
143 deferred.promise.then(function(data){
144 self.onComplete(data);
145 });
146 }
147 },
148
149 _xhrProgress: function(evt){
150 if(evt.lengthComputable){
151 var o = {
152 bytesLoaded:evt.loaded,
153 bytesTotal:evt.total,
154 type:evt.type,
155 timeStamp:evt.timeStamp
156 };
157 if(evt.type == "load"){
158 // 100%
159 o.percent = "100%";
160 o.decimal = 1;
161 }else{
162 o.decimal = evt.loaded / evt.total;
163 o.percent = Math.ceil((evt.loaded / evt.total)*100)+"%";
164 }
165 this.onProgress(o);
166 }
167 }
168 });
169
170 });