swfFileProgress.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*
  2. A simple class for displaying file information and progress
  3. Note: This is a demonstration only and not part of SWFUpload.
  4. Note: Some have had problems adapting this class in IE7. It may not be suitable for your application.
  5. */
  6. // Constructor
  7. // file is a SWFUpload file object
  8. // targetID is the HTML element id attribute that the FileProgress HTML structure will be added to.
  9. // Instantiating a new FileProgress object with an existing file will reuse/update the existing DOM elements
  10. function FileProgress(file,targetId)
  11. {
  12. this.fileProgressID = file.id;
  13. this.opacity = 100;
  14. this.height = 0;
  15. this.fileProgressWrapper = document.getElementById(this.fileProgressID);
  16. if (!this.fileProgressWrapper)
  17. {
  18. this.fileProgressWrapper = document.createElement("div");
  19. this.fileProgressWrapper.className = "progressWrapper";
  20. this.fileProgressWrapper.id = this.fileProgressID;
  21. this.fileProgressElement = document.createElement("div");
  22. this.fileProgressElement.className = "progressContainer";
  23. var progressThumbImg = document.createElement("img");
  24. progressThumbImg.className = "progressThumbImg";
  25. progressThumbImg.href = "#";
  26. progressThumbImg.style.display = "none";
  27. progressThumbImg.appendChild(document.createTextNode(" "));
  28. var progressCancel = document.createElement("a");
  29. progressCancel.className = "progressCancel";
  30. progressCancel.href = "#";
  31. progressCancel.style.visibility = "hidden";
  32. progressCancel.appendChild(document.createTextNode(" "));
  33. var progressText = document.createElement("div");
  34. progressText.className = "progressName";
  35. progressText.appendChild(document.createTextNode(file.name));
  36. var progressBar = document.createElement("div");
  37. progressBar.className = "progressBarInProgress";
  38. var progressStatus = document.createElement("div");
  39. progressStatus.className = "progressBarStatus";
  40. progressStatus.innerHTML = " ";
  41. this.fileProgressElement.appendChild(progressCancel);
  42. this.fileProgressElement.appendChild(progressText);
  43. this.fileProgressElement.appendChild(progressStatus);
  44. this.fileProgressElement.appendChild(progressBar);
  45. this.fileProgressElement.appendChild(progressThumbImg);
  46. this.fileProgressWrapper.appendChild(this.fileProgressElement);
  47. document.getElementById(targetId).appendChild(this.fileProgressWrapper);
  48. }
  49. else
  50. {
  51. this.fileProgressElement = this.fileProgressWrapper.firstChild;
  52. this.reset();
  53. }
  54. this.height = this.fileProgressWrapper.offsetHeight;
  55. this.setTimer(null);
  56. }
  57. //ÉèÖÃËõÂÔͼµÄ·¾¶
  58. FileProgress.prototype.SetThumbImg = function (data)
  59. {
  60. $(this.fileProgressElement).find("img[class=progressThumbImg]").attr({
  61. "src": $.parseJSON(data).FileUrl,"hight": "200px","width": "200px"
  62. });
  63. $(this.fileProgressElement).find("img[class=progressThumbImg]").show();
  64. };
  65. FileProgress.prototype.setTimer = function (timer)
  66. {
  67. this.fileProgressElement["FP_TIMER"] = timer;
  68. };
  69. FileProgress.prototype.getTimer = function (timer)
  70. {
  71. return this.fileProgressElement["FP_TIMER"] || null;
  72. };
  73. FileProgress.prototype.reset = function ()
  74. {
  75. this.fileProgressElement.className = "progressContainer";
  76. this.fileProgressElement.childNodes[2].innerHTML = " ";
  77. this.fileProgressElement.childNodes[2].className = "progressBarStatus";
  78. this.fileProgressElement.childNodes[3].className = "progressBarInProgress";
  79. this.fileProgressElement.childNodes[3].style.width = "0%";
  80. this.appear();
  81. };
  82. FileProgress.prototype.setProgress = function (percentage)
  83. {
  84. this.fileProgressElement.className = "progressContainer green";
  85. this.fileProgressElement.childNodes[3].className = "progressBarInProgress";
  86. this.fileProgressElement.childNodes[3].style.width = percentage + "%";
  87. this.appear();
  88. };
  89. FileProgress.prototype.setComplete = function ()
  90. {
  91. this.fileProgressElement.className = "progressContainer blue";
  92. this.fileProgressElement.childNodes[3].className = "progressBarComplete";
  93. this.fileProgressElement.childNodes[3].style.width = "";
  94. // var oSelf = this;
  95. // this.setTimer(setTimeout(function () {
  96. // oSelf.disappear();
  97. // }, 10000));
  98. };
  99. FileProgress.prototype.setError = function ()
  100. {
  101. this.fileProgressElement.className = "progressContainer red";
  102. this.fileProgressElement.childNodes[3].className = "progressBarError";
  103. this.fileProgressElement.childNodes[3].style.width = "";
  104. var oSelf = this;
  105. this.setTimer(setTimeout(function ()
  106. {
  107. oSelf.disappear();
  108. },5000));
  109. };
  110. FileProgress.prototype.setCancelled = function ()
  111. {
  112. this.fileProgressElement.className = "progressContainer";
  113. this.fileProgressElement.childNodes[3].className = "progressBarError";
  114. this.fileProgressElement.childNodes[3].style.width = "";
  115. var oSelf = this;
  116. this.setTimer(setTimeout(function ()
  117. {
  118. oSelf.disappear();
  119. },2000));
  120. };
  121. FileProgress.prototype.setStatus = function (status)
  122. {
  123. this.fileProgressElement.childNodes[2].innerHTML = status;
  124. };
  125. // Show/Hide the cancel button
  126. FileProgress.prototype.toggleCancel = function (show,swfUploadInstance)
  127. {
  128. this.fileProgressElement.childNodes[0].style.visibility = show ? "visible" : "hidden";
  129. if (swfUploadInstance)
  130. {
  131. var fileId = this.fileProgressID;
  132. this.fileProgressElement.childNodes[0].onclick = function ()
  133. {
  134. swfUploadInstance.cancelUpload(fileId);
  135. var fNode = document.getElementById(fileId);
  136. var status = document.getElementById("divStatus");
  137. var num = parseInt(status.innerHTML);
  138. if (num > 0)
  139. status.innerHTML = num - 1;
  140. fNode.parentNode.removeChild(fNode);
  141. return false;
  142. };
  143. }
  144. };
  145. FileProgress.prototype.appear = function ()
  146. {
  147. if (this.getTimer() !== null)
  148. {
  149. clearTimeout(this.getTimer());
  150. this.setTimer(null);
  151. }
  152. if (this.fileProgressWrapper.filters)
  153. {
  154. try
  155. {
  156. this.fileProgressWrapper.filters.item("DXImageTransform.Microsoft.Alpha").opacity = 100;
  157. } catch (e)
  158. {
  159. // If it is not set initially, the browser will throw an error. This will set it if it is not set yet.
  160. this.fileProgressWrapper.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=100)";
  161. }
  162. } else
  163. {
  164. this.fileProgressWrapper.style.opacity = 1;
  165. }
  166. this.fileProgressWrapper.style.height = "";
  167. this.height = this.fileProgressWrapper.offsetHeight;
  168. this.opacity = 100;
  169. this.fileProgressWrapper.style.display = "";
  170. };
  171. // Fades out and clips away the FileProgress box.
  172. FileProgress.prototype.disappear = function ()
  173. {
  174. var reduceOpacityBy = 15;
  175. var reduceHeightBy = 4;
  176. var rate = 30; // 15 fps
  177. if (this.opacity > 0)
  178. {
  179. this.opacity -= reduceOpacityBy;
  180. if (this.opacity < 0)
  181. {
  182. this.opacity = 0;
  183. }
  184. if (this.fileProgressWrapper.filters)
  185. {
  186. try
  187. {
  188. this.fileProgressWrapper.filters.item("DXImageTransform.Microsoft.Alpha").opacity = this.opacity;
  189. } catch (e)
  190. {
  191. // If it is not set initially, the browser will throw an error. This will set it if it is not set yet.
  192. this.fileProgressWrapper.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=" + this.opacity + ")";
  193. }
  194. } else
  195. {
  196. this.fileProgressWrapper.style.opacity = this.opacity / 100;
  197. }
  198. }
  199. if (this.height > 0)
  200. {
  201. this.height -= reduceHeightBy;
  202. if (this.height < 0)
  203. {
  204. this.height = 0;
  205. }
  206. this.fileProgressWrapper.style.height = this.height + "px";
  207. }
  208. if (this.height > 0 || this.opacity > 0)
  209. {
  210. var oSelf = this;
  211. this.setTimer(setTimeout(function ()
  212. {
  213. oSelf.disappear();
  214. },rate));
  215. } else
  216. {
  217. this.fileProgressWrapper.style.display = "none";
  218. this.setTimer(null);
  219. }
  220. };
  221. FileProgress.prototype.setFileValue = function (id,url)
  222. {
  223. $('<input />',{
  224. type: "hidden",
  225. val: url,
  226. id: "value_" + id,
  227. name: "fileUrl"
  228. }).appendTo(this.fileProgressElement);
  229. };