FileList.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. define("dojox/form/uploader/FileList", [
  2. "dojo/_base/fx",
  3. "dojo/dom-style",
  4. "dojo/dom-class",
  5. "dojo/_base/declare",
  6. "dojo/_base/lang",
  7. "dojo/_base/array",
  8. "dijit/_base/manager",
  9. "dojox/form/uploader/Base"
  10. ],function(fx, domStyle, domClass, declare, lang, array, manager, formUploaderBase){
  11. /*=====
  12. formUploaderBase = dojox.form.uploader.Base;
  13. =====*/
  14. return declare("dojox.form.uploader.FileList", [formUploaderBase], {
  15. //
  16. // Version: 1.6
  17. //
  18. // summary:
  19. // A simple widget that provides a list of the files currently selected by
  20. // dojox.form.Uploader
  21. //
  22. // description:
  23. // There is a required CSS file: resources/UploaderFileList.css.
  24. // This is a very simple widget, and not beautifully styled. It is here mainly for test
  25. // cases, but could very easily be used, extended, modified, or copied.
  26. //
  27. // uploaderId: String
  28. // The id of the dojox.form.Uploader to connect to.
  29. uploaderId:"",
  30. // uploader: dojox.form.Uploader
  31. // The dojox.form.Uploader to connect to. Use either this property of unploaderId. This
  32. // property is populated if uploaderId is used.
  33. //
  34. uploader:null,
  35. // headerIndex: String
  36. // The label for the index column.
  37. //
  38. headerIndex:"#",
  39. // headerType: String
  40. // The label for the file type column.
  41. //
  42. headerType:"Type",
  43. // headerFilename: String
  44. // The label for the file name column.
  45. //
  46. headerFilename:"File Name",
  47. // headerFilesize: String
  48. // The label for the file size column.
  49. //
  50. headerFilesize:"Size",
  51. _upCheckCnt:0,
  52. rowAmt:0,
  53. templateString: '<div class="dojoxUploaderFileList">' +
  54. '<div dojoAttachPoint="progressNode" class="dojoxUploaderFileListProgress"><div dojoAttachPoint="percentBarNode" class="dojoxUploaderFileListProgressBar"></div><div dojoAttachPoint="percentTextNode" class="dojoxUploaderFileListPercentText">0%</div></div>' +
  55. '<table class="dojoxUploaderFileListTable">'+
  56. '<thead><tr class="dojoxUploaderFileListHeader"><th class="dojoxUploaderIndex">${headerIndex}</th><th class="dojoxUploaderIcon">${headerType}</th><th class="dojoxUploaderFileName">${headerFilename}</th><th class="dojoxUploaderFileSize" dojoAttachPoint="sizeHeader">${headerFilesize}</th></tr></thead>'+
  57. '<tbody class="dojoxUploaderFileListContent" dojoAttachPoint="listNode">'+
  58. '</tbody>'+
  59. '</table>'+
  60. '<div>'
  61. ,
  62. postCreate: function(){
  63. this.setUploader();
  64. this.hideProgress();
  65. },
  66. reset: function(){
  67. // summary:
  68. // Clears all rows of items. Happens automatically if Uploader is reset, but you
  69. // could call this directly.
  70. //
  71. for(var i=0;i<this.rowAmt;i++){
  72. this.listNode.deleteRow(0);
  73. }
  74. this.rowAmt = 0;
  75. },
  76. setUploader: function(){
  77. // summary:
  78. // Connects to the Uploader based on the uploader or the uploaderId properties.
  79. //
  80. if(!this.uploaderId && !this.uploader){
  81. console.warn("uploaderId not passed to UploaderFileList");
  82. }else if(this.uploaderId && !this.uploader){
  83. this.uploader = manager.byId(this.uploaderId);
  84. }else if(this._upCheckCnt>4){
  85. console.warn("uploader not found for ID ", this.uploaderId);
  86. return;
  87. }
  88. if(this.uploader){
  89. this.connect(this.uploader, "onChange", "_onUploaderChange");
  90. this.connect(this.uploader, "reset", "reset");
  91. this.connect(this.uploader, "onBegin", function(){
  92. this.showProgress(true);
  93. });
  94. this.connect(this.uploader, "onProgress", "_progress");
  95. this.connect(this.uploader, "onComplete", function(){
  96. setTimeout(lang.hitch(this, function(){
  97. this.hideProgress(true);
  98. }), 1250);
  99. });
  100. if(!(this._fileSizeAvail = {'html5':1,'flash':1}[this.uploader.uploadType])){
  101. //if uploadType is neither html5 nor flash, file size is not available
  102. //hide the size header
  103. this.sizeHeader.style.display="none";
  104. }
  105. }else{
  106. this._upCheckCnt++;
  107. setTimeout(lang.hitch(this, "setUploader"), 250);
  108. }
  109. },
  110. hideProgress: function(/* Boolean */animate){
  111. var o = animate ? {
  112. ani:true,
  113. endDisp:"none",
  114. beg:15,
  115. end:0
  116. } : {
  117. endDisp:"none",
  118. ani:false
  119. };
  120. this._hideShowProgress(o);
  121. },
  122. showProgress: function(/* Boolean */animate){
  123. var o = animate ? {
  124. ani:true,
  125. endDisp:"block",
  126. beg:0,
  127. end:15
  128. } : {
  129. endDisp:"block",
  130. ani:false
  131. };
  132. this._hideShowProgress(o);
  133. },
  134. _progress: function(/* Object */ customEvent){
  135. this.percentTextNode.innerHTML = customEvent.percent;
  136. domStyle.set(this.percentBarNode, "width", customEvent.percent);
  137. },
  138. _hideShowProgress: function(o){
  139. var node = this.progressNode;
  140. var onEnd = function(){
  141. domStyle.set(node, "display", o.endDisp);
  142. }
  143. if(o.ani){
  144. domStyle.set(node, "display", "block");
  145. fx.animateProperty({
  146. node: node,
  147. properties:{
  148. height:{
  149. start:o.beg,
  150. end:o.end,
  151. units:"px"
  152. }
  153. },
  154. onEnd:onEnd
  155. }).play();
  156. }else{
  157. onEnd();
  158. }
  159. },
  160. _onUploaderChange: function(fileArray){
  161. this.reset();
  162. array.forEach(fileArray, function(f, i){
  163. this._addRow(i+1, this.getFileType(f.name), f.name, f.size);
  164. }, this)
  165. },
  166. _addRow: function(index, type, name, size){
  167. var c, r = this.listNode.insertRow(-1);
  168. c = r.insertCell(-1);
  169. domClass.add(c, "dojoxUploaderIndex");
  170. c.innerHTML = index;
  171. c = r.insertCell(-1);
  172. domClass.add(c, "dojoxUploaderIcon");
  173. c.innerHTML = type;
  174. c = r.insertCell(-1);
  175. domClass.add(c, "dojoxUploaderFileName");
  176. c.innerHTML = name;
  177. if(this._fileSizeAvail){
  178. c = r.insertCell(-1);
  179. domClass.add(c, "dojoxUploaderSize");
  180. c.innerHTML = this.convertBytes(size).value;
  181. }
  182. this.rowAmt++;
  183. }
  184. });
  185. });