X-Git-Url: http://www.aleph1.co.uk/gitweb/?a=blobdiff_plain;ds=sidebyside;f=web%2Fcore%2Fmisc%2Fbatch.es6.js;fp=web%2Fcore%2Fmisc%2Fbatch.es6.js;h=995d1c51d12435aa9874a73f51cdfda673f79a24;hb=9917807b03b64faf00f6a1f29dcb6eafc454efa5;hp=0000000000000000000000000000000000000000;hpb=aea91e65e895364e460983b890e295aa5d5540a5;p=yaffs-website diff --git a/web/core/misc/batch.es6.js b/web/core/misc/batch.es6.js new file mode 100644 index 000000000..995d1c51d --- /dev/null +++ b/web/core/misc/batch.es6.js @@ -0,0 +1,42 @@ +/** + * @file + * Drupal's batch API. + */ + +(function ($, Drupal) { + /** + * Attaches the batch behavior to progress bars. + * + * @type {Drupal~behavior} + */ + Drupal.behaviors.batch = { + attach(context, settings) { + const batch = settings.batch; + const $progress = $('[data-drupal-progress]').once('batch'); + let progressBar; + + // Success: redirect to the summary. + function updateCallback(progress, status, pb) { + if (progress === '100') { + pb.stopMonitoring(); + window.location = `${batch.uri}&op=finished`; + } + } + + function errorCallback(pb) { + $progress.prepend($('

').html(batch.errorMessage)); + $('#wait').hide(); + } + + if ($progress.length) { + progressBar = new Drupal.ProgressBar('updateprogress', updateCallback, 'POST', errorCallback); + progressBar.setProgress(-1, batch.initMessage); + progressBar.startMonitoring(`${batch.uri}&op=do`, 10); + // Remove HTML from no-js progress bar. + $progress.empty(); + // Append the JS progressbar element. + $progress.append(progressBar.element); + } + }, + }; +}(jQuery, Drupal));