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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
|
odoo.define('web.framework', function (require) {
"use strict";
var core = require('web.core');
var ajax = require('web.ajax');
var Widget = require('web.Widget');
var disableCrashManager = require('web.CrashManager').disable;
const {sprintf} = require('web.utils')
var _t = core._t;
var messages_by_seconds = function() {
return [
[0, _t("Loading...")],
[20, _t("Still loading...")],
[60, _t("Still loading...<br />Please be patient.")],
[120, _t("Don't leave yet,<br />it's still loading...")],
[300, _t("You may not believe it,<br />but the application is actually loading...")],
[420, _t("Take a minute to get a coffee,<br />because it's loading...")],
[3600, _t("Maybe you should consider reloading the application by pressing F5...")]
];
};
var Throbber = Widget.extend({
template: "Throbber",
start: function() {
this.start_time = new Date().getTime();
this.act_message();
},
act_message: function() {
var self = this;
setTimeout(function() {
if (self.isDestroyed())
return;
var seconds = (new Date().getTime() - self.start_time) / 1000;
var mes;
_.each(messages_by_seconds(), function(el) {
if (seconds >= el[0])
mes = el[1];
});
self.$(".oe_throbber_message").html(mes);
self.act_message();
}, 1000);
},
});
/** Setup blockui */
if ($.blockUI) {
$.blockUI.defaults.baseZ = 1100;
$.blockUI.defaults.message = '<div class="openerp oe_blockui_spin_container" style="background-color: transparent;">';
$.blockUI.defaults.css.border = '0';
$.blockUI.defaults.css["background-color"] = '';
}
/**
* Remove the "accesskey" attributes to avoid the use of the access keys
* while the blockUI is enable.
*/
function blockAccessKeys() {
var elementWithAccessKey = [];
elementWithAccessKey = document.querySelectorAll('[accesskey]');
_.each(elementWithAccessKey, function (elem) {
elem.setAttribute("data-accesskey",elem.getAttribute('accesskey'));
elem.removeAttribute('accesskey');
});
}
function unblockAccessKeys() {
var elementWithDataAccessKey = [];
elementWithDataAccessKey = document.querySelectorAll('[data-accesskey]');
_.each(elementWithDataAccessKey, function (elem) {
elem.setAttribute('accesskey', elem.getAttribute('data-accesskey'));
elem.removeAttribute('data-accesskey');
});
}
var throbbers = [];
function blockUI() {
var tmp = $.blockUI.apply($, arguments);
var throbber = new Throbber();
throbbers.push(throbber);
throbber.appendTo($(".oe_blockui_spin_container"));
$(document.body).addClass('o_ui_blocked');
blockAccessKeys();
return tmp;
}
function unblockUI() {
_.invoke(throbbers, 'destroy');
throbbers = [];
$(document.body).removeClass('o_ui_blocked');
unblockAccessKeys();
return $.unblockUI.apply($, arguments);
}
/**
* Redirect to url by replacing window.location
* If wait is true, sleep 1s and wait for the server i.e. after a restart.
*/
function redirect (url, wait) {
// Dont display a dialog if some xmlhttprequest are in progress
disableCrashManager();
var load = function() {
var old = "" + window.location;
var old_no_hash = old.split("#")[0];
var url_no_hash = url.split("#")[0];
location.assign(url);
if (old_no_hash === url_no_hash) {
location.reload(true);
}
};
var wait_server = function() {
ajax.rpc("/web/webclient/version_info", {}).then(load).guardedCatch(function () {
setTimeout(wait_server, 250);
});
};
if (wait) {
setTimeout(wait_server, 1000);
} else {
load();
}
}
// * Client action to reload the whole interface.
// * If params.menu_id, it opens the given menu entry.
// * If params.wait, reload will wait the openerp server to be reachable before reloading
function Reload(parent, action) {
var params = action.params || {};
var menu_id = params.menu_id || false;
var l = window.location;
var sobj = $.deparam(l.search.substr(1));
if (params.url_search) {
sobj = _.extend(sobj, params.url_search);
}
var search = '?' + $.param(sobj);
var hash = l.hash;
if (menu_id) {
hash = "#menu_id=" + menu_id;
}
var url = l.protocol + "//" + l.host + l.pathname + search + hash;
// Clear cache
core.bus.trigger('clear_cache');
redirect(url, params.wait);
}
core.action_registry.add("reload", Reload);
/**
* Client action to go back home.
*/
function Home (parent, action) {
var url = '/' + (window.location.search || '');
redirect(url, action && action.params && action.params.wait);
}
core.action_registry.add("home", Home);
function login() {
redirect('/web/login');
}
core.action_registry.add("login", login);
function logout() {
redirect('/web/session/logout');
return new Promise();
}
core.action_registry.add("logout", logout);
/**
* @param {ActionManager} parent
* @param {Object} action
* @param {Object} action.params notification params
* @see ServiceMixin.displayNotification
*/
function displayNotification(parent, action) {
let {title='', message='', links=[], type='info', sticky=false, next} = action.params || {};
links = links.map(({url, label}) => `<a href="${_.escape(url)}" target="_blank">${_.escape(label)}</a>`)
parent.displayNotification({
title: _.escape(title),
message: sprintf(_.escape(message), ...links),
type,
sticky
});
return next;
}
core.action_registry.add("display_notification", displayNotification);
/**
* Client action to refresh the session context (making sure
* HTTP requests will have the right one) then reload the
* whole interface.
*/
function ReloadContext (parent, action) {
// side-effect of get_session_info is to refresh the session context
ajax.rpc("/web/session/get_session_info", {}).then(function() {
Reload(parent, action);
});
}
core.action_registry.add("reload_context", ReloadContext);
// In Internet Explorer, document doesn't have the contains method, so we make a
// polyfill for the method in order to be compatible.
if (!document.contains) {
document.contains = function contains (node) {
if (!(0 in arguments)) {
throw new TypeError('1 argument is required');
}
do {
if (this === node) {
return true;
}
} while (node = node && node.parentNode);
return false;
};
}
return {
blockUI: blockUI,
unblockUI: unblockUI,
redirect: redirect,
};
});
|