annotate core/misc/progress.js @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 4c8ae668cc8c
children
rev   line source
Chris@0 1 /**
Chris@0 2 * DO NOT EDIT THIS FILE.
Chris@0 3 * See the following change record for more information,
Chris@0 4 * https://www.drupal.org/node/2815083
Chris@0 5 * @preserve
Chris@0 6 **/
Chris@0 7
Chris@0 8 (function ($, Drupal) {
Chris@0 9 Drupal.theme.progressBar = function (id) {
Chris@0 10 return '<div id="' + id + '" class="progress" aria-live="polite">' + '<div class="progress__label">&nbsp;</div>' + '<div class="progress__track"><div class="progress__bar"></div></div>' + '<div class="progress__percentage"></div>' + '<div class="progress__description">&nbsp;</div>' + '</div>';
Chris@0 11 };
Chris@0 12
Chris@0 13 Drupal.ProgressBar = function (id, updateCallback, method, errorCallback) {
Chris@0 14 this.id = id;
Chris@0 15 this.method = method || 'GET';
Chris@0 16 this.updateCallback = updateCallback;
Chris@0 17 this.errorCallback = errorCallback;
Chris@0 18
Chris@0 19 this.element = $(Drupal.theme('progressBar', id));
Chris@0 20 };
Chris@0 21
Chris@0 22 $.extend(Drupal.ProgressBar.prototype, {
Chris@0 23 setProgress: function setProgress(percentage, message, label) {
Chris@0 24 if (percentage >= 0 && percentage <= 100) {
Chris@0 25 $(this.element).find('div.progress__bar').css('width', percentage + '%');
Chris@0 26 $(this.element).find('div.progress__percentage').html(percentage + '%');
Chris@0 27 }
Chris@0 28 $('div.progress__description', this.element).html(message);
Chris@0 29 $('div.progress__label', this.element).html(label);
Chris@0 30 if (this.updateCallback) {
Chris@0 31 this.updateCallback(percentage, message, this);
Chris@0 32 }
Chris@0 33 },
Chris@0 34 startMonitoring: function startMonitoring(uri, delay) {
Chris@0 35 this.delay = delay;
Chris@0 36 this.uri = uri;
Chris@0 37 this.sendPing();
Chris@0 38 },
Chris@0 39 stopMonitoring: function stopMonitoring() {
Chris@0 40 clearTimeout(this.timer);
Chris@0 41
Chris@0 42 this.uri = null;
Chris@0 43 },
Chris@0 44 sendPing: function sendPing() {
Chris@0 45 if (this.timer) {
Chris@0 46 clearTimeout(this.timer);
Chris@0 47 }
Chris@0 48 if (this.uri) {
Chris@0 49 var pb = this;
Chris@0 50
Chris@0 51 var uri = this.uri;
Chris@0 52 if (uri.indexOf('?') === -1) {
Chris@0 53 uri += '?';
Chris@0 54 } else {
Chris@0 55 uri += '&';
Chris@0 56 }
Chris@0 57 uri += '_format=json';
Chris@0 58 $.ajax({
Chris@0 59 type: this.method,
Chris@0 60 url: uri,
Chris@0 61 data: '',
Chris@0 62 dataType: 'json',
Chris@0 63 success: function success(progress) {
Chris@0 64 if (progress.status === 0) {
Chris@0 65 pb.displayError(progress.data);
Chris@0 66 return;
Chris@0 67 }
Chris@0 68
Chris@0 69 pb.setProgress(progress.percentage, progress.message, progress.label);
Chris@0 70
Chris@0 71 pb.timer = setTimeout(function () {
Chris@0 72 pb.sendPing();
Chris@0 73 }, pb.delay);
Chris@0 74 },
Chris@0 75 error: function error(xmlhttp) {
Chris@0 76 var e = new Drupal.AjaxError(xmlhttp, pb.uri);
Chris@0 77 pb.displayError('<pre>' + e.message + '</pre>');
Chris@0 78 }
Chris@0 79 });
Chris@0 80 }
Chris@0 81 },
Chris@0 82 displayError: function displayError(string) {
Chris@0 83 var error = $('<div class="messages messages--error"></div>').html(string);
Chris@0 84 $(this.element).before(error).hide();
Chris@0 85
Chris@0 86 if (this.errorCallback) {
Chris@0 87 this.errorCallback(this);
Chris@0 88 }
Chris@0 89 }
Chris@0 90 });
Chris@0 91 })(jQuery, Drupal);