2 * Worker manager with support for custom callbacks and loop counts.
4 * @author Alexander Ebert
5 * @copyright 2001-2017 WoltLab GmbH
6 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
7 * @module WoltLabSuite/Core/Acp/Ui/Worker
9 define(['Ajax', 'Core', 'Language', 'Ui/Dialog'], function(Ajax
, Core
, Language
, UiDialog
) {
13 * Creates a new worker instance.
15 * @param {Object} options configuration options
18 function AcpUiWorker(options
) { this.init(options
); }
19 AcpUiWorker
.prototype = {
21 * Creates a new worker instance.
23 * @param {Object} options configuration options
25 init: function (options
) {
26 this._aborted
= false;
27 this._options
= Core
.extend({
39 callbackFailure
: null,
42 this._options
.dialogId
+= 'Worker';
44 this._request
= Ajax
.api(this);
47 _ajaxSuccess: function (data
) {
48 if (this._aborted
) return;
50 if (typeof data
.template
=== 'string') {
51 UiDialog
.open(this, data
.template
);
54 var content
= UiDialog
.getDialog(this).content
;
57 var progress
= elBySel('progress', content
);
58 progress
.value
= data
.progress
;
59 progress
.nextElementSibling
.textContent
= data
.progress
+ '%';
61 // worker is still busy
62 if (data
.progress
< 100) {
64 loopCount
: data
.loopCount
,
65 parameters
: data
.parameters
69 var spinner
= elBySel('.fa-spinner', content
);
70 spinner
.classList
.remove('fa-spinner');
71 spinner
.classList
.add('fa-check');
72 spinner
.classList
.add('green');
74 var formSubmit
= elCreate('div');
75 formSubmit
.className
= 'formSubmit';
76 formSubmit
.innerHTML
= '<button class="buttonPrimary">' + Language
.get('wcf.global.button.next') + '</button>';
78 content
.appendChild(formSubmit
);
79 UiDialog
.rebuild(this);
81 var button
= formSubmit
.children
[0];
82 button
.addEventListener(WCF_CLICK_EVENT
, (function(event
) {
83 event
.preventDefault();
85 if (typeof this._options
.callbackSuccess
=== 'function') {
86 this._options
.callbackSuccess(data
);
91 window
.location
= data
.proceedURL
;
97 _ajaxFailure: function () {
98 var dialog
= UiDialog
.getDialog(this);
99 if (dialog
!== null) {
100 var spinner
= elBySel('.fa-spinner', dialog
.content
);
101 spinner
.classList
.remove('fa-spinner');
102 spinner
.classList
.add('fa-times');
103 spinner
.classList
.add('red');
107 _ajaxSetup: function () {
110 className
: this._options
.className
,
111 loopCount
: this._options
.loopCount
,
112 parameters
: this._options
.parameters
115 url
: 'index.php?worker-proxy/&t=' + SECURITY_TOKEN
119 _dialogSetup: function () {
121 id
: this._options
.dialogId
,
122 onClose
: (function () {
123 this._aborted
= true;
124 this._request
.abortPrevious();
126 if (typeof this._options
.callbackAbort
=== 'function') {
127 this._options
.callbackAbort();
130 window
.location
.reload();
134 backdropCloseOnClick
: false,
135 title
: this._options
.dialogTitle