changeset 2f272a66182b in sao:default
details: https://hg.tryton.org/sao?cmd=changeset;node=2f272a66182b
description:
        Manage deletable and writable state from ir.rule

        issue9357
        review290101002
diffstat:

 CHANGELOG        |   1 +
 src/model.js     |  16 +++++++++++++++-
 src/screen.js    |  23 +++++++++++++++++++++--
 src/tab.js       |   6 ++++++
 src/view/form.js |   9 ++++++---
 src/window.js    |   5 +++--
 6 files changed, 52 insertions(+), 8 deletions(-)

diffs (187 lines):

diff -r f3d0a8d2b7f7 -r 2f272a66182b CHANGELOG
--- a/CHANGELOG Tue Sep 22 10:18:39 2020 +0200
+++ b/CHANGELOG Wed Sep 23 10:04:19 2020 +0200
@@ -1,3 +1,4 @@
+* Manage deletable and writable state
 * Support e-mail template
 * Always encode CSV export in UTF-8
 * Use tempusdominus and Popper for date time picker
diff -r f3d0a8d2b7f7 -r 2f272a66182b src/model.js
--- a/src/model.js      Tue Sep 22 10:18:39 2020 +0200
+++ b/src/model.js      Wed Sep 23 10:04:19 2020 +0200
@@ -551,6 +551,8 @@
             this._loaded = {};
             this.fields = {};
             this._timestamp = null;
+            this._write = true;
+            this._delete = true;
             this.resources = null;
             this.button_clicks = {};
             this.links_counts = {};
@@ -685,6 +687,8 @@
                 fnames_to_fetch.push('rec_name');
             }
             fnames_to_fetch.push('_timestamp');
+            fnames_to_fetch.push('_write');
+            fnames_to_fetch.push('_delete');
 
             var context = jQuery.extend({}, this.get_context());
             if (loading == 'eager') {
@@ -813,6 +817,10 @@
                     }
                     continue;
                 }
+                if (name == '_write' || name == '_delete') {
+                    this[name] = value;
+                    continue;
+                }
                 if (!(name in this.model.fields)) {
                     if (name == 'rec_name') {
                         this._values[name] = value;
@@ -1342,7 +1350,13 @@
             return Boolean(~this.group.record_removed.indexOf(this));
         },
         get readonly() {
-            return this.deleted || this.removed || this.exception;
+            return (this.deleted ||
+                this.removed ||
+                this.exception ||
+                !this._write);
+        },
+        get deletable() {
+            return this._delete;
         },
         get identity() {
             return JSON.stringify(
diff -r f3d0a8d2b7f7 -r 2f272a66182b src/screen.js
--- a/src/screen.js     Tue Sep 22 10:18:39 2020 +0200
+++ b/src/screen.js     Wed Sep 23 10:04:19 2020 +0200
@@ -813,6 +813,17 @@
             // switch_view to avoid unnecessary call to fields_view_get by
             // domain_parser.
         },
+        get readonly() {
+            var readonly_records = this.selected_records.some(function(r) {
+                return r.readonly;
+            });
+            return this.attributes.readonly || readonly_records;
+        },
+        get deletable() {
+            return this.selected_records.every(function(r) {
+                return r.deletable;
+            });
+        },
         load_next_view: function() {
             if (!jQuery.isEmptyObject(this.view_to_load)) {
                 var view_id;
@@ -1181,7 +1192,7 @@
                 context = this.context;
             }
             var group = new Sao.Group(this.model, context, []);
-            group.readonly = this.attributes.readonly || false;
+            group.readonly = this.attributes.readonly;
             this.set_group(group);
         },
         get current_record() {
@@ -1328,6 +1339,12 @@
             this.set_cursor(false, false);
             return view.display();
         },
+        get selected_records() {
+            if (this.current_view) {
+                return this.current_view.selected_records;
+            }
+            return [];
+        },
         clear: function() {
             this.current_record = null;
             this.group.clear();
@@ -1902,7 +1919,9 @@
                     this.new_();
                 }
             } else if (action == 'delete') {
-                if (access['delete']) {
+                if (access['delete'] && (
+                        this.current_record ? this.current_record.deletable :
+                        true)) {
                     this.remove(!this.group.parent, false, !this.group.parent);
                 }
             } else if (action == 'remove') {
diff -r f3d0a8d2b7f7 -r 2f272a66182b src/tab.js
--- a/src/tab.js        Tue Sep 22 10:18:39 2020 +0200
+++ b/src/tab.js        Wed Sep 23 10:04:19 2020 +0200
@@ -1288,6 +1288,12 @@
                 }.bind(this));
                 this.buttons.switch_.prop('disabled',
                     this.attributes.view_ids > 1);
+
+                this.menu_buttons.delete_.toggleClass(
+                    'disabled', !this.screen.deletable);
+                this.menu_buttons.save.toggleClass(
+                    'disabled', this.screen.readonly);
+
                 var msg = name + ' / ' + data[1];
                 if (data[1] < data[2]) {
                     msg += Sao.i18n.gettext(' of ') + data[2];
diff -r f3d0a8d2b7f7 -r 2f272a66182b src/view/form.js
--- a/src/view/form.js  Tue Sep 22 10:18:39 2020 +0200
+++ b/src/view/form.js  Wed Sep 23 10:04:19 2020 +0200
@@ -3034,6 +3034,7 @@
                 o2m_size = null;
                 size_limit = false;
             }
+            var deletable = this.screen.deletable;
             var create = this.attributes.create;
             if (create === undefined) {
                 create = true;
@@ -3046,7 +3047,7 @@
                 delete_ = true;
             }
             this.but_del.prop('disabled', this._readonly || !delete_ ||
-                !access['delete'] || !this._position);
+                !access['delete'] || !this._position || !deletable);
             this.but_undel.prop('disabled', this._readonly || size_limit ||
                  !this._position);
             this.but_open.prop('disabled', !access.read || !this._position);
@@ -3186,7 +3187,8 @@
         },
         remove: function(event_) {
             var access = Sao.common.MODELACCESS.get(this.screen.model_name);
-            if (!access.write || !access.read) {
+            var writable = !this.screen.readonly;
+            if (!access.write || !access.read || !writable) {
                 return;
             }
             this.screen.remove(false, true, false);
@@ -3301,7 +3303,8 @@
             return this.edit();
         },
         delete_: function(event_) {
-            if (!Sao.common.MODELACCESS.get(this.screen.model_name)['delete']) 
{
+            if (!Sao.common.MODELACCESS.get(this.screen.model_name)['delete'] 
||
+                !this.screen.deletable) {
                 return;
             }
             this.screen.remove(false, false, false);
diff -r f3d0a8d2b7f7 -r 2f272a66182b src/window.js
--- a/src/window.js     Tue Sep 22 10:18:39 2020 +0200
+++ b/src/window.js     Wed Sep 23 10:04:19 2020 +0200
@@ -303,7 +303,8 @@
         record_label: function(data) {
             var name = '_';
             var access = Sao.common.MODELACCESS.get(this.screen.model_name);
-            var readonly = this.screen.group.readonly;
+            var deletable = this.screen.deletable;
+            var readonly = this.screen.group.readonly || this.screen.readonly;
             if (data[0] >= 1) {
                 name = data[0];
                 if (this.domain) {
@@ -311,7 +312,7 @@
                 }
                 this.but_next.prop('disabled', data[0] >= data[1]);
                 this.but_previous.prop('disabled', data[0] <= 1);
-                if (access.delete && !readonly) {
+                if (access.delete && !readonly && deletable) {
                     this.but_del.prop('disabled', false);
                     this.but_undel.prop('disabled', false);
                 }

Reply via email to