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
|
odoo.define('web.Notification', function (require) {
'use strict';
var Widget = require('web.Widget');
/**
* Widget which is used to display a warning/information message on the top
* right of the screen.
*
* If you want to display such a notification, you probably do not want to do it
* by importing this file. The proper way is to use the do_warn or do_notify
* methods on the Widget class.
*/
var Notification = Widget.extend({
template: 'Notification',
events: {
'hidden.bs.toast': '_onClose',
'click .o_notification_buttons button': '_onClickButton',
'mouseenter': '_onMouseEnter',
'mouseleave': '_onMouseLeave',
},
_autoCloseDelay: 4000,
_animation: true,
/**
* @override
* @param {Widget} parent
* @param {Object} params
* @param {string} params.title
* @param {string} params.subtitle
* @param {string} [params.message]
* @param {string} [params.type='warning'] 'info', 'success', 'warning', 'danger' or ''
* @param {boolean} [params.sticky=false] if true, the notification will
* stay visible until the user clicks on it.
* @param {string} [params.className]
* @param {function} [params.onClose] callback when the user click on the x
* or when the notification is auto close (no sticky)
* @param {Object[]} params.buttons
* @param {function} params.buttons[0].click callback on click
* @param {boolean} [params.buttons[0].primary] display the button as primary
* @param {string} [params.buttons[0].text] button label
* @param {string} [params.buttons[0].icon] font-awsome className or image src
*/
init: function (parent, params) {
this._super.apply(this, arguments);
this.title = params.title;
this.subtitle = params.subtitle;
this.message = params.message;
this.buttons = params.buttons || [];
this.sticky = !!this.buttons.length || !!params.sticky;
this.type = params.type === undefined ? 'warning' : params.type;
this.className = params.className || '';
this._closeCallback = params.onClose;
if (this.type === 'danger') {
this.icon = 'fa-exclamation';
this.className += ' bg-danger';
} else if (this.type === 'warning') {
this.icon = 'fa-lightbulb-o';
this.className += ' bg-warning';
} else if (this.type === 'success') {
this.icon = 'fa-check';
this.className += ' bg-success';
} else if (this.type === 'info') {
this.icon = 'fa-info';
this.className += ' bg-info';
}
if (this.buttons && this.buttons.length) {
this.icon = 'fa-question-circle-o';
}
},
/**
* @override
*/
start: function () {
this.$el.toast({
animation: this._animation,
autohide: false,
});
void this.$el[0].offsetWidth; // Force a paint refresh before showing the toast
if (!this.sticky) {
this.autohide = _.cancellableThrottleRemoveMeSoon(this.close, this._autoCloseDelay, {leading: false});
this.$el.on('shown.bs.toast', () => {
this.autohide();
});
}
this.$el.toast('show');
return this._super.apply(this, arguments);
},
/**
* @override
*/
destroy: function () {
this.$el.toast('dispose');
this._super.apply(this, arguments);
},
//--------------------------------------------------------------------------
// Public
//--------------------------------------------------------------------------
/**
* Destroys the widget with a nice animation.
*
* @private
* @param {boolean} [silent=false] if true, the notification does not call
* _closeCallback method
*/
close: function (silent) {
this.silent = silent;
this.$el.toast('hide');
// Make 'close' work if the notification is not shown yet but will be.
// Should not be needed but the calendar notification system is an
// example of feature that does not work without this yet.
var self = this;
this.$el.one('shown.bs.toast', function () {
self.$el.toast('hide');
});
},
//--------------------------------------------------------------------------
// Handlers
//--------------------------------------------------------------------------
/**
* @private
* @param {MouseEvent} ev
*/
_onClickButton: function (ev) {
ev.preventDefault();
if (this._buttonClicked) {
return;
}
this._buttonClicked = true;
var index = $(ev.currentTarget).index();
var button = this.buttons[index];
if (button.click) {
button.click();
}
this.close(true);
},
/**
* @private
* @param {Event} ev
*/
_onClose: function (ev) {
this.trigger_up('close');
if (!this.silent && !this._buttonClicked) {
if (this._closeCallback) {
this._closeCallback();
}
}
this.destroy();
},
/**
* @private
*/
_onMouseEnter: function () {
if (!this.sticky) {
this.autohide.cancel();
}
},
/**
* @private
*/
_onMouseLeave: function () {
if (!this.sticky) {
this.autohide();
}
},
});
return Notification;
});
|