FileList.js 5.3 KB

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