@@ -113,18 +113,19 @@ odoo.define('web_m2x_options.web_m2x_options', function (require) {
113113 return db + "/" + model + "/" + view_id + "/" + self . name ;
114114 } ,
115115
116- get_search_mru : function ( ) {
116+ get_mru_ids : function ( ) {
117117 var mru_option = 'web_m2x_options_mru' ,
118118 self = this ;
119- var restore_mru_list = JSON . parse ( localStorage . getItem ( mru_option ) ) ,
119+ var restore_mru_ids = JSON . parse ( localStorage . getItem ( mru_option ) ) ,
120120 key = self . compute_mru_key ( ) ;
121- if ( restore_mru_list ) {
122- if ( ! _ . isUndefined ( restore_mru_list [ key ] ) ) {
123- return [ 'id' , 'in' , restore_mru_list [ key ] ] ;
121+ if ( restore_mru_ids ) {
122+ if ( ! _ . isUndefined ( restore_mru_ids [ key ] ) ) {
123+ return restore_mru_ids [ key ] ;
124124 }
125125 }
126126 return [ ] ;
127127 } ,
128+
128129 get_search_result : function ( search_val ) {
129130 var Objects = new Model ( this . field . relation ) ;
130131 var def = $ . Deferred ( ) ;
@@ -156,11 +157,11 @@ odoo.define('web_m2x_options.web_m2x_options', function (require) {
156157 search_mru_undef = _ . isUndefined ( self . options . search_mru ) ,
157158 search_mru = self . is_option_set ( self . view . ir_options [ 'web_m2x_options.search_mru' ] ) ;
158159
159- var mru_list = self . get_search_mru ( ) ;
160+ var mru_ids = self . get_mru_ids ( ) ;
160161 var in_search_mru = false ;
161162 if ( search_val == "" && ( can_search_mru || ( search_mru_undef && search_mru ) ) ) {
162- if ( ! _ ( mru_list ) . isEmpty ( ) ) {
163- domain_list . push ( mru_list ) ;
163+ if ( ! _ ( mru_ids ) . isEmpty ( ) ) {
164+ domain_list . push ( [ 'id' , 'in' , mru_ids ] ) ;
164165 in_search_mru = true ;
165166 }
166167 }
@@ -197,6 +198,14 @@ odoo.define('web_m2x_options.web_m2x_options', function (require) {
197198 }
198199 return val ;
199200 } ) ;
201+ // If we are in a mru search, reorder the result list in the
202+ // same order as the one stored to keep the saved preference
203+ // order (The most recent ones first)
204+ if ( in_search_mru ) {
205+ values = _ ( values ) . sortBy ( function ( item ) {
206+ return mru_ids . indexOf ( item . id ) ;
207+ } ) ;
208+ }
200209
201210 // Search result value colors
202211 if ( self . colors && self . field_color ) {
@@ -299,15 +308,15 @@ odoo.define('web_m2x_options.web_m2x_options', function (require) {
299308 return def ;
300309 } ,
301310
302- update_mru_list : function ( ) {
311+ update_mru_ids : function ( ) {
303312 var self = this ,
304313 mru_option = 'web_m2x_options_mru' ;
305314 var key = self . compute_mru_key ( ) ;
306315 // check if the localstorage has some items for the current model
307316 if ( localStorage . getItem ( mru_option ) ) {
308- var restore_mru_list = JSON . parse ( localStorage . getItem ( mru_option ) ) ;
309- if ( restore_mru_list [ key ] ) {
310- var queue = restore_mru_list [ key ] ;
317+ var restore_mru_ids = JSON . parse ( localStorage . getItem ( mru_option ) ) ;
318+ if ( restore_mru_ids [ key ] ) {
319+ var queue = restore_mru_ids [ key ] ;
311320 // if the element doesn't exist in the stack
312321 if ( queue . indexOf ( self . get_value ( true ) ) < 0 && self . get_value ( true ) ) {
313322 if ( queue . length < 5 ) {
@@ -319,7 +328,7 @@ odoo.define('web_m2x_options.web_m2x_options', function (require) {
319328 // add the new element at the beginning
320329 queue . unshift ( self . get_value ( true ) ) ;
321330 }
322- restore_mru_list [ key ] = queue ;
331+ restore_mru_ids [ key ] = queue ;
323332 } else {
324333 // if the element already exist in the stack
325334 if ( queue . indexOf ( self . get_value ( true ) ) >= 0 && self . get_value ( true ) ) {
@@ -333,10 +342,10 @@ odoo.define('web_m2x_options.web_m2x_options', function (require) {
333342 } else {
334343 // if the element is the first one
335344 if ( self . get_value ( true ) ) {
336- restore_mru_list [ key ] = [ self . get_value ( true ) ] ;
345+ restore_mru_ids [ key ] = [ self . get_value ( true ) ] ;
337346 }
338347 }
339- localStorage . setItem ( mru_option , JSON . stringify ( restore_mru_list ) ) ;
348+ localStorage . setItem ( mru_option , JSON . stringify ( restore_mru_ids ) ) ;
340349 } else {
341350 // first time to create an entry in the localstorage
342351 if ( self . get_value ( true ) ) {
@@ -356,7 +365,7 @@ odoo.define('web_m2x_options.web_m2x_options', function (require) {
356365 search_mru = self . is_option_set ( self . view . ir_options [ 'web_m2x_options.search_mru' ] ) ;
357366
358367 if ( can_search_mru || ( search_mru_undef && search_mru ) ) {
359- self . update_mru_list ( ) ;
368+ self . update_mru_ids ( ) ;
360369 }
361370 }
362371 }
0 commit comments