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
|
define([
'backbone',
'backbone.marionette'
], function (Backbone, Marionette) {
var $ = jQuery;
var defaults = {
queue: {},
timeout: 300,
fadeTimeout: 100
};
var Process = Backbone.Model.extend({
defaults: {
state: 'ok'
},
timeout: function () {
this.set({
state: 'timeout',
message: 'Still Working...'
});
},
finish: function (options) {
options = _.defaults(options || {}, { force: false });
if (this.get('state') !== 'failed' || !!options.force) {
this.trigger('destroy', this, this.collection, options);
}
},
fail: function (message) {
var that = this,
msg = message || window.t('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);
}
}),
Processes = Backbone.Collection.extend({
model: Process
}),
ProcessesView = Marionette.ItemView.extend({
tagName: 'ul',
className: 'processes-container',
collectionEvents: {
'all': 'render'
},
render: function () {
var failed = this.collection.findWhere({ state: 'failed' }),
timeout = this.collection.findWhere({ state: 'timeout' }),
el;
this.$el.empty();
if (failed != null) {
el = $('<li></li>')
.html(failed.get('message'))
.addClass('process-spinner process-spinner-failed shown');
var 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;
}
});
var processes = new Processes(),
processesView = new ProcessesView({
collection: processes
});
/**
* Add background process
* @returns {number}
*/
function addBackgroundProcess () {
var uid = _.uniqueId('process'),
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) {
var process = processes.get(uid);
if (process != null) {
process.finish();
}
}
/**
* Fail background process
* @param {number} uid
* @param {string} message
*/
function failBackgroundProcess (uid, message) {
var process = processes.get(uid);
if (process != null) {
process.fail(message);
}
}
/**
* Handle ajax error
* @param jqXHR
*/
function handleAjaxError (jqXHR) {
if (jqXHR.processId != null) {
var message = null;
if (jqXHR != null && jqXHR.responseJSON != null && jqXHR.responseJSON.errors != null) {
message = _.pluck(jqXHR.responseJSON.errors, 'msg').join('. ');
}
failBackgroundProcess(jqXHR.processId, message);
}
}
$.ajaxSetup({
beforeSend: function (jqXHR) {
jqXHR.processId = addBackgroundProcess();
},
complete: function (jqXHR) {
if (jqXHR.processId != null) {
finishBackgroundProcess(jqXHR.processId);
}
},
statusCode: {
400: handleAjaxError,
401: handleAjaxError,
403: handleAjaxError,
500: handleAjaxError
}
});
$(function () {
processesView.render().$el.appendTo(document.body);
});
});
|