changeset bf4e530c1069 in sao:6.0
details: https://hg.tryton.org/sao?cmd=changeset&node=bf4e530c1069
description:
        Disable toolbar buttons based on record message

        In order to update the disabled property, we must store the button tag 
in
        Sao.Tab.buttons.
        The property must be computed by taking into account if the dropdown has
        children.
        And the resource buttons must check against undefined record_id.

        issue10410
        review355431002
        (grafted from a7d07e635e86983fee5ef8decb4e5294ba72f78a)
diffstat:

 src/tab.js |  42 +++++++++++++++++++++++++++++++-----------
 1 files changed, 31 insertions(+), 11 deletions(-)

diffs (112 lines):

diff -r c5fc52a8bf75 -r bf4e530c1069 src/tab.js
--- a/src/tab.js        Wed Sep 01 22:48:53 2021 +0200
+++ b/src/tab.js        Thu Sep 02 23:17:33 2021 +0200
@@ -525,7 +525,7 @@
                 ['print', 'tryton-print',
                      Sao.i18n.gettext('Print report')]
                 ].forEach(function(menu_action) {
-                    var button = jQuery('<div/>', {
+                    var dropdown = jQuery('<div/>', {
                         'class': 'btn-group dropdown',
                         'role': 'group'
                     })
@@ -551,8 +551,9 @@
                         'aria-labelledby': menu_action[0]
                     }))
                     .insertBefore(toolbar.find('button#email'));
+                    var button = dropdown.find('button');
                     buttons[menu_action[0]] = button;
-                    var dropdown = button
+                    dropdown
                         .on('show.bs.dropdown', function() {
                             jQuery(this).parents('.btn-group').removeClass(
                                     'hidden-xs');
@@ -560,7 +561,7 @@
                             jQuery(this).parents('.btn-group').addClass(
                                     'hidden-xs');
                         });
-                    var menu = button.find('.dropdown-menu');
+                    var menu = dropdown.find('.dropdown-menu');
                     button.click(function() {
                         menu.find([
                             '.' + menu_action[0] + '_button',
@@ -694,12 +695,18 @@
                         .appendTo(menu);
                     }.bind(this));
 
+                    if (menu_action[0] != 'action') {
+                        button._can_be_sensitive = Boolean(
+                            menu.children().length);
+                    }
+
                     if (menu_action[0] == 'print') {
                         if (toolbars.exports.length && toolbars.print.length) {
                             menu.append(jQuery('<li/>', {
                                 'role': 'separator',
                                 'class': 'divider',
                             }));
+                            button._can_be_sensitive = true;
                         }
                         toolbars.exports.forEach(function(export_) {
                             var item = jQuery('<li/>', {
@@ -1383,7 +1390,8 @@
                 resources = {};
             }
             var record_id = this.screen.get_id();
-            var disabled = record_id < 0 || record_id === null;
+            var disabled = (
+                record_id < 0 || record_id === null || record_id === 
undefined);
 
             var update = function(name, title, text, color) {
                 var button = this.buttons[name];
@@ -1439,13 +1447,25 @@
                 if (data[0] !== 0) {
                     name = data[0];
                 }
-                var buttons = ['print', 'relate', 'attach'];
+                var buttons = ['print', 'relate', 'email', 'save', 'attach'];
                 buttons.forEach(function(button_id){
                     var button = this.buttons[button_id];
-                    if (button) {
-                        var disabled = button.is(':disabled');
-                        button.prop('disabled', disabled || data[0] === 0);
+                    var can_be_sensitive = button._can_be_sensitive;
+                    if (can_be_sensitive === undefined) {
+                        can_be_sensitive = true;
                     }
+                    if ((button_id == 'print') ||
+                        (button_id == 'relate') ||
+                        (button_id == 'email')) {
+                        can_be_sensitive |= this.screen.get_buttons().some(
+                            function(button) {
+                                var keyword = button.attributes.keyword || 
'action';
+                                return keyword == button_id;
+                            });
+                    } else if (button_id == 'save') {
+                        can_be_sensitive &= !this.screen.readonly;
+                    }
+                    button.prop('disabled', !(data[0] && can_be_sensitive));
                 }.bind(this));
                 this.buttons.switch_.prop('disabled',
                     this.attributes.view_ids > 1);
@@ -1467,17 +1487,17 @@
         },
         action: function() {
             window.setTimeout(function() {
-                this.buttons.action.find('button').click();
+                this.buttons.action.click();
             }.bind(this));
         },
         relate: function() {
             window.setTimeout(function() {
-                this.buttons.relate.find('button').click();
+                this.buttons.relate.click();
             }.bind(this));
         },
         print: function() {
             window.setTimeout(function() {
-                this.buttons.print.find('button').click();
+                this.buttons.print.click();
             }.bind(this));
         },
         export: function(){

Reply via email to