| 1 | |
| 2 | var upload_progress_failures = 0; |
| 3 | var TOTAL_UPLOAD_PROGRESS_FAILURES = 10; |
| 4 | |
| 5 | function getxy(){ |
| 6 | var x,y; |
| 7 | if (self.innerHeight) // all except Explorer |
| 8 | { |
| 9 | x = self.innerWidth; |
| 10 | y = self.innerHeight; |
| 11 | } |
| 12 | else if (document.documentElement && document.documentElement.clientHeight) |
| 13 | // Explorer 6 Strict Mode |
| 14 | { |
| 15 | x = document.documentElement.clientWidth; |
| 16 | y = document.documentElement.clientHeight; |
| 17 | } |
| 18 | else if (document.body) // other Explorers |
| 19 | { |
| 20 | x = document.body.clientWidth; |
| 21 | y = document.body.clientHeight; |
| 22 | } |
| 23 | return {'x':x,'y':y} |
| 24 | } |
| 25 | |
| 26 | |
| 27 | function upload_ajax_problem() { |
| 28 | /* If there's a problem, will cancel after |
| 29 | TOTAL_UPLOAD_PROGRESS_FAILURES tries. */ |
| 30 | |
| 31 | progress_wrap = document.getElementById('progress_wrap'); |
| 32 | upload_progress_failures++; |
| 33 | if (upload_progress_failures >= TOTAL_UPLOAD_PROGRESS_FAILURES){ |
| 34 | window.clearTimeout(interval); |
| 35 | } |
| 36 | progress_wrap.style.visibility = 'hidden'; |
| 37 | } |
| 38 | |
| 39 | var humanvalue = ['B','KB','MB','GB'] |
| 40 | function humanize(bytes) { |
| 41 | curbytes = bytes; |
| 42 | iterations = 0; |
| 43 | while (curbytes>1024) { |
| 44 | iterations++; |
| 45 | curbytes=curbytes/1024; |
| 46 | } |
| 47 | return curbytes.toFixed(1) + ' ' + humanvalue[iterations]; |
| 48 | } |
| 49 | |
| 50 | interval = null; |
| 51 | function fetch(uuid) { |
| 52 | /* no ajax here */ |
| 53 | if (!xmlhttp) { |
| 54 | upload_ajax_problem(); |
| 55 | return; |
| 56 | } |
| 57 | |
| 58 | req = xmlhttp; |
| 59 | req.open("GET", "/admin/upload_progress/", true); |
| 60 | req.onreadystatechange = function () { |
| 61 | if (req.readyState == 4) { |
| 62 | try { |
| 63 | request_status = req.status; |
| 64 | } catch (e) { |
| 65 | /* Really bad. */ |
| 66 | request_status = -1; |
| 67 | } |
| 68 | if (request_status == 200) { |
| 69 | var upload = new Function(" return "+req.responseText)(); |
| 70 | if (upload) { |
| 71 | progress_wrap = document.getElementById('progress_wrap'); |
| 72 | |
| 73 | if (!upload.state) { |
| 74 | progress_wrap.style.visibility = 'hidden'; |
| 75 | return |
| 76 | } else if (upload.state == 'done') { |
| 77 | window.clearTimeout(interval); |
| 78 | progress_wrap.style.visibility = 'hidden'; |
| 79 | return; |
| 80 | } else { |
| 81 | progress_bar = document.getElementById('progress_bar'); |
| 82 | bar_txt = document.getElementById('progress_text'); |
| 83 | |
| 84 | progress_wrap.style.visibility = 'visible'; |
| 85 | bar_txt.innerHTML = ((upload.received / upload.size) * 100).toFixed(1) |
| 86 | + '% - ' + humanize(upload.received) + ' of ' |
| 87 | + humanize(upload.size); |
| 88 | bar_width__px = 400 * upload.received / upload.size; |
| 89 | progress_bar.style.width = bar_width__px + 'px'; |
| 90 | } |
| 91 | } else { |
| 92 | upload_ajax_problem(); |
| 93 | } |
| 94 | } else { |
| 95 | upload_ajax_problem(); |
| 96 | } |
| 97 | } |
| 98 | }; |
| 99 | try { |
| 100 | req.setRequestHeader("X-Progress-Id", uuid); |
| 101 | } catch (e) { |
| 102 | /* couldn't set the header, the request is broken. */ |
| 103 | req.abort(); |
| 104 | } |
| 105 | req.send(null); |
| 106 | |
| 107 | } |
| 108 | |
| 109 | |
| 110 | function openprogress(e) { |
| 111 | upload_progress_failures = 0; |
| 112 | uuid = ""; |
| 113 | for (i = 0; i < 32; i++) { |
| 114 | uuid += Math.floor(Math.random() * 16).toString(16); |
| 115 | } |
| 116 | frm = e.target||e.srcElement; |
| 117 | |
| 118 | if (frm.action.indexOf('?') == -1) { |
| 119 | frm.action=frm.action+"?progress_id=" + uuid; |
| 120 | } else { |
| 121 | frm.action=frm.action+"&progress_id=" + uuid; |
| 122 | } |
| 123 | |
| 124 | if (document.getElementById('progress_wrap')) { |
| 125 | document.getElementById('progress_wrap').style.visibility = 'hidden'; |
| 126 | document.getElementById('progress_bar').style.width = '0'; |
| 127 | document.getElementById('progress_text').innerHTML = '0%'; |
| 128 | |
| 129 | interval = window.setInterval( |
| 130 | function () { |
| 131 | fetch(uuid); |
| 132 | }, |
| 133 | 1000 |
| 134 | ); |
| 135 | return; |
| 136 | } |
| 137 | |
| 138 | pos = getxy() |
| 139 | posx = parseInt((pos.x/2)-(420/2), 10) |
| 140 | posy = parseInt((pos.y/2)-(50/2), 10) |
| 141 | |
| 142 | progress_wrap = quickElement('div', document.body, '', 'style', |
| 143 | 'position: absolute; top: '+posy+'px; left: '+posx+'px; height: 50px; ' + |
| 144 | 'padding: 10px; width: 420px; background: #ffffff; ' + |
| 145 | 'border: solid 1px #dddddd;' + |
| 146 | 'visibility: hidden;', 'id', 'progress_wrap') |
| 147 | |
| 148 | progress_label = quickElement('h1', progress_wrap, 'Upload progress') |
| 149 | |
| 150 | progress = quickElement('div', progress_wrap, '', 'style', |
| 151 | 'top: 0; left: 0; width: 0px; ', 'id', 'progress_bar', 'class', 'submit-row') |
| 152 | |
| 153 | progress_text = quickElement('div', progress_wrap, '0%', 'style', |
| 154 | 'color: #000000; ', 'id', 'progress_text') |
| 155 | progress_wrap.style.visibility = 'hidden'; |
| 156 | interval = window.setInterval( |
| 157 | function () { |
| 158 | fetch(uuid); |
| 159 | }, |
| 160 | 1000 |
| 161 | ); |
| 162 | } |
| 163 | |
| 164 | addEvent(window, 'load', function() { |
| 165 | frms = document.getElementsByTagName('form'); |
| 166 | for (var i=0; i<frms.length; i++) { |
| 167 | if (frms[i].encoding.toLowerCase() == 'multipart/form-data') { |
| 168 | addEvent(frms[i], 'submit', openprogress); |
| 169 | return; |
| 170 | } |
| 171 | } |
| 172 | }); |