X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=web%2Fcore%2Fmisc%2Fprogress.es6.js;fp=web%2Fcore%2Fmisc%2Fprogress.es6.js;h=182c9bfc35d25db2f8c12b341f9419f362d570d9;hp=bec63d535acb4569150a8ecd51149aa9cb2f09bf;hb=0bf8d09d2542548982e81a441b1f16e75873a04f;hpb=74df008bdbb3a11eeea356744f39b802369bda3c diff --git a/web/core/misc/progress.es6.js b/web/core/misc/progress.es6.js index bec63d535..182c9bfc3 100644 --- a/web/core/misc/progress.es6.js +++ b/web/core/misc/progress.es6.js @@ -3,7 +3,7 @@ * Progress bar. */ -(function ($, Drupal) { +(function($, Drupal) { /** * Theme function for the progress bar. * @@ -13,13 +13,15 @@ * @return {string} * The HTML for the progress bar. */ - Drupal.theme.progressBar = function (id) { - return `
` + + Drupal.theme.progressBar = function(id) { + return ( + `
` + '
 
' + '
' + '
' + '
 
' + - '
'; + '
' + ); }; /** @@ -44,7 +46,7 @@ * @param {function} errorCallback * Callback to call on error. */ - Drupal.ProgressBar = function (id, updateCallback, method, errorCallback) { + Drupal.ProgressBar = function(id, updateCallback, method, errorCallback) { this.id = id; this.method = method || 'GET'; this.updateCallback = updateCallback; @@ -57,111 +59,124 @@ this.element = $(Drupal.theme('progressBar', id)); }; - $.extend(Drupal.ProgressBar.prototype, /** @lends Drupal.ProgressBar# */{ - - /** - * Set the percentage and status message for the progressbar. - * - * @param {number} percentage - * The progress percentage. - * @param {string} message - * The message to show the user. - * @param {string} label - * The text for the progressbar label. - */ - setProgress(percentage, message, label) { - if (percentage >= 0 && percentage <= 100) { - $(this.element).find('div.progress__bar').css('width', `${percentage}%`); - $(this.element).find('div.progress__percentage').html(`${percentage}%`); - } - $('div.progress__description', this.element).html(message); - $('div.progress__label', this.element).html(label); - if (this.updateCallback) { - this.updateCallback(percentage, message, this); - } - }, - - /** - * Start monitoring progress via Ajax. - * - * @param {string} uri - * The URI to use for monitoring. - * @param {number} delay - * The delay for calling the monitoring URI. - */ - startMonitoring(uri, delay) { - this.delay = delay; - this.uri = uri; - this.sendPing(); - }, + $.extend( + Drupal.ProgressBar.prototype, + /** @lends Drupal.ProgressBar# */ { + /** + * Set the percentage and status message for the progressbar. + * + * @param {number} percentage + * The progress percentage. + * @param {string} message + * The message to show the user. + * @param {string} label + * The text for the progressbar label. + */ + setProgress(percentage, message, label) { + if (percentage >= 0 && percentage <= 100) { + $(this.element) + .find('div.progress__bar') + .css('width', `${percentage}%`); + $(this.element) + .find('div.progress__percentage') + .html(`${percentage}%`); + } + $('div.progress__description', this.element).html(message); + $('div.progress__label', this.element).html(label); + if (this.updateCallback) { + this.updateCallback(percentage, message, this); + } + }, - /** - * Stop monitoring progress via Ajax. - */ - stopMonitoring() { - clearTimeout(this.timer); - // This allows monitoring to be stopped from within the callback. - this.uri = null; - }, + /** + * Start monitoring progress via Ajax. + * + * @param {string} uri + * The URI to use for monitoring. + * @param {number} delay + * The delay for calling the monitoring URI. + */ + startMonitoring(uri, delay) { + this.delay = delay; + this.uri = uri; + this.sendPing(); + }, - /** - * Request progress data from server. - */ - sendPing() { - if (this.timer) { + /** + * Stop monitoring progress via Ajax. + */ + stopMonitoring() { clearTimeout(this.timer); - } - if (this.uri) { - const pb = this; - // When doing a post request, you need non-null data. Otherwise a - // HTTP 411 or HTTP 406 (with Apache mod_security) error may result. - let uri = this.uri; - if (uri.indexOf('?') === -1) { - uri += '?'; + // This allows monitoring to be stopped from within the callback. + this.uri = null; + }, + + /** + * Request progress data from server. + */ + sendPing() { + if (this.timer) { + clearTimeout(this.timer); } - else { - uri += '&'; + if (this.uri) { + const pb = this; + // When doing a post request, you need non-null data. Otherwise a + // HTTP 411 or HTTP 406 (with Apache mod_security) error may result. + let uri = this.uri; + if (uri.indexOf('?') === -1) { + uri += '?'; + } else { + uri += '&'; + } + uri += '_format=json'; + $.ajax({ + type: this.method, + url: uri, + data: '', + dataType: 'json', + success(progress) { + // Display errors. + if (progress.status === 0) { + pb.displayError(progress.data); + return; + } + // Update display. + pb.setProgress( + progress.percentage, + progress.message, + progress.label, + ); + // Schedule next timer. + pb.timer = setTimeout(() => { + pb.sendPing(); + }, pb.delay); + }, + error(xmlhttp) { + const e = new Drupal.AjaxError(xmlhttp, pb.uri); + pb.displayError(`
${e.message}
`); + }, + }); } - uri += '_format=json'; - $.ajax({ - type: this.method, - url: uri, - data: '', - dataType: 'json', - success(progress) { - // Display errors. - if (progress.status === 0) { - pb.displayError(progress.data); - return; - } - // Update display. - pb.setProgress(progress.percentage, progress.message, progress.label); - // Schedule next timer. - pb.timer = setTimeout(() => { - pb.sendPing(); - }, pb.delay); - }, - error(xmlhttp) { - const e = new Drupal.AjaxError(xmlhttp, pb.uri); - pb.displayError(`
${e.message}
`); - }, - }); - } - }, + }, - /** - * Display errors on the page. - * - * @param {string} string - * The error message to show the user. - */ - displayError(string) { - const error = $('
').html(string); - $(this.element).before(error).hide(); + /** + * Display errors on the page. + * + * @param {string} string + * The error message to show the user. + */ + displayError(string) { + const error = $('
').html( + string, + ); + $(this.element) + .before(error) + .hide(); - if (this.errorCallback) { - this.errorCallback(this); - } + if (this.errorCallback) { + this.errorCallback(this); + } + }, }, - }); -}(jQuery, Drupal)); + ); +})(jQuery, Drupal);