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