function $(id) { return document.getElementById(id); } (function() { if (getCookie('file1').length == 0) { window.location.href = '/'; } })(); function countdown() { var countDown = 5; setInterval(function() { if (countDown == 1) { window.location.href = '/'; } if (countDown > 0) { countDown--; } document.getElementById('count-number').innerHTML = countDown; return countDown; }, 1000); } function getCookie(name) { 'use strict'; var strCookie = document.cookie, arrCookie = strCookie.split('; '); for (var i = 0; i < arrCookie.length; i++) { var arr = arrCookie[i].split('='); if (arr[0] == name) return unescape(arr[1]); } return ''; } function setCookie(name, value, expirehours) { 'use strict'; var cookieString = name + '=' + escape(value); if (expirehours > 0) { var date = new Date(); date.setTime(date.getTime() + expirehours * 3600 * 1000); cookieString = cookieString + '; expires=' + date.toGMTString(); } document.cookie = cookieString; } function getCGI(url) { var question; var canceled = 'Возврат отменен.'; if (url == 'goback.cgi') { question = confirm('Вы уверены что хотите вернуться в основной интерфейс?'); } else { question = true; } if (question) { xmlhttp = new XMLHttpRequest(); xmlhttp.open('GET', url, true); xmlhttp.send(); return true; } else { alert(canceled); } } // http://192.168.14.26:8095/upload // /upload.cgi function log(html) { document.getElementById('log1').innerHTML = html; } function log2(html) { document.getElementById('log2').innerHTML = html; } function upload(file1, file2) { var fl = new FormData(); fl.append('file1', file1); var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP'); xhr.open('POST', '/upload.cgi', true); // обработчик для закачки xhr.upload.onprogress = function(event) { var progress = Math.floor((event.loaded / event.total) * 100); if (progress == 100) { log(progress + '%'); updateProgressBar(progress); log('Идет проверка...'); } else { log(progress + '%'); updateProgressBar(progress); } }; xhr.onload = xhr.onerror = function() { if (this.status == 200) { if (xhr.responseText == '1') { log('Успешно'); upload2(file2); } else { log('Ошибка установки'); } } else { log('Ошибка соединения с контроллером'); } }; xhr.send(fl); } function upload2(file) { var fl = new FormData(); fl.append('file2', file); var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP'); xhr.open('POST', '/upload.cgi', true); xhr.upload.onprogress = function(event) { var progress = Math.floor((event.loaded / event.total) * 100); if (progress == 100) { log2(progress + '%'); updateProgressBar(getCookie('file1') === 'NONE' ? progress : 100 + progress); log2('Идет проверка...'); } else { log2(progress + '%'); updateProgressBar(getCookie('file1') === 'NONE' ? progress : 100 + progress); } }; xhr.onload = xhr.onerror = function() { if (this.status == 200) { if (xhr.responseText == '1') { log2('Успешно'); setTimeout(runCount, 2000); } else { log2('Ошибка установки'); } } else { log2('Ошибка соединения с контроллером'); } }; xhr.send(fl); } function runCount() { $('count-wrap').style.display = 'block'; $('countdown').style.display = 'block'; countdown(); } var FileApiSupported = !!(window.File && window.FileReader && window.FileList && window.Blob); document.addEventListener('DOMContentLoaded', function(event) { if (getCookie('file1') === 'NONE') { $('uf1').style.display = 'none'; } $('uf1lbl').innerHTML = getCookie('file1'); $('uf2lbl').innerHTML = getCookie('file2'); if (FileApiSupported) { document.forms.upload.onsubmit = function() { var file1 = this.elements.datafile.files[0]; var file2 = this.elements.datafile2.files[0]; // var f1nmreg = /RTPS_05605(..)\.bin/g; // var f2nmreg = /RTPS_05601(..)\.bin/g; log(''); log2(''); resetProgressBar(); if (getCookie('file1') !== 'NONE') { if (file1 && file1.name === getCookie('file1')) { if (file2 && file2.name === getCookie('file2')) { upload(file1, file2); } else { log2('Не указан файл ПО!'); } } else { log('Не указан файл ПО!'); } } else { if (file2 && file2.name === getCookie('file2')) { upload2(file2); } else { log2('Не указан файл ПО!'); } } return false; }; } else { // IE 9 upload var myFileInput = $('uploadBtn'); var myFileInput2 = $('uploadBtn2'); var handleUpload = function(response) { if ('' + response === '1') { log2('Успешно'); setTimeout(runCount, 2000); } else { log2('Ошибка установки'); } }; var handleUploadTwoFW = function(response) { if ('' + response === '1') { log('Успешно'); updateProgressBar(200); log2('Идет проверка...'); stoneAgeUpload([myFileInput2], '/upload.cgi', null, handleUpload); } else { log('Ошибка установки'); } }; document.forms.upload.onsubmit = function() { var file1 = $('uploadFile'); var file2 = $('uploadFile2'); if (getCookie('file1') !== 'NONE') { if (file1 && file1.value === getCookie('file1')) { if (file2 && file2.value === getCookie('file2')) { stoneAgeUpload([myFileInput], '/upload.cgi', null, handleUploadTwoFW); updateProgressBar(100); log('Идет проверка...'); } else { log2('Не указан файл ПО!'); } } else { log('Не указан файл ПО!'); } } else { if (file2 && file2.value === getCookie('file2')) { stoneAgeUpload([myFileInput2], '/upload.cgi', null, handleUpload); updateProgressBar(getCookie('file1') === 'NONE' ? 100 : 200); log2('Идет проверка...'); } else { log2('Не указан файл ПО!'); } } return false; }; } $('uploadBtn').onchange = function() { $('uploadFile').value = this.files ? this.files[0].name : this.value.match(/[^\/\\]*$/)[0]; }; $('uploadBtn2').onchange = function() { $('uploadFile2').value = this.files ? this.files[0].name : this.value.match(/[^\/\\]*$/)[0]; }; $('goback').onclick = function() { if (getCGI('goback.cgi') === true) { runCount(); } }; //################ Progress Bar handlers ############## var progressbar = $('progressbar'); var progress = $('progress'); window.parent = progressbar.parentNode; var nbSlide = getCookie('file1') === 'NONE' ? 1 : 2; // var nbSlide = 1; window.step = parent.offsetWidth / nbSlide; // Draw separatiion line var steps = document.getElementsByClassName('step'); for (i = 0; i < nbSlide - 1; i++) { var node = document.createElement('div'); node.className = 'step'; progressbar.appendChild(node); } for (index = 0; index < steps.length; index++) { steps[index].style.width = (window.step - 1) * 100 / window.parent.offsetWidth + '%'; } }); function updateProgressBar(percent) { progress.style.width = (window.step * percent) / window.parent.offsetWidth + '%'; } function resetProgressBar() { progress.style.width = 0; } //polyfill window.offsetWidth and window.offsetHeight var windowOffset = function() { var getOffset = function() { if (typeof(_cacheResize) === 'function') { _cacheResize; } window.offsetWidth = (function() { if (window.innerWidth != undefined) { return window.innerWidth; } else { var _b = document.body, _d = document.documentElement; return Math.max(_d.clientWidth, _b.clientWidth); } })(); window.parent.offsetWidth = (function() { if (window.parent.innerWidth != undefined) { return window.parent.innerWidth; } else { var _b = document.body, _d = document.documentElement; return Math.max(_d.clientWidth, _b.clientWidth); } })(); window.offsetHeight = (function() { if (window.innerHeight != undefined) { return window.innerHeight; } else { var _b = document.body, _d = document.documentElement; return Math.max(_d.clientHeight, _b.clientHeight); } })(); }; var _cacheResize = window.onresize; window.onresize = function() { _cacheResize(); getOffset(); }; getOffset(); }; //invoke function windowOffset();