@@ -17,7 +17,8 @@ odoo.define('web_m2x_options.web_m2x_options', function (require) {
1717 'web_m2x_options.create_edit' ,
1818 'web_m2x_options.limit' ,
1919 'web_m2x_options.search_more' ,
20- 'web_m2x_options.m2o_dialog' , ] ;
20+ 'web_m2x_options.m2o_dialog' ,
21+ 'web_m2x_options.search_mru' , ] ;
2122
2223 // In odoo 9.c FielMany2One is not exposed by form_relational
2324 // To bypass this limitation we use the widget registry to get the
@@ -104,6 +105,27 @@ odoo.define('web_m2x_options.web_m2x_options', function (require) {
104105 }
105106 } ,
106107
108+ compute_mru_key : function ( ) {
109+ var self = this ,
110+ model = self . view . model ,
111+ db = self . session . db ,
112+ view_id = self . view . fields_view . view_id || self . view . dataset . parent_view . fields_view . view_id ;
113+ return db + "/" + model + "/" + view_id + "/" + self . name ;
114+ } ,
115+
116+ get_mru_ids : function ( ) {
117+ var mru_option = 'web_m2x_options_mru' ,
118+ self = this ;
119+ var restore_mru_ids = JSON . parse ( localStorage . getItem ( mru_option ) ) ,
120+ key = self . compute_mru_key ( ) ;
121+ if ( restore_mru_ids ) {
122+ if ( ! _ . isUndefined ( restore_mru_ids [ key ] ) ) {
123+ return restore_mru_ids [ key ] ;
124+ }
125+ }
126+ return [ ] ;
127+ } ,
128+
107129 get_search_result : function ( search_val ) {
108130 var Objects = new Model ( this . field . relation ) ;
109131 var def = $ . Deferred ( ) ;
@@ -126,13 +148,30 @@ odoo.define('web_m2x_options.web_m2x_options', function (require) {
126148
127149 var dataset = new data . DataSet ( this , this . field . relation ,
128150 self . build_context ( ) ) ;
151+ var domain_list = [ ] ;
129152 var blacklist = this . get_search_blacklist ( ) ;
153+ if ( ! _ ( blacklist ) . isEmpty ( ) ) {
154+ domain_list . push ( [ 'id' , 'not in' , blacklist ] ) ;
155+ }
156+ var can_search_mru = ( self . options && self . is_option_set ( self . options . search_mru ) ) ,
157+ search_mru_undef = _ . isUndefined ( self . options . search_mru ) ,
158+ search_mru = self . is_option_set ( self . view . ir_options [ 'web_m2x_options.search_mru' ] ) ;
159+
160+ var mru_ids = [ ] ;
161+ var in_search_mru = false ;
162+ if ( search_val == "" && ( can_search_mru || ( search_mru_undef && search_mru ) ) ) {
163+ mru_ids = self . get_mru_ids ( ) ;
164+ if ( ! _ ( mru_ids ) . isEmpty ( ) ) {
165+ domain_list . push ( [ 'id' , 'in' , mru_ids ] ) ;
166+ in_search_mru = true ;
167+ }
168+ }
130169 this . last_query = search_val ;
131170
132171 var search_result = this . orderer . add ( dataset . name_search (
133172 search_val ,
134173 new data . CompoundDomain (
135- self . build_domain ( ) , [ [ "id" , "not in" , blacklist ] ] ) ,
174+ self . build_domain ( ) , domain_list ) ,
136175 'ilike' , this . limit + 1 ,
137176 self . build_context ( ) ) ) ;
138177
@@ -149,13 +188,25 @@ odoo.define('web_m2x_options.web_m2x_options', function (require) {
149188 // possible selections for the m2o
150189 var values = _ . map ( data , function ( x ) {
151190 x [ 1 ] = x [ 1 ] . split ( "\n" ) [ 0 ] ;
152- return {
191+ var val = {
153192 label : _ . str . escapeHTML ( x [ 1 ] ) ,
154193 value : x [ 1 ] ,
155194 name : x [ 1 ] ,
156195 id : x [ 0 ] ,
157196 } ;
197+ if ( in_search_mru ) {
198+ val [ 'classname' ] = 'web_m2x_dropdown_option_mru' ;
199+ }
200+ return val ;
158201 } ) ;
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+ }
159210
160211 // Search result value colors
161212 if ( self . colors && self . field_color ) {
@@ -188,7 +239,7 @@ odoo.define('web_m2x_options.web_m2x_options', function (require) {
188239 search_more_undef = _ . isUndefined ( self . options . search_more ) && _ . isUndefined ( self . view . ir_options [ 'web_m2x_options.search_more' ] ) ,
189240 search_more = self . is_option_set ( self . view . ir_options [ 'web_m2x_options.search_more' ] ) ;
190241
191- if ( values . length > self . limit && ( can_search_more || search_more_undef || search_more ) ) {
242+ if ( ( values . length > self . limit || in_search_mru ) && ( can_search_more || search_more_undef || search_more ) ) {
192243 values = values . slice ( 0 , self . limit ) ;
193244 values . push ( {
194245 label : _t ( "Search More..." ) ,
@@ -202,7 +253,7 @@ odoo.define('web_m2x_options.web_m2x_options', function (require) {
202253 self . _search_create_popup ( "search" , data ) ;
203254 } ) ;
204255 } ,
205- classname : 'oe_m2o_dropdown_option o_m2o_dropdown_option'
256+ classname : 'oe_m2o_dropdown_option o_m2o_dropdown_option'
206257 } ) ;
207258 }
208259
@@ -227,7 +278,7 @@ odoo.define('web_m2x_options.web_m2x_options', function (require) {
227278 action : function ( ) {
228279 self . _quick_create ( search_val ) ;
229280 } ,
230- classname : 'oe_m2o_dropdown_option o_m2o_dropdown_option'
281+ classname : 'oe_m2o_dropdown_option o_m2o_dropdown_option'
231282 } ) ;
232283 }
233284 }
@@ -246,7 +297,7 @@ odoo.define('web_m2x_options.web_m2x_options', function (require) {
246297 "form" , undefined ,
247298 self . _create_context ( search_val ) ) ;
248299 } ,
249- classname : 'oe_m2o_dropdown_option o_m2o_dropdown_option'
300+ classname : 'oe_m2o_dropdown_option o_m2o_dropdown_option'
250301 } ) ;
251302 }
252303 // Check if colors specified to wait for RPC
@@ -264,7 +315,7 @@ odoo.define('web_m2x_options.web_m2x_options', function (require) {
264315 'click .o_delete' : function ( e ) {
265316 this . remove_id ( $ ( e . target ) . parent ( ) . data ( 'id' ) ) ;
266317 } ,
267- 'click .badge ' : 'open_badge' ,
318+ 'click .o_badge_text ' : 'open_badge' ,
268319 'mousedown .o_colorpicker span' : 'update_color' ,
269320 'focusout .o_colorpicker' : 'close_color_picker' ,
270321 } ,
@@ -362,7 +413,7 @@ odoo.define('web_m2x_options.web_m2x_options', function (require) {
362413 self . _search_create_popup ( "search" , data ) ;
363414 } ) ;
364415 } ,
365- classname : 'oe_m2o_dropdown_option o_m2o_dropdown_option'
416+ classname : 'oe_m2o_dropdown_option o_m2o_dropdown_option'
366417 } ) ;
367418 }
368419 // quick create
@@ -381,7 +432,7 @@ odoo.define('web_m2x_options.web_m2x_options', function (require) {
381432 action : function ( ) {
382433 self . _quick_create ( search_val ) ;
383434 } ,
384- classname : 'oe_m2o_dropdown_option o_m2o_dropdown_option'
435+ classname : 'oe_m2o_dropdown_option o_m2o_dropdown_option'
385436 } ) ;
386437 }
387438 }
@@ -399,7 +450,7 @@ odoo.define('web_m2x_options.web_m2x_options', function (require) {
399450 action : function ( ) {
400451 self . _search_create_popup ( "form" , undefined , self . _create_context ( search_val ) ) ;
401452 } ,
402- classname : 'oe_m2o_dropdown_option o_m2o_dropdown_option'
453+ classname : 'oe_m2o_dropdown_option o_m2o_dropdown_option'
403454 } ) ;
404455 }
405456
@@ -411,15 +462,17 @@ odoo.define('web_m2x_options.web_m2x_options', function (require) {
411462 var self = this ;
412463 var open = ( self . options && self . is_option_set ( self . options . open ) ) ;
413464 if ( open ) {
414- self . mutex . exec ( function ( ) {
415- var id = parseInt ( $ ( ev . currentTarget ) . data ( 'id' ) ) ;
416- self . do_action ( {
417- type : 'ir.actions.act_window' ,
418- res_model : self . field . relation ,
419- views : [ [ false , 'form' ] ] ,
420- res_id : id ,
421- target : "new"
422- } ) ;
465+ this . mutex . exec ( function ( ) {
466+ var id = parseInt ( $ ( ev . target ) . parent ( ) . data ( 'id' ) ) ;
467+ if ( id ) {
468+ self . do_action ( {
469+ type : 'ir.actions.act_window' ,
470+ res_model : self . field . relation ,
471+ views : [ [ false , 'form' ] ] ,
472+ res_id : id ,
473+ target : "new"
474+ } ) ;
475+ }
423476 } . bind ( this ) ) ;
424477 } else {
425478 self . open_color_picker ( ev ) ;
0 commit comments