Index: trunk/kernel/admin_templates/incs/ajax.js =================================================================== diff -u -r5464 -r5494 --- trunk/kernel/admin_templates/incs/ajax.js (.../ajax.js) (revision 5464) +++ trunk/kernel/admin_templates/incs/ajax.js (.../ajax.js) (revision 5494) @@ -182,23 +182,38 @@ }; // AJAX ProgressBar classs -function AjaxProgressBar($prefix, $url) { +function AjaxProgressBar($prefix, $url, $format) { this.Prefix = $prefix; this.URL = $url; + this.DisplayFormat = $format; this.BusyRequest = false; + this.LastResponceTime = this.GetMicroTime(); + this.ProgressPercent = 0; // progress percent + this.ProgressTime = new Array(); this.Query(); } +AjaxProgressBar.prototype.GetMicroTime = function() { + var $now = new Date(); + return Math.round($now.getTime() / 1000); // because miliseconds are returned too +} + AjaxProgressBar.prototype.Query = function() { Request.makeRequest(this.URL, this.BusyRequest, '', this.successCallback, this.errorCallback, '', this); } +// return time needed for progress to finish +AjaxProgressBar.prototype.GetEstimatedTime = function() { + return Math.ceil((100 - this.ProgressPercent) * Math.sum(this.ProgressTime) / this.ProgressPercent); +} + AjaxProgressBar.prototype.successCallback = function($request, $params, $object) { var $responce = $request.responseText; - if ($responce.match(/^#redirect#(.*)/)) { + var $match_redirect = new RegExp('^#redirect#(.*)').exec($responce); + if ($match_redirect != null) { $object.showProgress(100); // redirect to external template requested - window.location.href = RegExp.$1; + window.location.href = $match_redirect[1]; return false; } @@ -210,13 +225,26 @@ alert('AJAX ERROR: ' + Request.getErrorHtml($request)); } +AjaxProgressBar.prototype.FormatTime = function ($seconds) { + $seconds = parseInt($seconds); + return Math.floor($seconds / 60) + ':' + $seconds % 60; +} + AjaxProgressBar.prototype.showProgress = function ($percent) { - $percent = parseInt($percent); + this.ProgressPercent = $percent; + var $now = this.GetMicroTime(); + this.ProgressTime[this.ProgressTime.length] = $now - this.LastResponceTime; + this.LastResponceTime = $now; + + var $display_progress = parseInt(this.ProgressPercent); var $display_div = document.getElementById('progress_display[' + this.Prefix + ']'); if ($display_div) { - $display_div.innerHTML = '(' + $percent + '%)'; + var $result = this.DisplayFormat; + $result = $result.replace('#PERCENT_DONE#', $display_progress); + $result = $result.replace('#TIME_LEFT#', this.FormatTime( this.GetEstimatedTime() )); + $result = $result.replace('#TIME_USED#', this.FormatTime( Math.sum(this.ProgressTime) )); + $display_div.innerHTML = $result; } - - document.getElementById('progress_done[' + this.Prefix + ']').style.width = $percent + '%'; - document.getElementById('progress_left[' + this.Prefix + ']').style.width = (100 - $percent) + '%'; + document.getElementById('progress_done[' + this.Prefix + ']').style.width = $display_progress + '%'; + document.getElementById('progress_left[' + this.Prefix + ']').style.width = (100 - $display_progress) + '%'; } \ No newline at end of file