fileprogress.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. function FileProgress(file, targetID) {
  7. this.fileProgressID = file.id;
  8. this.opacity = 100;
  9. this.height = 0;
  10. this.fileProgressWrapper = document.getElementById(this.fileProgressID);
  11. if (!this.fileProgressWrapper) {
  12. this.fileProgressWrapper = document.createElement("div");
  13. this.fileProgressWrapper.className = "progressWrapper";
  14. this.fileProgressWrapper.id = this.fileProgressID;
  15. this.fileProgressElement = document.createElement("div");
  16. this.fileProgressElement.className = "progressContainer";
  17. var progressCancel = document.createElement("a");
  18. progressCancel.className = "progressCancel";
  19. progressCancel.href = "#";
  20. progressCancel.style.visibility = "hidden";
  21. progressCancel.appendChild(document.createTextNode(" "));
  22. var progressText = document.createElement("div");
  23. progressText.className = "progressName";
  24. progressText.appendChild(document.createTextNode(file.name));
  25. var progressBar = document.createElement("div");
  26. progressBar.className = "progressBarInProgress";
  27. var progressStatus = document.createElement("div");
  28. progressStatus.className = "progressBarStatus";
  29. progressStatus.innerHTML = " ";
  30. this.fileProgressElement.appendChild(progressCancel);
  31. this.fileProgressElement.appendChild(progressText);
  32. this.fileProgressElement.appendChild(progressStatus);
  33. this.fileProgressElement.appendChild(progressBar);
  34. this.fileProgressWrapper.appendChild(this.fileProgressElement);
  35. document.getElementById(targetID).appendChild(this.fileProgressWrapper);
  36. } else {
  37. this.fileProgressElement = this.fileProgressWrapper.firstChild;
  38. this.reset();
  39. }
  40. this.height = this.fileProgressWrapper.offsetHeight;
  41. this.setTimer(null);
  42. }
  43. FileProgress.prototype.setTimer = function (timer) {
  44. this.fileProgressElement["FP_TIMER"] = timer;
  45. };
  46. FileProgress.prototype.getTimer = function (timer) {
  47. return this.fileProgressElement["FP_TIMER"] || null;
  48. };
  49. FileProgress.prototype.reset = function () {
  50. this.fileProgressElement.className = "progressContainer";
  51. this.fileProgressElement.childNodes[2].innerHTML = " ";
  52. this.fileProgressElement.childNodes[2].className = "progressBarStatus";
  53. this.fileProgressElement.childNodes[3].className = "progressBarInProgress";
  54. this.fileProgressElement.childNodes[3].style.width = "0%";
  55. this.appear();
  56. };
  57. FileProgress.prototype.setProgress = function (percentage) {
  58. this.fileProgressElement.className = "progressContainer green";
  59. this.fileProgressElement.childNodes[3].className = "progressBarInProgress";
  60. this.fileProgressElement.childNodes[3].style.width = percentage + "%";
  61. this.appear();
  62. };
  63. FileProgress.prototype.setComplete = function () {
  64. this.fileProgressElement.className = "progressContainer blue";
  65. this.fileProgressElement.childNodes[3].className = "progressBarComplete";
  66. this.fileProgressElement.childNodes[3].style.width = "";
  67. };
  68. FileProgress.prototype.setError = function () {
  69. this.fileProgressElement.className = "progressContainer red";
  70. this.fileProgressElement.childNodes[3].className = "progressBarError";
  71. this.fileProgressElement.childNodes[3].style.width = "";
  72. var oSelf = this;
  73. this.setTimer(setTimeout(function () {
  74. oSelf.disappear();
  75. }, 5000));
  76. };
  77. FileProgress.prototype.setCancelled = function () {
  78. this.fileProgressElement.className = "progressContainer";
  79. this.fileProgressElement.childNodes[3].className = "progressBarError";
  80. this.fileProgressElement.childNodes[3].style.width = "";
  81. var oSelf = this;
  82. this.setTimer(setTimeout(function () {
  83. oSelf.disappear();
  84. }, 2000));
  85. };
  86. FileProgress.prototype.setStatus = function (status) {
  87. this.fileProgressElement.childNodes[2].innerHTML = status;
  88. };
  89. FileProgress.prototype.toggleCancel = function (show, swfUploadInstance) {
  90. this.fileProgressElement.childNodes[0].style.visibility = show ? "visible" : "hidden";
  91. if (swfUploadInstance) {
  92. var fileID = this.fileProgressID;
  93. this.fileProgressElement.childNodes[0].onclick = function () {
  94. swfUploadInstance.cancelUpload(fileID);
  95. return false;
  96. };
  97. }
  98. };
  99. FileProgress.prototype.appear = function () {
  100. if (this.getTimer() !== null) {
  101. clearTimeout(this.getTimer());
  102. this.setTimer(null);
  103. }
  104. if (this.fileProgressWrapper.filters) {
  105. try {
  106. this.fileProgressWrapper.filters.item("DXImageTransform.Microsoft.Alpha").opacity = 100;
  107. } catch (e) {
  108. this.fileProgressWrapper.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=100)";
  109. }
  110. } else {
  111. this.fileProgressWrapper.style.opacity = 1;
  112. }
  113. this.fileProgressWrapper.style.height = "";
  114. this.height = this.fileProgressWrapper.offsetHeight;
  115. this.opacity = 100;
  116. this.fileProgressWrapper.style.display = "";
  117. };
  118. FileProgress.prototype.disappear = function () {
  119. var reduceOpacityBy = 15;
  120. var reduceHeightBy = 4;
  121. var rate = 30; // 15 fps
  122. if (this.opacity > 0) {
  123. this.opacity -= reduceOpacityBy;
  124. if (this.opacity < 0) {
  125. this.opacity = 0;
  126. }
  127. if (this.fileProgressWrapper.filters) {
  128. try {
  129. this.fileProgressWrapper.filters.item("DXImageTransform.Microsoft.Alpha").opacity = this.opacity;
  130. } catch (e) {
  131. this.fileProgressWrapper.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=" + this.opacity + ")";
  132. }
  133. } else {
  134. this.fileProgressWrapper.style.opacity = this.opacity / 100;
  135. }
  136. }
  137. if (this.height > 0) {
  138. this.height -= reduceHeightBy;
  139. if (this.height < 0) {
  140. this.height = 0;
  141. }
  142. this.fileProgressWrapper.style.height = this.height + "px";
  143. }
  144. if (this.height > 0 || this.opacity > 0) {
  145. var oSelf = this;
  146. this.setTimer(setTimeout(function () {
  147. oSelf.disappear();
  148. }, rate));
  149. } else {
  150. this.fileProgressWrapper.style.display = "none";
  151. this.setTimer(null);
  152. }
  153. };