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