summaryrefslogtreecommitdiff
path: root/addons/web/static/src/js/widgets
diff options
context:
space:
mode:
authorGéry Debongnie <ged@odoo.com>2017-10-06 11:15:46 +0200
committerGéry Debongnie <ged@odoo.com>2017-10-18 13:31:17 +0200
commit1dda32335979869c27ac4f216ccda32d02101b63 (patch)
treebabdb2b607a903e30e30aaf2ec0acf409d628038 /addons/web/static/src/js/widgets
parent1503ec888362216b90099bd5cfb9affc53de20d4 (diff)
[REF] web: refactor notification system
This commit aims to bring the code closer to an ideal state. We want notifications to be a simple service, easy to use, and well documented.
Diffstat (limited to 'addons/web/static/src/js/widgets')
-rw-r--r--addons/web/static/src/js/widgets/notification.js113
1 files changed, 64 insertions, 49 deletions
diff --git a/addons/web/static/src/js/widgets/notification.js b/addons/web/static/src/js/widgets/notification.js
index 6e11b4c8647..21c24c99e1b 100644
--- a/addons/web/static/src/js/widgets/notification.js
+++ b/addons/web/static/src/js/widgets/notification.js
@@ -1,73 +1,88 @@
-odoo.define('web.notification', function (require) {
+odoo.define('web.Notification', function (require) {
"use strict";
-var core = require('web.core');
+/**
+ * Notification
+ *
+ * This file contains the widget which is used to display a warning/information
+ * message in 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 Widget = require('web.Widget');
var Notification = Widget.extend({
template: 'Notification',
events: {
- 'click .o_close': function(e) {
- e.preventDefault();
- this.destroy(true);
- }
+ 'click .o_close': '_onClose',
},
- init: function(parent, title, text, sticky) {
+ /**
+ * @override
+ * @param {Widget} parent
+ * @param {Object} params
+ * @param {string} params.title notification title
+ * @param {string} params.text notification main text
+ * @param {string} params.type 'notification' or 'warning'
+ * @param {boolean} [params.sticky=false] if true, the notification will stay
+ * visible until the user clicks on it.
+ */
+ init: function (parent, params) {
this._super.apply(this, arguments);
- this.title = title;
- this.text = text;
- this.sticky = !!sticky;
+ this.title = params.title;
+ this.text = params.text;
+ this.sticky = !!params.sticky;
+ if (params.type === 'warning') {
+ this.template = 'Warning';
+ }
},
- start: function() {
- this._super.apply(this, arguments);
-
+ /**
+ * @override
+ */
+ start: function () {
var self = this;
- this.$el.animate({opacity: 1.0}, 400, "swing", function() {
- if(!self.sticky) {
- setTimeout(function() {
- self.destroy(true);
- }, 2500);
- }
+ return this._super.apply(this, arguments).then(function () {
+ self.$el.animate({opacity: 1.0}, 400, "swing", function () {
+ if(!self.sticky) {
+ setTimeout(self._destroy.bind(self), 2500);
+ }
+ });
});
},
- destroy: function(animate) {
- if(!animate) {
- return this._super.apply(this, arguments);
- }
- var self = this,
- superDestroy = this._super;
+ //--------------------------------------------------------------------------
+ // Private
+ //--------------------------------------------------------------------------
+
+ /**
+ * This method is used to destroy the widget with a nice animation. We
+ * first perform an animation, then call the real destroy method.
+ *
+ * @private
+ */
+ _destroy: function () {
+ var self = this;
this.$el.animate({opacity: 0.0}, 400, "swing", function() {
- self.$el.animate({height: 0}, 400, "swing", function() {
- superDestroy.call(self);
- });
+ self.$el.animate({height: 0}, 400, "swing", self.destroy.bind(self));
});
},
-});
-var Warning = Notification.extend({
- template: 'Warning',
-});
+ //--------------------------------------------------------------------------
+ // Handlers
+ //--------------------------------------------------------------------------
-var NotificationManager = Widget.extend({
- className: 'o_notification_manager',
-
- display: function(notification) {
- notification.appendTo(this.$el);
- return notification;
- },
- notify: function(title, text, sticky) {
- return this.display(new Notification(this, title, text, sticky));
- },
- warn: function(title, text, sticky) {
- return this.display(new Warning(this, title, text, sticky));
+ /**
+ * @private
+ * @param {MouseEvent} ev
+ */
+ _onClose: function (ev) {
+ ev.preventDefault();
+ this._destroy();
},
});
-return {
- Notification: Notification,
- Warning: Warning,
- NotificationManager: NotificationManager,
-};
+return Notification;
});