| 1 |
define([ |
| 2 |
"dojo/dom-form", |
| 3 |
"dojo/dom-style", |
| 4 |
"dojo/dom-construct", |
| 5 |
"dojo/dom-attr", |
| 6 |
"dojo/has", |
| 7 |
"dojo/_base/declare", |
| 8 |
"dojo/_base/event", |
| 9 |
"dijit/_Widget", |
| 10 |
"dijit/_TemplatedMixin", |
| 11 |
"dijit/_WidgetsInTemplateMixin" |
| 12 |
],function(domForm, domStyle, domConstruct, domAttr, has, declare, event, Widget, TemplatedMixin, WidgetsInTemplateMixin){ |
| 13 |
|
| 14 |
has.add('FormData', function(){return !!window.FormData;}); |
| 15 |
has.add('xhr-sendAsBinary', function(){var xhr=window.XMLHttpRequest && new window.XMLHttpRequest(); return xhr && !!xhr.sendAsBinary;}); |
| 16 |
has.add('file-multiple', function(){return !!({'true':1,'false':1}[domAttr.get(document.createElement('input',{type:"file"}), 'multiple')]);}); |
| 17 |
|
| 18 |
|
| 19 |
return declare("dojox.form.uploader._Base", [Widget, TemplatedMixin, WidgetsInTemplateMixin], { |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
getForm: function(){ |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
if(!this.form){ |
| 30 |
var n = this.domNode; |
| 31 |
while(n && n.tagName && n !== document.body){ |
| 32 |
if(n.tagName.toLowerCase() == "form"){ |
| 33 |
this.form = n; |
| 34 |
break; |
| 35 |
} |
| 36 |
n = n.parentNode; |
| 37 |
} |
| 38 |
} |
| 39 |
return this.form |
| 40 |
}, |
| 41 |
|
| 42 |
getUrl: function(){ |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
if(this.uploadUrl) this.url = this.uploadUrl; |
| 48 |
if(this.url) return this.url; |
| 49 |
if(this.getForm()) this.url = this.form.action; |
| 50 |
return this.url; |
| 51 |
}, |
| 52 |
|
| 53 |
|
| 54 |
connectForm: function(){ |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
this.url = this.getUrl(); |
| 59 |
if(!this._fcon && !!this.getForm()){ |
| 60 |
this._fcon = true; |
| 61 |
this.connect(this.form, "onsubmit", function(evt){ |
| 62 |
console.log('SUBMIT') |
| 63 |
event.stop(evt); |
| 64 |
this.submit(this.form); |
| 65 |
}); |
| 66 |
} |
| 67 |
}, |
| 68 |
|
| 69 |
supports: function(what){ |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
switch(what){ |
| 74 |
case "multiple": |
| 75 |
if(this.force == "flash" || this.force == "iframe") return false; |
| 76 |
return has("file-multiple"); |
| 77 |
case "FormData": |
| 78 |
return has(what); |
| 79 |
case "sendAsBinary": |
| 80 |
return has("xhr-sendAsBinary"); |
| 81 |
} |
| 82 |
return false; |
| 83 |
}, |
| 84 |
getMimeType: function(){ |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
return "application/octet-stream"; |
| 89 |
}, |
| 90 |
getFileType: function( name){ |
| 91 |
|
| 92 |
|
| 93 |
return name.substring(name.lastIndexOf(".")+1).toUpperCase(); |
| 94 |
}, |
| 95 |
convertBytes: function(bytes){ |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
var kb = Math.round(bytes/1024*100000)/100000; |
| 101 |
var mb = Math.round(bytes/1048576*100000)/100000; |
| 102 |
var gb = Math.round(bytes/1073741824*100000)/100000; |
| 103 |
var value = bytes; |
| 104 |
if(kb>1) value = kb.toFixed(1)+" kb"; |
| 105 |
if(mb>1) value = mb.toFixed(1)+" mb"; |
| 106 |
if(gb>1) value = gb.toFixed(1)+" gb"; |
| 107 |
return { |
| 108 |
kb:kb, |
| 109 |
mb:mb, |
| 110 |
gb:gb, |
| 111 |
bytes:bytes, |
| 112 |
value: value |
| 113 |
}; |
| 114 |
} |
| 115 |
}); |
| 116 |
}); |