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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
|
odoo.define('web.special_fields', function (require) {
"use strict";
var core = require('web.core');
var field_utils = require('web.field_utils');
var relational_fields = require('web.relational_fields');
var AbstractField = require('web.AbstractField');
var FieldSelection = relational_fields.FieldSelection;
var _t = core._t;
var _lt = core._lt;
/**
* This widget is intended to display a warning near a label of a 'timezone' field
* indicating if the browser timezone is identical (or not) to the selected timezone.
* This widget depends on a field given with the param 'tz_offset_field', which contains
* the time difference between UTC time and local time, in minutes.
*/
var FieldTimezoneMismatch = FieldSelection.extend({
/**
* @override
*/
start: function () {
var interval = navigator.platform.toUpperCase().indexOf('MAC') >= 0 ? 60000 : 1000;
this._datetime = setInterval(this._renderDateTimeTimezone.bind(this), interval);
return this._super.apply(this, arguments);
},
/**
* @override
*/
destroy: function () {
clearInterval(this._datetime);
return this._super();
},
//--------------------------------------------------------------------------
// Private
//--------------------------------------------------------------------------
/**
* @override
* @private
*/
_render: function () {
this._super.apply(this, arguments);
this._renderTimezoneMismatch();
},
/**
* Display the time in the user timezone (reload each second)
*
* @private
*/
_renderDateTimeTimezone: function () {
if (!this.mismatch || !this.$option.html()) {
return;
}
var offset = this.recordData.tz_offset.match(/([+-])([0-9]{2})([0-9]{2})/);
offset = (offset[1] === '-' ? -1 : 1) * (parseInt(offset[2])*60 + parseInt(offset[3]));
var datetime = field_utils.format.datetime(moment.utc().add(offset, 'minutes'), this.field, {timezone: false});
var content = this.$option.html().split(' ')[0];
content += ' ('+ datetime + ')';
this.$option.html(content);
},
/**
* Display the timezone alert
*
* Note: timezone alert is a span that is added after $el, and $el is now a
* set of two elements
*
* @private
*/
_renderTimezoneMismatch: function () {
// we need to clean the warning to have maximum one alert
this.$el.last().filter('.o_tz_warning').remove();
this.$el = this.$el.first();
var value = this.$el.val();
var $span = $('<span class="fa fa-exclamation-triangle o_tz_warning"/>');
if (this.$option && this.$option.html()) {
this.$option.html(this.$option.html().split(' ')[0]);
}
var userOffset = this.recordData.tz_offset;
this.mismatch = false;
if (userOffset && value !== "" && value !== "false") {
var offset = -(new Date().getTimezoneOffset());
var browserOffset = (offset < 0) ? "-" : "+";
browserOffset += _.str.sprintf("%02d", Math.abs(offset / 60));
browserOffset += _.str.sprintf("%02d", Math.abs(offset % 60));
this.mismatch = (browserOffset !== userOffset);
}
if (this.mismatch){
$span.insertAfter(this.$el);
$span.attr('title', _t("Timezone Mismatch : This timezone is different from that of your browser.\nPlease, set the same timezone as your browser's to avoid time discrepancies in your system."));
this.$el = this.$el.add($span);
this.$option = this.$('option').filter(function () {
return $(this).attr('value') === value;
});
this._renderDateTimeTimezone();
} else if (value == "false") {
$span.insertAfter(this.$el);
$span.attr('title', _t("Set a timezone on your user"));
this.$el = this.$el.add($span);
}
},
/**
* @override
* @private
* this.$el can have other elements than select
* that should not be touched
*/
_renderEdit: function () {
// FIXME: hack to handle multiple root elements
// in this.$el , which is a bad idea
// In master we should make this.$el a wrapper
// around multiple subelements
var $otherEl = this.$el.not('select');
this.$el = this.$el.first();
this._super.apply(this, arguments);
$otherEl.insertAfter(this.$el);
this.$el = this.$el.add($otherEl);
},
});
var FieldReportLayout = relational_fields.FieldMany2One.extend({
// this widget is not generic, so we disable its studio use
// supportedFieldTypes: ['many2one', 'selection'],
events: _.extend({}, relational_fields.FieldMany2One.prototype.events, {
'click img': '_onImgClicked',
}),
willStart: function () {
var self = this;
this.previews = {};
return this._super()
.then(function () {
return self._rpc({
model: 'report.layout',
method: "search_read"
}).then(function (values) {
self.previews = values;
});
});
},
//--------------------------------------------------------------------------
// Private
//--------------------------------------------------------------------------
/**
* @override
* @private
*/
_render: function () {
var self = this;
this.$el.empty();
var value = _.isObject(this.value) ? this.value.data.id : this.value;
_.each(this.previews, function (val) {
var $container = $('<div>').addClass('col-3 text-center');
var $img = $('<img>')
.addClass('img img-fluid img-thumbnail ml16')
.toggleClass('btn-info', val.view_id[0] === value)
.attr('src', val.image)
.data('key', val.view_id[0]);
$container.append($img);
if (val.pdf) {
var $previewLink = $('<a>')
.text('Example')
.attr('href', val.pdf)
.attr('target', '_blank');
$container.append($previewLink);
}
self.$el.append($container);
});
},
//--------------------------------------------------------------------------
// Handlers
//--------------------------------------------------------------------------
/**
* @override
* @private
* @param {MouseEvent} event
*/
_onImgClicked: function (event) {
this._setValue($(event.currentTarget).data('key'));
},
});
const IframeWrapper = AbstractField.extend({
description: _lt("Wrap raw html within an iframe"),
// If HTML, don't forget to adjust the sanitize options to avoid stripping most of the metadata
supportedFieldTypes: ['text', 'html'],
template: "web.IframeWrapper",
_render() {
const spinner = this.el.querySelector('.o_iframe_wrapper_spinner');
const iframe = this.el.querySelector('.o_preview_iframe');
iframe.style.display = 'none';
spinner.style.display = 'block';
// Promise for tests
let resolver;
$(iframe).data('ready', new Promise((resolve) => {
resolver = resolve;
}));
/**
* Certain browser don't trigger onload events of iframe for particular cases.
* In our case, chrome and safari could be problematic depending on version and environment.
* This rather unorthodox solution replace the onload event handler. (jquery on('load') doesn't fix it)
*/
const onloadReplacement = setInterval(() => {
const iframeDoc = iframe.contentDocument;
if (iframeDoc && (iframeDoc.readyState === 'complete' || iframeDoc.readyState === 'interactive')) {
/**
* The document.write is not recommended. It is better to manipulate the DOM through $.appendChild and
* others. In our case though, we deal with an iframe without src attribute and with metadata to put in
* head tag. If we use the usual dom methods, the iframe is automatically created with its document
* component containing html > head & body. Therefore, if we want to make it work that way, we would
* need to receive each piece at a time to append it to this document (with this.record.data and extra
* model fields or with an rpc). It also cause other difficulties getting attribute on the most parent
* nodes, parsing to HTML complex elements, etc.
* Therefore, document.write makes it much more trivial in our situation.
*/
iframeDoc.open();
iframeDoc.write(this.value);
iframeDoc.close();
iframe.style.display = 'block';
spinner.style.display = 'none';
resolver();
clearInterval(onloadReplacement);
}
}, 100);
}
});
return {
FieldTimezoneMismatch: FieldTimezoneMismatch,
FieldReportLayout: FieldReportLayout,
IframeWrapper,
};
});
|