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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
|
odoo.define('mail/static/src/models/chat_window/chat_window.js', function (require) {
'use strict';
const { registerNewModel } = require('mail/static/src/model/model_core.js');
const { attr, many2one, one2many, one2one } = require('mail/static/src/model/model_field.js');
const { clear } = require('mail/static/src/model/model_field_command.js');
function factory(dependencies) {
class ChatWindow extends dependencies['mail.model'] {
/**
* @override
*/
_created() {
const res = super._created(...arguments);
this._onShowHomeMenu.bind(this);
this._onHideHomeMenu.bind(this);
this.env.messagingBus.on('hide_home_menu', this, this._onHideHomeMenu);
this.env.messagingBus.on('show_home_menu', this, this._onShowHomeMenu);
return res;
}
/**
* @override
*/
_willDelete() {
this.env.messagingBus.off('hide_home_menu', this, this._onHideHomeMenu);
this.env.messagingBus.off('show_home_menu', this, this._onShowHomeMenu);
return super._willDelete(...arguments);
}
//----------------------------------------------------------------------
// Public
//----------------------------------------------------------------------
/**
* Close this chat window.
*
* @param {Object} [param0={}]
* @param {boolean} [param0.notifyServer]
*/
close({ notifyServer } = {}) {
if (notifyServer === undefined) {
notifyServer = !this.env.messaging.device.isMobile;
}
const thread = this.thread;
this.delete();
// Flux specific: 'closed' fold state should only be saved on the
// server when manually closing the chat window. Delete at destroy
// or sync from server value for example should not save the value.
if (thread && notifyServer) {
thread.notifyFoldStateToServer('closed');
}
if (this.env.device.isMobile && !this.env.messaging.discuss.isOpen) {
// If we are in mobile and discuss is not open, it means the
// chat window was opened from the messaging menu. In that
// case it should be re-opened to simulate it was always
// there in the background.
this.env.messaging.messagingMenu.update({ isOpen: true });
}
}
expand() {
if (this.thread) {
this.thread.open({ expanded: true });
}
}
/**
* Programmatically auto-focus an existing chat window.
*/
focus() {
this.update({ isDoFocus: true });
}
focusNextVisibleUnfoldedChatWindow() {
const nextVisibleUnfoldedChatWindow = this._getNextVisibleUnfoldedChatWindow();
if (nextVisibleUnfoldedChatWindow) {
nextVisibleUnfoldedChatWindow.focus();
}
}
focusPreviousVisibleUnfoldedChatWindow() {
const previousVisibleUnfoldedChatWindow =
this._getNextVisibleUnfoldedChatWindow({ reverse: true });
if (previousVisibleUnfoldedChatWindow) {
previousVisibleUnfoldedChatWindow.focus();
}
}
/**
* @param {Object} [param0={}]
* @param {boolean} [param0.notifyServer]
*/
fold({ notifyServer } = {}) {
if (notifyServer === undefined) {
notifyServer = !this.env.messaging.device.isMobile;
}
this.update({ isFolded: true });
// Flux specific: manually folding the chat window should save the
// new state on the server.
if (this.thread && notifyServer) {
this.thread.notifyFoldStateToServer('folded');
}
}
/**
* Makes this chat window active, which consists of making it visible,
* unfolding it, and focusing it.
*
* @param {Object} [options]
*/
makeActive(options) {
this.makeVisible();
this.unfold(options);
this.focus();
}
/**
* Makes this chat window visible by swapping it with the last visible
* chat window, or do nothing if it is already visible.
*/
makeVisible() {
if (this.isVisible) {
return;
}
const lastVisible = this.manager.lastVisible;
this.manager.swap(this, lastVisible);
}
/**
* Shift this chat window to the left on screen.
*/
shiftLeft() {
this.manager.shiftLeft(this);
}
/**
* Shift this chat window to the right on screen.
*/
shiftRight() {
this.manager.shiftRight(this);
}
/**
* @param {Object} [param0={}]
* @param {boolean} [param0.notifyServer]
*/
unfold({ notifyServer } = {}) {
if (notifyServer === undefined) {
notifyServer = !this.env.messaging.device.isMobile;
}
this.update({ isFolded: false });
// Flux specific: manually opening the chat window should save the
// new state on the server.
if (this.thread && notifyServer) {
this.thread.notifyFoldStateToServer('open');
}
}
//----------------------------------------------------------------------
// Private
//----------------------------------------------------------------------
/**
* @private
* @returns {boolean}
*/
_computeHasNewMessageForm() {
return this.isVisible && !this.isFolded && !this.thread;
}
/**
* @private
* @returns {boolean}
*/
_computeHasShiftLeft() {
if (!this.manager) {
return false;
}
const allVisible = this.manager.allOrderedVisible;
const index = allVisible.findIndex(visible => visible === this);
if (index === -1) {
return false;
}
return index < allVisible.length - 1;
}
/**
* @private
* @returns {boolean}
*/
_computeHasShiftRight() {
if (!this.manager) {
return false;
}
const index = this.manager.allOrderedVisible.findIndex(visible => visible === this);
if (index === -1) {
return false;
}
return index > 0;
}
/**
* @private
* @returns {boolean}
*/
_computeHasThreadView() {
return this.isVisible && !this.isFolded && this.thread;
}
/**
* @private
* @returns {boolean}
*/
_computeIsFolded() {
const thread = this.thread;
if (thread) {
return thread.foldState === 'folded';
}
return this.isFolded;
}
/**
* @private
* @returns {boolean}
*/
_computeIsVisible() {
if (!this.manager) {
return false;
}
return this.manager.allOrderedVisible.includes(this);
}
/**
* @private
* @returns {string}
*/
_computeName() {
if (this.thread) {
return this.thread.displayName;
}
return this.env._t("New message");
}
/**
* @private
* @returns {integer|undefined}
*/
_computeVisibleIndex() {
if (!this.manager) {
return clear();
}
const visible = this.manager.visual.visible;
const index = visible.findIndex(visible => visible.chatWindowLocalId === this.localId);
if (index === -1) {
return clear();
}
return index;
}
/**
* @private
* @returns {integer}
*/
_computeVisibleOffset() {
if (!this.manager) {
return 0;
}
const visible = this.manager.visual.visible;
const index = visible.findIndex(visible => visible.chatWindowLocalId === this.localId);
if (index === -1) {
return 0;
}
return visible[index].offset;
}
/**
* Cycles to the next possible visible and unfolded chat window starting
* from the `currentChatWindow`, following the natural order based on the
* current text direction, and with the possibility to `reverse` based on
* the given parameter.
*
* @private
* @param {Object} [param0={}]
* @param {boolean} [param0.reverse=false]
* @returns {mail.chat_window|undefined}
*/
_getNextVisibleUnfoldedChatWindow({ reverse = false } = {}) {
const orderedVisible = this.manager.allOrderedVisible;
/**
* Return index of next visible chat window of a given visible chat
* window index. The direction of "next" chat window depends on
* `reverse` option.
*
* @param {integer} index
* @returns {integer}
*/
const _getNextIndex = index => {
const directionOffset = reverse ? 1 : -1;
let nextIndex = index + directionOffset;
if (nextIndex > orderedVisible.length - 1) {
nextIndex = 0;
}
if (nextIndex < 0) {
nextIndex = orderedVisible.length - 1;
}
return nextIndex;
};
const currentIndex = orderedVisible.findIndex(visible => visible === this);
let nextIndex = _getNextIndex(currentIndex);
let nextToFocus = orderedVisible[nextIndex];
while (nextToFocus.isFolded) {
nextIndex = _getNextIndex(nextIndex);
nextToFocus = orderedVisible[nextIndex];
}
return nextToFocus;
}
//----------------------------------------------------------------------
// Handlers
//----------------------------------------------------------------------
/**
* @private
*/
async _onHideHomeMenu() {
if (!this.threadView) {
return;
}
this.threadView.addComponentHint('home-menu-hidden');
}
/**
* @private
*/
async _onShowHomeMenu() {
if (!this.threadView) {
return;
}
this.threadView.addComponentHint('home-menu-shown');
}
}
ChatWindow.fields = {
/**
* Determines whether "new message form" should be displayed.
*/
hasNewMessageForm: attr({
compute: '_computeHasNewMessageForm',
dependencies: [
'isFolded',
'isVisible',
'thread',
],
}),
hasShiftLeft: attr({
compute: '_computeHasShiftLeft',
dependencies: ['managerAllOrderedVisible'],
default: false,
}),
hasShiftRight: attr({
compute: '_computeHasShiftRight',
dependencies: ['managerAllOrderedVisible'],
default: false,
}),
/**
* Determines whether `this.thread` should be displayed.
*/
hasThreadView: attr({
compute: '_computeHasThreadView',
dependencies: [
'isFolded',
'isVisible',
'thread',
],
}),
/**
* Determine whether the chat window should be programmatically
* focused by observed component of chat window. Those components
* are responsible to unmark this record afterwards, otherwise
* any re-render will programmatically set focus again!
*/
isDoFocus: attr({
default: false,
}),
/**
* States whether `this` is focused. Useful for visual clue.
*/
isFocused: attr({
default: false,
}),
/**
* Determines whether `this` is folded.
*/
isFolded: attr({
default: false,
}),
/**
* States whether `this` is visible or not. Should be considered
* read-only. Setting this value manually will not make it visible.
* @see `makeVisible`
*/
isVisible: attr({
compute: '_computeIsVisible',
dependencies: [
'managerAllOrderedVisible',
],
}),
manager: many2one('mail.chat_window_manager', {
inverse: 'chatWindows',
}),
managerAllOrderedVisible: one2many('mail.chat_window', {
related: 'manager.allOrderedVisible',
}),
managerVisual: attr({
related: 'manager.visual',
}),
name: attr({
compute: '_computeName',
dependencies: [
'thread',
'threadDisplayName',
],
}),
/**
* Determines the `mail.thread` that should be displayed by `this`.
* If no `mail.thread` is linked, `this` is considered "new message".
*/
thread: one2one('mail.thread', {
inverse: 'chatWindow',
}),
threadDisplayName: attr({
related: 'thread.displayName',
}),
/**
* States the `mail.thread_view` displaying `this.thread`.
*/
threadView: one2one('mail.thread_view', {
related: 'threadViewer.threadView',
}),
/**
* Determines the `mail.thread_viewer` managing the display of `this.thread`.
*/
threadViewer: one2one('mail.thread_viewer', {
default: [['create']],
inverse: 'chatWindow',
isCausal: true,
}),
/**
* This field handle the "order" (index) of the visible chatWindow inside the UI.
*
* Using LTR, the right-most chat window has index 0, and the number is incrementing from right to left.
* Using RTL, the left-most chat window has index 0, and the number is incrementing from left to right.
*/
visibleIndex: attr({
compute: '_computeVisibleIndex',
dependencies: [
'manager',
'managerVisual',
],
}),
visibleOffset: attr({
compute: '_computeVisibleOffset',
dependencies: ['managerVisual'],
}),
};
ChatWindow.modelName = 'mail.chat_window';
return ChatWindow;
}
registerNewModel('mail.chat_window', factory);
});
|