Base.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. define("dojox/form/uploader/Base", [
  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. has.add('FormData', function(){return !!window.FormData;});
  14. has.add("xhr-sendAsBinary", function(){var xhr=window.XMLHttpRequest && new window.XMLHttpRequest(); return xhr && !!xhr.sendAsBinary;});
  15. has.add("file-multiple", function(){return !!({'true':1,'false':1}[domAttr.get(document.createElement('input',{type:"file"}), 'multiple')]);});
  16. /*=====
  17. Widget = dijit._Widget;
  18. TemplatedMixin = dijit._TemplatedMixin;
  19. WidgetsInTemplateMixin = dijit._WidgetsInTemplateMixin;
  20. =====*/
  21. return declare("dojox.form.uploader.Base", [Widget, TemplatedMixin, WidgetsInTemplateMixin], {
  22. //
  23. // Version: 1.6
  24. //
  25. // summary:
  26. // The Base class used for dojox.form.Uploader and dojox.form.uploader.FileList.
  27. //
  28. // description:
  29. // Should not be used as a standalone. To be mixed in with other classes.
  30. //
  31. getForm: function(){
  32. // summary:
  33. // Finds the parent form of the Uploader, if it exists.
  34. //
  35. if(!this.form){
  36. var n = this.domNode;
  37. while(n && n.tagName && n !== document.body){
  38. if(n.tagName.toLowerCase() == "form"){
  39. this.form = n;
  40. break;
  41. }
  42. n = n.parentNode;
  43. }
  44. }
  45. return this.form // Node;
  46. },
  47. getUrl: function(){
  48. // summary:
  49. // Finds the URL to upload to, whether it be the action in the parent form, this.url or
  50. // this.uploadUrl
  51. //
  52. if(this.uploadUrl) this.url = this.uploadUrl;
  53. if(this.url) return this.url;
  54. if(this.getForm()) this.url = this.form.action;
  55. return this.url; // String
  56. },
  57. connectForm: function(){
  58. // summary:
  59. // Internal. Connects to form if there is one.
  60. //
  61. this.url = this.getUrl();
  62. if(!this._fcon && !!this.getForm()){
  63. this._fcon = true;
  64. this.connect(this.form, "onsubmit", function(evt){
  65. event.stop(evt);
  66. this.submit(this.form);
  67. });
  68. }
  69. },
  70. supports: function(what){
  71. // summary:
  72. // Does feature testing for uploader capabilities. (No browser sniffing - yay)
  73. //
  74. switch(what){
  75. case "multiple":
  76. if(this.force == "flash" || this.force == "iframe") return false;
  77. return has("file-multiple");
  78. case "FormData":
  79. return has(what);
  80. case "sendAsBinary":
  81. return has("xhr-sendAsBinary");
  82. }
  83. return false; // Boolean
  84. },
  85. getMimeType: function(){
  86. // summary:
  87. // Returns the mime type that should be used in an HTML5 upload form. Return result
  88. // may change as the current use is very generic.
  89. //
  90. return "application/octet-stream"; //image/gif
  91. },
  92. getFileType: function(/* String */name){
  93. // summary:
  94. // Gets the extension of a file
  95. return name.substring(name.lastIndexOf(".")+1).toUpperCase(); // String
  96. },
  97. convertBytes: function(bytes){
  98. // summary:
  99. // Converts bytes. Returns an object with all conversions. The "value" property is
  100. // considered the most likely desired result.
  101. //
  102. var kb = Math.round(bytes/1024*100000)/100000;
  103. var mb = Math.round(bytes/1048576*100000)/100000;
  104. var gb = Math.round(bytes/1073741824*100000)/100000;
  105. var value = bytes;
  106. if(kb>1) value = kb.toFixed(1)+" kb";
  107. if(mb>1) value = mb.toFixed(1)+" mb";
  108. if(gb>1) value = gb.toFixed(1)+" gb";
  109. return {
  110. kb:kb,
  111. mb:mb,
  112. gb:gb,
  113. bytes:bytes,
  114. value: value
  115. }; // Object
  116. }
  117. });
  118. });