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 | // summary: |
---|
21 | // The Base class used for dojox/form/Uploader and dojox/form/uploader/FileList. |
---|
22 | // |
---|
23 | // Should not be used as a standalone. To be mixed in with other classes. |
---|
24 | // |
---|
25 | getForm: function(){ |
---|
26 | // summary: |
---|
27 | // Finds the parent form of the Uploader, if it exists. |
---|
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 // Node; |
---|
40 | }, |
---|
41 | |
---|
42 | getUrl: function(){ |
---|
43 | // summary: |
---|
44 | // Finds the URL to upload to, whether it be the action in the parent form, this.url or |
---|
45 | // this.uploadUrl |
---|
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; // String |
---|
51 | }, |
---|
52 | |
---|
53 | |
---|
54 | connectForm: function(){ |
---|
55 | // summary: |
---|
56 | // Internal. Connects to form if there is one. |
---|
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 | // summary: |
---|
71 | // Does feature testing for uploader capabilities. (No browser sniffing - yay) |
---|
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; // Boolean |
---|
83 | }, |
---|
84 | getMimeType: function(){ |
---|
85 | // summary: |
---|
86 | // Returns the mime type that should be used in an HTML5 upload form. Return result |
---|
87 | // may change as the current use is very generic. |
---|
88 | return "application/octet-stream"; //image/gif |
---|
89 | }, |
---|
90 | getFileType: function(/*String*/ name){ |
---|
91 | // summary: |
---|
92 | // Gets the extension of a file |
---|
93 | return name.substring(name.lastIndexOf(".")+1).toUpperCase(); // String |
---|
94 | }, |
---|
95 | convertBytes: function(bytes){ |
---|
96 | // summary: |
---|
97 | // Converts bytes. Returns an object with all conversions. The "value" property is |
---|
98 | // considered the most likely desired result. |
---|
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 | }; // Object |
---|
114 | } |
---|
115 | }); |
---|
116 | }); |
---|