fileprogress.js 5.7 KB

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