diff options
author | MateBartus <mate.bartus@gmail.com> | 2015-06-03 16:35:23 +0200 |
---|---|---|
committer | Mate Bartus <mate.bartus@gmail.com> | 2015-07-08 01:28:00 +0200 |
commit | 63c3500dacc8d272e85273a67f56faa51a9d5fba (patch) | |
tree | e2ac0a2b3e5966da4654f8ed5215f3f3233d1624 /phpBB/assets/javascript | |
parent | b7ef709549597f2fa4f9c8525bfd602f81644530 (diff) | |
download | forums-63c3500dacc8d272e85273a67f56faa51a9d5fba.tar forums-63c3500dacc8d272e85273a67f56faa51a9d5fba.tar.gz forums-63c3500dacc8d272e85273a67f56faa51a9d5fba.tar.bz2 forums-63c3500dacc8d272e85273a67f56faa51a9d5fba.tar.xz forums-63c3500dacc8d272e85273a67f56faa51a9d5fba.zip |
[ticket/13740] Clean up JS code some more
PHPBB3-13740
Diffstat (limited to 'phpBB/assets/javascript')
-rw-r--r-- | phpBB/assets/javascript/installer.js | 299 |
1 files changed, 183 insertions, 116 deletions
diff --git a/phpBB/assets/javascript/installer.js b/phpBB/assets/javascript/installer.js index ea5ca638b8..8afac0da78 100644 --- a/phpBB/assets/javascript/installer.js +++ b/phpBB/assets/javascript/installer.js @@ -3,6 +3,9 @@ */ (function($) { // Avoid conflicts with other libraries + + 'use strict'; + // Installer variables var pollTimer = null; var nextReadPosition = 0; @@ -14,58 +17,37 @@ var $contentWrapper = $('.install-body').find('.main'); // Intercept form submits - intercept_form_submit($('#install_install')); - - function poll_content(xhReq) { - var messages = xhReq.responseText; - - do { - var unprocessed = messages.substring(nextReadPosition); - var messageEndIndex = unprocessed.indexOf('}\n\n'); - - if (messageEndIndex !== -1) { - var endOfMessageIndex = messageEndIndex + 3; // 3 is the length of "}\n\n" - var message = unprocessed.substring(0, endOfMessageIndex); - parse_message(message); - nextReadPosition += endOfMessageIndex; - } - } while (messageEndIndex !== -1); - - if (xhReq.readyState === 4) { - $('#loading_indicator').css('display', 'none'); - reset_polling(); - } - } - - function parse_message(messageJSON) { - $('#loading_indicator').css('display', 'none'); - - messageJSON = messageJSON.trim(); - var responseObject = JSON.parse(messageJSON); - - // Parse object - if (responseObject.hasOwnProperty('errors')) { - add_message('error', responseObject.errors) - } + interceptFormSubmit($('#install_install')); - if (responseObject.hasOwnProperty('warnings')) { - add_message('warning', responseObject.warnings) - } + /** + * Creates an XHR object + * + * jQuery cannot be used as the response is streamed, and + * as of now, jQuery does not provide access to the response until + * the connection is not closed. + * + * @return XMLHttpRequest|ActiveXObject + */ + function createXhrObject() { + var xhReq; - if (responseObject.hasOwnProperty('logs')) { - add_message('log', responseObject.logs); + if (window.XMLHttpRequest) { + xhReq = new XMLHttpRequest(); } - - if (responseObject.hasOwnProperty('form')) { - add_form(responseObject.form); + else if (window.ActiveXObject) { + xhReq = new ActiveXObject("Msxml2.XMLHTTP"); } - if (responseObject.hasOwnProperty('progress')) { - set_progress(responseObject.progress); - } + return xhReq; } - function add_message(type, messages) { + /** + * Displays error, warning and log messages + * + * @param type + * @param messages + */ + function addMessage(type, messages) { // Get message containers var $errorContainer = $('#error-container'); var $warningContainer = $('#warning-container'); @@ -101,14 +83,24 @@ } } - function add_form(formHtml) { + /** + * Displays a form from the response + * + * @param formHtml + */ + function addForm(formHtml) { var $formContainer = $('#content-container'); $formContainer.html(formHtml); var $form = $('#install_install'); - intercept_form_submit($form); + interceptFormSubmit($form); } - function set_progress(progressObject) { + /** + * Renders progress bar + * + * @param progressObject + */ + function setProgress(progressObject) { var $statusText, $progressBar, $progressText, $progressFiller; if (progressObject.task_name.length) { @@ -140,101 +132,128 @@ // Update progress bar $statusText.text(progressObject.task_name + '…'); - increment_progress_bar(Math.round(progressObject.task_num / progressObject.task_count * 100)); + incrementProgressBar(Math.round(progressObject.task_num / progressObject.task_count * 100)); } } - function increment_progress_bar(progressLimit) { - var $progressFiller = $('#progress-bar-filler'); - var $progressText = $('#progress-bar-text'); - var progressStart = $progressFiller.width() / $progressFiller.offsetParent().width() * 100; - currentProgress = Math.floor(progressStart); + /** + * Parse messages from the response object + * + * @param messageJSON + */ + function parseMessage(messageJSON) { + $('#loading_indicator').css('display', 'none'); - clearInterval(progressTimer); - progressTimer = setInterval(function() { - incrementFiller($progressText, $progressFiller, progressLimit); - }, 10); - } + var responseObject = JSON.parse(messageJSON); - function incrementFiller($progressText, $progressFiller, progressLimit) { - currentProgress++; - $progressText.text(currentProgress + '%'); - $progressFiller.css('width', currentProgress + '%'); + // Parse object + if (responseObject.hasOwnProperty('errors')) { + addMessage('error', responseObject.errors); + } - if (currentProgress >= progressLimit || currentProgress >= 100) { - console.log("In if; " + progressLimit + "; " + currentProgress); - clearInterval(progressTimer); + if (responseObject.hasOwnProperty('warnings')) { + addMessage('warning', responseObject.warnings); } - } - function start_polling(xhReq) { - reset_polling(); - pollTimer = setInterval(function () { - poll_content(xhReq); - }, 500); - } + if (responseObject.hasOwnProperty('logs')) { + addMessage('log', responseObject.logs); + } - function reset_polling() { - clearInterval(pollTimer); - nextReadPosition = 0; + if (responseObject.hasOwnProperty('form')) { + addForm(responseObject.form); + } + + if (responseObject.hasOwnProperty('progress')) { + setProgress(responseObject.progress); + } } - function submit_form($form, $submitBtn) { - $form.css('display', 'none'); + /** + * Process updates in streamed response + * + * @param xhReq XHR object + */ + function pollContent(xhReq) { + var messages = xhReq.responseText; + var msgSeparator = '}\n\n'; + var unprocessed, messageEndIndex, endOfMessageIndex, message; - var xhReq = create_xhr_object(); - xhReq.open('POST', $form.attr('action'), true); - xhReq.setRequestHeader('X-Requested-With', 'XMLHttpRequest'); - xhReq.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); - xhReq.send(get_form_fields($form, $submitBtn)); + do { + unprocessed = messages.substring(nextReadPosition); + messageEndIndex = unprocessed.indexOf(msgSeparator); - // Clear content - setup_ajax_layout(); - $('#loading_indicator').css('display', 'block'); + if (messageEndIndex !== -1) { + endOfMessageIndex = messageEndIndex + msgSeparator.length; + message = unprocessed.substring(0, endOfMessageIndex); + parseMessage(message); + nextReadPosition += endOfMessageIndex; + } + } while (messageEndIndex !== -1); - start_polling(xhReq); + if (xhReq.readyState === 4) { + $('#loading_indicator').css('display', 'none'); + resetPolling(); + } } - // Workaround for submit buttons - function get_form_fields($form, $submitBtn) { - var formData = $form.serialize(); - formData += ((formData.length) ? '&' : '') + encodeURIComponent($submitBtn.attr('name')) + '='; - formData += encodeURIComponent($submitBtn.attr('value')); - - return formData; - } + /** + * Animates the progress bar + * + * @param $progressText + * @param $progressFiller + * @param progressLimit + */ + function incrementFiller($progressText, $progressFiller, progressLimit) { + currentProgress++; + $progressText.text(currentProgress + '%'); + $progressFiller.css('width', currentProgress + '%'); - function intercept_form_submit($form) { - if (!$form.length) { - return; + if (currentProgress >= progressLimit || currentProgress >= 100) { + clearInterval(progressTimer); } + } - $form.find(':submit').bind('click', function (event) { - event.preventDefault(); - submit_form($form, $(this)); - }); + /** + * Wrapper function for progress bar rendering and animating + * + * @param progressLimit + */ + function incrementProgressBar(progressLimit) { + var $progressFiller = $('#progress-bar-filler'); + var $progressText = $('#progress-bar-text'); + var progressStart = $progressFiller.width() / $progressFiller.offsetParent().width() * 100; + currentProgress = Math.floor(progressStart); + clearInterval(progressTimer); + progressTimer = setInterval(function() { + incrementFiller($progressText, $progressFiller, progressLimit); + }, 10); } /** - * jQuery cannot be used as the response is streamed, and - * as of now, jQuery does not provide access to the response until - * the connection is not closed. + * Sets up timer for processing the streamed HTTP response + * + * @param xhReq */ - function create_xhr_object() { - var xhReq; - - if (window.XMLHttpRequest) { - xhReq = new XMLHttpRequest(); - } - else if (window.ActiveXObject) { - xhReq = new ActiveXObject("Msxml2.XMLHTTP"); - } + function startPolling(xhReq) { + resetPolling(); + pollTimer = setInterval(function () { + pollContent(xhReq); + }, 500); + } - return xhReq; + /** + * Resets the polling timer + */ + function resetPolling() { + clearInterval(pollTimer); + nextReadPosition = 0; } - function setup_ajax_layout() { + /** + * Renders the AJAX UI layout + */ + function setupAjaxLayout() { // Clear content $contentWrapper.html(''); @@ -267,4 +286,52 @@ $spinner.html(' '); $contentWrapper.append($spinner); } + + function submitForm($form, $submitBtn) { + $form.css('display', 'none'); + + var xhReq = createXhrObject(); + xhReq.open('POST', $form.attr('action'), true); + xhReq.setRequestHeader('X-Requested-With', 'XMLHttpRequest'); + xhReq.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); + xhReq.send(getFormFields($form, $submitBtn)); + + // Clear content + setupAjaxLayout(); + $('#loading_indicator').css('display', 'block'); + + startPolling(xhReq); + } + + /** + * Add submit button to the POST information + * + * @param $form + * @param $submitBtn + * + * @returns {*} + */ + function getFormFields($form, $submitBtn) { + var formData = $form.serialize(); + formData += ((formData.length) ? '&' : '') + encodeURIComponent($submitBtn.attr('name')) + '='; + formData += encodeURIComponent($submitBtn.attr('value')); + + return formData; + } + + /** + * Intercept form submit events and determine the submit button used + * + * @param $form + */ + function interceptFormSubmit($form) { + if (!$form.length) { + return; + } + + $form.find(':submit').bind('click', function (event) { + event.preventDefault(); + submitForm($form, $(this)); + }); + } })(jQuery); // Avoid conflicts with other libraries |