annotate core/misc/progress.es6.js @ 0:c75dbcec494b

Initial commit from drush-created site
author Chris Cannam
date Thu, 05 Jul 2018 14:24:15 +0000
parents
children a9cd425dd02b
rev   line source
Chris@0 1 /**
Chris@0 2 * @file
Chris@0 3 * Progress bar.
Chris@0 4 */
Chris@0 5
Chris@0 6 (function ($, Drupal) {
Chris@0 7 /**
Chris@0 8 * Theme function for the progress bar.
Chris@0 9 *
Chris@0 10 * @param {string} id
Chris@0 11 * The id for the progress bar.
Chris@0 12 *
Chris@0 13 * @return {string}
Chris@0 14 * The HTML for the progress bar.
Chris@0 15 */
Chris@0 16 Drupal.theme.progressBar = function (id) {
Chris@0 17 return `<div id="${id}" class="progress" aria-live="polite">` +
Chris@0 18 '<div class="progress__label">&nbsp;</div>' +
Chris@0 19 '<div class="progress__track"><div class="progress__bar"></div></div>' +
Chris@0 20 '<div class="progress__percentage"></div>' +
Chris@0 21 '<div class="progress__description">&nbsp;</div>' +
Chris@0 22 '</div>';
Chris@0 23 };
Chris@0 24
Chris@0 25 /**
Chris@0 26 * A progressbar object. Initialized with the given id. Must be inserted into
Chris@0 27 * the DOM afterwards through progressBar.element.
Chris@0 28 *
Chris@0 29 * Method is the function which will perform the HTTP request to get the
Chris@0 30 * progress bar state. Either "GET" or "POST".
Chris@0 31 *
Chris@0 32 * @example
Chris@0 33 * pb = new Drupal.ProgressBar('myProgressBar');
Chris@0 34 * some_element.appendChild(pb.element);
Chris@0 35 *
Chris@0 36 * @constructor
Chris@0 37 *
Chris@0 38 * @param {string} id
Chris@0 39 * The id for the progressbar.
Chris@0 40 * @param {function} updateCallback
Chris@0 41 * Callback to run on update.
Chris@0 42 * @param {string} method
Chris@0 43 * HTTP method to use.
Chris@0 44 * @param {function} errorCallback
Chris@0 45 * Callback to call on error.
Chris@0 46 */
Chris@0 47 Drupal.ProgressBar = function (id, updateCallback, method, errorCallback) {
Chris@0 48 this.id = id;
Chris@0 49 this.method = method || 'GET';
Chris@0 50 this.updateCallback = updateCallback;
Chris@0 51 this.errorCallback = errorCallback;
Chris@0 52
Chris@0 53 // The WAI-ARIA setting aria-live="polite" will announce changes after
Chris@0 54 // users
Chris@0 55 // have completed their current activity and not interrupt the screen
Chris@0 56 // reader.
Chris@0 57 this.element = $(Drupal.theme('progressBar', id));
Chris@0 58 };
Chris@0 59
Chris@0 60 $.extend(Drupal.ProgressBar.prototype, /** @lends Drupal.ProgressBar# */{
Chris@0 61
Chris@0 62 /**
Chris@0 63 * Set the percentage and status message for the progressbar.
Chris@0 64 *
Chris@0 65 * @param {number} percentage
Chris@0 66 * The progress percentage.
Chris@0 67 * @param {string} message
Chris@0 68 * The message to show the user.
Chris@0 69 * @param {string} label
Chris@0 70 * The text for the progressbar label.
Chris@0 71 */
Chris@0 72 setProgress(percentage, message, label) {
Chris@0 73 if (percentage >= 0 && percentage <= 100) {
Chris@0 74 $(this.element).find('div.progress__bar').css('width', `${percentage}%`);
Chris@0 75 $(this.element).find('div.progress__percentage').html(`${percentage}%`);
Chris@0 76 }
Chris@0 77 $('div.progress__description', this.element).html(message);
Chris@0 78 $('div.progress__label', this.element).html(label);
Chris@0 79 if (this.updateCallback) {
Chris@0 80 this.updateCallback(percentage, message, this);
Chris@0 81 }
Chris@0 82 },
Chris@0 83
Chris@0 84 /**
Chris@0 85 * Start monitoring progress via Ajax.
Chris@0 86 *
Chris@0 87 * @param {string} uri
Chris@0 88 * The URI to use for monitoring.
Chris@0 89 * @param {number} delay
Chris@0 90 * The delay for calling the monitoring URI.
Chris@0 91 */
Chris@0 92 startMonitoring(uri, delay) {
Chris@0 93 this.delay = delay;
Chris@0 94 this.uri = uri;
Chris@0 95 this.sendPing();
Chris@0 96 },
Chris@0 97
Chris@0 98 /**
Chris@0 99 * Stop monitoring progress via Ajax.
Chris@0 100 */
Chris@0 101 stopMonitoring() {
Chris@0 102 clearTimeout(this.timer);
Chris@0 103 // This allows monitoring to be stopped from within the callback.
Chris@0 104 this.uri = null;
Chris@0 105 },
Chris@0 106
Chris@0 107 /**
Chris@0 108 * Request progress data from server.
Chris@0 109 */
Chris@0 110 sendPing() {
Chris@0 111 if (this.timer) {
Chris@0 112 clearTimeout(this.timer);
Chris@0 113 }
Chris@0 114 if (this.uri) {
Chris@0 115 const pb = this;
Chris@0 116 // When doing a post request, you need non-null data. Otherwise a
Chris@0 117 // HTTP 411 or HTTP 406 (with Apache mod_security) error may result.
Chris@0 118 let uri = this.uri;
Chris@0 119 if (uri.indexOf('?') === -1) {
Chris@0 120 uri += '?';
Chris@0 121 }
Chris@0 122 else {
Chris@0 123 uri += '&';
Chris@0 124 }
Chris@0 125 uri += '_format=json';
Chris@0 126 $.ajax({
Chris@0 127 type: this.method,
Chris@0 128 url: uri,
Chris@0 129 data: '',
Chris@0 130 dataType: 'json',
Chris@0 131 success(progress) {
Chris@0 132 // Display errors.
Chris@0 133 if (progress.status === 0) {
Chris@0 134 pb.displayError(progress.data);
Chris@0 135 return;
Chris@0 136 }
Chris@0 137 // Update display.
Chris@0 138 pb.setProgress(progress.percentage, progress.message, progress.label);
Chris@0 139 // Schedule next timer.
Chris@0 140 pb.timer = setTimeout(() => {
Chris@0 141 pb.sendPing();
Chris@0 142 }, pb.delay);
Chris@0 143 },
Chris@0 144 error(xmlhttp) {
Chris@0 145 const e = new Drupal.AjaxError(xmlhttp, pb.uri);
Chris@0 146 pb.displayError(`<pre>${e.message}</pre>`);
Chris@0 147 },
Chris@0 148 });
Chris@0 149 }
Chris@0 150 },
Chris@0 151
Chris@0 152 /**
Chris@0 153 * Display errors on the page.
Chris@0 154 *
Chris@0 155 * @param {string} string
Chris@0 156 * The error message to show the user.
Chris@0 157 */
Chris@0 158 displayError(string) {
Chris@0 159 const error = $('<div class="messages messages--error"></div>').html(string);
Chris@0 160 $(this.element).before(error).hide();
Chris@0 161
Chris@0 162 if (this.errorCallback) {
Chris@0 163 this.errorCallback(this);
Chris@0 164 }
Chris@0 165 },
Chris@0 166 });
Chris@0 167 }(jQuery, Drupal));