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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
/*
* SonarQube
* Copyright (C) 2009-2016 SonarSource SA
* mailto:contact AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import $ from 'jquery';
import _ from 'underscore';
import Backbone from 'backbone';
import Marionette from 'backbone.marionette';
import escapeHtml from 'escape-html';
import { translate } from '../../helpers/l10n';
import { getCSRFTokenName, getCSRFTokenValue } from '../../helpers/request';
const defaults = {
queue: {},
timeout: 300,
fadeTimeout: 100
};
const Process = Backbone.Model.extend({
defaults: {
state: 'ok'
},
timeout () {
this.set({
state: 'timeout',
message: 'Still Working...'
});
},
finish (options) {
options = _.defaults(options || {}, { force: false });
if (this.get('state') !== 'failed' || !!options.force) {
this.trigger('destroy', this, this.collection, options);
}
},
fail (message) {
const that = this;
let msg = message || translate('process.fail');
if (msg === 'process.fail') {
// no translation
msg = 'An error happened, some parts of the page might not render correctly. ' +
'Please contact the administrator if you keep on experiencing this error.';
}
clearInterval(this.get('timer'));
this.set({
state: 'failed',
message: msg
});
this.set('state', 'failed');
setTimeout(function () {
that.finish({ force: true });
}, 5000);
}
});
const Processes = Backbone.Collection.extend({
model: Process
});
const ProcessesView = Marionette.ItemView.extend({
tagName: 'ul',
className: 'processes-container',
collectionEvents: {
'all': 'render'
},
render () {
const failed = this.collection.findWhere({ state: 'failed' });
const timeout = this.collection.findWhere({ state: 'timeout' });
let el;
this.$el.empty();
if (failed != null) {
el = $('<li></li>')
.html(failed.get('message'))
.addClass('process-spinner process-spinner-failed shown');
const close = $('<button></button>').html('<i class="icon-close"></i>').addClass('process-spinner-close');
close.appendTo(el);
close.on('click', function () {
failed.finish({ force: true });
});
el.appendTo(this.$el);
} else if (timeout != null) {
el = $('<li></li>')
.html(timeout.get('message'))
.addClass('process-spinner shown');
el.appendTo(this.$el);
}
return this;
}
});
const processes = new Processes();
const processesView = new ProcessesView({
collection: processes
});
/**
* Add background process
* @returns {number}
*/
function addBackgroundProcess () {
const uid = _.uniqueId('process');
const process = new Process({
id: uid,
timer: setTimeout(function () {
process.timeout();
}, defaults.timeout)
});
processes.add(process);
return uid;
}
/**
* Finish background process
* @param {number} uid
*/
function finishBackgroundProcess (uid) {
const process = processes.get(uid);
if (process != null) {
process.finish();
}
}
/**
* Fail background process
* @param {number} uid
* @param {string} message
*/
function failBackgroundProcess (uid, message) {
const process = processes.get(uid);
if (process != null) {
process.fail(message);
}
}
/**
* Handle ajax error
* @param jqXHR
*/
function handleAjaxError (jqXHR) {
if (jqXHR != null && jqXHR.processId != null) {
let message = null;
if (jqXHR.responseJSON != null && jqXHR.responseJSON.errors != null) {
message = _.pluck(jqXHR.responseJSON.errors, 'msg').join('. ');
}
failBackgroundProcess(jqXHR.processId, message ? escapeHtml(message) : null);
}
}
$.ajaxSetup({
beforeSend (jqXHR) {
jqXHR.setRequestHeader(getCSRFTokenName(), getCSRFTokenValue());
jqXHR.processId = addBackgroundProcess();
},
complete (jqXHR) {
if (jqXHR.processId != null) {
finishBackgroundProcess(jqXHR.processId);
}
},
statusCode: {
400: handleAjaxError,
403: handleAjaxError,
500: handleAjaxError,
502: handleAjaxError,
503: handleAjaxError,
504: handleAjaxError
}
});
const startAjaxMonitoring = () => {
processesView.render().$el.insertBefore('#footer');
};
export default startAjaxMonitoring;
|