KXStudio Website https://kx.studio/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

151 lines
4.9KB

  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. }
  43. this.height = this.fileProgressWrapper.offsetHeight;
  44. }
  45. FileProgress.prototype.setProgress = function (percentage) {
  46. this.fileProgressElement.className = "progressContainer green";
  47. this.fileProgressElement.childNodes[3].className = "progressBarInProgress";
  48. this.fileProgressElement.childNodes[3].style.width = percentage + "%";
  49. };
  50. FileProgress.prototype.setComplete = function () {
  51. this.fileProgressElement.className = "progressContainer blue";
  52. this.fileProgressElement.childNodes[3].className = "progressBarComplete";
  53. this.fileProgressElement.childNodes[3].style.width = "";
  54. var oSelf = this;
  55. setTimeout(function () {
  56. oSelf.disappear();
  57. }, 10000);
  58. };
  59. FileProgress.prototype.setError = function () {
  60. this.fileProgressElement.className = "progressContainer red";
  61. this.fileProgressElement.childNodes[3].className = "progressBarError";
  62. this.fileProgressElement.childNodes[3].style.width = "";
  63. var oSelf = this;
  64. setTimeout(function () {
  65. oSelf.disappear();
  66. }, 5000);
  67. };
  68. FileProgress.prototype.setCancelled = function () {
  69. this.fileProgressElement.className = "progressContainer";
  70. this.fileProgressElement.childNodes[3].className = "progressBarError";
  71. this.fileProgressElement.childNodes[3].style.width = "";
  72. var oSelf = this;
  73. setTimeout(function () {
  74. oSelf.disappear();
  75. }, 2000);
  76. };
  77. FileProgress.prototype.setStatus = function (status) {
  78. this.fileProgressElement.childNodes[2].innerHTML = status;
  79. };
  80. // Show/Hide the cancel button
  81. FileProgress.prototype.toggleCancel = function (show, swfUploadInstance) {
  82. this.fileProgressElement.childNodes[0].style.visibility = show ? "visible" : "hidden";
  83. if (swfUploadInstance) {
  84. var fileID = this.fileProgressID;
  85. this.fileProgressElement.childNodes[0].onclick = function () {
  86. swfUploadInstance.cancelUpload(fileID);
  87. return false;
  88. };
  89. }
  90. };
  91. // Fades out and clips away the FileProgress box.
  92. FileProgress.prototype.disappear = function () {
  93. var reduceOpacityBy = 15;
  94. var reduceHeightBy = 4;
  95. var rate = 30; // 15 fps
  96. if (this.opacity > 0) {
  97. this.opacity -= reduceOpacityBy;
  98. if (this.opacity < 0) {
  99. this.opacity = 0;
  100. }
  101. if (this.fileProgressWrapper.filters) {
  102. try {
  103. this.fileProgressWrapper.filters.item("DXImageTransform.Microsoft.Alpha").opacity = this.opacity;
  104. } catch (e) {
  105. // If it is not set initially, the browser will throw an error. This will set it if it is not set yet.
  106. this.fileProgressWrapper.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=" + this.opacity + ")";
  107. }
  108. } else {
  109. this.fileProgressWrapper.style.opacity = this.opacity / 100;
  110. }
  111. }
  112. if (this.height > 0) {
  113. this.height -= reduceHeightBy;
  114. if (this.height < 0) {
  115. this.height = 0;
  116. }
  117. this.fileProgressWrapper.style.height = this.height + "px";
  118. }
  119. if (this.height > 0 || this.opacity > 0) {
  120. var oSelf = this;
  121. setTimeout(function () {
  122. oSelf.disappear();
  123. }, rate);
  124. } else {
  125. this.fileProgressWrapper.style.display = "none";
  126. }
  127. };