@@ -5,6 +5,11 @@ import {
55 Many2ManyTagsFieldColorEditable ,
66} from "@web/views/fields/many2many_tags/many2many_tags_field" ;
77
8+ import {
9+ isMruGlobalOptionEnabled ,
10+ updateMruLocalStorageValues ,
11+ } from "@web_m2x_options/utils/mru.esm" ;
12+
813import { Dialog } from "@web/core/dialog/dialog" ;
914import { FormController } from "@web/views/form/form_controller" ;
1015import { FormViewDialog } from "@web/views/view_dialogs/form_view_dialog" ;
@@ -159,6 +164,7 @@ patch(Many2OneField.prototype, "web_m2x_options.Many2OneField", {
159164 this . _super ( ...arguments ) ;
160165 this . ir_options = Component . env . session . web_m2x_options ;
161166 } ,
167+
162168 /**
163169 * @override
164170 */
@@ -170,6 +176,7 @@ patch(Many2OneField.prototype, "web_m2x_options.Many2OneField", {
170176 searchMore : this . props . searchMore ,
171177 canCreate : this . props . canCreate ,
172178 nodeOptions : this . props . nodeOptions ,
179+ fieldName : this . props . name ,
173180 } ;
174181 } ,
175182
@@ -356,7 +363,24 @@ patch(FormController.prototype, "web_m2x_options.FormController", {
356363 await self . _setSubViewLimit ( ) ;
357364 self . superBeforeLoadResolver ( ) ;
358365 } ;
366+ this . hasMru = self . hasMru ( ) ;
367+ } ,
368+
369+ hasMru ( ) {
370+ const activeFields = this . archInfo . activeFields ;
371+ for ( const fieldName in activeFields ) {
372+ const fieldInfo = activeFields [ fieldName ] ;
373+ if (
374+ Boolean ( fieldInfo ) &&
375+ Boolean ( fieldInfo . options ) &&
376+ fieldInfo . options . search_mru
377+ ) {
378+ return true ;
379+ }
380+ }
381+ return false ;
359382 } ,
383+
360384 /**
361385 * @override
362386 * add more method to add subview limit on formview
@@ -401,4 +425,69 @@ patch(FormController.prototype, "web_m2x_options.FormController", {
401425 }
402426 }
403427 } ,
428+
429+ async saveButtonClicked ( ) {
430+ const mruChanges = this . getUpdateMruLocalStorageValues ( ) ;
431+ const saved = this . _super ( ...arguments ) ;
432+ updateMruLocalStorageValues ( this . props . resModel , mruChanges ) ;
433+ return saved ;
434+ } ,
435+
436+ async beforeExecuteActionButton ( ) {
437+ const mruChanges = this . getUpdateMruLocalStorageValues ( ) ;
438+ const saved = this . _super ( ...arguments ) ;
439+ updateMruLocalStorageValues ( this . props . resModel , mruChanges ) ;
440+ return saved ;
441+ } ,
442+
443+ async beforeLeave ( ) {
444+ const mruChanges = this . getUpdateMruLocalStorageValues ( ) ;
445+ const saved = this . _super ( ...arguments ) ;
446+ updateMruLocalStorageValues ( this . props . resModel , mruChanges ) ;
447+ return saved ;
448+ } ,
449+
450+ async onPagerUpdate ( ) {
451+ const mruChanges = this . getUpdateMruLocalStorageValues ( ) ;
452+ const saved = this . _super ( ...arguments ) ;
453+ updateMruLocalStorageValues ( this . props . resModel , mruChanges ) ;
454+ return saved ;
455+ } ,
456+
457+ getUpdateMruLocalStorageValues ( ) {
458+ if ( ! this . hasMru || ! this . model . root . isDirty ) {
459+ return { } ;
460+ }
461+ const model = this . model ;
462+ if ( model . __bm__ ) {
463+ return { } ;
464+ }
465+ const changes = model . __bm__ . _generateChanges (
466+ model . __bm__ . localData [ model . root . __bm_handle__ ] ,
467+ { changesOnly : true }
468+ ) ;
469+ const mruChanges = { } ;
470+ let enableMru = false ;
471+ let nodeOptions = { } ;
472+ let fieldInfo = { } ;
473+ const activeFields = this . archInfo . activeFields ;
474+ Object . keys ( changes ) . forEach ( function ( key ) {
475+ fieldInfo = activeFields [ key ] ;
476+ if (
477+ Boolean ( fieldInfo ) &&
478+ Boolean ( fieldInfo . FieldComponent ) &&
479+ fieldInfo . FieldComponent . name === "Many2OneField"
480+ ) {
481+ nodeOptions = fieldInfo . options ;
482+ enableMru =
483+ nodeOptions . search_mru !== undefined
484+ ? nodeOptions . search_mru
485+ : isMruGlobalOptionEnabled ( ) ;
486+ if ( enableMru ) {
487+ mruChanges [ key ] = changes [ key ] ;
488+ }
489+ }
490+ } ) ;
491+ return mruChanges ;
492+ } ,
404493} ) ;
0 commit comments