2 * completely stolen off mongrel_upload_progress and by somebody
3 * with little JavaScript interest/knowledge
7 monitor: function(upid) {
8 if(!this.periodicExecuter) {
9 this.periodicExecuter = new PeriodicalExecuter(function() {
10 if(!UploadProgress.uploading) return;
11 new Ajax.Request('/files/progress?upload_id=' + upid);
15 this.uploading = true;
16 this.StatusBar.create();
19 update: function(total, current) {
20 if(!this.uploading) return;
23 var status = current / total;
24 var statusHTML = status.toPercentage();
25 $('results').innerHTML = statusHTML + "<br /><small>" + current.toHumanSize() + ' of ' + total.toHumanSize() + " uploaded.</small>";
26 this.StatusBar.update(status, statusHTML);
30 this.uploading = false;
31 this.StatusBar.finish();
32 $('results').innerHTML = 'finished!';
35 cancel: function(msg) {
36 if(!this.uploading) return;
37 this.uploading = false;
38 if(this.StatusBar.statusText) this.StatusBar.statusText.innerHTML = msg || 'canceled';
47 this.statusBar = this._createStatus('status-bar');
48 this.statusText = this._createStatus('status-text');
49 this.statusText.innerHTML = '0%';
50 this.statusBar.style.width = '0';
53 update: function(status, statusHTML) {
54 this.statusText.innerHTML = statusHTML;
55 this.statusBar.style.width = Math.floor(this.statusBarWidth * status);
59 this.statusText.innerHTML = '100%';
60 this.statusBar.style.width = '100%';
63 _createStatus: function(id) {
66 el = document.createElement('span');
67 el.setAttribute('id', id);
68 $('progress-bar').appendChild(el);
76 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>')
77 $$('#file-fields p').last().visualEffect('blind_down', {duration:0.3});
80 remove: function(anchor) {
81 anchor.parentNode.visualEffect('drop_out', {duration:0.25});
86 Number.prototype.bytes = function() { return this; };
87 Number.prototype.kilobytes = function() { return this * 1024; };
88 Number.prototype.megabytes = function() { return this * (1024).kilobytes(); };
89 Number.prototype.gigabytes = function() { return this * (1024).megabytes(); };
90 Number.prototype.terabytes = function() { return this * (1024).gigabytes(); };
91 Number.prototype.petabytes = function() { return this * (1024).terabytes(); };
92 Number.prototype.exabytes = function() { return this * (1024).petabytes(); };
93 ['byte', 'kilobyte', 'megabyte', 'gigabyte', 'terabyte', 'petabyte', 'exabyte'].each(function(meth) {
94 Number.prototype[meth] = Number.prototype[meth+'s'];
97 Number.prototype.toPrecision = function() {
98 var precision = arguments[0] || 2;
99 var s = Math.round(this * Math.pow(10, precision)).toString();
100 var pos = s.length - precision;
101 var last = s.substr(pos, precision);
102 return s.substr(0, pos) + (last.match("^0{" + precision + "}$") ? '' : '.' + last);
105 // (1/10).toPercentage()
107 Number.prototype.toPercentage = function() {
108 return (this * 100).toPrecision() + '%';
111 Number.prototype.toHumanSize = function() {
112 if(this < (1).kilobyte()) return this + " Bytes";
113 if(this < (1).megabyte()) return (this / (1).kilobyte()).toPrecision() + ' KB';
114 if(this < (1).gigabytes()) return (this / (1).megabyte()).toPrecision() + ' MB';
115 if(this < (1).terabytes()) return (this / (1).gigabytes()).toPrecision() + ' GB';
116 if(this < (1).petabytes()) return (this / (1).terabytes()).toPrecision() + ' TB';
117 if(this < (1).exabytes()) return (this / (1).petabytes()).toPrecision() + ' PB';
118 return (this / (1).exabytes()).toPrecision() + ' EB';