Improved rebuild data UI/UX
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / js / WoltLabSuite / Core / Acp / Ui / Worker.js
1 /**
2 * Worker manager with support for custom callbacks and loop counts.
3 *
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
8 */
9 define(['Ajax', 'Core', 'Language', 'Ui/Dialog'], function(Ajax, Core, Language, UiDialog) {
10 "use strict";
11
12 /**
13 * Creates a new worker instance.
14 *
15 * @param {Object} options configuration options
16 * @constructor
17 */
18 function AcpUiWorker(options) { this.init(options); }
19 AcpUiWorker.prototype = {
20 /**
21 * Creates a new worker instance.
22 *
23 * @param {Object} options configuration options
24 */
25 init: function (options) {
26 this._aborted = false;
27 this._options = Core.extend({
28 // dialog
29 dialogId: '',
30 dialogTitle: '',
31
32 // ajax
33 className: '',
34 loopCount: -1,
35 parameters: {},
36
37 // callbacks
38 callbackAbort: null,
39 callbackFailure: null,
40 callbackSuccess: null
41 }, options);
42 this._options.dialogId += 'Worker';
43
44 this._request = Ajax.api(this);
45 },
46
47 _ajaxSuccess: function (data) {
48 if (this._aborted) return;
49
50 if (typeof data.template === 'string') {
51 UiDialog.open(this, data.template);
52 }
53
54 var content = UiDialog.getDialog(this).content;
55
56 // update progress
57 var progress = elBySel('progress', content);
58 progress.value = data.progress;
59 progress.nextElementSibling.textContent = data.progress + '%';
60
61 // worker is still busy
62 if (data.progress < 100) {
63 Ajax.api(this, {
64 loopCount: data.loopCount,
65 parameters: data.parameters
66 });
67 }
68 else {
69 var spinner = elBySel('.fa-spinner', content);
70 spinner.classList.remove('fa-spinner');
71 spinner.classList.add('fa-check');
72 spinner.classList.add('green');
73
74 var formSubmit = elCreate('div');
75 formSubmit.className = 'formSubmit';
76 formSubmit.innerHTML = '<button class="buttonPrimary">' + Language.get('wcf.global.button.next') + '</button>';
77
78 content.appendChild(formSubmit);
79 UiDialog.rebuild(this);
80
81 var button = formSubmit.children[0];
82 button.addEventListener(WCF_CLICK_EVENT, (function(event) {
83 event.preventDefault();
84
85 if (typeof this._options.callbackSuccess === 'function') {
86 this._options.callbackSuccess(data);
87
88 UiDialog.close(this);
89 }
90 else {
91 window.location = data.proceedURL;
92 }
93 }).bind(this));
94 }
95 },
96
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');
104 }
105 },
106
107 _ajaxSetup: function () {
108 return {
109 data: {
110 className: this._options.className,
111 loopCount: this._options.loopCount,
112 parameters: this._options.parameters
113 },
114 silent: true,
115 url: 'index.php?worker-proxy/&t=' + SECURITY_TOKEN
116 };
117 },
118
119 _dialogSetup: function () {
120 return {
121 id: this._options.dialogId,
122 onClose: (function () {
123 this._aborted = true;
124 this._request.abortPrevious();
125
126 if (typeof this._options.callbackAbort === 'function') {
127 this._options.callbackAbort();
128 }
129 else {
130 window.location.reload();
131 }
132 }).bind(this),
133 options: {
134 backdropCloseOnClick: false,
135 title: this._options.dialogTitle
136 },
137 source: null
138 }
139 }
140 };
141
142 return AcpUiWorker;
143 });