Guest User

Untitled

a guest
Feb 19th, 2018
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.16 KB | None | 0 0
  1. ## view
  2. <style type="text/css">
  3. #progress-bar {
  4. width:500px;
  5. height:25px;
  6. margin:15px;
  7. border:solid 1px #000;
  8. position:relative;
  9. }
  10.  
  11. #progress-bar #status-bar {
  12. display:block;
  13. height:25px;
  14. width:0;
  15. background-color:#00f;
  16. border-right:solid 1px #000;
  17. position:absolute;
  18. top:0; left:0;
  19. }
  20.  
  21. #progress-bar #status-text {
  22. display:block;
  23. padding: 0 15px;
  24. line-height:25px;
  25. position:absolute;
  26. top:0; left:0;
  27. }
  28. </style>
  29.  
  30. <p><%= link_to (@upid = Time.now.to_i.to_s), :action => 'status', :upload_id => @upid %></p>
  31. <%= start_form_tag({:action => 'upload', :upload_id => @upid}, {:multipart => true, :target => 'upload',
  32. :onsubmit => "UploadProgress.monitor('#{escape_javascript @upid}')"}) %>
  33. <div id="file-fields">
  34. <p><%= file_field_tag :data %></p>
  35. </div>
  36. <p><%= link_to_function 'Add File Field', 'UploadProgress.FileField.add()' %>
  37. </p>
  38. <p><%= submit_tag :Upload %></p>
  39. </form>
  40.  
  41. <div id="results"></div>
  42. <div id="progress-bar"></div>
  43.  
  44. <iframe id="upload" name="upload" src="about:blank"></iframe>
  45.  
  46. ## upload progress js (probably not needed)
  47. var UploadProgress = {
  48. uploading: null,
  49. monitor: function(upid) {
  50. if(!this.periodicExecuter) {
  51. this.periodicExecuter = new PeriodicalExecuter(function() {
  52. if(!UploadProgress.uploading) return;
  53. new Ajax.Request('/files/progress?upload_id=' + upid);
  54. }, 3);
  55. }
  56.  
  57. this.uploading = true;
  58. this.StatusBar.create();
  59. },
  60.  
  61. update: function(total, current) {
  62. if(!this.uploading) return;
  63. var status = current / total;
  64. var statusHTML = status.toPercentage();
  65. $('results').innerHTML = statusHTML + "<br /><small>" + current.toHumanSize() + ' of ' + total.toHumanSize() + " uploaded.</small>";
  66. this.StatusBar.update(status, statusHTML);
  67. },
  68.  
  69. finish: function() {
  70. this.uploading = false;
  71. this.StatusBar.finish();
  72. $('results').innerHTML = 'finished!';
  73. },
  74.  
  75. cancel: function(msg) {
  76. if(!this.uploading) return;
  77. this.uploading = false;
  78. if(this.StatusBar.statusText) this.StatusBar.statusText.innerHTML = msg || 'canceled';
  79. },
  80.  
  81. StatusBar: {
  82. statusBar: null,
  83. statusText: null,
  84. statusBarWidth: 500,
  85.  
  86. create: function() {
  87. this.statusBar = this._createStatus('status-bar');
  88. this.statusText = this._createStatus('status-text');
  89. this.statusText.innerHTML = '0%';
  90. this.statusBar.style.width = '0';
  91. },
  92.  
  93. update: function(status, statusHTML) {
  94. this.statusText.innerHTML = statusHTML;
  95. this.statusBar.style.width = Math.floor(this.statusBarWidth * status);
  96. },
  97.  
  98. finish: function() {
  99. this.statusText.innerHTML = '100%';
  100. this.statusBar.style.width = '100%';
  101. },
  102.  
  103. _createStatus: function(id) {
  104. el = $(id);
  105. if(!el) {
  106. el = document.createElement('span');
  107. el.setAttribute('id', id);
  108. $('progress-bar').appendChild(el);
  109. }
  110. return el;
  111. }
  112. },
  113.  
  114. FileField: {
  115. add: function() {
  116. new Insertion.Bottom('file-fields', '<p style="display:none"><input id="data" name="data" type="file" /> <a href="#" onclick="UploadProgress.FileField.remove(this);return false;">x</a></p>')
  117. $$('#file-fields p').last().visualEffect('blind_down', {duration:0.3});
  118. },
  119.  
  120. remove: function(anchor) {
  121. anchor.parentNode.visualEffect('drop_out', {duration:0.25});
  122. }
  123. }
  124. }
  125.  
  126. Number.prototype.bytes = function() { return this; };
  127. Number.prototype.kilobytes = function() { return this * 1024; };
  128. Number.prototype.megabytes = function() { return this * (1024).kilobytes(); };
  129. Number.prototype.gigabytes = function() { return this * (1024).megabytes(); };
  130. Number.prototype.terabytes = function() { return this * (1024).gigabytes(); };
  131. Number.prototype.petabytes = function() { return this * (1024).terabytes(); };
  132. Number.prototype.exabytes = function() { return this * (1024).petabytes(); };
  133. ['byte', 'kilobyte', 'megabyte', 'gigabyte', 'terabyte', 'petabyte', 'exabyte'].each(function(meth) {
  134. Number.prototype[meth] = Number.prototype[meth+'s'];
  135. });
  136.  
  137. Number.prototype.toPrecision = function() {
  138. var precision = arguments[0] || 2;
  139. var s = Math.round(this * Math.pow(10, precision)).toString();
  140. var pos = s.length - precision;
  141. var last = s.substr(pos, precision);
  142. return s.substr(0, pos) + (last.match("^0{" + precision + "}$") ? '' : '.' + last);
  143. }
  144.  
  145. // (1/10).toPercentage()
  146. // # => '10%'
  147. Number.prototype.toPercentage = function() {
  148. return (this * 100).toPrecision() + '%';
  149. }
  150.  
  151. Number.prototype.toHumanSize = function() {
  152. if(this < (1).kilobyte()) return this + " Bytes";
  153. if(this < (1).megabyte()) return (this / (1).kilobyte()).toPrecision() + ' KB';
  154. if(this < (1).gigabytes()) return (this / (1).megabyte()).toPrecision() + ' MB';
  155. if(this < (1).terabytes()) return (this / (1).gigabytes()).toPrecision() + ' GB';
  156. if(this < (1).petabytes()) return (this / (1).terabytes()).toPrecision() + ' TB';
  157. if(this < (1).exabytes()) return (this / (1).petabytes()).toPrecision() + ' PB';
  158. return (this / (1).exabytes()).toPrecision() + ' EB';
  159. }
Add Comment
Please, Sign In to add comment