@@ -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,12 @@ 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 = [ ] ;
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+ mru_ids = self . get_mru_ids ( ) ;
164+ if ( ! _ ( mru_ids ) . isEmpty ( ) ) {
165+ domain_list . push ( [ 'id' , 'in' , mru_ids ] ) ;
164166 in_search_mru = true ;
165167 }
166168 }
@@ -197,6 +199,14 @@ odoo.define('web_m2x_options.web_m2x_options', function (require) {
197199 }
198200 return val ;
199201 } ) ;
202+ // If we are in a mru search, reorder the result list in the
203+ // same order as the one stored to keep the saved preference
204+ // order (The most recent ones first)
205+ if ( in_search_mru ) {
206+ values = _ ( values ) . sortBy ( function ( item ) {
207+ return mru_ids . indexOf ( item . id ) ;
208+ } ) ;
209+ }
200210
201211 // Search result value colors
202212 if ( self . colors && self . field_color ) {
@@ -299,15 +309,15 @@ odoo.define('web_m2x_options.web_m2x_options', function (require) {
299309 return def ;
300310 } ,
301311
302- update_mru_list : function ( ) {
312+ update_mru_ids : function ( ) {
303313 var self = this ,
304314 mru_option = 'web_m2x_options_mru' ;
305315 var key = self . compute_mru_key ( ) ;
306316 // check if the localstorage has some items for the current model
307317 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 ] ;
318+ var restore_mru_ids = JSON . parse ( localStorage . getItem ( mru_option ) ) ;
319+ if ( restore_mru_ids [ key ] ) {
320+ var queue = restore_mru_ids [ key ] ;
311321 // if the element doesn't exist in the stack
312322 if ( queue . indexOf ( self . get_value ( true ) ) < 0 && self . get_value ( true ) ) {
313323 if ( queue . length < 5 ) {
@@ -319,7 +329,7 @@ odoo.define('web_m2x_options.web_m2x_options', function (require) {
319329 // add the new element at the beginning
320330 queue . unshift ( self . get_value ( true ) ) ;
321331 }
322- restore_mru_list [ key ] = queue ;
332+ restore_mru_ids [ key ] = queue ;
323333 } else {
324334 // if the element already exist in the stack
325335 if ( queue . indexOf ( self . get_value ( true ) ) >= 0 && self . get_value ( true ) ) {
@@ -333,10 +343,10 @@ odoo.define('web_m2x_options.web_m2x_options', function (require) {
333343 } else {
334344 // if the element is the first one
335345 if ( self . get_value ( true ) ) {
336- restore_mru_list [ key ] = [ self . get_value ( true ) ] ;
346+ restore_mru_ids [ key ] = [ self . get_value ( true ) ] ;
337347 }
338348 }
339- localStorage . setItem ( mru_option , JSON . stringify ( restore_mru_list ) ) ;
349+ localStorage . setItem ( mru_option , JSON . stringify ( restore_mru_ids ) ) ;
340350 } else {
341351 // first time to create an entry in the localstorage
342352 if ( self . get_value ( true ) ) {
@@ -356,7 +366,7 @@ odoo.define('web_m2x_options.web_m2x_options', function (require) {
356366 search_mru = self . is_option_set ( self . view . ir_options [ 'web_m2x_options.search_mru' ] ) ;
357367
358368 if ( can_search_mru || ( search_mru_undef && search_mru ) ) {
359- self . update_mru_list ( ) ;
369+ self . update_mru_ids ( ) ;
360370 }
361371 }
362372 }
0 commit comments