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
|
odoo.define('mail/static/src/components/composer_suggested_recipient/composer_suggested_recipient.js', function (require) {
'use strict';
const useShouldUpdateBasedOnProps = require('mail/static/src/component_hooks/use_should_update_based_on_props/use_should_update_based_on_props.js');
const useStore = require('mail/static/src/component_hooks/use_store/use_store.js');
const useUpdate = require('mail/static/src/component_hooks/use_update/use_update.js');
const { FormViewDialog } = require('web.view_dialogs');
const { ComponentAdapter } = require('web.OwlCompatibility');
const { Component } = owl;
const { useRef } = owl.hooks;
class FormViewDialogComponentAdapter extends ComponentAdapter {
renderWidget() {
// Ensure the dialog is properly reconstructed. Without this line, it is
// impossible to open the dialog again after having it closed a first
// time, because the DOM of the dialog has disappeared.
return this.willStart();
}
}
const components = {
FormViewDialogComponentAdapter,
};
class ComposerSuggestedRecipient extends Component {
constructor(...args) {
super(...args);
this.id = _.uniqueId('o_ComposerSuggestedRecipient_');
useShouldUpdateBasedOnProps();
useStore(props => {
const suggestedRecipientInfo = this.env.models['mail.suggested_recipient_info'].get(props.suggestedRecipientLocalId);
const partner = suggestedRecipientInfo && suggestedRecipientInfo.partner;
return {
partner: partner && partner.__state,
suggestedRecipientInfo: suggestedRecipientInfo && suggestedRecipientInfo.__state,
};
});
useUpdate({ func: () => this._update() });
/**
* Form view dialog class. Useful to reference it in the template.
*/
this.FormViewDialog = FormViewDialog;
/**
* Reference of the checkbox. Useful to know whether it was checked or
* not, to properly update the corresponding state in the record or to
* prompt the user with the partner creation dialog.
*/
this._checkboxRef = useRef('checkbox');
/**
* Reference of the partner creation dialog. Useful to open it, for
* compatibility with old code.
*/
this._dialogRef = useRef('dialog');
/**
* Whether the dialog is currently open. `_dialogRef` cannot be trusted
* to know if the dialog is open due to manually calling `open` and
* potential out of sync with component adapter.
*/
this._isDialogOpen = false;
this._onDialogSaved = this._onDialogSaved.bind(this);
}
//--------------------------------------------------------------------------
// Public
//--------------------------------------------------------------------------
/**
* @returns {string|undefined}
*/
get ADD_AS_RECIPIENT_AND_FOLLOWER_REASON() {
if (!this.suggestedRecipientInfo) {
return undefined;
}
return this.env._t(_.str.sprintf(
"Add as recipient and follower (reason: %s)",
this.suggestedRecipientInfo.reason
));
}
/**
* @returns {string}
*/
get PLEASE_COMPLETE_CUSTOMER_S_INFORMATION() {
return this.env._t("Please complete customer's information");
}
/**
* @returns {mail.suggested_recipient_info}
*/
get suggestedRecipientInfo() {
return this.env.models['mail.suggested_recipient_info'].get(this.props.suggestedRecipientInfoLocalId);
}
//--------------------------------------------------------------------------
// Private
//--------------------------------------------------------------------------
/**
* @private
*/
_update() {
if (this._checkboxRef.el && this.suggestedRecipientInfo) {
this._checkboxRef.el.checked = this.suggestedRecipientInfo.isSelected;
}
}
//--------------------------------------------------------------------------
// Handler
//--------------------------------------------------------------------------
/**
* @private
*/
_onChangeCheckbox() {
const isChecked = this._checkboxRef.el.checked;
this.suggestedRecipientInfo.update({ isSelected: isChecked });
if (!this.suggestedRecipientInfo.partner) {
// Recipients must always be partners. On selecting a suggested
// recipient that does not have a partner, the partner creation form
// should be opened.
if (isChecked && this._dialogRef && !this._isDialogOpen) {
this._isDialogOpen = true;
this._dialogRef.comp.widget.on('closed', this, () => {
this._isDialogOpen = false;
});
this._dialogRef.comp.widget.open();
}
}
}
/**
* @private
*/
_onDialogSaved() {
const thread = this.suggestedRecipientInfo && this.suggestedRecipientInfo.thread;
if (!thread) {
return;
}
thread.fetchAndUpdateSuggestedRecipients();
}
}
Object.assign(ComposerSuggestedRecipient, {
components,
props: {
suggestedRecipientInfoLocalId: String,
},
template: 'mail.ComposerSuggestedRecipient',
});
return ComposerSuggestedRecipient;
});
|