Subject: changeset in sao:4.8 Use standard Array.sort and do not update 
search_count when only ids
From: Cédric Krier <[email protected]>

changeset 2c8a6f1d7fdc in sao:4.8
details: https://hg.tryton.org/sao?cmd=changeset;node=2c8a6f1d7fdc
description:
        Use standard Array.sort and do not update search_count when only ids

        The current implementation of Group.sort is not correct because it does 
not
        guarantee to replace all the records if the ordered list does not 
contain all
        the existing records. So it is better to use the standard Array.sort 
method.
        Also the search_count should not be updated when doing a search only 
for ids
        as the number of record does not change.

        issue8042
        review50731002
        (grafted from f10e3870cc1bb7d520c0cd51574abf4f7ddeef23)
diffstat:

 src/model.js     |  11 -----------
 src/screen.js    |  28 +++++++++++++++-------------
 src/view/tree.js |  14 +++++++++++++-
 3 files changed, 28 insertions(+), 25 deletions(-)

diffs (84 lines):

diff -r 0c5b0cca528b -r 2c8a6f1d7fdc src/model.js
--- a/src/model.js      Sun Feb 03 20:19:41 2019 +0100
+++ b/src/model.js      Sun Feb 03 20:29:41 2019 +0100
@@ -461,17 +461,6 @@
             };
             return jQuery.when().then(browse_child);
         };
-        array.sort = function(ids) {
-                var id2record = {};
-                this.forEach(function(record) {
-                    id2record[record.id] = record;
-                });
-                ids.filter(function(id) {
-                    return id in id2record;
-                }).forEach(function(id, i){
-                    this[i] = id2record[id];
-                }.bind(this));
-        };
         return array;
     };
 
diff -r 0c5b0cca528b -r 2c8a6f1d7fdc src/screen.js
--- a/src/screen.js     Sun Feb 03 20:19:41 2019 +0100
+++ b/src/screen.js     Sun Feb 03 20:29:41 2019 +0100
@@ -885,20 +885,22 @@
             return this.model.execute(
                 'search', [domain, this.offset, this.limit, this.order],
                 context).then(function(ids) {
-                    this.search_count = ids.length;
                     var count_prm = jQuery.when(this.search_count);
-                    if ((!only_ids) &&
-                        ((this.limit !== null) &&
-                            (ids.length == this.limit))) {
-                        count_prm = this.model.execute(
-                            'search_count', [domain], context)
-                            .then(function(count) {
-                                this.search_count = count;
-                                return this.search_count;
-                            }.bind(this), function() {
-                                this.search_count = 0;
-                                return this.search_count;
-                            }.bind(this));
+                    if (!only_ids) {
+                        if ((this.limit !== null) &&
+                            (ids.length == this.limit)) {
+                            count_prm = this.model.execute(
+                                'search_count', [domain], context)
+                                .then(function(count) {
+                                    this.search_count = count;
+                                    return this.search_count;
+                                }.bind(this), function() {
+                                    this.search_count = 0;
+                                    return this.search_count;
+                                }.bind(this));
+                        } else {
+                            this.search_count = ids.length;
+                        }
                     }
                     return count_prm.then(function(count) {
                         this.screen_container.but_next.prop('disabled',
diff -r 0c5b0cca528b -r 2c8a6f1d7fdc src/view/tree.js
--- a/src/view/tree.js  Sun Feb 03 20:19:41 2019 +0100
+++ b/src/view/tree.js  Sun Feb 03 20:29:41 2019 +0100
@@ -293,7 +293,19 @@
                     (this.screen.group.parent)) {
                 this.screen.search_filter(search_string, true).then(
                 function(ids) {
-                    this.screen.group.sort(ids);
+                    this.screen.group.sort(function(a, b) {
+                        a = ids.indexOf(a.id);
+                        a = a < 0 ? ids.length : a;
+                        b = ids.indexOf(b.id);
+                        b = b < 0 ? ids.length : b;
+                        if (a < b) {
+                            return -1;
+                        } else if (a > b) {
+                            return 1;
+                        } else {
+                            return 0;
+                        }
+                    });
                     this.screen.display();
                 }.bind(this));
             } else {

Reply via email to