| | 1 | function getxy(){ |
| | 2 | var x,y; |
| | 3 | if (self.innerHeight) // all except Explorer |
| | 4 | { |
| | 5 | x = self.innerWidth; |
| | 6 | y = self.innerHeight; |
| | 7 | } |
| | 8 | else if (document.documentElement && document.documentElement.clientHeight) |
| | 9 | // Explorer 6 Strict Mode |
| | 10 | { |
| | 11 | x = document.documentElement.clientWidth; |
| | 12 | y = document.documentElement.clientHeight; |
| | 13 | } |
| | 14 | else if (document.body) // other Explorers |
| | 15 | { |
| | 16 | x = document.body.clientWidth; |
| | 17 | y = document.body.clientHeight; |
| | 18 | } |
| | 19 | return {'x':x,'y':y} |
| | 20 | } |
| | 21 | |
| | 22 | var humanvalue = ['B','KB','MB','GB'] |
| | 23 | function humanize(bytes) { |
| | 24 | curbytes = bytes |
| | 25 | iterations = 0 |
| | 26 | while (curbytes>1024) { |
| | 27 | iterations++ |
| | 28 | curbytes=curbytes/1024 |
| | 29 | } |
| | 30 | return curbytes.toFixed(1) + ' ' + humanvalue[iterations] |
| | 31 | } |
| | 32 | |
| | 33 | interval = null; |
| | 34 | function fetch(uuid) { |
| | 35 | req = xmlhttp |
| | 36 | req.open("GET", "/upload_progress/", 1); |
| | 37 | req.setRequestHeader("X-Progress-Id", uuid); |
| | 38 | req.onreadystatechange = function () { |
| | 39 | if (req.readyState == 4) { |
| | 40 | if (req.status == 200) { |
| | 41 | |
| | 42 | var upload = eval( '(' + req.responseText + ')' ); |
| | 43 | |
| | 44 | if (upload.state == 'done' || upload.state == 'uploading') { |
| | 45 | bar = document.getElementById('progress_bar'); |
| | 46 | bar_txt = document.getElementById('progress_text') |
| | 47 | bar_txt.innerHTML = ((upload.received / upload.size) * 100).toFixed(1) + '% - ' + |
| | 48 | humanize(upload.received) + ' of ' + humanize(upload.size) |
| | 49 | w = 400 * upload.received / upload.size; |
| | 50 | bar.style.width = w + 'px'; |
| | 51 | |
| | 52 | } |
| | 53 | if (upload.state == 'done') { |
| | 54 | window.clearTimeout(interval); |
| | 55 | } |
| | 56 | } |
| | 57 | } |
| | 58 | } |
| | 59 | req.send(null); |
| | 60 | |
| | 61 | } |
| | 62 | |
| | 63 | function openprogress(e) { |
| | 64 | |
| | 65 | uuid = ""; |
| | 66 | for (i = 0; i < 32; i++) { |
| | 67 | uuid += Math.floor(Math.random() * 16).toString(16); |
| | 68 | } |
| | 69 | frm = e.target||e.srcElement |
| | 70 | |
| | 71 | frm.action=frm.action+"?" + uuid; |
| | 72 | |
| | 73 | pos = getxy() |
| | 74 | posx = parseInt((pos.x/2)-(420/2), 10) |
| | 75 | posy = parseInt((pos.y/2)-(50/2), 10) |
| | 76 | |
| | 77 | progress_wrap = quickElement('div', document.body, '', 'style', |
| | 78 | 'position: absolute; top: '+posy+'px; left: '+posx+'px; height: 50px; ' + |
| | 79 | 'padding: 10px; width: 420px; background: #ffffff; ' + |
| | 80 | 'border: solid 1px #dddddd;', 'id', 'progress_wrap') |
| | 81 | |
| | 82 | progress_label = quickElement('h1', progress_wrap, 'Upload progress') |
| | 83 | |
| | 84 | progress = quickElement('div', progress_wrap, '', 'style', |
| | 85 | 'top: 0; left: 0; width: 0px; ', 'id', 'progress_bar', 'class', 'submit-row') |
| | 86 | |
| | 87 | progress_text = quickElement('div', progress_wrap, '0%', 'style', |
| | 88 | 'color: #000000; ', 'id', 'progress_text') |
| | 89 | |
| | 90 | interval = window.setInterval( |
| | 91 | function () { |
| | 92 | fetch(uuid); |
| | 93 | }, |
| | 94 | 1000 |
| | 95 | ); |
| | 96 | } |
| | 97 | |
| | 98 | addEvent(window, 'load', function() { |
| | 99 | frm = document.getElementsByTagName('form')[0] |
| | 100 | addEvent(frm, 'submit', openprogress) |
| | 101 | } |
| | 102 | ) |