Skip to content

Commit

Permalink
[IMP] web_notify: action button name and close
Browse files Browse the repository at this point in the history
- We can now set a button name to the notification action.
- We can set an icon button as well.
- After the button is clicked, the notification gets closed.
  • Loading branch information
chienandalu committed Apr 1, 2024
1 parent 1683823 commit c0b534c
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 39 deletions.
17 changes: 9 additions & 8 deletions web_notify/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Web Notify
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:2ea37128bc36b2b82559d0718dff171b9d2e3dcde596a8c8750d6926821862fc
!! source digest: sha256:4450e6a941785c19484679309731b7caa01167228f50d7d7632ed84db3b37f5b
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |badge1| image:: https://img.shields.io/badge/maturity-Production%2FStable-green.png
Expand Down Expand Up @@ -91,16 +91,17 @@ The notifications can bring interactivity with some buttons.

The reload button is activated when sending the notification with:


The action can be used using the ``action`` keyword:
The action can be used using the ``action`` keyword and we can choose which name to
give to our button with the ``button_name`` key in the action:

.. code-block:: python
action = self.env["ir.actions.act_window"]._for_xml_id('sale.action_orders')
action.update({
'res_id': self.id,
'views': [(False, 'form')],
})
action = self.env["ir.actions.act_window"]._for_xml_id('sale.action_orders')
action.update({
'res_id': self.id,
'views': [(False, 'form')],
'button_name': 'Sales',
})
self.env.user.notify_info('My information message', action=action)
Expand Down
15 changes: 8 additions & 7 deletions web_notify/readme/USAGE.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,17 @@ The notifications can bring interactivity with some buttons.

The reload button is activated when sending the notification with:


The action can be used using the ``action`` keyword:
The action can be used using the ``action`` keyword and we can choose which name to
give to our button with the ``button_name`` key in the action:

.. code-block:: python
action = self.env["ir.actions.act_window"]._for_xml_id('sale.action_orders')
action.update({
'res_id': self.id,
'views': [(False, 'form')],
})
action = self.env["ir.actions.act_window"]._for_xml_id('sale.action_orders')
action.update({
'res_id': self.id,
'views': [(False, 'form')],
'button_name': 'Sales',
})
self.env.user.notify_info('My information message', action=action)
Expand Down
17 changes: 10 additions & 7 deletions web_notify/static/description/index.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
Expand Down Expand Up @@ -366,7 +367,7 @@ <h1 class="title">Web Notify</h1>
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:2ea37128bc36b2b82559d0718dff171b9d2e3dcde596a8c8750d6926821862fc
!! source digest: sha256:4450e6a941785c19484679309731b7caa01167228f50d7d7632ed84db3b37f5b
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Production/Stable" src="https://img.shields.io/badge/maturity-Production%2FStable-green.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/licence-AGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/web/tree/15.0/web_notify"><img alt="OCA/web" src="https://img.shields.io/badge/github-OCA%2Fweb-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/web-15-0/web-15-0-web_notify"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/web&amp;target_branch=15.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
<p>Send instant notification messages to the user in live.</p>
Expand Down Expand Up @@ -425,13 +426,15 @@ <h1><a class="toc-backref" href="#toc-entry-2">Usage</a></h1>
<li>Another allowing to send a window / client action</li>
</ul>
<p>The reload button is activated when sending the notification with:</p>
<p>The action can be used using the <tt class="docutils literal">action</tt> keyword:</p>
<p>The action can be used using the <tt class="docutils literal">action</tt> keyword and we can choose which name to
give to our button with the <tt class="docutils literal">button_name</tt> key in the action:</p>
<pre class="code python literal-block">
<span class="n">action</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">env</span><span class="p">[</span><span class="s2">&quot;ir.actions.act_window&quot;</span><span class="p">]</span><span class="o">.</span><span class="n">_for_xml_id</span><span class="p">(</span><span class="s1">'sale.action_orders'</span><span class="p">)</span><span class="w">
</span> <span class="n">action</span><span class="o">.</span><span class="n">update</span><span class="p">({</span><span class="w">
</span> <span class="s1">'res_id'</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">id</span><span class="p">,</span><span class="w">
</span> <span class="s1">'views'</span><span class="p">:</span> <span class="p">[(</span><span class="kc">False</span><span class="p">,</span> <span class="s1">'form'</span><span class="p">)],</span><span class="w">
</span> <span class="p">})</span><span class="w">
<span class="n">action</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">env</span><span class="p">[</span><span class="s2">&quot;ir.actions.act_window&quot;</span><span class="p">]</span><span class="o">.</span><span class="n">_for_xml_id</span><span class="p">(</span><span class="s1">'sale.action_orders'</span><span class="p">)</span><span class="w">
</span><span class="n">action</span><span class="o">.</span><span class="n">update</span><span class="p">({</span><span class="w">
</span> <span class="s1">'res_id'</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">id</span><span class="p">,</span><span class="w">
</span> <span class="s1">'views'</span><span class="p">:</span> <span class="p">[(</span><span class="kc">False</span><span class="p">,</span> <span class="s1">'form'</span><span class="p">)],</span><span class="w">
</span> <span class="s1">'button_name'</span><span class="p">:</span> <span class="s1">'Sales'</span><span class="p">,</span><span class="w">
</span><span class="p">})</span><span class="w">
</span><span class="bp">self</span><span class="o">.</span><span class="n">env</span><span class="o">.</span><span class="n">user</span><span class="o">.</span><span class="n">notify_info</span><span class="p">(</span><span class="s1">'My information message'</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="n">action</span><span class="p">)</span>
</pre>
<div class="figure">
Expand Down
33 changes: 16 additions & 17 deletions web_notify/static/src/js/services/notification_services.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,27 @@ export const webNotificationService = {
browser.clearTimeout(notif)
);
webNotifTimeouts = {};
notifications.forEach(function (notif) {
browser.setTimeout(function () {
let buttons = [];

if (notif.action) {
buttons = [
{
name: env._t("Open"),
primary: true,
onClick: async () => {
await action.doAction(notif.action);
},
},
];
}
notification.add(Markup(notif.message), {
notifications.forEach((notif) => {
browser.setTimeout(() => {
const notificationRemove = notification.add(Markup(notif.message), {
title: notif.title,
type: notif.type,
sticky: notif.sticky,
className: notif.className,
messageIsHtml: notif.html,
buttons: buttons,
buttons: notif.action
? [
{
name: notif.action.button_name || env._t("Open"),
primary: true,
onClick: async function () {
await action.doAction(notif.action);
notificationRemove();
},
icon: notif.action.button_icon || undefined,
},
]
: [],
});
});
});
Expand Down

0 comments on commit c0b534c

Please sign in to comment.